MinHosts extends CriteriaCommon { @Override public List<TopologyWrapper> filter(List<TopologyWrapper> topologies) { int minCount = Collections.min(topologies, Comparator.comparingInt(t -> t.getNodeTemplates().size())) .getNodeTemplates().size(); return topologies.stream().filter(t -> t.getNodeTemplates().size() == minCount).collect(Collectors.toList()); } MinHosts(JsonNode params, int outputCap); @Override List<TopologyWrapper> filter(List<TopologyWrapper> topologies); @Override List<TopologyWrapper> generateTargetLabelTopologies(TopologyWrapper topology);  }
@Test public void testOneTopLevelTwoPossibilitiesNoTargetLabel() throws Exception { ServiceTemplateId id = new ServiceTemplateId("http: "TargetAllocationMinHostsTest1", false); TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate()); Criteria minHosts = new MinHosts(null, Integer.MAX_VALUE); List<String> ntIdsWithoutTargetLabel = topology.getTopLevelNTs().stream() .filter(nt -> !ModelUtilities.getTargetLabel(nt).isPresent()).map(HasId::getId).collect(Collectors.toList()); List<TopologyWrapper> topologies = minHosts.allocate(topology); assertEquals(2, topologies.size()); TTopologyTemplate topology1 = topologies.get(0).getTopology(); TTopologyTemplate topology2 = topologies.get(1).getTopology(); assertEquals(9, topology1.getNodeTemplateOrRelationshipTemplate().size()); assertEquals(9, topology2.getNodeTemplateOrRelationshipTemplate().size()); List<String> types1 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology1.getNodeTemplates()) { types1.add(nodeTemplate.getType().getLocalPart()); } assertEquals(6, types1.size()); assertTrue(types1.contains("shetland_pony")); assertTrue(types1.contains("pasture")); for (TNodeTemplate nodeTemplate : topologies.get(0).getTopLevelNTs()) { if (ntIdsWithoutTargetLabel.contains(nodeTemplate.getId())) { assertEquals("pasture", topologies.get(0).getHostedOnSuccessor(nodeTemplate).getType().getLocalPart()); } } List<String> types2 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology2.getNodeTemplates()) { types2.add(nodeTemplate.getType().getLocalPart()); } assertEquals(6, types2.size()); assertTrue(types2.contains("shetland_pony")); assertTrue(types2.contains("field_-w1-wip1")); for (TNodeTemplate nodeTemplate : topologies.get(1).getTopLevelNTs()) { if (ntIdsWithoutTargetLabel.contains(nodeTemplate.getId())) { assertEquals("field_-w1-wip1", topologies.get(1).getHostedOnSuccessor(nodeTemplate).getType().getLocalPart()); } } }
@Test public void testOneTargetLabelPresent() throws Exception { ServiceTemplateId id = new ServiceTemplateId("http: "TargetAllocationMinHostsTest2", false); TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate()); Criteria minHosts = new MinHosts(null, Integer.MAX_VALUE); List<TopologyWrapper> topologies = minHosts.allocate(topology); assertEquals(1, topologies.size()); TTopologyTemplate topology1 = topologies.get(0).getTopology(); assertEquals(7, topology1.getNodeTemplateOrRelationshipTemplate().size()); List<String> types1 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology1.getNodeTemplates()) { types1.add(nodeTemplate.getType().getLocalPart()); } assertEquals(4, types1.size()); assertEquals(3, types1.stream().filter(type -> type.equalsIgnoreCase("shetland_pony")).count()); assertEquals(1, types1.stream().filter(type -> type.equalsIgnoreCase("pasture")).count()); for (TNodeTemplate nodeTemplate : topologies.get(0).getTopLevelNTs()) { assertEquals("pasture", topologies.get(0).getHostedOnSuccessor(nodeTemplate).getType().getLocalPart()); } }
@Test public void testTwoTargetLabelsPresent() throws Exception { ServiceTemplateId id = new ServiceTemplateId("http: "TargetAllocationMinHostsTest3", false); TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate()); Criteria minHosts = new MinHosts(null, Integer.MAX_VALUE); List<TopologyWrapper> topologies = minHosts.allocate(topology); assertEquals(2, topologies.size()); TTopologyTemplate topology1 = topologies.get(0).getTopology(); TTopologyTemplate topology2 = topologies.get(1).getTopology(); assertEquals(8, topology1.getNodeTemplateOrRelationshipTemplate().size()); assertEquals(8, topology2.getNodeTemplateOrRelationshipTemplate().size()); List<String> types1 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology1.getNodeTemplates()) { types1.add(nodeTemplate.getType().getLocalPart()); } assertEquals(5, types1.size()); assertEquals(3, types1.stream().filter(type -> type.equalsIgnoreCase("shetland_pony")).count()); assertEquals(1, types1.stream().filter(type -> type.equalsIgnoreCase("pasture")).count()); assertEquals(1, types1.stream().filter(type -> type.equalsIgnoreCase("field_-w1-wip1")).count()); for (TNodeTemplate nodeTemplate : topologies.get(0).getTopLevelHosts()) { List<TNodeTemplate> predecessors = topologies.get(0).getHostedOnPredecessors(nodeTemplate); if (nodeTemplate.getType().getLocalPart().equalsIgnoreCase("pasture")) { assertEquals(predecessors.size(), 2); } else if (nodeTemplate.getType().getLocalPart().equalsIgnoreCase("field")) { assertEquals(predecessors.size(), 1); } } List<String> types2 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology2.getNodeTemplates()) { types2.add(nodeTemplate.getType().getLocalPart()); } assertEquals(5, types2.size()); assertEquals(3, types2.stream().filter(type -> type.equalsIgnoreCase("shetland_pony")).count()); assertEquals(1, types2.stream().filter(type -> type.equalsIgnoreCase("pasture")).count()); assertEquals(1, types2.stream().filter(type -> type.equalsIgnoreCase("field_-w1-wip1")).count()); for (TNodeTemplate nodeTemplate : topologies.get(1).getTopLevelHosts()) { List<TNodeTemplate> predecessors = topologies.get(1).getHostedOnPredecessors(nodeTemplate); if (nodeTemplate.getType().getLocalPart().equalsIgnoreCase("pasture")) { assertEquals(predecessors.size(), 1); } else if (nodeTemplate.getType().getLocalPart().equalsIgnoreCase("field")) { assertEquals(predecessors.size(), 2); } } }
@Test public void testNoTargetLabelsPresent() throws Exception { ServiceTemplateId id = new ServiceTemplateId("http: "TargetAllocationMinHostsTest4", false); TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate()); Criteria minHosts = new MinHosts(null, Integer.MAX_VALUE); List<String> ntIdsWithoutTargetLabel = topology.getTopLevelNTs().stream() .filter(nt -> !ModelUtilities.getTargetLabel(nt).isPresent()).map(HasId::getId).collect(Collectors.toList()); List<TopologyWrapper> topologies = minHosts.allocate(topology); assertEquals(2, topologies.size()); TTopologyTemplate topology1 = topologies.get(0).getTopology(); TTopologyTemplate topology2 = topologies.get(1).getTopology(); assertEquals(17, topology1.getNodeTemplateOrRelationshipTemplate().size()); assertEquals(17, topology2.getNodeTemplateOrRelationshipTemplate().size()); List<String> types1 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology1.getNodeTemplates()) { types1.add(nodeTemplate.getType().getLocalPart()); } assertEquals(10, types1.size()); assertEquals(7, types1.stream().filter(type -> type.equalsIgnoreCase("shetland_pony")).count()); assertEquals(2, types1.stream().filter(type -> type.equalsIgnoreCase("pasture")).count()); for (TNodeTemplate nodeTemplate : topologies.get(0).getTopLevelNTs()) { if (ntIdsWithoutTargetLabel.contains(nodeTemplate.getId())) { assertEquals("pasture", topologies.get(0).getHostedOnSuccessor(nodeTemplate).getType().getLocalPart()); } } List<String> types2 = new ArrayList<>(); for (TNodeTemplate nodeTemplate : topology2.getNodeTemplates()) { types2.add(nodeTemplate.getType().getLocalPart()); } assertEquals(10, types2.size()); assertEquals(7, types2.stream().filter(type -> type.equalsIgnoreCase("shetland_pony")).count()); assertEquals(2, types2.stream().filter(type -> type.equalsIgnoreCase("field_-w1-wip1")).count()); for (TNodeTemplate nodeTemplate : topologies.get(1).getTopLevelNTs()) { if (ntIdsWithoutTargetLabel.contains(nodeTemplate.getId())) { assertEquals("field_-w1-wip1", topologies.get(1).getHostedOnSuccessor(nodeTemplate).getType().getLocalPart()); } } }
@Test public void testFilter() throws Exception { List<TopologyWrapper> topologies = new ArrayList<>(); for (int i = 1; i < 5; i++) { ServiceTemplateId id = new ServiceTemplateId("http: "TargetAllocationMinHostsTest" + i, false); topologies.add(new TopologyWrapper(repository.getElement(id).getTopologyTemplate())); } assertEquals(4, topologies.size()); Criteria minHosts = new MinHosts(null, Integer.MAX_VALUE); List<TopologyWrapper> filtered = minHosts.filter(topologies); assertEquals(2, filtered.size()); assertEquals(5, filtered.get(0).getNodeTemplates().size()); }