summaryrefslogtreecommitdiffstats
path: root/vid-app-common/src
diff options
context:
space:
mode:
authorEinat Vinouze <einat.vinouze@intl.att.com>2020-02-12 12:03:35 +0200
committerEinat Vinouze <einat.vinouze@intl.att.com>2020-02-13 10:31:37 +0200
commitd59a9fd8aef8442c1c96a524ff06fb0582d0d814 (patch)
treea1e69c8c89bbd5193aba90cd5590e5812b09f2c0 /vid-app-common/src
parentf3ae1b251eecdf18c2827eb03db23e10ad9ddb76 (diff)
translate owning entity name to owning entity ID
Issue-ID: VID-758 Change-Id: Ic89829ac4590537f0279052ceb9e8432ea0de878 Signed-off-by: Einat Vinouze <einat.vinouze@intl.att.com>
Diffstat (limited to 'vid-app-common/src')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java41
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java90
2 files changed, 120 insertions, 11 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java
index c35f5f704..84b0fa934 100644
--- a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java
+++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java
@@ -21,6 +21,9 @@
package org.onap.vid.roles;
+import static java.util.stream.Collectors.toMap;
+import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
+
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
@@ -31,14 +34,19 @@ import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
+import org.jetbrains.annotations.NotNull;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.web.support.UserUtils;
import org.onap.vid.aai.AaiResponse;
import org.onap.vid.aai.exceptions.RoleParsingException;
+import org.onap.vid.category.CategoryParameterOptionRep;
+import org.onap.vid.category.CategoryParametersResponse;
+import org.onap.vid.model.CategoryParameter.Family;
import org.onap.vid.model.ModelConstants;
import org.onap.vid.model.Subscriber;
import org.onap.vid.model.SubscriberList;
import org.onap.vid.services.AaiService;
+import org.onap.vid.services.CategoryParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -54,21 +62,27 @@ public class RoleProvider {
private Function<HttpServletRequest, Integer> getUserIdFunction;
private Function<HttpServletRequest, Map> getRolesFunction;
private final RoleValidatorFactory roleValidatorFactory;
+ private final CategoryParameterService categoryParameterService;
+
@Autowired
- public RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory) {
+ public RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory,
+ CategoryParameterService categoryParameterService) {
this.aaiService=aaiService;
this.roleValidatorFactory = roleValidatorFactory;
+ this.categoryParameterService = categoryParameterService;
getUserIdFunction = UserUtils::getUserId;
getRolesFunction = UserUtils::getRoles;
}
RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory,
- Function<HttpServletRequest, Integer> getUserIdFunction, Function<HttpServletRequest, Map> getRolesFunction) {
+ Function<HttpServletRequest, Integer> getUserIdFunction, Function<HttpServletRequest, Map> getRolesFunction,
+ CategoryParameterService categoryParameterService) {
this.aaiService = aaiService;
this.roleValidatorFactory = roleValidatorFactory;
this.getRolesFunction = getRolesFunction;
this.getUserIdFunction = getUserIdFunction;
+ this.categoryParameterService = categoryParameterService;
}
public List<Role> getUserRoles(HttpServletRequest request) {
@@ -79,6 +93,8 @@ public class RoleProvider {
List<Role> roleList = new ArrayList<>();
Map roles = getRolesFunction.apply(request);
+ final Map<String, String> owningEntityMap = owningEntityNameToOwningEntityIdMapper();
+
for (Object role : roles.keySet()) {
org.onap.portalsdk.core.domain.Role sdkRol = (org.onap.portalsdk.core.domain.Role) roles.get(role);
@@ -90,7 +106,7 @@ public class RoleProvider {
continue;
}
String[] roleParts = splitRole((sdkRol.getName()), logPrefix);
- roleList.add(createRoleFromStringArr(roleParts, logPrefix));
+ roleList.add(createRoleFromStringArr(roleParts, logPrefix, owningEntityMap));
String msg = String.format("%s User %s got permissions %s", logPrefix, userId, Arrays.toString(roleParts));
LOG.debug(EELFLoggerDelegate.debugLogger, msg);
} catch (Exception e) {
@@ -140,9 +156,9 @@ public class RoleProvider {
return replacement;
}
- public Role createRoleFromStringArr(String[] roleParts, String rolePrefix) throws RoleParsingException {
+ public Role createRoleFromStringArr(String[] roleParts, String rolePrefix, Map<String, String> owningEntityMap) throws RoleParsingException {
String globalCustomerID = replaceSubscriberNameToGlobalCustomerID(roleParts[0], rolePrefix);
- String owningEntityId = translateOwningEntityNameToOwningEntityId(roleParts[0]);
+ String owningEntityId = translateOwningEntityNameToOwningEntityId(roleParts[0], owningEntityMap);
try {
if (roleParts.length > 2) {
@@ -162,8 +178,19 @@ public class RoleProvider {
}
- private String translateOwningEntityNameToOwningEntityId(String owningEntityName) {
- return owningEntityName; // TODO: translate to id
+ // in case the owningEntity name is not found in the owningEntityMap - return the name
+ protected String translateOwningEntityNameToOwningEntityId(String owningEntityName, Map<String, String> owningEntityMap) {
+ return owningEntityMap.getOrDefault(owningEntityName, owningEntityName);
+ }
+
+ @NotNull
+ protected Map<String, String> owningEntityNameToOwningEntityIdMapper() {
+ CategoryParametersResponse categoryParametersResponse = categoryParameterService.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
+ Map<String, List<CategoryParameterOptionRep>> categoryMap = categoryParametersResponse.getCategoryParameters();
+ List<CategoryParameterOptionRep> owningEntityList = categoryMap.get("owningEntity");
+
+ return emptyIfNull(owningEntityList).stream().collect(toMap(
+ CategoryParameterOptionRep::getName, CategoryParameterOptionRep::getId));
}
public RoleValidator getUserRolesValidator(HttpServletRequest request) {
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 8d81c929c..5f4fc7861 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
@@ -21,11 +21,15 @@
package org.onap.vid.roles;
+import static java.util.Collections.emptyMap;
+import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
@@ -34,10 +38,15 @@ import org.assertj.core.util.Lists;
import org.mockito.Mock;
import org.onap.vid.aai.AaiResponse;
import org.onap.vid.aai.exceptions.RoleParsingException;
+import org.onap.vid.category.CategoryParametersResponse;
+import org.onap.vid.model.CategoryParameter.Family;
import org.onap.vid.model.Subscriber;
import org.onap.vid.model.SubscriberList;
import org.onap.vid.services.AaiService;
+import org.onap.vid.services.CategoryParameterService;
+import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class RoleProviderTest {
@@ -49,6 +58,9 @@ public class RoleProviderTest {
private static final String SAMPLE_SERVICE = "sampleService";
private static final String SAMPLE_TENANT = "sampleTenant";
private static final String SAMPLE_ROLE_PREFIX = "prefix";
+ private static final String EXISTING_OWNING_ENTITY_NAME = "WayneHolland";
+ private static final String EXISTING_OWNING_ENTITY_ID = "d61e6f2d-12fa-4cc2-91df-7c244011d6fc";
+ private static final String NOT_EXISTING_OWNING_ENTITY_NAME = "notExistingOwningEntity";
@Mock
private AaiService aaiService;
@@ -62,13 +74,21 @@ public class RoleProviderTest {
@Mock
private RoleValidatorFactory roleValidatorFactory;
+ @Mock
+ private CategoryParameterService categoryParameterService;
+
private RoleProvider roleProvider;
@BeforeMethod
public void setUp() {
initMocks(this);
- roleProvider = new RoleProvider(aaiService, roleValidatorFactory, httpServletRequest -> 5, httpServletRequest -> createRoles());
+ roleProvider = new RoleProvider(aaiService, roleValidatorFactory, httpServletRequest -> 5,
+ httpServletRequest -> createRoles(),
+ categoryParameterService);
+
+ when(categoryParameterService.getCategoryParameters(any()))
+ .thenReturn(new CategoryParametersResponse(emptyMap()));
}
@Test
@@ -84,7 +104,7 @@ public class RoleProviderTest {
setSubscribers();
String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
- Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
+ Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX, emptyMap());
assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID);
@@ -98,7 +118,7 @@ public class RoleProviderTest {
String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
- Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
+ Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX, emptyMap());
assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID);
@@ -110,7 +130,7 @@ public class RoleProviderTest {
public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
setSubscribers();
- roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
+ roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX, emptyMap());
}
@Test
@@ -158,6 +178,28 @@ public class RoleProviderTest {
assertThat(result).isEqualTo(expectedRoleValidator);
}
+ @DataProvider
+ public static Object[][] owningEntityNameAndId() {
+ return new Object[][] {
+ {"owning entity name exist on the response, id is returned ", EXISTING_OWNING_ENTITY_NAME, EXISTING_OWNING_ENTITY_ID},
+ {"owning entity name dont exist on the response, name is returned", NOT_EXISTING_OWNING_ENTITY_NAME, NOT_EXISTING_OWNING_ENTITY_NAME},
+ };
+ }
+
+ @Test(dataProvider = "owningEntityNameAndId")
+ public void translateOwningEntityNameToOwningEntityId_shouldTranslateNameToId(String description,
+ String owningEntityName, String expectedId) {
+ String owningEntityId = roleProvider.translateOwningEntityNameToOwningEntityId(owningEntityName,
+ ImmutableMap.of(
+ EXISTING_OWNING_ENTITY_NAME, EXISTING_OWNING_ENTITY_ID,
+ "anyName", "anyId"
+ ));
+
+ Assert.assertEquals(owningEntityId, expectedId);
+ }
+
+
+
private String owningEntityId() {
// while translateOwningEntityNameToOwningEntityId does nothing, no translation happens.
// this will be changed later.
@@ -180,4 +222,44 @@ public class RoleProviderTest {
role2.setName("sampleSubscriber___sampleService___sampleTenant");
return ImmutableMap.of(1L, role1, 2L, role2);
}
+
+ @Test
+ public void owningEntityNameToOwningEntityIdMapper_readsOwningEntityCorrectly() throws JsonProcessingException {
+
+ final String categoryParametersResponse = ""
+ + "{ "
+ + " \"categoryParameters\": { "
+ + " \"lineOfBusiness\": [ "
+ + " { \"id\": \"ONAP\", \"name\": \"ONAP\" }, "
+ + " { \"id\": \"zzz1\", \"name\": \"zzz1\" } "
+ + " ], "
+ + " \"owningEntity\": [ "
+ + " { \"id\": \"aaa1\", \"name\": \"aaa1\" }, "
+ + " { \"id\": \"" + EXISTING_OWNING_ENTITY_ID + "\", \"name\": \"" + EXISTING_OWNING_ENTITY_NAME + "\" }, "
+ + " { \"id\": \"Melissa\", \"name\": \"Melissa\" } ], "
+ + " \"project\": [ "
+ + " { \"id\": \"WATKINS\", \"name\": \"WATKINS\" }, "
+ + " { \"id\": \"x1\", \"name\": \"x1\" }, "
+ + " { \"id\": \"yyy1\", \"name\": \"yyy1\" } "
+ + " ], "
+ + " \"platform\": [ "
+ + " { \"id\": \"platform\", \"name\": \"platform\" }, "
+ + " { \"id\": \"xxx1\", \"name\": \"xxx1\" } "
+ + " ] "
+ + " } "
+ + "}";
+
+ CategoryParametersResponse categoryParameterResponse =
+ new ObjectMapper().readValue(categoryParametersResponse, CategoryParametersResponse.class);
+
+ when(categoryParameterService.getCategoryParameters(Family.PARAMETER_STANDARDIZATION))
+ .thenReturn(categoryParameterResponse);
+
+ org.hamcrest.MatcherAssert.assertThat(roleProvider.owningEntityNameToOwningEntityIdMapper(),
+ jsonEquals(ImmutableMap.of(
+ "aaa1", "aaa1",
+ "Melissa", "Melissa",
+ EXISTING_OWNING_ENTITY_NAME, EXISTING_OWNING_ENTITY_ID
+ )));
+ }
}