2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
22 import static org.junit.Assert.*;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import java.io.BufferedReader;
27 import java.io.StringReader;
28 import java.io.UnsupportedEncodingException;
30 import javax.servlet.http.HttpServletRequest;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.springframework.mock.web.MockHttpServletResponse;
42 public class DecisionPolicyDictionaryControllerTest {
43 private static Logger logger = 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);
55 jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
56 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
57 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
58 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
59 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
60 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
61 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
62 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
64 BufferedReader br = new BufferedReader(new StringReader(jsonString));
66 //--- mock the getReader() call
67 when(request.getReader()).thenReturn(br);
69 controller = new DecisionPolicyDictionaryController(commonClassDao);
70 new DictionaryUtils(commonClassDao);
71 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
72 mock(DictionaryUtils.class);
73 logger.info("setUp: exit");
77 public void tearDown() throws Exception {
81 public void testGetSettingsDictionaryByNameEntityData() {
82 logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
84 MockHttpServletResponse response = new MockHttpServletResponse();
86 controller.getSettingsDictionaryByNameEntityData(request, response);
89 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
90 logger.info("response.getContentAsString(): " + response.getContentAsString());
91 } catch (UnsupportedEncodingException e) {
92 logger.error("Exception Occured"+e);
93 fail("Exception: " + e);
96 logger.info("testGetSettingsDictionaryByNameEntityData: exit");
101 public void testGetSettingsDictionaryEntityData() {
102 logger.info("testGetSettingsDictionaryEntityData: Entering");
104 MockHttpServletResponse response = new MockHttpServletResponse();
106 controller.getSettingsDictionaryEntityData(response);
109 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
110 logger.info("response.getContentAsString(): " + response.getContentAsString());
111 } catch (UnsupportedEncodingException e) {
112 logger.error("Exception Occured"+e);
113 fail("Exception: " + e);
116 logger.info("testGetSettingsDictionaryEntityData: exit");
121 public void testSaveSettingsDictionary() {
122 logger.info("testSaveSettingsDictionary: Entering");
124 MockHttpServletResponse response = new MockHttpServletResponse();
125 request = mock(HttpServletRequest.class);
128 // mock the getReader() call
129 jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}, \"userid\":\"test\"}";
131 BufferedReader br = new BufferedReader(new StringReader(jsonString));
132 when(request.getReader()).thenReturn(br);
133 controller.saveSettingsDictionary(request, response);
134 logger.info("response.getContentAsString(): " + response.getContentAsString());
135 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
137 } catch (Exception e) {
138 logger.error("Exception Occured"+e);
139 fail("Exception: " + e);
142 logger.info("testSaveSettingsDictionary: exit");
147 public void testRemoveSettingsDictionary() {
148 logger.info("testRemoveSettingsDictionary: Entering");
150 MockHttpServletResponse response = new MockHttpServletResponse();
151 request = mock(HttpServletRequest.class);
154 // mock the getReader() call
155 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
156 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
157 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
158 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
159 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
160 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
161 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
162 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
163 BufferedReader br = new BufferedReader(new StringReader(jsonString));
164 when(request.getReader()).thenReturn(br);
165 controller.removeSettingsDictionary(request, response);
166 logger.info("response.getContentAsString(): " + response.getContentAsString());
167 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
169 } catch (Exception e) {
170 logger.error("Exception Occured"+e);
171 fail("Exception: " + e);
174 logger.info("testRemoveSettingsDictionary: exit");
179 public void testGetRainyDayDictionaryByNameEntityData() {
180 logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
182 MockHttpServletResponse response = new MockHttpServletResponse();
184 controller.getRainyDayDictionaryByNameEntityData(request, response);
187 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
188 logger.info("response.getContentAsString(): " + response.getContentAsString());
189 } catch (UnsupportedEncodingException e) {
190 logger.error("Exception Occured"+e);
191 fail("Exception: " + e);
194 logger.info("testGetRainyDayDictionaryByNameEntityData: exit");
199 public void testGetRainyDayDictionaryEntityData() {
200 logger.info("testGetRainyDayDictionaryEntityData: Entering");
202 MockHttpServletResponse response = new MockHttpServletResponse();
204 controller.getRainyDayDictionaryEntityData(response);
207 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
208 logger.info("response.getContentAsString(): " + response.getContentAsString());
209 } catch (UnsupportedEncodingException e) {
210 logger.error("Exception Occured"+e);
211 fail("Exception: " + e);
214 logger.info("testGetRainyDayDictionaryEntityData: exit");
219 public void testSaveRainyDayDictionary() {
220 logger.info("testSaveRainyDayDictionary: Entering");
222 MockHttpServletResponse response = new MockHttpServletResponse();
223 request = mock(HttpServletRequest.class);
226 // mock the getReader() call
227 jsonString = "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\",\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}";
229 BufferedReader br = new BufferedReader(new StringReader(jsonString));
230 when(request.getReader()).thenReturn(br);
231 controller.saveRainyDayDictionary(request, response);
232 logger.info("response.getContentAsString(): " + response.getContentAsString());
233 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
235 } catch (Exception e) {
236 logger.error("Exception Occured"+e);
237 fail("Exception: " + e);
240 logger.info("testSaveRainyDayDictionary: exit");
245 public void testRemoveRainyDayDictionary() {
246 logger.info("testRemoveRainyDayDictionary: Entering");
248 MockHttpServletResponse response = new MockHttpServletResponse();
249 request = mock(HttpServletRequest.class);
252 // mock the getReader() call
253 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
254 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
255 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
256 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
257 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
258 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
259 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
260 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
261 BufferedReader br = new BufferedReader(new StringReader(jsonString));
262 when(request.getReader()).thenReturn(br);
263 controller.removeRainyDayDictionary(request, response);
264 logger.info("response.getContentAsString(): " + response.getContentAsString());
265 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
267 } catch (Exception e) {
268 logger.error("Exception Occured"+e);
269 fail("Exception: " + e);
272 logger.info("testRemoveRainyDayDictionary: exit");