2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
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.servlet.http.HttpServletRequest;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
43 import org.onap.policy.rest.dao.CommonClassDao;
44 import org.onap.policy.rest.jpa.Attribute;
45 import org.onap.policy.rest.jpa.Category;
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);
71 MicroServiceModels testData = new MicroServiceModels();
72 testData.setVersion("1707.4.1.2-Junit");
74 // --- mock the getDataByColumn() call
75 List<String> microList = new ArrayList<String>();
77 List<Object> listId = new ArrayList<Object>();
78 when(commonClassDao.getDataByColumn(Attribute.class, "xacmlId")).thenReturn(microList);
79 List<Object> object = new ArrayList<>();
80 object.add(new Category());
81 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
82 PolicyEditorScopes editorScope = new PolicyEditorScopes();
83 doNothing().when(commonClassDao).save(editorScope);
84 doNothing().when(commonClassDao).update(editorScope);
86 when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
88 request = mock(HttpServletRequest.class);
89 controller = new DictionaryController(commonClassDao);
90 new DictionaryUtils(commonClassDao);
91 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
92 mock(DictionaryUtils.class);
93 logger.info("setUp: exit");
97 public void testGetAttributeDictionaryEntityDatabyAttributeName() {
98 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
100 MockHttpServletResponse response = new MockHttpServletResponse();
102 controller.getAttributeDictionaryEntityDatabyAttributeName(response);
105 assertTrue(response.getContentAsString() != null
106 && response.getContentAsString().contains("attributeDictionaryDatas"));
107 logger.info("response.getContentAsString(): " + response.getContentAsString());
108 } catch (UnsupportedEncodingException e) {
109 fail("Exception: " + e);
112 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
116 public void testGetAttributeDictionaryEntityData() {
117 logger.info("testGetAttributeDictionaryEntityData: Entering");
119 MockHttpServletResponse response = new MockHttpServletResponse();
121 controller.getAttributeDictionaryEntityData(response);
124 assertTrue(response.getContentAsString() != null
125 && response.getContentAsString().contains("attributeDictionaryDatas"));
126 logger.info("response.getContentAsString(): " + response.getContentAsString());
127 } catch (UnsupportedEncodingException e) {
128 fail("Exception: " + e);
131 logger.info("testGetAttributeDictionaryEntityData: exit");
135 public void testSaveAttributeDictionary() {
136 logger.info("testSaveAttributeDictionary: Entering");
138 MockHttpServletResponse response = new MockHttpServletResponse();
139 request = mock(HttpServletRequest.class);
143 "{\"attributeDictionaryData\":{\"datatypeBean\":{\"shortName\":\"string\"},\"description\":"
144 + "\"Qwerty\",\"priority\":\"High\",\"userDataTypeValues\":[{\"$$hashKey\":"
145 + "\"object:641\",\"attributeValues\":\"test\",\"id\":\"choice1\"},{\"$$hashKey\":"
146 + "\"object:646\",\"attributeValues\":\"test\",\"id\":\"choice2\"}],\"xacmlId\":"
147 + "\"Qwerty\"},\"userid\":\"demo\"}";
148 BufferedReader br = new BufferedReader(new StringReader(jsonString));
149 when(request.getReader()).thenReturn(br);
150 controller.saveAttributeDictionary(request, response);
151 logger.info("response.getContentAsString(): " + response.getContentAsString());
152 assertTrue(response.getContentAsString() != null
153 && response.getContentAsString().contains("attributeDictionaryDatas"));
155 } catch (Exception e) {
156 fail("Exception: " + e);
159 logger.info("testSaveAttributeDictionary: exit");
163 public void testRemoveAttributeDictionary() {
164 logger.info("testRemoveAttributeDictionary: Entering");
166 MockHttpServletResponse response = new MockHttpServletResponse();
167 request = mock(HttpServletRequest.class);
170 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"xacmlId\":\"Test\"}}";
171 BufferedReader br = new BufferedReader(new StringReader(jsonString));
172 when(request.getReader()).thenReturn(br);
173 controller.removeAttributeDictionary(request, response);
174 logger.info("response.getContentAsString(): " + response.getContentAsString());
175 assertTrue(response.getContentAsString() != null
176 && response.getContentAsString().contains("attributeDictionaryDatas"));
178 } catch (Exception e) {
179 fail("Exception: " + e);
182 logger.info("testRemoveAttributeDictionary: exit");
186 public void testGetOnapNameDictionaryByNameEntityData() {
187 logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
189 MockHttpServletResponse response = new MockHttpServletResponse();
191 controller.getOnapNameDictionaryByNameEntityData(response);
194 assertTrue(response.getContentAsString() != null
195 && response.getContentAsString().contains("onapNameDictionaryDatas"));
196 logger.info("response.getContentAsString(): " + response.getContentAsString());
197 } catch (UnsupportedEncodingException e) {
198 fail("Exception: " + e);
201 logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
205 public void testGetOnapNameDictionaryEntityData() {
206 logger.info("testGetOnapNameDictionaryEntityData: Entering");
208 MockHttpServletResponse response = new MockHttpServletResponse();
210 controller.getOnapNameDictionaryEntityData(response);
213 assertTrue(response.getContentAsString() != null
214 && response.getContentAsString().contains("onapNameDictionaryDatas"));
215 logger.info("response.getContentAsString(): " + response.getContentAsString());
216 } catch (UnsupportedEncodingException e) {
217 fail("Exception: " + e);
220 logger.info("testGetOnapNameDictionaryEntityData: exit");
224 public void testSaveOnapDictionary() {
226 logger.info("testSaveOnapDictionary: Entering");
228 MockHttpServletResponse response = new MockHttpServletResponse();
229 request = mock(HttpServletRequest.class);
233 "{\"userid\":\"demo\",\"onapNameDictionaryData\":{\"description\":\"test\",\"onapName\":\"Test\"}}";
234 BufferedReader br = new BufferedReader(new StringReader(jsonString));
235 when(request.getReader()).thenReturn(br);
236 controller.saveOnapDictionary(request, response);
237 logger.info("response.getContentAsString(): " + response.getContentAsString());
238 assertTrue(response.getContentAsString() != null
239 && response.getContentAsString().contains("onapNameDictionaryDatas"));
241 } catch (Exception e) {
242 fail("Exception: " + e);
245 logger.info("testSaveOnapDictionary: exit");
249 public void testRemoveOnapDictionary() {
250 logger.info("testRemoveOnapDictionary: Entering");
252 MockHttpServletResponse response = new MockHttpServletResponse();
253 request = mock(HttpServletRequest.class);
256 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"onapName\":\"Test\"}}";
257 BufferedReader br = new BufferedReader(new StringReader(jsonString));
258 when(request.getReader()).thenReturn(br);
259 controller.removeOnapDictionary(request, response);
260 logger.info("response.getContentAsString(): " + response.getContentAsString());
261 assertTrue(response.getContentAsString() != null
262 && response.getContentAsString().contains("onapNameDictionaryDatas"));
264 } catch (Exception e) {
265 fail("Exception: " + e);
268 logger.info("testRemoveOnapDictionary: exit");