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