Add unit test for portal in dashboard
[nonrtric.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / portalapi / PortalAuthManagerTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.portalapi;
21
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.lang.reflect.InvocationTargetException;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.Cookie;
28
29 import org.junit.Assert;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
33 import org.oransc.ric.portal.dashboard.DashboardUserManager;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
39 import org.springframework.mock.web.MockHttpServletRequest;
40 import org.springframework.mock.web.MockHttpServletResponse;
41 import org.springframework.test.context.ActiveProfiles;
42 import org.springframework.test.context.junit.jupiter.SpringExtension;
43
44 @ExtendWith(SpringExtension.class)
45 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
46 @ActiveProfiles("test")
47 public class PortalAuthManagerTest {
48
49     @Value("${portalapi.decryptor}")
50     private String decryptor;
51
52     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54     @Test
55     public void testPortalStuff() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
56         InvocationTargetException, NoSuchMethodException, IOException, ServletException {
57
58         PortalAuthManager m = new PortalAuthManager("app", "user", "secret", decryptor, "cookie");
59         Assert.assertNotNull(m.getAppCredentials());
60         String s = null;
61
62         MockHttpServletRequest request = new MockHttpServletRequest();
63         s = m.validateEcompSso(request);
64         logger.debug("validateEcompSso answers {}", s);
65         Assert.assertNull(s);
66
67         Cookie cookie = new Cookie(PortalApiConstants.EP_SERVICE, "bogus");
68         request.setCookies(cookie);
69         s = m.validateEcompSso(request);
70         logger.debug("validateEcompSso answers {}", s);
71         Assert.assertNull(s);
72
73         DashboardUserManager dum = new DashboardUserManager(true);
74         PortalAuthenticationFilter filter = new PortalAuthenticationFilter(false, m, dum);
75         filter.init(null);
76         filter.destroy();
77         MockHttpServletResponse response = new MockHttpServletResponse();
78         try {
79             filter.doFilter(request, response, null);
80         } catch (NullPointerException ex) {
81             logger.debug("chain is null");
82         }
83
84         filter = new PortalAuthenticationFilter(true, m, dum);
85         try {
86             filter.doFilter(request, response, null);
87         } catch (NullPointerException ex) {
88             logger.debug("chain is null");
89         }
90     }
91
92 }