summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org
diff options
context:
space:
mode:
authorKartik Hegde <kh00735564@techmahindra.com>2022-11-12 14:29:11 +0530
committerKartik Hegde <kh00735564@techmahindra.com>2022-12-21 12:04:30 +0000
commitcf04a1a714ef4a1df973929dc750232b4d67d7b4 (patch)
treeedd2e6e5074ba58efb98663a1ea1851208895fff /catalog-be/src/test/java/org
parentf995db01ee95606b6cded82822a73435ebc190c8 (diff)
Multitenancy in SDC
Issue-ID: SDC-4215 Change-Id: Ie24ba38acc9f1998d4a7e722e8f98456dab9201d Signed-off-by: Kartik Hegde <kh00735564@techmahindra.com>
Diffstat (limited to 'catalog-be/src/test/java/org')
-rw-r--r--catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java84
-rw-r--r--catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java57
-rw-r--r--catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java43
3 files changed, 184 insertions, 0 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java
index 9fb0efb862..60d45ea752 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ElementBusinessLogicTest.java
@@ -38,6 +38,9 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.HashMap;
+import java.util.stream.Collectors;
+import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
@@ -68,6 +71,8 @@ import org.openecomp.sdc.exception.ResponseFormat;
class ElementBusinessLogicTest extends BaseBusinessLogicMock {
+ private static final boolean MULTITENANCY_ENABLED = true;
+ private static final String TEST_TENANT = "test_tenant";
private User user;
@Mock
@@ -319,4 +324,83 @@ class ElementBusinessLogicTest extends BaseBusinessLogicMock {
assertTrue(elementBusinessLogic.getBaseTypes("CAT01", user.getUserId(), null).isRight());
}
+
+ @Test
+ void testGetFollowed_withMultitenancyValidTenant_thenReturnsSuccess() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ user.setUserId("admin1");
+ user.setRole(Role.ADMIN.name());
+ Set<Component> resources = new HashSet<>();
+ Set<Component> services = new HashSet<>();
+ Resource resource = new Resource();
+ Service service = new Service();
+ service.setTenant(TEST_TENANT);
+ resource.setTenant(TEST_TENANT);
+ Assert.assertNotNull(service.getTenant());
+ Assert.assertNotNull(resource.getTenant());
+ resources.add(resource);
+ services.add(service);
+ Assert.assertNotNull(getTestRoles());
+ Assert.assertTrue(getTestRoles().contains(TEST_TENANT));
+
+ when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
+ .thenReturn(Either.left(resources));
+ when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.SERVICE)))
+ .thenReturn(Either.left(services));
+ Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
+ Set<String> realmroles =getTestRoles();
+ Map<String, List<? extends Component>> dataResponse = new HashMap<>();
+ result.entrySet().stream()
+ .forEach(component->{component.setValue(component.getValue().stream().filter(cm->realmroles.stream()
+ .anyMatch(role->cm.getTenant().equals(role))).collect(Collectors.toList()));
+ dataResponse.put(component.getKey(), component.getValue());
+ });
+ assertEquals(result.size(), dataResponse.values().size());
+ assertEquals(1, dataResponse.get("services").size());
+ assertEquals(1, dataResponse.get("resources").size());
+ }
+
+ @Test
+ void testGetFollowed_withMultitenancyInValidTenant_thenReturnsEmptyList() {
+ String INVALID_TENANT="invalid_tenant";
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ user.setUserId("admin1");
+ user.setRole(Role.ADMIN.name());
+ Set<Component> resources = new HashSet<>();
+ Set<Component> services = new HashSet<>();
+ Resource resource = new Resource();
+ Service service = new Service();
+ service.setTenant(INVALID_TENANT);
+ resource.setTenant(INVALID_TENANT);
+ Assert.assertNotNull(service.getTenant());
+ Assert.assertNotNull(resource.getTenant());
+ resources.add(resource);
+ services.add(service);
+ Assert.assertNotNull(getTestRoles());
+
+ when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.RESOURCE)))
+ .thenReturn(Either.left(resources));
+ when(toscaOperationFacade.getFollowed(any(), anySet(), any(), eq(ComponentTypeEnum.SERVICE)))
+ .thenReturn(Either.left(services));
+ Map<String, List<? extends Component>> result = elementBusinessLogic.getFollowed(user).left().value();
+ Set<String> realmroles =getTestRoles();
+ Map<String, List<? extends Component>> dataResponse = new HashMap<>();
+ result.entrySet().stream()
+ .forEach(component->{component.setValue(component.getValue().stream().filter(cm->realmroles.stream()
+ .anyMatch(role->cm.getTenant().equals(role))).collect(Collectors.toList()));
+ dataResponse.put(component.getKey(), component.getValue());
+ });
+
+ assertEquals(result.size(), dataResponse.values().size(),"No Data available for "+INVALID_TENANT);
+ assertEquals(0, dataResponse.get("services").size());
+ assertEquals(0, dataResponse.get("resources").size());
+ }
+
+ private Set<String> getTestRoles(){
+ Set<String> roles = new HashSet<>();
+ roles.add("test_admin");
+ roles.add("test_tenant");
+ return roles;
+ }
+
} \ No newline at end of file
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java
index 714ec20c10..e9735710da 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java
@@ -47,12 +47,16 @@ import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.ServletContext;
import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.hamcrest.MatcherAssert;
+import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
@@ -171,6 +175,8 @@ class ResourceBusinessLogicTest {
private static final String GENERIC_VF_NAME = "org.openecomp.resource.abstract.nodes.VF";
private static final String GENERIC_CR_NAME = "org.openecomp.resource.abstract.nodes.CR";
private static final String GENERIC_PNF_NAME = "org.openecomp.resource.abstract.nodes.PNF";
+ private static final boolean MULTITENANCY_ENABLED = true;
+ private static final String TEST_TENANT = "test_tenant";
private final ServletContext servletContext = Mockito.mock(ServletContext.class);
private IElementOperation mockElementDao;
@@ -2556,4 +2562,55 @@ class ResourceBusinessLogicTest {
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;
+ }
+
}
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java
index 69938cd527..a5f823b12f 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java
@@ -30,6 +30,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.when;
@@ -44,8 +46,12 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
+import java.util.Set;
+import java.util.HashSet;
import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.hamcrest.MatcherAssert;
+import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
@@ -83,6 +89,8 @@ class ServiceBusinessLogicTest extends ServiceBusinessLogicBaseTestSetup {
private final static String DEFAULT_ICON = "defaulticon";
private static final String ALREADY_EXIST = "alreadyExist";
+ private static final boolean MULTITENANCY_ENABLED = true;
+ private static final String TEST_TENANT = "test_tenant";
@Test
void testGetComponentAuditRecordsCertifiedVersion() {
@@ -1069,4 +1077,39 @@ class ServiceBusinessLogicTest extends ServiceBusinessLogicBaseTestSetup {
return propertyList;
}
+
+ @Test
+ void testCreateService_withMultitenancyValidTenant_Success() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ Service service = createServiceObject(false);
+ service.setTenant(TEST_TENANT);
+ when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
+ Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
+
+ if (createResponse.isRight()) {
+ assertEquals(new Integer(200), createResponse.right().value().getStatus());
+ }
+ MatcherAssert.assertThat("Unauthorized Tenant", getTestRoles().contains(service.getTenant()));
+ assertEquals(TEST_TENANT, service.getTenant());
+ assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
+ }
+
+
+ @Test
+ void testCreateService_withMultitenancyInvalidTenant_Failure() {
+ Service service = createServiceObject(false);
+ service.setTenant("invalid_tenant");
+ when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
+ Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
+ MatcherAssert.assertThat("Unauthorized Tenant", !getTestRoles().contains(service.getTenant()));
+ assertNotEquals(TEST_TENANT, service.getTenant());
+ assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
+ }
+
+ private Set<String> getTestRoles(){
+ Set<String> roles = new HashSet<>();
+ roles.add("test_admin");
+ roles.add("test_tenant");
+ return roles;
+ }
}