+ List<Role> listOfRoles = Stream.of(roles)
+ .collect(Collectors.toList());
+ }
+
+ @Test
+ void testUpdateVolumeGroup() {
+ Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
+ bl.updateVolumeGroup(resource);
+ assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(true));
+ }
+
+ @Test
+ void testUpdateVolumeGroupNull() {
+ Resource resource = getResourceWithType("HEAT_VOL", "org.openecomp.groups.VfModule");
+ resource.setGroups(null);
+ bl.updateVolumeGroup(resource);
+ assertThat(resource.getGroups()).isNull();
+ }
+
+ @Test
+ void testUpdateVolumeGroupFail() {
+ Resource resource = getResourceWithType("NON_EXIST_HEAT", "org.openecomp.groups.VfModule");
+ bl.updateVolumeGroup(resource);
+ assertThat(resource.getGroups().get(0).getProperties().get(0).getValue()).isEqualTo(Boolean.toString(false));
+ }
+
+ private Resource getResourceWithType(String artifactType, String groupDefinitionType) {
+ ArtifactDefinition artifactToUpdate = new ArtifactDefinition();
+ List<GroupDefinition> groups = new ArrayList<>();
+ GroupDefinition gd = new GroupDefinition();
+ List<PropertyDataDefinition> properties = new ArrayList<>();
+ PropertyDataDefinition pdd = new PropertyDataDefinition();
+ Map<String, ArtifactDefinition> artifacts = new HashMap<>();
+ List<String> artifactsList = new ArrayList<>();
+
+ artifactToUpdate.setArtifactType(artifactType);
+ artifactToUpdate.setArtifactName(artifactType);
+ artifactToUpdate.setUniqueId(artifactType);
+ Resource resource = createResourceObjectCsar(true);
+ artifactsList.add(artifactToUpdate.getArtifactName());
+
+ pdd.setName("volume_group");
+ pdd.setValue("true");
+ pdd.setType(ToscaPropertyType.BOOLEAN.getType());
+
+ artifacts.put(artifactToUpdate.getArtifactName(), artifactToUpdate);
+
+ properties.add(pdd);
+ gd.setType(groupDefinitionType);
+ gd.setProperties(properties);
+ gd.setArtifacts(artifactsList);
+ groups.add(gd);
+
+ resource.setGroups(groups);
+ resource.setDeploymentArtifacts(artifacts);
+ return resource;
+ }
+
+ @Test
+ void testGetAllCertifiedResources() {
+ List<Resource> list = bl.getAllCertifiedResources(true, HighestFilterEnum.HIGHEST_ONLY, "USER");
+ assertEquals(reslist, list);
+ }
+
+ @Test
+ void testGetAllCertifiedResources_exception() {
+ assertThrows(StorageException.class, () -> {
+ List<Resource> list = bl.getAllCertifiedResources(false, HighestFilterEnum.NON_HIGHEST_ONLY, "USER");
+ assertEquals(reslist, list);
+ });
+ }
+
+ @Test
+ void testValidateResourceNameExists() {
+ Either<Map<String, Boolean>, ResponseFormat> res = bl.validateResourceNameExists("Resource", ResourceTypeEnum.CR, "jh0003");
+ assertEquals(true, res.isLeft());
+ }
+
+ @Test
+ void rollbackWithEitherAlwaysReturnARuntimeException() {
+ JanusGraphDao janusGraphDao = mockJanusGraphDao;
+ ActionStatus actionStatus = ActionStatus.INPUTS_NOT_FOUND;
+ String params = "testName";
+
+ Either<Object, RuntimeException> result =
+ ResourceBusinessLogic.rollbackWithEither(janusGraphDao, actionStatus, params);
+
+ assertTrue(result.isRight());
+ assertTrue(result.right().value() instanceof ByActionStatusComponentException);
+ }
+
+ @Test
+ void rollbackWithEitherWorksWithNullJanusGraphDao() {
+ JanusGraphDao janusGraphDao = null;
+ ActionStatus actionStatus = ActionStatus.INPUTS_NOT_FOUND;
+ String params = "testName";
+
+ Either<Object, RuntimeException> result =
+ ResourceBusinessLogic.rollbackWithEither(janusGraphDao, actionStatus, params);
+
+ assertTrue(result.isRight());
+ assertTrue(result.right().value() instanceof ByActionStatusComponentException);
+ }
+
+ @Test
+ void testDeleteResource_NotFound() {
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+ ResponseFormat respFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND),
+ "");
+ ResponseFormat actualResponseFormat = bl.deleteResource("1", user);
+ assertEquals(respFormat.getStatus(), actualResponseFormat.getStatus());
+ }
+
+ @Test
+ void testDeleteResource_NotArchived() {
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(Either.left(resourceResponse));
+ ComponentException actualComponentException = assertThrows(ComponentException.class,
+ () -> bl.deleteResourceAllVersions(resourceResponse.getUniqueId(), user));
+ assertEquals(ActionStatus.COMPONENT_NOT_ARCHIVED, actualComponentException.getActionStatus());
+ assertEquals("my-resource_name with space:0.1", actualComponentException.getParams()[0]);
+ }
+
+ @Test
+ void testDeleteResource_IsInUse() {
+ Resource resourceObject = createResourceObject(true);
+ Mockito.when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(resourceObject));
+ resourceObject.setArchived(true);
+ OperationException oe = new OperationException(ActionStatus.COMPONENT_IN_USE_BY_ANOTHER_COMPONENT, "resource_name");
+ Mockito.when(toscaOperationFacade.deleteComponent(resourceObject.getInvariantUUID(), NodeTypeEnum.Resource, true)).thenThrow(oe);
+ OperationException actualOperationException = assertThrows(OperationException.class,
+ () -> bl.deleteResourceAllVersions(resourceResponse.getUniqueId(), user));
+ assertEquals(ActionStatus.COMPONENT_IN_USE_BY_ANOTHER_COMPONENT, actualOperationException.getActionStatus());
+ assertEquals("resource_name", actualOperationException.getParams()[0]);
+ }
+
+
+ @Test
+ void testCreateResource_withMultitenancyWithTenant_Success() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ validateUserRoles(Role.ADMIN, Role.DESIGNER);
+ Resource resource = createResourceObject(false);
+ resource.setTenant(TEST_TENANT);
+ Resource createdResource = null;
+ try {
+ when(toscaOperationFacade
+ .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
+ .thenReturn(Either.left(false));
+ createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
+ assertThat(createResourceObject(true)).isEqualTo(createdResource);
+ MatcherAssert.assertThat("Unauthorized Tenant", getTestRoles().contains(resource.getTenant()));
+ } catch (ComponentException e) {
+ assertThat(Integer.valueOf(200)).isEqualTo(e.getResponseFormat()
+ .getStatus());
+ }
+ }
+
+ @Test
+ void testCreateResource_withMultitenancyWithInvalidTenant_Failure() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ validateUserRoles(Role.ADMIN, Role.DESIGNER);
+ Resource resource = createResourceObject(false);
+ resource.setTenant("invalid_tenant");
+ Resource createdResource = null;
+ try {
+ MatcherAssert.assertThat("Unauthorized Tenant", !getTestRoles().contains(resource.getTenant()));
+ when(toscaOperationFacade
+ .validateComponentNameAndModelExists(resource.getName(), null, ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE))
+ .thenReturn(Either.left(false));
+ createdResource = bl.createResource(resource, AuditingActionEnum.CREATE_RESOURCE, user, null, null);
+
+ assertThat(createResourceObject(true)).isEqualTo(createdResource);
+ MatcherAssert.assertThat("Unauthorized Tenant", !getTestRoles().contains(resource.getTenant()));
+ } catch (ComponentException e) {
+ assertThat(new Integer(200)).isEqualTo(e.getResponseFormat()
+ .getStatus());
+ }
+ }
+
+ private Set<String> getTestRoles(){
+ Set<String> roles = new HashSet<>();
+ roles.add("test_admin");
+ roles.add("test_tenant");
+ return roles;