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