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.openecomp.policy.pap.xacml.rest.controller;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.doNothing;
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;
32 import java.util.ArrayList;
33 import java.util.List;
35 import javax.json.Json;
36 import javax.json.JsonObject;
37 import javax.servlet.http.HttpServletRequest;
39 import org.json.JSONObject;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
45 import org.openecomp.policy.common.logging.flexlogger.Logger;
46 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
47 import org.openecomp.policy.rest.dao.CommonClassDao;
48 import org.openecomp.policy.rest.jpa.Attribute;
49 import org.openecomp.policy.rest.jpa.MicroServiceModels;
50 import org.openecomp.policy.rest.jpa.PolicyEditorScopes;
51 import org.springframework.mock.web.MockHttpServletResponse;
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.JsonNode;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56 import com.github.fge.jackson.JsonLoader;
59 * The class <code>DictionaryControllerTest</code> contains tests
60 * for the class {@link <code>DictionaryController</code>}*
62 * All JUnits are designed to run in the local development environment
63 * where they have write privileges and can execute time-sensitive
66 public class DictionaryControllerTest {
68 private static Logger logger = FlexLogger.getLogger(DictionaryControllerTest.class);
69 private static CommonClassDao commonClassDao;
70 private String jsonString = null;
71 private String configBodyString = null;
72 private HttpServletRequest request = null;
73 private DictionaryController controller = null;
74 private BufferedReader br = null;
77 public void setUp() throws Exception {
78 logger.info("setUp: Entering");
79 commonClassDao = Mockito.mock(CommonClassDao.class);
80 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
82 List<Object> microServiceModelsData = new ArrayList<Object>();
83 MicroServiceModels testData = new MicroServiceModels();
84 testData.setVersion("1707.4.1.2-Junit");
86 //--- mock the getDataByColumn() call
87 List<String> microList = new ArrayList<String>();
89 List<Object> listId = new ArrayList<Object>();
90 when(commonClassDao.getDataByColumn(Attribute.class, "xacmlId")).thenReturn(microList);
91 PolicyEditorScopes editorScope = new PolicyEditorScopes();
92 doNothing().when(commonClassDao).save(editorScope);
93 doNothing().when(commonClassDao).update(editorScope);
95 when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
97 jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
98 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
99 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
100 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
101 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
102 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
103 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
104 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
106 configBodyString = "{\"service\":\"SniroPolicyEntityTest\",\"policyName\":\"someone\",\"description\":\"test\",\"templateVersion\":\"1607\",\"version\":\"HD\","
107 + "\"priority\":\"2\",\"content\":{\"lastPolled\":\"1\",\"boolen-test\":\"true\",\"created\":\"test\",\"retiredDate\":\"test\",\"scope\":\"SNIRO_PLACEMENT_VDHV\","
108 + "\"name\":\"test\",\"lastModified\":\"test\",\"state\":\"CREATED\",\"type\":\"CONFIG\",\"intent\":\"test\",\"target\":\"SNIRO\"}}";
110 request = mock(HttpServletRequest.class);
111 BufferedReader br = new BufferedReader(new StringReader(jsonString));
112 //--- mock the getReader() call
113 when(request.getReader()).thenReturn(br);
115 controller = new DictionaryController(commonClassDao);
117 logger.info("setUp: exit");
121 public void tearDown() throws Exception {
125 public void testGetAttributeDictionaryEntityDatabyAttributeName() {
126 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
128 MockHttpServletResponse response = new MockHttpServletResponse();
130 controller.getAttributeDictionaryEntityDatabyAttributeName(request, response);
133 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
134 logger.info("response.getContentAsString(): " + response.getContentAsString());
135 } catch (UnsupportedEncodingException e) {
136 fail("Exception: " + e);
139 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
143 public void testGetAttributeDictionaryEntityData() {
144 logger.info("testGetAttributeDictionaryEntityData: Entering");
146 MockHttpServletResponse response = new MockHttpServletResponse();
148 controller.getAttributeDictionaryEntityData(request, response);
151 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
152 logger.info("response.getContentAsString(): " + response.getContentAsString());
153 } catch (UnsupportedEncodingException e) {
154 fail("Exception: " + e);
157 logger.info("testGetAttributeDictionaryEntityData: exit");
161 public void testSaveAttributeDictionary() {
162 logger.info("testSaveAttributeDictionary: Entering");
164 MockHttpServletResponse response = new MockHttpServletResponse();
165 request = mock(HttpServletRequest.class);
168 // mock the getReader() call
169 jsonString = "{\"attributeDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}], \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
170 + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
171 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
172 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
173 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
174 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
175 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
176 + " \"policyJSON\": {\"some\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
178 BufferedReader br = new BufferedReader(new StringReader(jsonString));
179 when(request.getReader()).thenReturn(br);
180 controller.saveAttributeDictionary(request, response);
181 logger.info("response.getContentAsString(): " + response.getContentAsString());
182 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
184 } catch (Exception e) {
185 fail("Exception: " + e);
188 logger.info("testSaveAttributeDictionary: exit");
192 public void testRemoveAttributeDictionary() {
193 logger.info("testRemoveAttributeDictionary: Entering");
195 MockHttpServletResponse response = new MockHttpServletResponse();
196 request = mock(HttpServletRequest.class);
199 // mock the getReader() call
200 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
201 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
202 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
203 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
204 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
205 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
206 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
207 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
208 BufferedReader br = new BufferedReader(new StringReader(jsonString));
209 when(request.getReader()).thenReturn(br);
210 controller.removeAttributeDictionary(request, response);
211 logger.info("response.getContentAsString(): " + response.getContentAsString());
212 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
214 } catch (Exception e) {
215 fail("Exception: " + e);
218 logger.info("testRemoveAttributeDictionary: exit");
222 public void testGetEcompNameDictionaryByNameEntityData() {
223 logger.info("testGetEcompNameDictionaryByNameEntityData: Entering");
225 MockHttpServletResponse response = new MockHttpServletResponse();
227 controller.getEcompNameDictionaryByNameEntityData(request, response);
230 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
231 logger.info("response.getContentAsString(): " + response.getContentAsString());
232 } catch (UnsupportedEncodingException e) {
233 fail("Exception: " + e);
236 logger.info("testGetEcompNameDictionaryByNameEntityData: exit");
240 public void testGetEcompNameDictionaryEntityData() {
241 logger.info("testGetEcompNameDictionaryEntityData: Entering");
243 MockHttpServletResponse response = new MockHttpServletResponse();
245 controller.getEcompNameDictionaryEntityData(request, response);
248 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
249 logger.info("response.getContentAsString(): " + response.getContentAsString());
250 } catch (UnsupportedEncodingException e) {
251 fail("Exception: " + e);
254 logger.info("testGetEcompNameDictionaryEntityData: exit");
258 public void testSaveEcompDictionary() {
260 logger.info("testSaveEcompDictionary: Entering");
262 MockHttpServletResponse response = new MockHttpServletResponse();
263 request = mock(HttpServletRequest.class);
266 // mock the getReader() call
267 jsonString = "{\"ecompNameDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}], \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
268 + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
269 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
270 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
271 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
272 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
273 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
274 + " \"policyJSON\": {\"some\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
276 BufferedReader br = new BufferedReader(new StringReader(jsonString));
277 when(request.getReader()).thenReturn(br);
278 controller.saveEcompDictionary(request, response);
279 logger.info("response.getContentAsString(): " + response.getContentAsString());
280 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
282 } catch (Exception e) {
283 fail("Exception: " + e);
286 logger.info("testSaveEcompDictionary: exit");
290 public void testRemoveEcompDictionary() {
291 logger.info("testRemoveEcompDictionary: Entering");
293 MockHttpServletResponse response = new MockHttpServletResponse();
294 request = mock(HttpServletRequest.class);
297 // mock the getReader() call
298 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
299 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
300 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
301 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
302 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
303 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
304 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
305 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
306 BufferedReader br = new BufferedReader(new StringReader(jsonString));
307 when(request.getReader()).thenReturn(br);
308 controller.removeEcompDictionary(request, response);
309 logger.info("response.getContentAsString(): " + response.getContentAsString());
310 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
312 } catch (Exception e) {
313 fail("Exception: " + e);
316 logger.info("testRemoveEcompDictionary: exit");