2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
22 package org.onap.policy.pap.xacml.rest.controller;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.io.BufferedReader;
31 import java.io.StringReader;
32 import java.io.UnsupportedEncodingException;
33 import java.util.ArrayList;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.rest.dao.CommonClassDao;
45 import org.onap.policy.rest.jpa.Attribute;
46 import org.onap.policy.rest.jpa.MicroServiceModels;
47 import org.onap.policy.rest.jpa.PolicyEditorScopes;
48 import org.springframework.mock.web.MockHttpServletResponse;
51 * The class <code>DictionaryControllerTest</code> contains tests
52 * for the class {@link <code>DictionaryController</code>}*
54 * All JUnits are designed to run in the local development environment
55 * where they have write privileges and can execute time-sensitive
58 public class DictionaryControllerTest {
60 private static Logger logger = FlexLogger.getLogger(DictionaryControllerTest.class);
61 private static CommonClassDao commonClassDao;
62 private String jsonString = null;
63 private HttpServletRequest request = null;
64 private DictionaryController controller = null;
67 public void setUp() throws Exception {
68 logger.info("setUp: Entering");
69 commonClassDao = Mockito.mock(CommonClassDao.class);
70 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
72 MicroServiceModels testData = new MicroServiceModels();
73 testData.setVersion("1707.4.1.2-Junit");
75 //--- mock the getDataByColumn() call
76 List<String> microList = new ArrayList<String>();
78 List<Object> listId = new ArrayList<Object>();
79 when(commonClassDao.getDataByColumn(Attribute.class, "xacmlId")).thenReturn(microList);
80 PolicyEditorScopes editorScope = new PolicyEditorScopes();
81 doNothing().when(commonClassDao).save(editorScope);
82 doNothing().when(commonClassDao).update(editorScope);
84 when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
86 jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
87 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
88 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
89 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
90 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
91 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
92 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
93 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
94 request = mock(HttpServletRequest.class);
95 BufferedReader br = new BufferedReader(new StringReader(jsonString));
96 //--- mock the getReader() call
97 when(request.getReader()).thenReturn(br);
99 controller = new DictionaryController(commonClassDao);
101 logger.info("setUp: exit");
105 public void tearDown() throws Exception {
109 public void testGetAttributeDictionaryEntityDatabyAttributeName() {
110 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
112 MockHttpServletResponse response = new MockHttpServletResponse();
114 controller.getAttributeDictionaryEntityDatabyAttributeName(response);
117 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
118 logger.info("response.getContentAsString(): " + response.getContentAsString());
119 } catch (UnsupportedEncodingException e) {
120 fail("Exception: " + e);
123 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
127 public void testGetAttributeDictionaryEntityData() {
128 logger.info("testGetAttributeDictionaryEntityData: Entering");
130 MockHttpServletResponse response = new MockHttpServletResponse();
132 controller.getAttributeDictionaryEntityData(response);
135 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
136 logger.info("response.getContentAsString(): " + response.getContentAsString());
137 } catch (UnsupportedEncodingException e) {
138 fail("Exception: " + e);
141 logger.info("testGetAttributeDictionaryEntityData: exit");
145 public void testSaveAttributeDictionary() {
146 logger.info("testSaveAttributeDictionary: Entering");
148 MockHttpServletResponse response = new MockHttpServletResponse();
149 request = mock(HttpServletRequest.class);
152 // mock the getReader() call
153 jsonString = "{\"attributeDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}], \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
154 + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
155 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
156 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
157 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
158 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
159 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
160 + " \"policyJSON\": {\"some\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
162 BufferedReader br = new BufferedReader(new StringReader(jsonString));
163 when(request.getReader()).thenReturn(br);
164 controller.saveAttributeDictionary(request, response);
165 logger.info("response.getContentAsString(): " + response.getContentAsString());
166 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
168 } catch (Exception e) {
169 fail("Exception: " + e);
172 logger.info("testSaveAttributeDictionary: exit");
176 public void testRemoveAttributeDictionary() {
177 logger.info("testRemoveAttributeDictionary: Entering");
179 MockHttpServletResponse response = new MockHttpServletResponse();
180 request = mock(HttpServletRequest.class);
183 // mock the getReader() call
184 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
185 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
186 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
187 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
188 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
189 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
190 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
191 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
192 BufferedReader br = new BufferedReader(new StringReader(jsonString));
193 when(request.getReader()).thenReturn(br);
194 controller.removeAttributeDictionary(request, response);
195 logger.info("response.getContentAsString(): " + response.getContentAsString());
196 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
198 } catch (Exception e) {
199 fail("Exception: " + e);
202 logger.info("testRemoveAttributeDictionary: exit");
206 public void testGetOnapNameDictionaryByNameEntityData() {
207 logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
209 MockHttpServletResponse response = new MockHttpServletResponse();
211 controller.getOnapNameDictionaryByNameEntityData(response);
214 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
215 logger.info("response.getContentAsString(): " + response.getContentAsString());
216 } catch (UnsupportedEncodingException e) {
217 fail("Exception: " + e);
220 logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
224 public void testGetOnapNameDictionaryEntityData() {
225 logger.info("testGetOnapNameDictionaryEntityData: Entering");
227 MockHttpServletResponse response = new MockHttpServletResponse();
229 controller.getOnapNameDictionaryEntityData(response);
232 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
233 logger.info("response.getContentAsString(): " + response.getContentAsString());
234 } catch (UnsupportedEncodingException e) {
235 fail("Exception: " + e);
238 logger.info("testGetOnapNameDictionaryEntityData: exit");
242 public void testSaveOnapDictionary() {
244 logger.info("testSaveOnapDictionary: Entering");
246 MockHttpServletResponse response = new MockHttpServletResponse();
247 request = mock(HttpServletRequest.class);
250 // mock the getReader() call
251 jsonString = "{\"onapNameDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}], \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
252 + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
253 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
254 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
255 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
256 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
257 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
258 + " \"policyJSON\": {\"some\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
260 BufferedReader br = new BufferedReader(new StringReader(jsonString));
261 when(request.getReader()).thenReturn(br);
262 controller.saveOnapDictionary(request, response);
263 logger.info("response.getContentAsString(): " + response.getContentAsString());
264 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
266 } catch (Exception e) {
267 fail("Exception: " + e);
270 logger.info("testSaveOnapDictionary: exit");
274 public void testRemoveOnapDictionary() {
275 logger.info("testRemoveOnapDictionary: Entering");
277 MockHttpServletResponse response = new MockHttpServletResponse();
278 request = mock(HttpServletRequest.class);
281 // mock the getReader() call
282 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
283 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
284 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
285 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
286 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
287 + " \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
288 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
289 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
290 BufferedReader br = new BufferedReader(new StringReader(jsonString));
291 when(request.getReader()).thenReturn(br);
292 controller.removeOnapDictionary(request, response);
293 logger.info("response.getContentAsString(): " + response.getContentAsString());
294 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
296 } catch (Exception e) {
297 fail("Exception: " + e);
300 logger.info("testRemoveOnapDictionary: exit");