NodeTemplateUtils { public static Capability getCapabilityByTypeOrFail(NodeTemplate nodeTemplate, String capabilityType) { Capability capability = getCapabilityByType(nodeTemplate, capabilityType); if (capability == null) { throw new NotFoundException(""); } return capability; } 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 getCapabilityByTypeOrFailTest() { NodeTemplate nodeTemplate = new NodeTemplate(); Capability nodeCapability = new Capability("org.alien4cloud.capabilities.SampleCapability", null); nodeTemplate.setCapabilities(Maps.newHashMap("test", nodeCapability)); Capability capability = getCapabilityByTypeOrFail(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(() -> getCapabilityByTypeOrFail(nodeTemplate, "org.alien4cloud.capabilities.TestCapability"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertSame(nodeCapability, capability); }
@Test(expected = NotFoundException.class) public void getMissingCapabilityByTypeOrFailTest() { 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(() -> getCapabilityByTypeOrFail(nodeTemplate, "org.alien4cloud.capabilities.Unknown"), false, Sets.newHashSet(new CSARDependency("org.alien4cloud.testArchive", "1.0.0-SNAPSHOT"))); assertNull(capability); }