aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'vid-app-common/src/test/java/org')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/controller/PropertyControllerTest.java164
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/controller/RoleGeneratorControllerTest.java84
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java113
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java164
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/roles/RoleTest.java60
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java105
6 files changed, 481 insertions, 209 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/PropertyControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/PropertyControllerTest.java
index e9d2cfd44..5dbe010bf 100644
--- a/vid-app-common/src/test/java/org/onap/vid/controller/PropertyControllerTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/controller/PropertyControllerTest.java
@@ -1,39 +1,139 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Modifications Copyright 2019 Nokia
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
package org.onap.vid.controller;
-import org.junit.Test;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.servlet.ModelAndView;
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import javax.servlet.http.HttpServletRequest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.log4j.BasicConfigurator;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.vid.category.CategoryParameterOptionRep;
+import org.onap.vid.category.CategoryParametersResponse;
+import org.onap.vid.model.CategoryParameter.Family;
+import org.onap.vid.services.CategoryParameterService;
+import org.onap.vid.utils.SystemPropertiesWrapper;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+@RunWith(MockitoJUnitRunner.class)
public class PropertyControllerTest {
- private PropertyController createTestSubject() {
- return new PropertyController();
- }
-
- @Test
- public void testWelcome() throws Exception {
- PropertyController testSubject;
- HttpServletRequest request = null;
- ModelAndView result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.welcome(request);
- }
-
-
- @Test
- public void testGetProperty() throws Exception {
- PropertyController testSubject;
- String name = "";
- String defaultvalue = "";
- HttpServletRequest request = null;
- ResponseEntity<String> result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getProperty(name, defaultvalue, request);
- }
+ private static final String GET_PROPERTY = "/get_property/{name}/{defaultvalue}";
+ private static final String CATEGORY_PARAMETER = "/category_parameter";
+
+ private static final String ERROR_MSG = "Internal error occurred: ";
+ private static final String FAMILY_NAME = "familyName";
+
+ private PropertyController propertyController;
+ private MockMvc mockMvc;
+ private ObjectMapper objectMapper;
+
+ @Mock
+ private CategoryParameterService service;
+ @Mock
+ private SystemPropertiesWrapper systemPropertiesWrapper;
+
+ @Before
+ public void setUp() {
+ propertyController = new PropertyController(service, systemPropertiesWrapper);
+ BasicConfigurator.configure();
+ mockMvc = MockMvcBuilders.standaloneSetup(propertyController).build();
+ objectMapper = new ObjectMapper();
+ }
+
+ @Test
+ public void shouldReturnInputJson_whenPropertyIsNotFound() throws Exception {
+ String inputJson = "{key1: val1}";
+ given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(inputJson);
+
+ mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(inputJson));
+ }
+
+ @Test
+ public void shouldReturnGivenJson_whenPropertyIsFound() throws Exception {
+ String propertyJson = "{key1: val1}";
+ String inputJson = "{key2: val2}";
+ given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(propertyJson);
+
+ mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(propertyJson));
+ }
+
+ @Test
+ public void shouldReturnInternalServerError_whenExceptionIsThrownFromSystemProperties() throws Exception {
+ String exceptionMessage = "Test exception message from system properties";
+ String inputJson = "{key1: val1}";
+ given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willThrow(new RuntimeException(exceptionMessage));
+
+ mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isInternalServerError())
+ .andExpect(content().string(ERROR_MSG + exceptionMessage));
+ }
+
+ @Test
+ public void shouldReturnResponse_whenResponseIsFound() throws Exception {
+
+ CategoryParametersResponse categoryParametersResponse =
+ new CategoryParametersResponse(
+ ImmutableMap.of(
+ "key1", ImmutableList.of(
+ new CategoryParameterOptionRep("testId", "testName"))));
+
+ given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willReturn(categoryParametersResponse);
+
+ mockMvc.perform(get(CATEGORY_PARAMETER)
+ .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse)));
+ }
+
+ @Test
+ public void shouldReturnInternalServerError_whenExceptionIsThrownFromService() throws Exception {
+ String exceptionMessage = "Test exception message from category parameter service";
+ given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willThrow(new RuntimeException(
+ exceptionMessage));
+
+ mockMvc.perform(get(CATEGORY_PARAMETER)
+ .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isInternalServerError())
+ .andExpect(content().string(ERROR_MSG + exceptionMessage));
+ }
} \ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/RoleGeneratorControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/RoleGeneratorControllerTest.java
index c19b810d0..e1c6504ed 100644
--- a/vid-app-common/src/test/java/org/onap/vid/controller/RoleGeneratorControllerTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/controller/RoleGeneratorControllerTest.java
@@ -1,26 +1,80 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Modifications Copyright 2019 Nokia
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
package org.onap.vid.controller;
+import static org.mockito.BDDMockito.given;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import org.apache.log4j.BasicConfigurator;
+import org.junit.Before;
import org.junit.Test;
-import org.springframework.http.ResponseEntity;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.vid.services.RoleGeneratorService;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+@RunWith(MockitoJUnitRunner.class)
public class RoleGeneratorControllerTest {
- private RoleGeneratorController createTestSubject() {
- return new RoleGeneratorController();
+ private static final String PATH = "/generateRoleScript/{firstRun}";
+
+ private static final String FIRST_JSON = "{key1: val1}";
+ private static final String SECOND_JSON = "{key2: val2}";
+
+ private RoleGeneratorController roleGeneratorController;
+ private MockMvc mockMvc;
+
+ @Mock
+ private RoleGeneratorService service;
+
+ @Before
+ public void setUp() {
+ roleGeneratorController = new RoleGeneratorController(service);
+ BasicConfigurator.configure();
+ mockMvc = MockMvcBuilders.standaloneSetup(roleGeneratorController).build();
+
+ given(service.generateRoleScript(true)).willReturn(FIRST_JSON);
+ given(service.generateRoleScript(false)).willReturn(SECOND_JSON);
+ }
+
+ @Test
+ public void generateRoleScript_shouldReturnJson_whenFirstRun() throws Exception {
+ mockMvc.perform(get(PATH, "true")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(FIRST_JSON));
}
@Test
- public void testGenerateRoleScript() throws Exception {
- RoleGeneratorController testSubject;
- boolean firstRun = false;
- ResponseEntity<String> result;
-
- // default test
- try {
- testSubject = createTestSubject();
- result = testSubject.generateRoleScript(firstRun);
- } catch (Exception e) {
-
- }
+ public void generateRoleScript_shouldReturnJson_whenNoFirstRun() throws Exception {
+ mockMvc.perform(get(PATH, "false")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(SECOND_JSON));
}
} \ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java
index 9d625c4f7..82242b6d1 100644
--- a/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/mso/MsoUtilTest.java
@@ -1,50 +1,83 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
package org.onap.vid.mso;
-import org.glassfish.jersey.client.ClientResponse;
-import org.junit.Assert;
+import io.joshworks.restclient.http.HttpResponse;
+import io.joshworks.restclient.http.mapper.ObjectMapper;
+import org.apache.http.HttpResponseFactory;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.message.BasicStatusLine;
import org.junit.Test;
-public class MsoUtilTest {
-
- private MsoUtil createTestSubject() {
- return new MsoUtil();
- }
-
- @Test
- public void testWrapResponse() throws Exception {
- String body = "";
- int statusCode = 0;
- MsoResponseWrapper result;
+import static org.apache.http.HttpStatus.SC_OK;
+import static org.apache.http.HttpVersion.HTTP_1_1;
+import static org.assertj.core.api.Assertions.assertThat;
- // default test
- result = MsoUtil.wrapResponse(body, statusCode);
- }
-
-
- @Test
- public void testWrapResponse_2() throws Exception {
- RestObject<String> rs = null;
- MsoResponseWrapper result;
-
- // test 1
- result = MsoUtil.wrapResponse(rs);
- Assert.assertNotNull(result);
- }
+public class MsoUtilTest {
- @Test
- public void testConvertPojoToString() throws Exception {
- String result;
+ @Test
+ public void shouldWrapRestObject() {
+ // given
+ String entity = "entity";
+ RestObject<String> restObject = new RestObject<>();
+ restObject.set(entity);
+ restObject.setStatusCode(SC_OK);
+ // when
+ MsoResponseWrapper result = MsoUtil.wrapResponse(restObject);
+ // then
+ assertThat(result.getEntity()).isEqualTo(entity);
+ assertThat(result.getStatus()).isEqualTo(SC_OK);
+ }
- // test 1
- result = MsoUtil.convertPojoToString(null);
- Assert.assertEquals("", result);
- }
+ @Test
+ public void shouldWrapHttpResponse() throws Exception {
+ // given
+ HttpResponse<String> httpResponse = createTestHttpResponse(SC_OK, null);
+ // when
+ MsoResponseWrapper result = MsoUtil.wrapResponse(httpResponse);
+ // then
+ assertThat(result.getEntity()).isEqualTo(null);
+ assertThat(result.getStatus()).isEqualTo(SC_OK);
+ }
- @Test
- public void testMain() throws Exception {
- String[] args = new String[] { "" };
+ @Test
+ public void shouldWrapHttpResponseWithEntity() throws Exception {
+ // given
+ String entity = "entity";
+ HttpResponse<String> httpResponse = createTestHttpResponse(SC_OK, entity);
+ // when
+ MsoResponseWrapper result = MsoUtil.wrapResponse(httpResponse);
+ // then
+ assertThat(result.getEntity()).isEqualTo(entity);
+ assertThat(result.getStatus()).isEqualTo(SC_OK);
+ }
- // default test
- MsoUtil.main(args);
- }
+ private HttpResponse<String> createTestHttpResponse(int statusCode, String entity) throws Exception {
+ HttpResponseFactory factory = new DefaultHttpResponseFactory();
+ org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HTTP_1_1, statusCode, null), null);
+ if (entity != null) {
+ response.setEntity(new StringEntity(entity));
+ }
+ return new HttpResponse<>(response, String.class, null);
+ }
} \ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java
index 6fdc21f78..3c22ea718 100644
--- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java
@@ -1,36 +1,144 @@
package org.onap.vid.roles;
-import org.junit.Test;
+import com.google.common.collect.ImmutableMap;
+import io.joshworks.restclient.http.HttpResponse;
+import org.assertj.core.util.Lists;
+import org.mockito.Mock;
+import org.onap.vid.aai.exceptions.RoleParsingException;
+import org.onap.vid.model.Subscriber;
+import org.onap.vid.model.SubscriberList;
+import org.onap.vid.services.AaiService;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
public class RoleProviderTest {
- private RoleProvider createTestSubject() {
- return new RoleProvider();
- }
-
-
-// @Test
-// public void testGetUserRoles() throws Exception {
-// RoleProvider testSubject;
-// HttpServletRequest request = null;
-// List<Role> result;
-//
-// // default test
-// testSubject = createTestSubject();
-// result = testSubject.getUserRoles(request);
-// }
-
-
- @Test
- public void testSplitRole() throws Exception {
- RoleProvider testSubject;
- String roleAsString = "";
- String[] result;
-
- // default test
- testSubject = createTestSubject();
- //TODO:fix result = testSubject.splitRole(roleAsString);
- }
+ private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
+ private static final String SAMPLE_CUSTOMER_ID = "sampleCustomerId";
+ private static final String SERVICE_TYPE_LOGS = "LOGS";
+ private static final String TENANT_PERMITTED = "PERMITTED";
+ private static final String SAMPLE_SERVICE = "sampleService";
+ private static final String SAMPLE_TENANT = "sampleTenant";
+ private static final String SAMPLE_ROLE_PREFIX = "prefix";
+
+ @Mock
+ private AaiService aaiService;
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Mock
+ private HttpResponse<SubscriberList> subscriberListHttpResponse;
+
+
+ private RoleProvider roleProvider;
+
+
+ @BeforeMethod
+ public void setUp() {
+ initMocks(this);
+ roleProvider = new RoleProvider(aaiService, httpServletRequest -> 5, httpServletRequest -> createRoles());
+ }
+
+ @Test
+ public void shouldSplitRolesWhenDelimiterIsPresent() {
+ String roles = "role_a___role_b";
+
+ assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
+ }
+
+
+ @Test
+ public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
+ setSubscribers();
+ String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
+
+ Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
+
+ assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
+ assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID);
+ assertThat(role.getTenant()).isEqualTo(SAMPLE_TENANT);
+ assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
+ }
+
+ @Test
+ public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
+ setSubscribers();
+
+ String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
+
+ Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
+
+ assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
+ assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID);
+ assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
+ assertThat(role.getTenant()).isNullOrEmpty();
+ }
+
+ @Test(expectedExceptions = RoleParsingException.class)
+ public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
+ setSubscribers();
+
+ roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
+ }
+
+ @Test
+ public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
+ Role expectedRole = new Role(EcompRole.READ, SAMPLE_CUSTOMER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
+ setSubscribers();
+
+ List<Role> userRoles = roleProvider.getUserRoles(request);
+
+
+ assertThat(userRoles.size()).isEqualTo(1);
+ Role actualRole = userRoles.get(0);
+
+ assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
+ assertThat(actualRole.getSubscribeName()).isEqualTo(expectedRole.getSubscribeName());
+ assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
+ }
+
+ @Test
+ public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
+ assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
+ }
+
+ @Test
+ public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
+ assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
+ }
+
+ @Test
+ public void userShouldHavePermissionToReadLogsWhenServiceAndTenantAreCorrect() {
+ Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT);
+ Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED);
+
+ assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
+ }
+
+ private void setSubscribers() {
+ Subscriber subscriber = new Subscriber();
+ subscriber.subscriberName = SAMPLE_SUBSCRIBER;
+ subscriber.globalCustomerId = SAMPLE_CUSTOMER_ID;
+ SubscriberList subscriberList = new SubscriberList(Lists.list(subscriber));
+ when(aaiService.getFullSubscriberList()).thenReturn(subscriberListHttpResponse);
+ when(subscriberListHttpResponse.getBody()).thenReturn(subscriberList);
+ }
+ private Map<Long, org.onap.portalsdk.core.domain.Role> createRoles() {
+ org.onap.portalsdk.core.domain.Role role1 = new org.onap.portalsdk.core.domain.Role();
+ role1.setName("read___role2");
+ org.onap.portalsdk.core.domain.Role role2 = new org.onap.portalsdk.core.domain.Role();
+ role2.setName("sampleSubscriber___sampleService___sampleTenant");
+ return ImmutableMap.of(1L, role1, 2L, role2);
+ }
} \ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleTest.java
deleted file mode 100644
index 463b29f57..000000000
--- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleTest.java
+++ /dev/null
@@ -1,60 +0,0 @@
-package org.onap.vid.roles;
-
-import org.junit.Test;
-
-public class RoleTest {
-
- private Role createTestSubject() {
- return new Role(EcompRole.READ, "", "", "");
- }
-
- @Test
- public void testGetEcompRole() throws Exception {
- Role testSubject;
- EcompRole result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getEcompRole();
- }
-
- @Test
- public void testGetSubscribeName() throws Exception {
- Role testSubject;
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getSubscribeName();
- }
-
- @Test
- public void testSetSubscribeName() throws Exception {
- Role testSubject;
- String subscribeName = "";
-
- // default test
- testSubject = createTestSubject();
- testSubject.setSubscribeName(subscribeName);
- }
-
- @Test
- public void testGetServiceType() throws Exception {
- Role testSubject;
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getServiceType();
- }
-
- @Test
- public void testGetTenant() throws Exception {
- Role testSubject;
- String result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getTenant();
- }
-} \ No newline at end of file
diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java
index b303b257c..adb257b01 100644
--- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java
@@ -1,59 +1,96 @@
package org.onap.vid.roles;
-import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import org.onap.vid.mso.rest.RequestDetails;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
public class RoleValidatorTest {
- private RoleValidator createTestSubject() {
- return new RoleValidator(null);
+ private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
+ private static final String NOT_MATCHING_SUBSCRIBER = "notMatchingSubscriber";
+ private static final String SAMPLE_SERVICE_TYPE = "sampleServiceType";
+ private static final String NOT_MATCHING_TENANT = "notMatchingTenant";
+ private static final String SAMPLE_TENANT = "sampleTenant";
+
+ private static final Role SAMPLE_ROLE = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT);
+
+ private List<Role> roles = ImmutableList.of(SAMPLE_ROLE);
+ private Map<String, Object> subscriberInfo = ImmutableMap.of("globalSubscriberId", SAMPLE_SUBSCRIBER);
+ private Map<String, Object> requestParameters = ImmutableMap.of("subscriptionServiceType", SAMPLE_SERVICE_TYPE);
+ private Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters);
+ private RequestDetails requestDetails;
+ private RoleValidator roleValidator;
+
+ @BeforeMethod
+ public void setUp() {
+ roleValidator = new RoleValidator(roles);
+ roleValidator.enableRoles();
+ requestDetails = new RequestDetails();
}
@Test
- public void testIsMsoRequestValid() throws Exception {
- RoleValidator testSubject;
- RequestDetails mso_request = null;
- boolean result;
+ public void shouldPermitSubscriberWhenNameMatchesAndRolesAreEnabled() {
+ assertThat(roleValidator.isSubscriberPermitted(SAMPLE_SUBSCRIBER)).isTrue();
+ }
- // default test
- testSubject = createTestSubject();
- result = testSubject.isMsoRequestValid(mso_request);
+ @Test
+ public void shouldNotPermitSubscriberWhenNameNotMatches() {
+ assertThat(roleValidator.isSubscriberPermitted(NOT_MATCHING_SUBSCRIBER)).isFalse();
}
@Test
- public void testIsServicePermitted() throws Exception {
- RoleValidator testSubject;
- String subscriberName = "";
- String serviceType = "";
- boolean result;
+ public void shouldPermitServiceWhenNamesMatches() {
+ assertThat(roleValidator.isServicePermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE)).isTrue();
+ }
- // default test
- testSubject = createTestSubject();
- result = testSubject.isServicePermitted(subscriberName, serviceType);
+
+ @Test
+ public void shouldNotPermitServiceWhenSubscriberNameNotMatches() {
+ assertThat(roleValidator.isServicePermitted(NOT_MATCHING_SUBSCRIBER, SAMPLE_SERVICE_TYPE)).isFalse();
}
@Test
- public void testIsSubscriberPermitted() throws Exception {
- RoleValidator testSubject;
- String subscriberName = "";
- boolean result;
+ public void shouldNotPermitServiceWhenServiceTypeNotMatches() {
+ assertThat(roleValidator.isServicePermitted(SAMPLE_SUBSCRIBER, NOT_MATCHING_SUBSCRIBER)).isFalse();
+ }
- // default test
- testSubject = createTestSubject();
- result = testSubject.isSubscriberPermitted(subscriberName);
+ @Test
+ public void shouldPermitTenantWhenNameMatches() {
+ assertThat(roleValidator.isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT)).isTrue();
+ }
+
+
+ @Test
+ public void shouldNotPermitTenantWhenNameNotMatches() {
+ assertThat(roleValidator.isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, NOT_MATCHING_TENANT)).isFalse();
}
@Test
- public void testIsTenantPermitted() throws Exception {
- RoleValidator testSubject;
- String globalCustomerId = "";
- String serviceType = "";
- String tenantName = "";
- boolean result;
+ public void shouldValidateProperlySORequest() {
+ requestDetails.setAdditionalProperty("requestDetails", requestDetailsProperties);
- // default test
- testSubject = createTestSubject();
- result = testSubject.isTenantPermitted(globalCustomerId, serviceType, tenantName);
+ assertThat(roleValidator.isMsoRequestValid(requestDetails)).isTrue();
}
+ @Test
+ public void shouldValidateUnknownSORequest() {
+ assertThat(roleValidator.isMsoRequestValid(new RequestDetails())).isTrue();
+ }
+
+ @Test
+ public void shouldRejectSORequestWhenSubscriberNotMatches() {
+ Map<String, Object> subscriberInfo = ImmutableMap.of("globalSubscriberId", "sample");
+ Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters);
+ requestDetails.setAdditionalProperty("requestDetails", requestDetailsProperties);
+
+ assertThat(roleValidator.isMsoRequestValid(requestDetails)).isFalse();
+ }
} \ No newline at end of file