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;
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.io.UnsupportedEncodingException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
39 import org.openecomp.policy.common.logging.flexlogger.Logger;
40 import org.openecomp.policy.rest.dao.CommonClassDao;
41 import org.openecomp.policy.rest.jpa.Attribute;
42 import org.openecomp.policy.rest.jpa.MicroServiceModels;
43 import org.openecomp.policy.rest.jpa.PolicyEditorScopes;
44 import org.springframework.mock.web.MockHttpServletResponse;
47 * The class <code>DictionaryControllerTest</code> contains tests
48 * for the class {@link <code>DictionaryController</code>}*
50 * All JUnits are designed to run in the local development environment
51 * where they have write privileges and can execute time-sensitive
55 public class DictionaryControllerTest {
57 private static Logger logger = FlexLogger.getLogger(DictionaryControllerTest.class);
58 private static CommonClassDao commonClassDao;
59 private String jsonString = null;
60 private String configBodyString = null;
61 private HttpServletRequest request = null;
62 private DictionaryController controller = null;
63 private BufferedReader br = null;
66 public void setUp() throws Exception {
67 logger.info("setUp: Entering");
68 commonClassDao = Mockito.mock(CommonClassDao.class);
69 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
71 List<Object> microServiceModelsData = new ArrayList<Object>();
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\", \"ecompName\": \"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\"} }";
95 configBodyString = "{\"service\":\"SniroPolicyEntityTest\",\"policyName\":\"someone\",\"description\":\"test\",\"templateVersion\":\"1607\",\"version\":\"HD\","
96 + "\"priority\":\"2\",\"content\":{\"lastPolled\":\"1\",\"boolen-test\":\"true\",\"created\":\"test\",\"retiredDate\":\"test\",\"scope\":\"SNIRO_PLACEMENT_VDHV\","
97 + "\"name\":\"test\",\"lastModified\":\"test\",\"state\":\"CREATED\",\"type\":\"CONFIG\",\"intent\":\"test\",\"target\":\"SNIRO\"}}";
99 request = mock(HttpServletRequest.class);
100 BufferedReader br = new BufferedReader(new StringReader(jsonString));
101 //--- mock the getReader() call
102 when(request.getReader()).thenReturn(br);
104 controller = new DictionaryController(commonClassDao);
106 logger.info("setUp: exit");
110 public void tearDown() throws Exception {
114 public void testGetAttributeDictionaryEntityDatabyAttributeName() {
115 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
117 MockHttpServletResponse response = new MockHttpServletResponse();
119 controller.getAttributeDictionaryEntityDatabyAttributeName(request, response);
122 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
123 logger.info("response.getContentAsString(): " + response.getContentAsString());
124 } catch (UnsupportedEncodingException e) {
125 fail("Exception: " + e);
128 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
132 public void testGetAttributeDictionaryEntityData() {
133 logger.info("testGetAttributeDictionaryEntityData: Entering");
135 MockHttpServletResponse response = new MockHttpServletResponse();
137 controller.getAttributeDictionaryEntityData(request, response);
140 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
141 logger.info("response.getContentAsString(): " + response.getContentAsString());
142 } catch (UnsupportedEncodingException e) {
143 fail("Exception: " + e);
146 logger.info("testGetAttributeDictionaryEntityData: exit");
150 public void testSaveAttributeDictionary() {
151 logger.info("testSaveAttributeDictionary: Entering");
153 MockHttpServletResponse response = new MockHttpServletResponse();
154 request = mock(HttpServletRequest.class);
157 // mock the getReader() call
158 jsonString = "{\"attributeDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}], \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
159 + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
160 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
161 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
162 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
163 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
164 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
165 + " \"policyJSON\": {\"some\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
167 BufferedReader br = new BufferedReader(new StringReader(jsonString));
168 when(request.getReader()).thenReturn(br);
169 controller.saveAttributeDictionary(request, response);
170 logger.info("response.getContentAsString(): " + response.getContentAsString());
171 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
173 } catch (Exception e) {
174 fail("Exception: " + e);
177 logger.info("testSaveAttributeDictionary: exit");
181 public void testRemoveAttributeDictionary() {
182 logger.info("testRemoveAttributeDictionary: Entering");
184 MockHttpServletResponse response = new MockHttpServletResponse();
185 request = mock(HttpServletRequest.class);
188 // mock the getReader() call
189 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
190 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
191 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
192 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
193 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
194 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
195 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
196 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
197 BufferedReader br = new BufferedReader(new StringReader(jsonString));
198 when(request.getReader()).thenReturn(br);
199 controller.removeAttributeDictionary(request, response);
200 logger.info("response.getContentAsString(): " + response.getContentAsString());
201 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
203 } catch (Exception e) {
204 fail("Exception: " + e);
207 logger.info("testRemoveAttributeDictionary: exit");
211 public void testGetEcompNameDictionaryByNameEntityData() {
212 logger.info("testGetEcompNameDictionaryByNameEntityData: Entering");
214 MockHttpServletResponse response = new MockHttpServletResponse();
216 controller.getEcompNameDictionaryByNameEntityData(request, response);
219 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
220 logger.info("response.getContentAsString(): " + response.getContentAsString());
221 } catch (UnsupportedEncodingException e) {
222 fail("Exception: " + e);
225 logger.info("testGetEcompNameDictionaryByNameEntityData: exit");
229 public void testGetEcompNameDictionaryEntityData() {
230 logger.info("testGetEcompNameDictionaryEntityData: Entering");
232 MockHttpServletResponse response = new MockHttpServletResponse();
234 controller.getEcompNameDictionaryEntityData(request, response);
237 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
238 logger.info("response.getContentAsString(): " + response.getContentAsString());
239 } catch (UnsupportedEncodingException e) {
240 fail("Exception: " + e);
243 logger.info("testGetEcompNameDictionaryEntityData: exit");
247 public void testSaveEcompDictionary() {
249 logger.info("testSaveEcompDictionary: Entering");
251 MockHttpServletResponse response = new MockHttpServletResponse();
252 request = mock(HttpServletRequest.class);
255 // mock the getReader() call
256 jsonString = "{\"ecompNameDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}], \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
257 + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
258 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
259 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
260 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
261 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
262 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
263 + " \"policyJSON\": {\"some\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
265 BufferedReader br = new BufferedReader(new StringReader(jsonString));
266 when(request.getReader()).thenReturn(br);
267 controller.saveEcompDictionary(request, response);
268 logger.info("response.getContentAsString(): " + response.getContentAsString());
269 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
271 } catch (Exception e) {
272 fail("Exception: " + e);
275 logger.info("testSaveEcompDictionary: exit");
279 public void testRemoveEcompDictionary() {
280 logger.info("testRemoveEcompDictionary: Entering");
282 MockHttpServletResponse response = new MockHttpServletResponse();
283 request = mock(HttpServletRequest.class);
286 // mock the getReader() call
287 jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
288 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
289 + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false},"
290 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" },"
291 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
292 + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
293 + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
294 + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
295 BufferedReader br = new BufferedReader(new StringReader(jsonString));
296 when(request.getReader()).thenReturn(br);
297 controller.removeEcompDictionary(request, response);
298 logger.info("response.getContentAsString(): " + response.getContentAsString());
299 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("ecompNameDictionaryDatas"));
301 } catch (Exception e) {
302 fail("Exception: " + e);
305 logger.info("testRemoveEcompDictionary: exit");