d665aae5e32462e13dda6461249222bcf263fca5
[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  * 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 package org.onap.policy.controller;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.io.UnsupportedEncodingException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpSession;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
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.portalsdk.core.domain.User;
40 import org.onap.portalsdk.core.util.SystemProperties;
41 import org.springframework.mock.web.MockHttpServletResponse;
42
43 public class PolicyRolesControllerTest {
44     private static Logger logger = FlexLogger.getLogger(PolicyRolesControllerTest.class);
45     private HttpServletRequest request = null;
46     private MockHttpServletResponse response = null;
47     private PolicyRolesController controller = null;
48     private static CommonClassDao commonClassDao;
49
50     @Before
51     public void setUp() throws Exception {
52         logger.info("setUp: Entering");
53         controller = new PolicyRolesController();
54         commonClassDao = Mockito.mock(CommonClassDao.class);
55         request = mock(HttpServletRequest.class);
56         response = new MockHttpServletResponse();
57         controller.setCommonClassDao(commonClassDao);
58
59         HttpSession mockSession = mock(HttpSession.class);
60         User user = new User();
61         user.setOrgUserId("Test");
62         Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name")))
63                 .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
74                     && response.getContentAsString().contains("rolesDatas"));
75         } catch (UnsupportedEncodingException e) {
76             logger.error("Exception Occured" + e);
77             fail();
78         }
79     }
80
81     @Test
82     public void testGetPolicyScopesEntityData() {
83         try {
84             controller.getPolicyScopesEntityData(request, response);
85             assertTrue(response.getContentAsString() != null
86                     && response.getContentAsString().contains("scopeDatas"));
87         } catch (UnsupportedEncodingException e) {
88             logger.error("Exception Occured" + e);
89             fail();
90         }
91     }
92
93     @Test
94     public void testSaveRolesEntityData() {
95         logger.info("PolicyRolesControllerTest: Entering");
96         String jsonString =
97                 "{\"editRoleData\": {\"id\": 1,\"loginId\": {\"userLoginId\" : \"test\", \"userName\" : \"test\"},"
98                         + "\"role\":\"test\",\"scope\":[\"scope\"]}}";
99         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
100             when(request.getReader()).thenReturn(br);
101             controller.SaveRolesEntityData(request, response);
102             logger.info("response.getContentAsString(): " + response.getContentAsString());
103             assertTrue(response.getContentAsString() != null
104                     && response.getContentAsString().contains("rolesDatas"));
105         } catch (Exception e) {
106             fail("Exception: " + e);
107         }
108         logger.info("PolicyRolesControllerTest: exit");
109     }
110 }