Use lombok for data objects
[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-2019 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
23 package org.onap.policy.controller;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.BufferedReader;
31 import java.io.StringReader;
32 import java.io.UnsupportedEncodingException;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpSession;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.rest.dao.CommonClassDao;
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 PolicyRolesControllerTest {
48     private static Logger logger = FlexLogger.getLogger(PolicyRolesControllerTest.class);
49     private HttpServletRequest request = null;
50     private MockHttpServletResponse response = null;
51     private PolicyRolesController controller = null;
52     private static CommonClassDao commonClassDao;
53
54     /**
55      * Before.
56      *
57      * @throws Exception exception
58      */
59     @Before
60     public void setUp() throws Exception {
61         logger.info("setUp: Entering");
62         controller = new PolicyRolesController();
63         commonClassDao = Mockito.mock(CommonClassDao.class);
64         request = mock(HttpServletRequest.class);
65         response = new MockHttpServletResponse();
66         controller.setCommonClassDao(commonClassDao);
67
68         HttpSession mockSession = mock(HttpSession.class);
69         User user = new User();
70         user.setOrgUserId("Test");
71         Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
72         Mockito.when(request.getSession(false)).thenReturn(mockSession);
73
74         logger.info("setUp: exit");
75     }
76
77     @Test
78     public void testGetPolicyRolesEntityData() {
79         try {
80             controller.getPolicyRolesEntityData(request, response);
81             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("rolesDatas"));
82         } catch (UnsupportedEncodingException e) {
83             logger.error("Exception Occured" + e);
84             fail();
85         }
86     }
87
88     @Test
89     public void testGetPolicyScopesEntityData() {
90         try {
91             controller.getPolicyScopesEntityData(request, response);
92             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("scopeDatas"));
93         } catch (UnsupportedEncodingException e) {
94             logger.error("Exception Occured" + e);
95             fail();
96         }
97     }
98
99     @Test
100     public void testSaveRolesEntityData() {
101         logger.info("PolicyRolesControllerTest: Entering");
102         String jsonString =
103                 "{\"editRoleData\": {\"id\": 1,\"loginId\": {\"userLoginId\" : \"test\", \"userName\" : \"test\"},"
104                         + "\"role\":\"test\",\"scope\":[\"scope\"]}}";
105         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
106             when(request.getReader()).thenReturn(br);
107             controller.SaveRolesEntityData(request, response);
108             logger.info("response.getContentAsString(): " + response.getContentAsString());
109             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("rolesDatas"));
110         } catch (Exception e) {
111             fail("Exception: " + e);
112         }
113         logger.info("PolicyRolesControllerTest: exit");
114     }
115 }