2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.controller;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.io.BufferedReader;
28 import java.io.StringReader;
29 import java.io.UnsupportedEncodingException;
30 import java.util.ArrayList;
31 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.GlobalRoleSettings;
43 import org.onap.portalsdk.core.domain.User;
44 import org.onap.portalsdk.core.util.SystemProperties;
45 import org.springframework.mock.web.MockHttpServletResponse;
47 public class AdminTabControllerTest {
49 private static Logger logger = FlexLogger.getLogger(AdminTabControllerTest.class);
50 private static CommonClassDao commonClassDao;
51 private HttpServletRequest request;
52 private MockHttpServletResponse response;
55 public void setUp() throws Exception {
57 logger.info("setUp: Entering");
58 commonClassDao = mock(CommonClassDao.class);
60 request = mock(HttpServletRequest.class);
61 response = new MockHttpServletResponse();
63 HttpSession mockSession = mock(HttpSession.class);
64 User user = new User();
65 user.setOrgUserId("Test");
66 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
67 Mockito.when(request.getSession(false)).thenReturn(mockSession);
69 AdminTabController.setCommonClassDao(commonClassDao);
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);
80 public void testGetAdminRole(){
81 AdminTabController admin = new AdminTabController();
83 admin.getAdminTabEntityData(request, response);
84 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("lockdowndata"));
85 } catch (UnsupportedEncodingException e) {
86 logger.error("Exception Occured"+e);
92 public void testSaveAdminRole() throws Exception{
93 AdminTabController admin = new AdminTabController();
94 String data = "{\"lockdowndata\":{\"lockdown\":true}}";
95 BufferedReader reader = new BufferedReader(new StringReader(data));
97 when(request.getReader()).thenReturn(reader);
98 admin.saveAdminTabLockdownValue(request, response);
99 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("descriptiveScopeDictionaryDatas"));
100 } catch (UnsupportedEncodingException e) {
101 logger.error("Exception Occured"+e);