NodeTemplateUtils { public static Capability getCapabilityByType(NodeTemplate nodeTemplate, String capabilityTypeName) { Entry<String, Capability> capabilityEntry = getCapabilitEntryyByType(nodeTemplate, capabilityTypeName); return capabilityEntry == null ? null : capabilityEntry.getValue(); } private  NodeTemplateUtils(); static Entry<String, Capability> getCapabilitEntryyByType(NodeTemplate nodeTemplate, String capabilityTypeName); static Capability getCapabilityByType(NodeTemplate nodeTemplate, String capabilityTypeName); static Capability getCapabilityByTypeOrFail(NodeTemplate nodeTemplate, String capabilityType); static void setCapability(NodeTemplate nodeTemplate, String name, Capability capability); static int countRelationshipsForRequirement(NodeTemplate nodeTemplate, RequirementDefinition requirementDefinition);  }
@Test public void getCapabilityByTypeTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); Capability capability = getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.SampleCapability"); assertSame(nodeCapability, capability); CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.TestCapability"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertSame(nodeCapability, capability); }
@Test public void getMissingCapabilityByTypeTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); CapabilityType capabilityType = new CapabilityType(); capabilityType.setElementId("org.alien4cloud.capabilities.SampleCapability"); capabilityType.setDerivedFrom(Lists.newArrayList("org.alien4cloud.capabilities.TestCapability")); Mockito.reset(csarRepositorySearchService); Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("org.alien4cloud.capabilities.SampleCapability"), Mockito.any(Set.class))).thenReturn(capabilityType); Capability capability = toscaContextualAspect.execInToscaContext(() -> getCapabilityByType(nodeTemplate, "org.alien4cloud.capabilities.Unknown"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertNull(capability); }