JUnit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / BRMSDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-2019 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
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.servlet.http.HttpServletRequest;
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.pap.xacml.rest.util.DictionaryUtils;
43 import org.onap.policy.rest.dao.CommonClassDao;
44 import org.onap.policy.rest.jpa.BrmsParamTemplate;
45 import org.onap.policy.rest.jpa.UserInfo;
46 import org.springframework.mock.web.MockHttpServletResponse;
47
48 public class BRMSDictionaryControllerTest {
49
50     private static Logger logger = FlexLogger.getLogger(BRMSDictionaryControllerTest.class);
51     private static CommonClassDao commonClassDao;
52     private String jsonString = null;
53     private HttpServletRequest request = null;
54     private BRMSDictionaryController controller = null;
55     private MockHttpServletResponse response = null;
56
57     @Before
58     public void setUp() throws Exception {
59         logger.info("setUp: Entering");
60         commonClassDao = Mockito.mock(CommonClassDao.class);
61         UserInfo userInfo = new UserInfo();
62         userInfo.setUserLoginId("testUserId");
63         userInfo.setUserName("John");
64         when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "testing")).thenReturn(userInfo);
65         List<String> brms = new ArrayList<String>();
66         brms.add("BRMS-Model");
67         when(commonClassDao.getDataByColumn(BrmsParamTemplate.class, "name")).thenReturn(brms);
68         doNothing().when(commonClassDao).delete(new BrmsParamTemplate());
69         doNothing().when(commonClassDao).save(new BrmsParamTemplate());
70         controller = new BRMSDictionaryController();
71         request = Mockito.mock(HttpServletRequest.class);
72         response = new MockHttpServletResponse();
73         new DictionaryUtils(commonClassDao);
74         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
75         mock(DictionaryUtils.class);
76         logger.info("setUp: exit");
77     }
78
79     @Test
80     public void testGetBRMSParamDictionaryByNameEntityData() {
81         logger.info("testGetBRMSParamDictionaryByNameEntityData: Entering");
82         BRMSDictionaryController.setCommonClassDao(commonClassDao);
83         controller.getBRMSParamDictionaryByNameEntityData(response);
84         try {
85             assertTrue(response.getContentAsString() != null
86                     && response.getContentAsString().contains("brmsParamDictionaryDatas"));
87             logger.info("response.getContentAsString(): " + response.getContentAsString());
88         } catch (UnsupportedEncodingException e) {
89             fail("Exception: " + e);
90         }
91         logger.info("testGetBRMSParamDictionaryByNameEntityData: exit");
92     }
93
94     @Test
95     public void testGetBRMSParamDictionaryEntityData() {
96         logger.info("testGetBRMSParamDictionaryEntityData: Entering");
97         controller.getBRMSParamDictionaryEntityData(response);
98         try {
99             assertTrue(response.getContentAsString() != null
100                     && response.getContentAsString().contains("brmsParamDictionaryDatas"));
101             logger.info("response.getContentAsString(): " + response.getContentAsString());
102         } catch (UnsupportedEncodingException e) {
103             fail("Exception: " + e);
104         }
105         logger.info("testGetBRMSParamDictionaryEntityData: exit");
106     }
107
108     @Test
109     public void testSaveBRMSParamDictionary() {
110         logger.info("testSaveBRMSParamDictionary: Entering");
111         jsonString =
112                 "{\"brmsParamDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
113         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
114             when(request.getReader()).thenReturn(br);
115             controller.saveBRMSParamDictionary(request, response);
116             assertTrue(response.getContentAsString() != null
117                     && response.getContentAsString().contains("brmsParamDictionaryData"));
118             logger.info("response.getContentAsString(): " + response.getContentAsString());
119         } catch (Exception e) {
120             logger.error("Exception" + e);
121         }
122         logger.info("testSaveBRMSParamDictionary: exit");
123     }
124
125     @Test
126     public void testRemoveBRMSParamDictionary() {
127         logger.info("testRemoveBRMSParamDictionary: Entering");
128         jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
129         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
130             when(request.getReader()).thenReturn(br);
131             controller.removeBRMSParamDictionary(request, response);
132             logger.info("response.getContentAsString(): " + response.getContentAsString());
133             assertTrue(response.getContentAsString() != null
134                     && response.getContentAsString().contains("brmsParamDictionaryDatas"));
135         } catch (Exception e) {
136             fail("Exception: " + e);
137         }
138         logger.info("testRemoveBRMSParamDictionary: exit");
139     }
140
141     @Test
142     public void testGetBRMSDependencyDictionaryByNameEntityData() {
143         logger.info("testGetBRMSDependencyDictionaryByNameEntityData: Entering");
144         BRMSDictionaryController.setCommonClassDao(commonClassDao);
145         controller.getBRMSDependencyDictionaryByNameEntityData(response);
146         try {
147             assertTrue(response.getContentAsString() != null
148                     && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
149             logger.info("response.getContentAsString(): " + response.getContentAsString());
150         } catch (UnsupportedEncodingException e) {
151             fail("Exception: " + e);
152         }
153         logger.info("testGetBRMSDependencyDictionaryByNameEntityData: exit");
154     }
155
156     @Test
157     public void testGetBRMSDependencyDictionaryEntityData() {
158         logger.info("testGetBRMSDependencyDictionaryEntityData: Entering");
159         BRMSDictionaryController.setCommonClassDao(commonClassDao);
160         controller.getBRMSDependencyDictionaryEntityData(response);
161         try {
162             assertTrue(response.getContentAsString() != null
163                     && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
164             logger.info("response.getContentAsString(): " + response.getContentAsString());
165         } catch (UnsupportedEncodingException e) {
166             fail("Exception: " + e);
167         }
168
169         logger.info("testGetBRMSDependencyDictionaryEntityData: exit");
170     }
171
172     @Test
173     public void testSaveBRMSDependencyDictionary() {
174         logger.info("testSaveBRMSDependencyDictionary: Entering");
175         jsonString =
176                 "{\"brmsDependencyDictionaryData\": "
177                         + "{\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
178         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
179             when(request.getReader()).thenReturn(br);
180             controller.saveBRMSDependencyDictionary(request, response);
181             logger.info("response.getContentAsString(): " + response.getContentAsString());
182             assertTrue(response.getContentAsString() != null
183                     && response.getContentAsString().contains("brmsDependencyDictionaryData"));
184         } catch (Exception e) {
185             fail("Exception: " + e);
186         }
187         logger.info("testSaveBRMSDependencyDictionary: exit");
188     }
189
190     @Test
191     public void testRemoveBRMSDependencyDictionary() {
192         logger.info("testRemoveBRMSDependencyDictionary: Entering");
193         MockHttpServletResponse response = new MockHttpServletResponse();
194         request = mock(HttpServletRequest.class);
195         jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
196         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
197             when(request.getReader()).thenReturn(br);
198             controller.removeBRMSDependencyDictionary(request, response);
199             logger.info("response.getContentAsString(): " + response.getContentAsString());
200             assertTrue(response.getContentAsString() != null
201                     && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
202         } catch (Exception e) {
203             fail("Exception: " + e);
204         }
205         logger.info("testRemoveBRMSDependencyDictionary: exit");
206     }
207
208     @Test
209     public void testGetBRMSControllerDictionaryByNameEntityData() {
210         logger.info("testGetBRMSControllerDictionaryByNameEntityData: Entering");
211         MockHttpServletResponse response = new MockHttpServletResponse();
212         BRMSDictionaryController.setCommonClassDao(commonClassDao);
213         controller.getBRMSControllerDictionaryByNameEntityData(response);
214         try {
215             assertTrue(response.getContentAsString() != null
216                     && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
217             logger.info("response.getContentAsString(): " + response.getContentAsString());
218         } catch (UnsupportedEncodingException e) {
219             fail("Exception: " + e);
220         }
221         logger.info("testGetBRMSControllerDictionaryByNameEntityData: exit");
222     }
223
224     @Test
225     public void testGetBRMSControllerDictionaryEntityData() {
226         logger.info("testGetBRMSControllerDictionaryEntityData: Entering");
227         MockHttpServletResponse response = new MockHttpServletResponse();
228         BRMSDictionaryController.setCommonClassDao(commonClassDao);
229         controller.getBRMSControllerDictionaryEntityData(response);
230         try {
231             assertTrue(response.getContentAsString() != null
232                     && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
233             logger.info("response.getContentAsString(): " + response.getContentAsString());
234         } catch (UnsupportedEncodingException e) {
235             fail("Exception: " + e);
236         }
237         logger.info("testGetBRMSControllerDictionaryEntityData: exit");
238     }
239
240     @Test
241     public void testSaveBRMSControllerDictionary() {
242         logger.info("testSaveBRMSControllerDictionary: Entering");
243
244         MockHttpServletResponse response = new MockHttpServletResponse();
245         request = mock(HttpServletRequest.class);
246         jsonString =
247                 "{\"brmsControllerDictionaryData\": "
248                         + "{\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
249         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
250             when(request.getReader()).thenReturn(br);
251             controller.saveBRMSControllerDictionary(request, response);
252             logger.info("response.getContentAsString(): " + response.getContentAsString());
253             assertTrue(response.getContentAsString() != null
254                     && response.getContentAsString().contains("brmsControllerDictionaryData"));
255         } catch (Exception e) {
256             fail("Exception: " + e);
257         }
258         logger.info("testSaveBRMSControllerDictionary: exit");
259     }
260
261     @Test
262     public void testRemoveBRMSControllerDictionary() {
263         logger.info("testRemoveBRMSControllerDictionary: Entering");
264         MockHttpServletResponse response = new MockHttpServletResponse();
265         request = mock(HttpServletRequest.class);
266         jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
267         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
268             when(request.getReader()).thenReturn(br);
269             controller.removeBRMSControllerDictionary(request, response);
270             logger.info("response.getContentAsString(): " + response.getContentAsString());
271             assertTrue(response.getContentAsString() != null
272                     && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
273
274         } catch (Exception e) {
275             fail("Exception: " + e);
276         }
277         logger.info("testRemoveBRMSControllerDictionary: exit");
278     }
279 }