1334f96a0caca3fca5ffbf98ccd627cbb17987a9
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / DecisionPolicyDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-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.pap.xacml.rest.controller;
23
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import java.io.BufferedReader;
28 import java.io.StringReader;
29 import java.io.UnsupportedEncodingException;
30 import javax.servlet.http.HttpServletRequest;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.springframework.mock.web.MockHttpServletResponse;
40
41 public class DecisionPolicyDictionaryControllerTest {
42     private static Logger logger =
43             FlexLogger.getLogger(DecisionPolicyDictionaryControllerTest.class);
44     private static CommonClassDao commonClassDao;
45     private String jsonString = null;
46     private HttpServletRequest request = null;
47     private DecisionPolicyDictionaryController controller = null;
48
49     @Before
50     public void setUp() throws Exception {
51         logger.info("setUp: Entering");
52         commonClassDao = Mockito.mock(CommonClassDao.class);
53         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
54
55         jsonString =
56                 "{\"attributeDictionaryDatas\": {\"error\": \"\",\"inprocess\": false,\"model\": "
57                         + "{\"name\": \"testingdata\",\"subScopename\": \"\",\"path\": [],\"type\": \"dir\","
58                         + "\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\",\"version\": \"\","
59                         + "\"createdBy\": \"someone\",\"modifiedBy\": \"someone\",\"content\": \"\","
60                         + "\"recursive\": false},"
61                         + "\"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
62                         + "\"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
63                         + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
64                         + "\"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
65                         + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
66                         + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
67                         + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
68
69         BufferedReader br = new BufferedReader(new StringReader(jsonString));
70
71         // --- mock the getReader() call
72         when(request.getReader()).thenReturn(br);
73
74         controller = new DecisionPolicyDictionaryController(commonClassDao);
75         new DictionaryUtils(commonClassDao);
76         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
77         mock(DictionaryUtils.class);
78         logger.info("setUp: exit");
79     }
80
81     @After
82     public void tearDown() throws Exception {}
83
84     @Test
85     public void testGetSettingsDictionaryByNameEntityData() {
86         logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
87
88         MockHttpServletResponse response = new MockHttpServletResponse();
89
90         controller.getSettingsDictionaryByNameEntityData(request, response);
91
92         try {
93             assertTrue(response.getContentAsString() != null
94                     && response.getContentAsString().contains("settingsDictionaryDatas"));
95             logger.info("response.getContentAsString(): " + response.getContentAsString());
96         } catch (UnsupportedEncodingException e) {
97             logger.error("Exception Occured" + e);
98             fail("Exception: " + e);
99         }
100
101         logger.info("testGetSettingsDictionaryByNameEntityData: exit");
102
103     }
104
105     @Test
106     public void testGetSettingsDictionaryEntityData() {
107         logger.info("testGetSettingsDictionaryEntityData: Entering");
108
109         MockHttpServletResponse response = new MockHttpServletResponse();
110
111         controller.getSettingsDictionaryEntityData(response);
112
113         try {
114             assertTrue(response.getContentAsString() != null
115                     && response.getContentAsString().contains("settingsDictionaryDatas"));
116             logger.info("response.getContentAsString(): " + response.getContentAsString());
117         } catch (UnsupportedEncodingException e) {
118             logger.error("Exception Occured" + e);
119             fail("Exception: " + e);
120         }
121
122         logger.info("testGetSettingsDictionaryEntityData: exit");
123
124     }
125
126     @Test
127     public void testSaveSettingsDictionary() {
128         logger.info("testSaveSettingsDictionary: Entering");
129
130         MockHttpServletResponse response = new MockHttpServletResponse();
131         request = mock(HttpServletRequest.class);
132
133         try {
134             // mock the getReader() call
135             jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\","
136                     + "\"datatypeBean\": {\"shortName\":\"string\"},\"description\":\"test\","
137                     + "\"priority\":\"High\"}, \"userid\":\"test\"}";
138
139             BufferedReader br = new BufferedReader(new StringReader(jsonString));
140             when(request.getReader()).thenReturn(br);
141             controller.saveSettingsDictionary(request, response);
142             logger.info("response.getContentAsString(): " + response.getContentAsString());
143             assertTrue(response.getContentAsString() != null
144                     && response.getContentAsString().contains("settingsDictionaryDatas"));
145
146         } catch (Exception e) {
147             logger.error("Exception Occured" + e);
148             fail("Exception: " + e);
149         }
150
151         logger.info("testSaveSettingsDictionary: exit");
152
153     }
154
155     @Test
156     public void testRemoveSettingsDictionary() {
157         logger.info("testRemoveSettingsDictionary: Entering");
158
159         MockHttpServletResponse response = new MockHttpServletResponse();
160         request = mock(HttpServletRequest.class);
161
162         try {
163             // mock the getReader() call
164             jsonString =
165                     "{\"data\": {\"modelName\": \"test\",\"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
166                             + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,"
167                             + "\"date\": \"2017-04-12T21:26:57.000Z\",\"version\": \"\","
168                             + "\"createdBy\": \"someone\",\"modifiedBy\": \"someone\",\"content\": \"\","
169                             + "\"recursive\": false},\"tempModel\": {\"name\": \"testingdata\","
170                             + "\"subScopename\": \"\"},\"policy\": "
171                             + "{\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
172                             + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
173                             + "\"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
174                             + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
175                             + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
176                             + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
177             BufferedReader br = new BufferedReader(new StringReader(jsonString));
178             when(request.getReader()).thenReturn(br);
179             controller.removeSettingsDictionary(request, response);
180             logger.info("response.getContentAsString(): " + response.getContentAsString());
181             assertTrue(response.getContentAsString() != null
182                     && response.getContentAsString().contains("settingsDictionaryDatas"));
183
184         } catch (Exception e) {
185             logger.error("Exception Occured" + e);
186             fail("Exception: " + e);
187         }
188         logger.info("testRemoveSettingsDictionary: exit");
189     }
190
191     @Test
192     public void testGetRainyDayDictionaryByNameEntityData() {
193         logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
194
195         MockHttpServletResponse response = new MockHttpServletResponse();
196
197         controller.getRainyDayDictionaryByNameEntityData(request, response);
198
199         try {
200             assertTrue(response.getContentAsString() != null
201                     && response.getContentAsString().contains("rainyDayDictionaryDatas"));
202             logger.info("response.getContentAsString(): " + response.getContentAsString());
203         } catch (UnsupportedEncodingException e) {
204             logger.error("Exception Occured" + e);
205             fail("Exception: " + e);
206         }
207         logger.info("testGetRainyDayDictionaryByNameEntityData: exit");
208     }
209
210     @Test
211     public void testGetRainyDayDictionaryEntityData() {
212         logger.info("testGetRainyDayDictionaryEntityData: Entering");
213
214         MockHttpServletResponse response = new MockHttpServletResponse();
215
216         controller.getRainyDayDictionaryEntityData(response);
217
218         try {
219             assertTrue(response.getContentAsString() != null
220                     && response.getContentAsString().contains("rainyDayDictionaryDatas"));
221             logger.info("response.getContentAsString(): " + response.getContentAsString());
222         } catch (UnsupportedEncodingException e) {
223             logger.error("Exception Occured" + e);
224             fail("Exception: " + e);
225         }
226         logger.info("testGetRainyDayDictionaryEntityData: exit");
227     }
228
229     @Test
230     public void testSaveRainyDayDictionary() {
231         logger.info("testSaveRainyDayDictionary: Entering");
232
233         MockHttpServletResponse response = new MockHttpServletResponse();
234         request = mock(HttpServletRequest.class);
235
236         try {
237             // mock the getReader() call
238             jsonString =
239                     "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\""
240                             + ":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\","
241                             + "\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}";
242
243             BufferedReader br = new BufferedReader(new StringReader(jsonString));
244             when(request.getReader()).thenReturn(br);
245             controller.saveRainyDayDictionary(request, response);
246             logger.info("response.getContentAsString(): " + response.getContentAsString());
247             assertTrue(response.getContentAsString() != null
248                     && response.getContentAsString().contains("rainyDayDictionaryDatas"));
249
250         } catch (Exception e) {
251             logger.error("Exception Occured" + e);
252             fail("Exception: " + e);
253         }
254         logger.info("testSaveRainyDayDictionary: exit");
255     }
256
257     @Test
258     public void testRemoveRainyDayDictionary() {
259         logger.info("testRemoveRainyDayDictionary: Entering");
260
261         MockHttpServletResponse response = new MockHttpServletResponse();
262         request = mock(HttpServletRequest.class);
263
264         try {
265             // mock the getReader() call
266             jsonString =
267                     "{\"data\": {\"modelName\": \"test\",\"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
268                             + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,"
269                             + "\"date\": \"2017-04-12T21:26:57.000Z\",\"version\": \"\",\"createdBy\": "
270                             + "\"someone\",\"modifiedBy\": \"someone\",\"content\": \"\",\"recursive\": false},"
271                             + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"},"
272                             + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\","
273                             + "\"policyName\": \"may1501\",\"policyDescription\": \"testing input\","
274                             + "\"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\","
275                             + "\"riskLevel\": \"2\",\"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\","
276                             + "\"version\": \"1707.41.02\",\"ruleGridData\": [[\"fileId\"]],\"ttlDate\": null}},"
277                             + "\"policyJSON\": {\"pmTableName\": \"test\",\"dmdTopic\": \"1\",\"fileId\": \"56\"}}";
278             BufferedReader br = new BufferedReader(new StringReader(jsonString));
279             when(request.getReader()).thenReturn(br);
280             controller.removeRainyDayDictionary(request, response);
281             logger.info("response.getContentAsString(): " + response.getContentAsString());
282             assertTrue(response.getContentAsString() != null
283                     && response.getContentAsString().contains("rainyDayDictionaryDatas"));
284         } catch (Exception e) {
285             logger.error("Exception Occured" + e);
286             fail("Exception: " + e);
287         }
288         logger.info("testRemoveRainyDayDictionary: exit");
289     }
290 }