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