First version of NonRT RIC Controlpanel
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / config / PortalApIMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.config;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doAnswer;
25 import static org.mockito.Mockito.mock;
26
27 import java.lang.invoke.MethodHandles;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.onap.portalsdk.core.onboarding.crossapi.PortalRestAPIProxy;
34 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
35 import org.oransc.portal.nonrtric.controlpanel.portalapi.PortalAuthManager;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.boot.web.servlet.ServletRegistrationBean;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Profile;
42
43 @Configuration
44 @Profile("test")
45 public class PortalApIMockConfiguration {
46
47     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48
49     // Unfortunately EPSDK-FW does not define these as constants
50     public static final String PORTAL_USERNAME_HEADER_KEY = "username";
51     public static final String PORTAL_PASSWORD_HEADER_KEY = "password";
52
53     @Bean
54     public ServletRegistrationBean<PortalRestAPIProxy> portalApiProxyServlet() {
55         PortalRestAPIProxy servlet = new PortalRestAPIProxy();
56         final ServletRegistrationBean<PortalRestAPIProxy> servletBean =
57             new ServletRegistrationBean<>(servlet, PortalApiConstants.API_PREFIX + "/*");
58         servletBean.setName("PortalRestApiProxyServlet");
59         return servletBean;
60     }
61
62     @Bean
63     public PortalAuthManager portalAuthManager() throws Exception {
64         PortalAuthManager mockManager = mock(PortalAuthManager.class);
65         final Map<String, String> credentialsMap = new HashMap<>();
66         credentialsMap.put("appName", "appName");
67         credentialsMap.put(PORTAL_USERNAME_HEADER_KEY, PORTAL_USERNAME_HEADER_KEY);
68         credentialsMap.put(PORTAL_PASSWORD_HEADER_KEY, PORTAL_PASSWORD_HEADER_KEY);
69         doAnswer(inv -> {
70             logger.debug("getAppCredentials");
71             return credentialsMap;
72         }).when(mockManager).getAppCredentials();
73         doAnswer(inv -> {
74             logger.debug("getUserId");
75             return "userId";
76         }).when(mockManager).validateEcompSso(any(HttpServletRequest.class));
77         doAnswer(inv -> {
78             logger.debug("getAppCredentials");
79             return credentialsMap;
80         }).when(mockManager).getAppCredentials();
81         return mockManager;
82     }
83
84 }