Search in sources :

Example 51 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class TestGetSplunk method testGetWithManagedFromCurrentUsingEventTime.

@Test
public void testGetWithManagedFromCurrentUsingEventTime() throws IOException, ParseException {
    final String query = "search tcp:7879";
    final String outputMode = GetSplunk.ATOM_VALUE.getValue();
    runner.setProperty(GetSplunk.QUERY, query);
    runner.setProperty(GetSplunk.OUTPUT_MODE, outputMode);
    runner.setProperty(GetSplunk.TIME_RANGE_STRATEGY, GetSplunk.MANAGED_CURRENT_VALUE.getValue());
    final String resultContent = "fake results";
    final ByteArrayInputStream input = new ByteArrayInputStream(resultContent.getBytes(StandardCharsets.UTF_8));
    when(service.export(eq(query), any(JobExportArgs.class))).thenReturn(input);
    // run once and don't shut down, shouldn't produce any results first time
    runner.run(1, false);
    runner.assertAllFlowFilesTransferred(GetSplunk.REL_SUCCESS, 0);
    // capture what the args were on last run
    verify(service, times(0)).export(eq(query), any(JobExportArgs.class));
    final StateMap state = runner.getStateManager().getState(Scope.CLUSTER);
    Assert.assertNotNull(state);
    Assert.assertTrue(state.getVersion() > 0);
    // save the latest time from the first run which should be earliest time of next run
    final String lastLatest = state.get(GetSplunk.LATEST_TIME_KEY);
    final SimpleDateFormat format = new SimpleDateFormat(GetSplunk.DATE_TIME_FORMAT);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    final Date lastLatestDate = format.parse(lastLatest);
    final String expectedLatest = format.format(new Date(lastLatestDate.getTime() + 1));
    // run again
    runner.run(1, false);
    runner.assertAllFlowFilesTransferred(GetSplunk.REL_SUCCESS, 1);
    final ArgumentCaptor<JobExportArgs> capture = ArgumentCaptor.forClass(JobExportArgs.class);
    verify(service, times(1)).export(eq(query), capture.capture());
    // second execution the earliest time should be the previous latest_time
    final JobExportArgs actualArgs = capture.getValue();
    Assert.assertNotNull(actualArgs);
    Assert.assertEquals(expectedLatest, actualArgs.get("earliest_time"));
    Assert.assertNotNull(actualArgs.get("latest_time"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StateMap(org.apache.nifi.components.state.StateMap) JobExportArgs(com.splunk.JobExportArgs) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 52 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class ITRedisStateProvider method testReplaceWithNonExistingValueAndVersionGreaterThanNegativeOne.

@Test
public void testReplaceWithNonExistingValueAndVersionGreaterThanNegativeOne() throws Exception {
    final StateProvider provider = getProvider();
    final StateMap stateMap = new StateMap() {

        @Override
        public long getVersion() {
            return 4;
        }

        @Override
        public String get(String key) {
            return null;
        }

        @Override
        public Map<String, String> toMap() {
            return Collections.emptyMap();
        }
    };
    final Map<String, String> newValue = new HashMap<>();
    newValue.put("value", "value");
    final boolean replaced = provider.replace(stateMap, newValue, componentId);
    assertFalse(replaced);
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 53 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class ITRedisStateProvider method testToMap.

@Test
public void testToMap() throws IOException {
    final String key = "testKeySet";
    final StateProvider provider = getProvider();
    Map<String, String> map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    provider.setState(Collections.singletonMap(key, "value"), componentId);
    map = provider.getState(componentId).toMap();
    assertNotNull(map);
    assertEquals(1, map.size());
    assertEquals("value", map.get(key));
    provider.setState(Collections.<String, String>emptyMap(), componentId);
    final StateMap stateMap = provider.getState(componentId);
    map = stateMap.toMap();
    assertNotNull(map);
    assertTrue(map.isEmpty());
    assertEquals(1, stateMap.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 54 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class ITRedisStateProvider method testReplaceWithWrongVersion.

@Test
public void testReplaceWithWrongVersion() throws IOException {
    final String key = "testReplaceWithWrongVersion";
    final StateProvider provider = getProvider();
    provider.setState(Collections.singletonMap(key, "value1"), componentId);
    StateMap stateMap = provider.getState(componentId);
    assertNotNull(stateMap);
    assertEquals("value1", stateMap.get(key));
    assertEquals(0, stateMap.getVersion());
    provider.setState(Collections.singletonMap(key, "intermediate value"), componentId);
    assertFalse(provider.replace(stateMap, Collections.singletonMap(key, "value2"), componentId));
    stateMap = provider.getState(componentId);
    assertEquals(key, stateMap.toMap().keySet().iterator().next());
    assertEquals(1, stateMap.toMap().size());
    assertEquals("intermediate value", stateMap.get(key));
    assertEquals(1, stateMap.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Example 55 with StateMap

use of org.apache.nifi.components.state.StateMap in project nifi by apache.

the class ITRedisStateProvider method testReplaceSuccessful.

@Test
public void testReplaceSuccessful() throws IOException {
    final String key = "testReplaceSuccessful";
    final StateProvider provider = getProvider();
    StateMap map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(-1, map.getVersion());
    assertNotNull(map.toMap());
    assertTrue(map.toMap().isEmpty());
    provider.setState(Collections.singletonMap(key, "value1"), componentId);
    map = provider.getState(componentId);
    assertNotNull(map);
    assertEquals(0, map.getVersion());
    assertEquals("value1", map.get(key));
    assertEquals("value1", map.toMap().get(key));
    final Map<String, String> newMap = new HashMap<>(map.toMap());
    newMap.put(key, "value2");
    assertTrue(provider.replace(map, newMap, componentId));
    map = provider.getState(componentId);
    assertEquals("value2", map.get(key));
    assertEquals(1L, map.getVersion());
}
Also used : StateProvider(org.apache.nifi.components.state.StateProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StateMap(org.apache.nifi.components.state.StateMap) Test(org.junit.Test)

Aggregations

StateMap (org.apache.nifi.components.state.StateMap)70 HashMap (java.util.HashMap)31 Test (org.junit.Test)29 IOException (java.io.IOException)18 StateProvider (org.apache.nifi.components.state.StateProvider)14 ArrayList (java.util.ArrayList)11 StateManager (org.apache.nifi.components.state.StateManager)11 FlowFile (org.apache.nifi.flowfile.FlowFile)10 TestRunner (org.apache.nifi.util.TestRunner)10 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)9 ComponentLog (org.apache.nifi.logging.ComponentLog)8 Map (java.util.Map)7 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)7 ProcessException (org.apache.nifi.processor.exception.ProcessException)7 Date (java.util.Date)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 Scope (org.apache.nifi.components.state.Scope)6 ProcessSession (org.apache.nifi.processor.ProcessSession)6 Collections (java.util.Collections)5