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"));
}
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);
}
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());
}
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());
}
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());
}
Aggregations