Added Junits for POLICY-SDK-APP controllers
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / PolicyRolesControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * POLICY-SDK-APP
4  * ================================================================================
5  * Copyright (C) 2018 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
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpSession;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.portalsdk.core.domain.User;
41 import org.onap.portalsdk.core.util.SystemProperties;
42 import org.springframework.mock.web.MockHttpServletResponse;
43
44 public class PolicyRolesControllerTest {
45         private static Logger logger = FlexLogger.getLogger(PolicyRolesControllerTest.class);
46         private HttpServletRequest request = null;
47         private MockHttpServletResponse response = null;
48         private PolicyRolesController controller = null;        
49         private static CommonClassDao commonClassDao;
50         
51         @Before
52         public void setUp() throws Exception {
53                 logger.info("setUp: Entering");
54                 controller = new PolicyRolesController();
55                 commonClassDao = Mockito.mock(CommonClassDao.class);
56                 request = mock(HttpServletRequest.class);       
57                 response =  new MockHttpServletResponse();
58                 controller.setCommonClassDao(commonClassDao);
59                 
60                 HttpSession mockSession = mock(HttpSession.class);
61         User user = new User();
62                 user.setOrgUserId("Test");
63                 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
64                 Mockito.when(request.getSession(false)).thenReturn(mockSession);
65                 
66                 logger.info("setUp: exit");
67         }
68
69         @Test
70         public void testGetPolicyRolesEntityData() {
71         try {
72                 controller.getPolicyRolesEntityData(request, response);
73                 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("rolesDatas"));
74         } catch (UnsupportedEncodingException e) {
75                 logger.error("Exception Occured"+e);
76                 fail();
77         }
78 }
79
80         @Test
81         public void testGetPolicyScopesEntityData() {
82                 try {
83                         controller.getPolicyScopesEntityData(request, response);
84                         assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("scopeDatas"));
85                 } catch (UnsupportedEncodingException e) {
86                         logger.error("Exception Occured"+e);
87                         fail();
88                 }
89         }
90
91         @Test
92         public void testSaveRolesEntityData() {
93                         logger.info("PolicyRolesControllerTest: Entering");
94                         String jsonString = "{\"editRoleData\": {\"id\": 1,\"loginId\": {\"userLoginId\" : \"test\", \"userName\" : \"test\"},\"role\":\"test\",\"scope\":[\"scope\"]}}";
95                         try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
96                                 when(request.getReader()).thenReturn(br);                   
97                                 controller.SaveRolesEntityData(request, response);
98                                 logger.info("response.getContentAsString(): " + response.getContentAsString());
99                                 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rolesDatas"));
100                         } catch (Exception e) {
101                         fail("Exception: " + e);
102                 }
103                 logger.info("PolicyRolesControllerTest: exit");
104         }
105 }
106