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