use of org.apache.nifi.controller.service.mock.MockProcessGroup in project nifi by apache.
the class TestStandardProcessScheduler method setup.
@Before
public void setup() throws InitializationException {
final Map<String, String> overrideProperties = new HashMap<>();
overrideProperties.put(NiFiProperties.ADMINISTRATIVE_YIELD_DURATION, "2 millis");
overrideProperties.put(NiFiProperties.PROCESSOR_SCHEDULING_TIMEOUT, "10 millis");
this.nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, overrideProperties);
// load the system bundle
systemBundle = SystemBundle.create(nifiProperties);
ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
scheduler = new StandardProcessScheduler(new FlowEngine(1, "Unit Test", true), Mockito.mock(FlowController.class), null, stateMgrProvider, nifiProperties);
scheduler.setSchedulingAgent(SchedulingStrategy.TIMER_DRIVEN, Mockito.mock(SchedulingAgent.class));
reportingTask = new TestReportingTask();
final ReportingInitializationContext config = new StandardReportingInitializationContext(UUID.randomUUID().toString(), "Test", SchedulingStrategy.TIMER_DRIVEN, "5 secs", Mockito.mock(ComponentLog.class), null, nifiProperties, null);
reportingTask.initialize(config);
final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(null, variableRegistry);
final TerminationAwareLogger logger = Mockito.mock(TerminationAwareLogger.class);
final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
final LoggableComponent<ReportingTask> loggableComponent = new LoggableComponent<>(reportingTask, systemBundle.getBundleDetails().getCoordinate(), logger);
taskNode = new StandardReportingTaskNode(loggableComponent, UUID.randomUUID().toString(), null, scheduler, validationContextFactory, new StandardComponentVariableRegistry(variableRegistry), reloadComponent);
controller = Mockito.mock(FlowController.class);
final ConcurrentMap<String, ProcessorNode> processorMap = new ConcurrentHashMap<>();
Mockito.doAnswer(new Answer<ProcessorNode>() {
@Override
public ProcessorNode answer(InvocationOnMock invocation) throws Throwable {
final String id = invocation.getArgumentAt(0, String.class);
return processorMap.get(id);
}
}).when(controller).getProcessorNode(Mockito.anyString());
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
final ProcessorNode procNode = invocation.getArgumentAt(0, ProcessorNode.class);
processorMap.putIfAbsent(procNode.getIdentifier(), procNode);
return null;
}
}).when(controller).onProcessorAdded(Mockito.any(ProcessorNode.class));
rootGroup = new MockProcessGroup(controller);
Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(rootGroup);
}
use of org.apache.nifi.controller.service.mock.MockProcessGroup in project nifi by apache.
the class TestStandardControllerServiceProvider method testOrderingOfServices.
@Test
public void testOrderingOfServices() {
final ProcessGroup procGroup = new MockProcessGroup(controller);
final FlowController controller = Mockito.mock(FlowController.class);
Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(procGroup);
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, null, null, stateManagerProvider, variableRegistry, niFiProperties);
final ControllerServiceNode serviceNode1 = provider.createControllerService(ServiceA.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
final ControllerServiceNode serviceNode2 = provider.createControllerService(ServiceB.class.getName(), "2", systemBundle.getBundleDetails().getCoordinate(), null, false);
setProperty(serviceNode1, ServiceA.OTHER_SERVICE.getName(), "2");
final Map<String, ControllerServiceNode> nodeMap = new LinkedHashMap<>();
nodeMap.put("1", serviceNode1);
nodeMap.put("2", serviceNode2);
List<List<ControllerServiceNode>> branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(2, branches.size());
List<ControllerServiceNode> ordered = branches.get(0);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode1);
assertEquals(1, branches.get(1).size());
assertTrue(branches.get(1).get(0) == serviceNode2);
nodeMap.clear();
nodeMap.put("2", serviceNode2);
nodeMap.put("1", serviceNode1);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(2, branches.size());
ordered = branches.get(1);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode1);
assertEquals(1, branches.get(0).size());
assertTrue(branches.get(0).get(0) == serviceNode2);
// add circular dependency on self.
nodeMap.clear();
setProperty(serviceNode1, ServiceA.OTHER_SERVICE_2.getName(), "1");
nodeMap.put("1", serviceNode1);
nodeMap.put("2", serviceNode2);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(2, branches.size());
ordered = branches.get(0);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode1);
nodeMap.clear();
nodeMap.put("2", serviceNode2);
nodeMap.put("1", serviceNode1);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(2, branches.size());
ordered = branches.get(1);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode1);
// add circular dependency once removed. In this case, we won't actually be able to enable these because of the
// circular dependency because they will never be valid because they will always depend on a disabled service.
// But we want to ensure that the method returns successfully without throwing a StackOverflowException or anything
// like that.
nodeMap.clear();
final ControllerServiceNode serviceNode3 = provider.createControllerService(ServiceA.class.getName(), "3", systemBundle.getBundleDetails().getCoordinate(), null, false);
setProperty(serviceNode1, ServiceA.OTHER_SERVICE.getName(), "3");
setProperty(serviceNode3, ServiceA.OTHER_SERVICE.getName(), "1");
nodeMap.put("1", serviceNode1);
nodeMap.put("3", serviceNode3);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(2, branches.size());
ordered = branches.get(0);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode3);
assertTrue(ordered.get(1) == serviceNode1);
nodeMap.clear();
nodeMap.put("3", serviceNode3);
nodeMap.put("1", serviceNode1);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(2, branches.size());
ordered = branches.get(1);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode3);
assertTrue(ordered.get(1) == serviceNode1);
// Add multiple completely disparate branches.
nodeMap.clear();
setProperty(serviceNode1, ServiceA.OTHER_SERVICE.getName(), "2");
final ControllerServiceNode serviceNode4 = provider.createControllerService(ServiceB.class.getName(), "4", systemBundle.getBundleDetails().getCoordinate(), null, false);
final ControllerServiceNode serviceNode5 = provider.createControllerService(ServiceB.class.getName(), "5", systemBundle.getBundleDetails().getCoordinate(), null, false);
setProperty(serviceNode3, ServiceA.OTHER_SERVICE.getName(), "4");
nodeMap.put("1", serviceNode1);
nodeMap.put("2", serviceNode2);
nodeMap.put("3", serviceNode3);
nodeMap.put("4", serviceNode4);
nodeMap.put("5", serviceNode5);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(5, branches.size());
ordered = branches.get(0);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode1);
assertEquals(1, branches.get(1).size());
assertTrue(branches.get(1).get(0) == serviceNode2);
ordered = branches.get(2);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode4);
assertTrue(ordered.get(1) == serviceNode3);
assertEquals(1, branches.get(3).size());
assertTrue(branches.get(3).get(0) == serviceNode4);
assertEquals(1, branches.get(4).size());
assertTrue(branches.get(4).get(0) == serviceNode5);
// create 2 branches both dependent on the same service
nodeMap.clear();
setProperty(serviceNode1, ServiceA.OTHER_SERVICE.getName(), "2");
setProperty(serviceNode3, ServiceA.OTHER_SERVICE.getName(), "2");
nodeMap.put("1", serviceNode1);
nodeMap.put("2", serviceNode2);
nodeMap.put("3", serviceNode3);
branches = StandardControllerServiceProvider.determineEnablingOrder(nodeMap);
assertEquals(3, branches.size());
ordered = branches.get(0);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode1);
ordered = branches.get(1);
assertEquals(1, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
ordered = branches.get(2);
assertEquals(2, ordered.size());
assertTrue(ordered.get(0) == serviceNode2);
assertTrue(ordered.get(1) == serviceNode3);
}
use of org.apache.nifi.controller.service.mock.MockProcessGroup in project nifi by apache.
the class StandardControllerServiceProviderIT method testEnableReferencingServicesGraph.
public void testEnableReferencingServicesGraph(final StandardProcessScheduler scheduler) {
final FlowController controller = Mockito.mock(FlowController.class);
final ProcessGroup procGroup = new MockProcessGroup(controller);
Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(procGroup);
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateManagerProvider, variableRegistry, niFiProperties);
// build a graph of controller services with dependencies as such:
//
// A -> B -> D
// C ---^----^
//
// In other words, A references B, which references D.
// AND
// C references B and D.
//
// So we have to verify that if D is enabled, when we enable its referencing services,
// we enable C and B, even if we attempt to enable C before B... i.e., if we try to enable C, we cannot do so
// until B is first enabled so ensure that we enable B first.
final ControllerServiceNode serviceNode1 = provider.createControllerService(ServiceA.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
final ControllerServiceNode serviceNode2 = provider.createControllerService(ServiceA.class.getName(), "2", systemBundle.getBundleDetails().getCoordinate(), null, false);
final ControllerServiceNode serviceNode3 = provider.createControllerService(ServiceA.class.getName(), "3", systemBundle.getBundleDetails().getCoordinate(), null, false);
final ControllerServiceNode serviceNode4 = provider.createControllerService(ServiceB.class.getName(), "4", systemBundle.getBundleDetails().getCoordinate(), null, false);
procGroup.addControllerService(serviceNode1);
procGroup.addControllerService(serviceNode2);
procGroup.addControllerService(serviceNode3);
procGroup.addControllerService(serviceNode4);
setProperty(serviceNode1, ServiceA.OTHER_SERVICE.getName(), "2");
setProperty(serviceNode2, ServiceA.OTHER_SERVICE.getName(), "4");
setProperty(serviceNode3, ServiceA.OTHER_SERVICE.getName(), "2");
setProperty(serviceNode3, ServiceA.OTHER_SERVICE_2.getName(), "4");
provider.enableControllerService(serviceNode4);
provider.enableReferencingServices(serviceNode4);
// Verify that the services are either ENABLING or ENABLED, and wait for all of them to become ENABLED.
// Note that we set a timeout of 10 seconds, in case a bug occurs and the services never become ENABLED.
final Set<ControllerServiceState> validStates = new HashSet<>();
validStates.add(ControllerServiceState.ENABLED);
validStates.add(ControllerServiceState.ENABLING);
while (serviceNode3.getState() != ControllerServiceState.ENABLED || serviceNode2.getState() != ControllerServiceState.ENABLED || serviceNode1.getState() != ControllerServiceState.ENABLED) {
assertTrue(validStates.contains(serviceNode3.getState()));
assertTrue(validStates.contains(serviceNode2.getState()));
assertTrue(validStates.contains(serviceNode1.getState()));
}
}
use of org.apache.nifi.controller.service.mock.MockProcessGroup in project nifi by apache.
the class TestStandardControllerServiceProvider method testEnableDisableWithReference.
@Test(timeout = 10000)
public void testEnableDisableWithReference() {
final ProcessGroup group = new MockProcessGroup(controller);
final FlowController controller = Mockito.mock(FlowController.class);
Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(group);
final StandardProcessScheduler scheduler = createScheduler();
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateManagerProvider, variableRegistry, niFiProperties);
final ControllerServiceNode serviceNodeB = provider.createControllerService(ServiceB.class.getName(), "B", systemBundle.getBundleDetails().getCoordinate(), null, false);
final ControllerServiceNode serviceNodeA = provider.createControllerService(ServiceA.class.getName(), "A", systemBundle.getBundleDetails().getCoordinate(), null, false);
group.addControllerService(serviceNodeA);
group.addControllerService(serviceNodeB);
setProperty(serviceNodeA, ServiceA.OTHER_SERVICE.getName(), "B");
try {
provider.enableControllerService(serviceNodeA);
Assert.fail("Was able to enable Service A but Service B is disabled.");
} catch (final IllegalStateException expected) {
}
provider.enableControllerService(serviceNodeB);
provider.enableControllerService(serviceNodeA);
try {
provider.disableControllerService(serviceNodeB);
Assert.fail("Was able to disable Service B but Service A is enabled and references B");
} catch (final IllegalStateException expected) {
}
provider.disableControllerService(serviceNodeA);
waitForServiceState(serviceNodeA, ControllerServiceState.DISABLED);
provider.disableControllerService(serviceNodeB);
waitForServiceState(serviceNodeB, ControllerServiceState.DISABLED);
}
use of org.apache.nifi.controller.service.mock.MockProcessGroup in project nifi by apache.
the class TestStandardControllerServiceProvider method testDisableControllerService.
@Test
public void testDisableControllerService() {
final ProcessGroup procGroup = new MockProcessGroup(controller);
final FlowController controller = Mockito.mock(FlowController.class);
Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(procGroup);
final StandardProcessScheduler scheduler = createScheduler();
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateManagerProvider, variableRegistry, niFiProperties);
final ControllerServiceNode serviceNode = provider.createControllerService(ServiceB.class.getName(), "B", systemBundle.getBundleDetails().getCoordinate(), null, false);
provider.enableControllerService(serviceNode);
provider.disableControllerService(serviceNode);
}
Aggregations