2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.xacml.rest.controller;
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;
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;
50 public void setUp() throws Exception {
51 logger.info("setUp: Entering");
52 commonClassDao = Mockito.mock(CommonClassDao.class);
53 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
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\"}}";
69 BufferedReader br = new BufferedReader(new StringReader(jsonString));
71 // --- mock the getReader() call
72 when(request.getReader()).thenReturn(br);
74 controller = new DecisionPolicyDictionaryController(commonClassDao);
75 new DictionaryUtils(commonClassDao);
76 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
77 mock(DictionaryUtils.class);
78 logger.info("setUp: exit");
82 public void tearDown() throws Exception {}
85 public void testGetSettingsDictionaryByNameEntityData() {
86 logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
88 MockHttpServletResponse response = new MockHttpServletResponse();
90 controller.getSettingsDictionaryByNameEntityData(request, response);
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);
101 logger.info("testGetSettingsDictionaryByNameEntityData: exit");
106 public void testGetSettingsDictionaryEntityData() {
107 logger.info("testGetSettingsDictionaryEntityData: Entering");
109 MockHttpServletResponse response = new MockHttpServletResponse();
111 controller.getSettingsDictionaryEntityData(response);
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);
122 logger.info("testGetSettingsDictionaryEntityData: exit");
127 public void testSaveSettingsDictionary() {
128 logger.info("testSaveSettingsDictionary: Entering");
130 MockHttpServletResponse response = new MockHttpServletResponse();
131 request = mock(HttpServletRequest.class);
134 // mock the getReader() call
135 jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\","
136 + "\"datatypeBean\": {\"shortName\":\"string\"},\"description\":\"test\","
137 + "\"priority\":\"High\"}, \"userid\":\"test\"}";
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"));
146 } catch (Exception e) {
147 logger.error("Exception Occured" + e);
148 fail("Exception: " + e);
151 logger.info("testSaveSettingsDictionary: exit");
156 public void testRemoveSettingsDictionary() {
157 logger.info("testRemoveSettingsDictionary: Entering");
159 MockHttpServletResponse response = new MockHttpServletResponse();
160 request = mock(HttpServletRequest.class);
163 // mock the getReader() call
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"));
184 } catch (Exception e) {
185 logger.error("Exception Occured" + e);
186 fail("Exception: " + e);
188 logger.info("testRemoveSettingsDictionary: exit");
192 public void testGetRainyDayDictionaryByNameEntityData() {
193 logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
195 MockHttpServletResponse response = new MockHttpServletResponse();
197 controller.getRainyDayDictionaryByNameEntityData(request, response);
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);
207 logger.info("testGetRainyDayDictionaryByNameEntityData: exit");
211 public void testGetRainyDayDictionaryEntityData() {
212 logger.info("testGetRainyDayDictionaryEntityData: Entering");
214 MockHttpServletResponse response = new MockHttpServletResponse();
216 controller.getRainyDayDictionaryEntityData(response);
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);
226 logger.info("testGetRainyDayDictionaryEntityData: exit");
230 public void testSaveRainyDayDictionary() {
231 logger.info("testSaveRainyDayDictionary: Entering");
233 MockHttpServletResponse response = new MockHttpServletResponse();
234 request = mock(HttpServletRequest.class);
237 // mock the getReader() call
239 "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\""
240 + ":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\","
241 + "\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}";
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"));
250 } catch (Exception e) {
251 logger.error("Exception Occured" + e);
252 fail("Exception: " + e);
254 logger.info("testSaveRainyDayDictionary: exit");
258 public void testRemoveRainyDayDictionary() {
259 logger.info("testRemoveRainyDayDictionary: Entering");
261 MockHttpServletResponse response = new MockHttpServletResponse();
262 request = mock(HttpServletRequest.class);
265 // mock the getReader() call
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);
288 logger.info("testRemoveRainyDayDictionary: exit");