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=========================================================
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.Before;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
44 import org.onap.policy.rest.dao.CommonClassDao;
45 import org.onap.policy.rest.jpa.Attribute;
46 import org.onap.policy.rest.jpa.Category;
47 import org.onap.policy.rest.jpa.MicroServiceModels;
48 import org.onap.policy.rest.jpa.PolicyEditorScopes;
49 import org.springframework.mock.web.MockHttpServletResponse;
52 * The class <code>DictionaryControllerTest</code> contains tests
53 * for the class {@link <code>DictionaryController</code>}*
55 * All JUnits are designed to run in the local development environment
56 * where they have write privileges and can execute time-sensitive
59 public class DictionaryControllerTest {
61 private static Logger logger = FlexLogger.getLogger(DictionaryControllerTest.class);
62 private static CommonClassDao commonClassDao;
63 private String jsonString = null;
64 private HttpServletRequest request = null;
65 private DictionaryController controller = null;
68 public void setUp() throws Exception {
69 logger.info("setUp: Entering");
70 commonClassDao = Mockito.mock(CommonClassDao.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 List<Object> object = new ArrayList<>();
81 object.add(new Category());
82 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
83 PolicyEditorScopes editorScope = new PolicyEditorScopes();
84 doNothing().when(commonClassDao).save(editorScope);
85 doNothing().when(commonClassDao).update(editorScope);
87 when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
89 request = mock(HttpServletRequest.class);
90 controller = new DictionaryController(commonClassDao);
91 new DictionaryUtils(commonClassDao);
92 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
93 mock(DictionaryUtils.class);
94 logger.info("setUp: exit");
98 public void testGetAttributeDictionaryEntityDatabyAttributeName() {
99 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
101 MockHttpServletResponse response = new MockHttpServletResponse();
103 controller.getAttributeDictionaryEntityDatabyAttributeName(response);
106 assertTrue( response.getContentAsString() != null && 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 && response.getContentAsString().contains("attributeDictionaryDatas"));
125 logger.info("response.getContentAsString(): " + response.getContentAsString());
126 } catch (UnsupportedEncodingException e) {
127 fail("Exception: " + e);
130 logger.info("testGetAttributeDictionaryEntityData: exit");
134 public void testSaveAttributeDictionary() {
135 logger.info("testSaveAttributeDictionary: Entering");
137 MockHttpServletResponse response = new MockHttpServletResponse();
138 request = mock(HttpServletRequest.class);
141 jsonString = "{\"attributeDictionaryData\":{\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"Qwerty\",\"priority\":\"High\",\"userDataTypeValues\":[{\"$$hashKey\":\"object:641\",\"attributeValues\":\"test\",\"id\":\"choice1\"},{\"$$hashKey\":\"object:646\",\"attributeValues\":\"test\",\"id\":\"choice2\"}],\"xacmlId\":\"Qwerty\"},\"userid\":\"demo\"}";
142 BufferedReader br = new BufferedReader(new StringReader(jsonString));
143 when(request.getReader()).thenReturn(br);
144 controller.saveAttributeDictionary(request, response);
145 logger.info("response.getContentAsString(): " + response.getContentAsString());
146 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
148 } catch (Exception e) {
149 fail("Exception: " + e);
152 logger.info("testSaveAttributeDictionary: exit");
156 public void testRemoveAttributeDictionary() {
157 logger.info("testRemoveAttributeDictionary: Entering");
159 MockHttpServletResponse response = new MockHttpServletResponse();
160 request = mock(HttpServletRequest.class);
163 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"xacmlId\":\"Test\"}}";
164 BufferedReader br = new BufferedReader(new StringReader(jsonString));
165 when(request.getReader()).thenReturn(br);
166 controller.removeAttributeDictionary(request, response);
167 logger.info("response.getContentAsString(): " + response.getContentAsString());
168 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
170 } catch (Exception e) {
171 fail("Exception: " + e);
174 logger.info("testRemoveAttributeDictionary: exit");
178 public void testGetOnapNameDictionaryByNameEntityData() {
179 logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
181 MockHttpServletResponse response = new MockHttpServletResponse();
183 controller.getOnapNameDictionaryByNameEntityData(response);
186 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
187 logger.info("response.getContentAsString(): " + response.getContentAsString());
188 } catch (UnsupportedEncodingException e) {
189 fail("Exception: " + e);
192 logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
196 public void testGetOnapNameDictionaryEntityData() {
197 logger.info("testGetOnapNameDictionaryEntityData: Entering");
199 MockHttpServletResponse response = new MockHttpServletResponse();
201 controller.getOnapNameDictionaryEntityData(response);
204 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
205 logger.info("response.getContentAsString(): " + response.getContentAsString());
206 } catch (UnsupportedEncodingException e) {
207 fail("Exception: " + e);
210 logger.info("testGetOnapNameDictionaryEntityData: exit");
214 public void testSaveOnapDictionary() {
216 logger.info("testSaveOnapDictionary: Entering");
218 MockHttpServletResponse response = new MockHttpServletResponse();
219 request = mock(HttpServletRequest.class);
222 jsonString = "{\"userid\":\"demo\",\"onapNameDictionaryData\":{\"description\":\"test\",\"onapName\":\"Test\"}}";
223 BufferedReader br = new BufferedReader(new StringReader(jsonString));
224 when(request.getReader()).thenReturn(br);
225 controller.saveOnapDictionary(request, response);
226 logger.info("response.getContentAsString(): " + response.getContentAsString());
227 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
229 } catch (Exception e) {
230 fail("Exception: " + e);
233 logger.info("testSaveOnapDictionary: exit");
237 public void testRemoveOnapDictionary() {
238 logger.info("testRemoveOnapDictionary: Entering");
240 MockHttpServletResponse response = new MockHttpServletResponse();
241 request = mock(HttpServletRequest.class);
244 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"onapName\":\"Test\"}}";
245 BufferedReader br = new BufferedReader(new StringReader(jsonString));
246 when(request.getReader()).thenReturn(br);
247 controller.removeOnapDictionary(request, response);
248 logger.info("response.getContentAsString(): " + response.getContentAsString());
249 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
251 } catch (Exception e) {
252 fail("Exception: " + e);
255 logger.info("testRemoveOnapDictionary: exit");