use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.
the class TestPopularVoteFlowElection method testDifferentEmptyFlows.
@Test
public void testDifferentEmptyFlows() throws IOException {
final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenAnswer(new Answer<String>() {
@Override
public String answer(final InvocationOnMock invocation) throws Throwable {
final byte[] flow = invocation.getArgumentAt(0, byte[].class);
final String xml = new String(flow);
// Return the ID of the root group as the fingerprint.
final String fingerprint = xml.replaceAll("(?s:(.*<id>)(.*?)(</id>.*))", "$2");
return fingerprint;
}
});
final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 3, fingerprintFactory);
final byte[] flow1 = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
final byte[] flow2 = Files.readAllBytes(Paths.get("src/test/resources/conf/different-empty-flow.xml"));
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
assertNull(election.castVote(createDataFlow(flow1), createNodeId(1)));
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
assertNull(election.castVote(createDataFlow(flow1), createNodeId(2)));
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
final DataFlow electedDataFlow = election.castVote(createDataFlow(flow2), createNodeId(3));
assertNotNull(electedDataFlow);
final String electedFlowXml = new String(electedDataFlow.getFlow());
assertTrue(new String(flow1).equals(electedFlowXml) || new String(flow2).equals(electedFlowXml));
}
use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.
the class TestPopularVoteFlowElection method testOnlyEmptyFlows.
@Test
public void testOnlyEmptyFlows() throws IOException {
final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenReturn("fingerprint");
final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 3, fingerprintFactory);
final byte[] flow = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
assertNull(election.castVote(createDataFlow(flow), createNodeId(1)));
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
assertNull(election.castVote(createDataFlow(flow), createNodeId(2)));
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
final DataFlow electedDataFlow = election.castVote(createDataFlow(flow), createNodeId(3));
assertNotNull(electedDataFlow);
assertEquals(new String(flow), new String(electedDataFlow.getFlow()));
}
use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.
the class TestPopularVoteFlowElection method testEmptyFlowIgnoredIfNonEmptyFlowExists.
@Test
public void testEmptyFlowIgnoredIfNonEmptyFlowExists() throws IOException {
final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenReturn("fingerprint");
final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 8, fingerprintFactory);
final byte[] emptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
final byte[] nonEmptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/non-empty-flow.xml"));
for (int i = 0; i < 8; i++) {
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
final DataFlow dataFlow;
if (i % 4 == 0) {
dataFlow = createDataFlow(nonEmptyFlow);
} else {
dataFlow = createDataFlow(emptyFlow);
}
final DataFlow electedDataFlow = election.castVote(dataFlow, createNodeId(i));
if (i == 7) {
assertNotNull(electedDataFlow);
assertEquals(new String(nonEmptyFlow), new String(electedDataFlow.getFlow()));
} else {
assertNull(electedDataFlow);
}
}
}
use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.
the class PopularVoteFlowElectionFactoryBean method getObject.
@Override
public PopularVoteFlowElection getObject() throws Exception {
final String maxWaitTime = properties.getFlowElectionMaxWaitTime();
long maxWaitMillis;
try {
maxWaitMillis = FormatUtils.getTimeDuration(maxWaitTime, TimeUnit.MILLISECONDS);
} catch (final Exception e) {
logger.warn("Failed to parse value of property '{}' as a valid time period. Value was '{}'. Ignoring this value and using the default value of '{}'", NiFiProperties.FLOW_ELECTION_MAX_WAIT_TIME, maxWaitTime, NiFiProperties.DEFAULT_FLOW_ELECTION_MAX_WAIT_TIME);
maxWaitMillis = FormatUtils.getTimeDuration(NiFiProperties.DEFAULT_FLOW_ELECTION_MAX_WAIT_TIME, TimeUnit.MILLISECONDS);
}
final Integer maxNodes = properties.getFlowElectionMaxCandidates();
final StringEncryptor encryptor = StringEncryptor.createEncryptor(properties);
final FingerprintFactory fingerprintFactory = new FingerprintFactory(encryptor);
return new PopularVoteFlowElection(maxWaitMillis, TimeUnit.MILLISECONDS, maxNodes, fingerprintFactory);
}
use of org.apache.nifi.fingerprint.FingerprintFactory in project nifi by apache.
the class TestPopularVoteFlowElection method testAutoGeneratedVsPopulatedFlowElection.
@Test
public void testAutoGeneratedVsPopulatedFlowElection() throws IOException {
final FingerprintFactory fingerprintFactory = new FingerprintFactory(StringEncryptor.createEncryptor(getNiFiProperties()));
final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 4, fingerprintFactory);
final byte[] emptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/auto-generated-empty-flow.xml"));
final byte[] nonEmptyFlow = Files.readAllBytes(Paths.get("src/test/resources/conf/reporting-task-flow.xml"));
for (int i = 0; i < 4; i++) {
assertFalse(election.isElectionComplete());
assertNull(election.getElectedDataFlow());
final DataFlow dataFlow;
if (i % 2 == 0) {
dataFlow = createDataFlow(emptyFlow);
} else {
dataFlow = createDataFlow(nonEmptyFlow);
}
final DataFlow electedDataFlow = election.castVote(dataFlow, createNodeId(i));
if (i == 3) {
assertNotNull(electedDataFlow);
assertEquals(new String(nonEmptyFlow), new String(electedDataFlow.getFlow()));
} else {
assertNull(electedDataFlow);
}
}
}
Aggregations