a4999ff421bce36297d6941855ad94995680f0a2
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / AdminTabControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.policy.controller;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.io.UnsupportedEncodingException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.GlobalRoleSettings;
42 import org.onap.portalsdk.core.domain.User;
43 import org.onap.portalsdk.core.util.SystemProperties;
44 import org.springframework.mock.web.MockHttpServletResponse;
45
46 public class AdminTabControllerTest {
47
48     private static Logger logger = FlexLogger.getLogger(AdminTabControllerTest.class);
49     private static CommonClassDao commonClassDao;
50     private HttpServletRequest request;
51     private MockHttpServletResponse response;
52
53     @Before
54     public void setUp() throws Exception {
55
56         logger.info("setUp: Entering");
57         commonClassDao = mock(CommonClassDao.class);
58
59         request = mock(HttpServletRequest.class);
60         response = new MockHttpServletResponse();
61
62         HttpSession mockSession = mock(HttpSession.class);
63         User user = new User();
64         user.setOrgUserId("Test");
65         Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name")))
66                 .thenReturn(user);
67         Mockito.when(request.getSession(false)).thenReturn(mockSession);
68
69         AdminTabController.setCommonClassDao(commonClassDao);
70
71         GlobalRoleSettings globalRole = new GlobalRoleSettings();
72         globalRole.setLockdown(true);
73         globalRole.setRole("super-admin");
74         List<Object> globalRoles = new ArrayList<>();
75         globalRoles.add(globalRole);
76         when(commonClassDao.getData(GlobalRoleSettings.class)).thenReturn(globalRoles);
77     }
78
79     @Test
80     public void testGetAdminRole() {
81         AdminTabController admin = new AdminTabController();
82         try {
83             admin.getAdminTabEntityData(request, response);
84             assertTrue(response.getContentAsString() != null
85                     && response.getContentAsString().contains("lockdowndata"));
86         } catch (UnsupportedEncodingException e) {
87             logger.error("Exception Occured" + e);
88             fail();
89         }
90     }
91
92     @Test
93     public void testSaveAdminRole() throws Exception {
94         AdminTabController admin = new AdminTabController();
95         String data = "{\"lockdowndata\":{\"lockdown\":true}}";
96         BufferedReader reader = new BufferedReader(new StringReader(data));
97         try {
98             when(request.getReader()).thenReturn(reader);
99             admin.saveAdminTabLockdownValue(request, response);
100             assertTrue(response.getContentAsString() != null
101                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
102         } catch (UnsupportedEncodingException e) {
103             logger.error("Exception Occured" + e);
104             fail();
105         }
106     }
107 }