ComponentCommandOrder { public boolean canExecute(String component, Command command, Collection<ComponentInstanceState> currentStates) { if (!dependencies.containsKey(command)) { return true; } List<ComponentState> required = new ArrayList<>(); if (dependencies.get(command).containsKey(component)) { required.addAll(dependencies.get(command).get(component)); } String prefix = rolePrefixMap.get(component); if (prefix != null && dependencies.get(command).containsKey(prefix)) { required.addAll(dependencies.get(command).get(prefix)); } for (ComponentState stateToMatch : required) { for (ComponentInstanceState currState : currentStates) { log.debug("Checking schedule {} {} against dependency {} is {}", component, command, currState.getComponentName(), currState.getState()); if (currState.getComponentName().equals(stateToMatch.componentName) || (prefixRoleMap.containsKey(stateToMatch.componentName) && prefixRoleMap.get(stateToMatch.componentName).contains(currState.getComponentName()))) { if (currState.getState() != stateToMatch.state) { if (stateToMatch.state == State.STARTED) { log.info("Cannot schedule {} {} as dependency {} is {}", component, command, currState.getComponentName(), currState.getState()); return false; } else { if (currState.getState() != State.STARTING && currState.getState() != State.STARTED) { log.info("Cannot schedule {} {} as dependency {} is {}", component, command, currState.getComponentName(), currState.getState()); return false; } } } } } } return true; } ComponentCommandOrder();  ComponentCommandOrder(List<CommandOrder> commandOrders,
      ConfTreeOperations resources); boolean canExecute(String component, Command command, Collection<ComponentInstanceState> currentStates); static final Logger log; }
@Test public void testComponentCommandOrderBadInput() throws Exception { CommandOrder co = new CommandOrder(); co.setCommand(" A-START"); co.setRequires("B-STARTED , C-STARTED"); ComponentInstanceState cisB = new ComponentInstanceState("B", containerId, "aid"); ComponentInstanceState cisC = new ComponentInstanceState("C", containerId, "aid"); cisB.setState(State.STARTED); cisC.setState(State.STARTED); ComponentCommandOrder cco = new ComponentCommandOrder(Arrays.asList(co), resources); Assert.assertTrue(cco.canExecute("A", Command.START, Arrays.asList(cisB, cisC))); co.setCommand(" A-STAR"); co.setRequires("B-STARTED , C-STARTED"); try { cco = new ComponentCommandOrder(Arrays.asList(co), resources); Assert.fail("Instantiation should have failed."); } catch (IllegalArgumentException ie) { log.info(ie.getMessage()); } co.setCommand(" -START"); co.setRequires("B-STARTED , C-STARTED"); try { cco = new ComponentCommandOrder(Arrays.asList(co), resources); Assert.fail("Instantiation should have failed."); } catch (IllegalArgumentException ie) { log.info(ie.getMessage()); } co.setCommand(" A-START"); co.setRequires("B-STRTED , C-STARTED"); try { cco = new ComponentCommandOrder(Arrays.asList(co), resources); Assert.fail("Instantiation should have failed."); } catch (IllegalArgumentException ie) { log.info(ie.getMessage()); } co.setCommand(" A-START"); co.setRequires("B-STARTED , C-"); try { cco = new ComponentCommandOrder(Arrays.asList(co), resources); Assert.fail("Instantiation should have failed."); } catch (IllegalArgumentException ie) { log.info(ie.getMessage()); } co.setCommand(" A-INSTALL"); co.setRequires("B-STARTED"); try { cco = new ComponentCommandOrder(Arrays.asList(co), resources); Assert.fail("Instantiation should have failed."); } catch (IllegalArgumentException ie) { log.info(ie.getMessage()); } }
@Test public void testComponentCommandOrderPrefixes() throws Exception { ConfTreeOperations resources = new ConfTreeOperations(new ConfTree()); resources.getOrAddComponent("a-A").put(ROLE_PREFIX, "a-"); resources.getOrAddComponent("b-B1").put(ROLE_PREFIX, "b-"); resources.getOrAddComponent("b-B2").put(ROLE_PREFIX, "b-"); resources.getOrAddComponent("c-C").put(ROLE_PREFIX, "c-"); CommandOrder co1 = new CommandOrder(); co1.setCommand("b-START"); co1.setRequires("a-STARTED"); CommandOrder co2 = new CommandOrder(); co2.setCommand("c-START"); co2.setRequires("b-STARTED"); ComponentCommandOrder cco = new ComponentCommandOrder( Arrays.asList(co1, co2), resources); ComponentInstanceState cisA = new ComponentInstanceState("a-A", containerId, "aid"); ComponentInstanceState cisB1 = new ComponentInstanceState("b-B1", containerId, "aid"); ComponentInstanceState cisB2 = new ComponentInstanceState("b-B2", containerId, "aid"); ComponentInstanceState cisC = new ComponentInstanceState("c-C", containerId, "aid"); cisA.setState(State.INSTALLED); cisB1.setState(State.INSTALLED); cisB2.setState(State.INSTALLED); cisC.setState(State.INSTALLED); List<ComponentInstanceState> states = Arrays.asList(cisA, cisB1, cisB2, cisC); Assert.assertTrue(cco.canExecute("a-A", Command.START, states)); Assert.assertFalse(cco.canExecute("b-B1", Command.START, states)); Assert.assertFalse(cco.canExecute("b-B2", Command.START, states)); Assert.assertFalse(cco.canExecute("c-C", Command.START, states)); cisA.setState(State.STARTED); Assert.assertTrue(cco.canExecute("b-B1", Command.START, states)); Assert.assertTrue(cco.canExecute("b-B2", Command.START, states)); Assert.assertFalse(cco.canExecute("c-C", Command.START, states)); cisB1.setState(State.STARTED); Assert.assertFalse(cco.canExecute("c-C", Command.START, states)); cisB2.setState(State.STARTED); Assert.assertTrue(cco.canExecute("c-C", Command.START, states)); }