68da8e7143dda061df81ab14bb251f91b533f2a0
[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, 2019 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
23 package org.onap.policy.controller;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.BufferedReader;
31 import java.io.StringReader;
32 import java.io.UnsupportedEncodingException;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpSession;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.rest.dao.CommonClassDao;
45 import org.onap.policy.rest.jpa.GlobalRoleSettings;
46 import org.onap.portalsdk.core.domain.User;
47 import org.onap.portalsdk.core.util.SystemProperties;
48 import org.springframework.mock.web.MockHttpServletResponse;
49
50 public class AdminTabControllerTest {
51
52     private static Logger logger = FlexLogger.getLogger(AdminTabControllerTest.class);
53     private static CommonClassDao commonClassDao;
54     private HttpServletRequest request;
55     private MockHttpServletResponse response;
56
57     @Before
58     public void setUp() throws Exception {
59
60         logger.info("setUp: Entering");
61         commonClassDao = mock(CommonClassDao.class);
62
63         request = mock(HttpServletRequest.class);
64         response = new MockHttpServletResponse();
65
66         HttpSession mockSession = mock(HttpSession.class);
67         User user = new User();
68         user.setOrgUserId("Test");
69         Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
70         Mockito.when(request.getSession(false)).thenReturn(mockSession);
71
72         AdminTabController.setCommonClassDao(commonClassDao);
73
74         GlobalRoleSettings globalRole = new GlobalRoleSettings();
75         globalRole.setLockdown(true);
76         globalRole.setRole("super-admin");
77         List<Object> globalRoles = new ArrayList<>();
78         globalRoles.add(globalRole);
79         when(commonClassDao.getData(GlobalRoleSettings.class)).thenReturn(globalRoles);
80     }
81
82     @Test
83     public void testGetAdminRole() {
84         AdminTabController admin = new AdminTabController();
85         try {
86             admin.getAdminTabEntityData(request, response);
87             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("lockdowndata"));
88         } catch (UnsupportedEncodingException e) {
89             logger.error("Exception Occured" + e);
90             fail();
91         }
92     }
93
94     @Test
95     public void testSaveAdminRole() throws Exception {
96         AdminTabController admin = new AdminTabController();
97         String data = "{\"lockdowndata\":{\"lockdown\":true}}";
98         BufferedReader reader = new BufferedReader(new StringReader(data));
99         try {
100             when(request.getReader()).thenReturn(reader);
101             admin.saveAdminTabLockdownValue(request, response);
102             assertTrue(response.getContentAsString() != null
103                     && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
104         } catch (UnsupportedEncodingException e) {
105             logger.error("Exception Occured" + e);
106             fail();
107         }
108     }
109 }