2 * ============LICENSE_START=======================================================
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
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=========================================================
23 package org.onap.policy.pap.xacml.rest.controller;
25 import static org.junit.Assert.*;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.io.UnsupportedEncodingException;
33 import javax.servlet.http.HttpServletRequest;
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;
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;
52 public void setUp() throws Exception {
53 logger.info("setUp: Entering");
54 commonClassDao = Mockito.mock(CommonClassDao.class);
55 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
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\"}}";
70 BufferedReader br = new BufferedReader(new StringReader(jsonString));
72 // --- mock the getReader() call
73 when(request.getReader()).thenReturn(br);
75 controller = new DecisionPolicyDictionaryController(commonClassDao);
76 new DictionaryUtils(commonClassDao);
77 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
78 mock(DictionaryUtils.class);
79 logger.info("setUp: exit");
83 public void testGetSettingsDictionaryByNameEntityData() {
84 logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
86 MockHttpServletResponse response = new MockHttpServletResponse();
88 controller.getSettingsDictionaryByNameEntityData(request, response);
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);
99 logger.info("testGetSettingsDictionaryByNameEntityData: exit");
104 public void testGetSettingsDictionaryEntityData() {
105 logger.info("testGetSettingsDictionaryEntityData: Entering");
107 MockHttpServletResponse response = new MockHttpServletResponse();
109 controller.getSettingsDictionaryEntityData(response);
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);
120 logger.info("testGetSettingsDictionaryEntityData: exit");
125 public void testSaveSettingsDictionary() {
126 logger.info("testSaveSettingsDictionary: Entering");
128 MockHttpServletResponse response = new MockHttpServletResponse();
129 request = mock(HttpServletRequest.class);
132 // mock the getReader() call
133 jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\","
134 + "\"datatypeBean\": {\"shortName\":\"string\"},\"description\":\"test\","
135 + "\"priority\":\"High\"}, \"userid\":\"test\"}";
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"));
144 } catch (Exception e) {
145 logger.error("Exception Occured" + e);
146 fail("Exception: " + e);
149 logger.info("testSaveSettingsDictionary: exit");
154 public void testRemoveSettingsDictionary() {
155 logger.info("testRemoveSettingsDictionary: Entering");
157 MockHttpServletResponse response = new MockHttpServletResponse();
158 request = mock(HttpServletRequest.class);
161 // mock the getReader() call
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"));
182 } catch (Exception e) {
183 logger.error("Exception Occured" + e);
184 fail("Exception: " + e);
186 logger.info("testRemoveSettingsDictionary: exit");
190 public void testGetRainyDayDictionaryByNameEntityData() {
191 logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
193 MockHttpServletResponse response = new MockHttpServletResponse();
195 controller.getRainyDayDictionaryByNameEntityData(request, response);
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);
205 logger.info("testGetRainyDayDictionaryByNameEntityData: exit");
209 public void testGetRainyDayDictionaryEntityData() {
210 logger.info("testGetRainyDayDictionaryEntityData: Entering");
212 MockHttpServletResponse response = new MockHttpServletResponse();
214 controller.getRainyDayDictionaryEntityData(response);
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);
224 logger.info("testGetRainyDayDictionaryEntityData: exit");
228 public void testSaveRainyDayDictionary() {
229 logger.info("testSaveRainyDayDictionary: Entering");
231 MockHttpServletResponse response = new MockHttpServletResponse();
232 request = mock(HttpServletRequest.class);
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\"}";
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"));
247 } catch (Exception e) {
248 logger.error("Exception Occured" + e);
249 fail("Exception: " + e);
251 logger.info("testSaveRainyDayDictionary: exit");
255 public void testRemoveRainyDayDictionary() {
256 logger.info("testRemoveRainyDayDictionary: Entering");
258 MockHttpServletResponse response = new MockHttpServletResponse();
259 request = mock(HttpServletRequest.class);
262 // mock the getReader() call
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);
285 logger.info("testRemoveRainyDayDictionary: exit");