Upgraded the latest ONAP SDK
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.controller;
21
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;
26
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;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
35
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;
46
47 public class AdminTabControllerTest {
48
49         private static Logger logger = FlexLogger.getLogger(AdminTabControllerTest.class);
50         private static CommonClassDao commonClassDao;
51         private HttpServletRequest request;
52         private MockHttpServletResponse response;
53         
54         @Before
55         public void setUp() throws Exception {
56
57                 logger.info("setUp: Entering");
58         commonClassDao = mock(CommonClassDao.class);
59         
60         request = mock(HttpServletRequest.class);       
61                 response =  new MockHttpServletResponse();
62                 
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);
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 && response.getContentAsString().contains("lockdowndata"));
85                 } catch (UnsupportedEncodingException e) {
86                         logger.error("Exception Occured"+e);
87                         fail();
88                 }
89         }
90         
91         @Test
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));     
96                 try {
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);
102                         fail();
103                 }
104         }
105         
106 }