From 59b3997941ecf730beaa979c17ff6c9d303c87d5 Mon Sep 17 00:00:00 2001 From: Michal Kabaj Date: Sat, 23 Mar 2019 00:10:14 +0100 Subject: ControllerUtils tests and improvements - added junits - Injecting SystemPropertiesWrapper via constructor - switched extractUserId is now instance method instead of static Change-Id: Ic706b780c64248e44207a1dee723981a4ca7ed74 Issue-ID: VID-449 Signed-off-by: Michal Kabaj --- .../org/onap/vid/controller/AaiController.java | 6 ++- .../controller/AsyncInstantiationController.java | 7 +++- .../org/onap/vid/controller/ControllersUtils.java | 44 +++++++++++----------- .../OperationalEnvironmentController.java | 11 ++++-- 4 files changed, 38 insertions(+), 30 deletions(-) (limited to 'vid-app-common/src/main/java/org/onap') diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java index a8e1e2b02..e2a914004 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java @@ -41,6 +41,7 @@ import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleProvider; import org.onap.vid.roles.RoleValidator; import org.onap.vid.services.AaiService; +import org.onap.vid.utils.SystemPropertiesWrapper; import org.onap.vid.utils.Unchecked; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -96,9 +97,10 @@ public class AaiController extends RestrictedBaseController { private AaiService aaiService; @Autowired private RoleProvider roleProvider; - @Autowired private AAIRestInterface aaiRestInterface; + @Autowired + private SystemPropertiesWrapper systemPropertiesWrapper; /** * Welcome method. @@ -140,7 +142,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = {"/getuserID"}, method = RequestMethod.GET) public ResponseEntity getUserID(HttpServletRequest request) { - String userId = ControllersUtils.extractUserId(request); + String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(request); return new ResponseEntity<>(userId, HttpStatus.OK); } diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/AsyncInstantiationController.java b/vid-app-common/src/main/java/org/onap/vid/controller/AsyncInstantiationController.java index a204d3a9a..081e3c640 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/AsyncInstantiationController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/AsyncInstantiationController.java @@ -31,6 +31,7 @@ import org.onap.vid.model.serviceInstantiation.ServiceInstantiation; import org.onap.vid.mso.MsoResponseWrapper2; import org.onap.vid.services.AsyncInstantiationBusinessLogic; import org.onap.vid.services.AuditService; +import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -48,6 +49,7 @@ public class AsyncInstantiationController extends VidRestrictedBaseController { public static final String ASYNC_INSTANTIATION = "asyncInstantiation"; protected final AsyncInstantiationBusinessLogic asyncInstantiationBL; + private final SystemPropertiesWrapper systemPropertiesWrapper; protected ObjectMapper objectMapper = new ObjectMapper(); @@ -55,8 +57,9 @@ public class AsyncInstantiationController extends VidRestrictedBaseController { protected AuditService auditService; @Autowired - public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL) { + public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL, SystemPropertiesWrapper systemPropertiesWrapper) { this.asyncInstantiationBL = asyncInstantiationBL; + this.systemPropertiesWrapper = systemPropertiesWrapper; } @ExceptionHandler(OperationNotAllowedException.class) @@ -84,7 +87,7 @@ public class AsyncInstantiationController extends VidRestrictedBaseController { catch (Exception e) { LOGGER.error(EELFLoggerDelegate.errorLogger, "failed to log incoming ServiceInstantiation request ", e); } - String userId = ControllersUtils.extractUserId(httpServletRequest); + String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest); List uuids = asyncInstantiationBL.pushBulkJob(request, userId); return new MsoResponseWrapper2(200, uuids); diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/ControllersUtils.java b/vid-app-common/src/main/java/org/onap/vid/controller/ControllersUtils.java index 7139b29fb..befbe0320 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/ControllersUtils.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/ControllersUtils.java @@ -3,13 +3,14 @@ * VID * ================================================================================ * Copyright (C) 2017 - 2019 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. @@ -20,35 +21,26 @@ package org.onap.vid.controller; +import static org.onap.vid.utils.Logging.getMethodCallerName; + +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import javax.ws.rs.WebApplicationException; import org.apache.commons.lang3.exception.ExceptionUtils; import org.onap.portalsdk.core.domain.User; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.model.ExceptionResponse; +import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.http.ResponseEntity; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import javax.ws.rs.WebApplicationException; - -import static org.onap.vid.utils.Logging.getMethodCallerName; +public final class ControllersUtils { -public class ControllersUtils { + private final SystemPropertiesWrapper systemPropertiesWrapper; - - public static String extractUserId(HttpServletRequest request) { - String userId = ""; - HttpSession session = request.getSession(); - if (session != null) { - User user = (User) session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)); - if (user != null) { - //userId = user.getHrid(); - userId = user.getLoginId(); - if (userId == null) - userId = user.getOrgUserId(); - } - } - return userId; + public ControllersUtils(SystemPropertiesWrapper systemPropertiesWrapper) { + this.systemPropertiesWrapper = systemPropertiesWrapper; } public static ExceptionResponse handleException(Exception e, EELFLoggerDelegate logger) { @@ -62,4 +54,12 @@ public class ControllersUtils { return ResponseEntity.status(e.getResponse().getStatus()).body(ControllersUtils.handleException(e, logger)); } + public String extractUserId(HttpServletRequest request) { + Optional user = Optional.ofNullable(request.getSession()) + .map((HttpSession he) -> (User) he + .getAttribute(systemPropertiesWrapper.getProperty(SystemProperties.USER_ATTRIBUTE_NAME))); + + return user.map(User::getLoginId).isPresent() + ? user.map(User::getLoginId).orElse("") : user.map(User::getOrgUserId).orElse(""); + } } diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/OperationalEnvironmentController.java b/vid-app-common/src/main/java/org/onap/vid/controller/OperationalEnvironmentController.java index fe94dfd80..a6778ad0c 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/OperationalEnvironmentController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/OperationalEnvironmentController.java @@ -37,6 +37,7 @@ import org.onap.vid.mso.model.OperationalEnvironmentActivateInfo; import org.onap.vid.mso.model.OperationalEnvironmentDeactivateInfo; import org.onap.vid.mso.rest.OperationalEnvironment.OperationEnvironmentRequestDetails; import org.onap.vid.mso.rest.RequestDetails; +import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.MissingServletRequestParameterException; @@ -60,20 +61,22 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle private final MsoInterface restMso; private final MsoBusinessLogic msoBusinessLogic; + private final SystemPropertiesWrapper systemPropertiesWrapper; private static final Pattern RECOVERY_ACTION_MESSAGE_PATTERN = Pattern.compile("from String \"(.*)\": value not"); @Autowired - public OperationalEnvironmentController(MsoBusinessLogic msoBusinessLogic, MsoInterface msoClientInterface) { + public OperationalEnvironmentController(MsoBusinessLogic msoBusinessLogic, MsoInterface msoClientInterface, SystemPropertiesWrapper systemPropertiesWrapper) { this.restMso = msoClientInterface; this.msoBusinessLogic = msoBusinessLogic; + this.systemPropertiesWrapper = systemPropertiesWrapper; } @RequestMapping(value = "/create", method = RequestMethod.POST) public MsoResponseWrapper2 createOperationalEnvironment(HttpServletRequest request, @RequestBody OperationalEnvironmentCreateBody operationalEnvironment) { debugStart(operationalEnvironment); - String userId = ControllersUtils.extractUserId(request); + String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(request); RequestDetailsWrapper requestDetailsWrapper = msoBusinessLogic.convertParametersToRequestDetails(operationalEnvironment, userId); String path = msoBusinessLogic.getOperationalEnvironmentCreationPath(); @@ -94,7 +97,7 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle throw new BadManifestException("Manifest structure is wrong"); } - String userId = ControllersUtils.extractUserId(request); + String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(request); OperationalEnvironmentActivateInfo activateInfo = new OperationalEnvironmentActivateInfo(activateRequest, userId, operationalEnvironmentId); debugStart(activateInfo); @@ -115,7 +118,7 @@ public class OperationalEnvironmentController extends VidRestrictedBaseControlle verifyIsNotEmpty(operationalEnvironmentId, "operationalEnvironment"); - String userId = ControllersUtils.extractUserId(request); + String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(request); OperationalEnvironmentDeactivateInfo deactivateInfo = new OperationalEnvironmentDeactivateInfo(userId, operationalEnvironmentId); debugStart(deactivateInfo); -- cgit 1.2.3-korg