Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / DictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
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;
28
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;
34
35 import javax.servlet.http.HttpServletRequest;
36
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;
49
50 /**
51  * The class <code>DictionaryControllerTest</code> contains tests
52  * for the class {@link <code>DictionaryController</code>}*
53  *
54  * All JUnits are designed to run in the local development environment
55  * where they have write privileges and can execute time-sensitive
56  * tasks.
57  */
58 public class DictionaryControllerTest {
59
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;
65
66     @Before
67     public void setUp() throws Exception {
68         logger.info("setUp: Entering");
69         commonClassDao = Mockito.mock(CommonClassDao.class);
70
71         MicroServiceModels testData = new MicroServiceModels();
72         testData.setVersion("1707.4.1.2-Junit");
73
74         // --- mock the getDataByColumn() call
75         List<String> microList = new ArrayList<String>();
76         microList.add("123");
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);
85
86         when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
87
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");
94     }
95
96     @Test
97     public void testGetAttributeDictionaryEntityDatabyAttributeName() {
98         logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
99
100         MockHttpServletResponse response = new MockHttpServletResponse();
101
102         controller.getAttributeDictionaryEntityDatabyAttributeName(response);
103
104         try {
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);
110         }
111
112         logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
113     }
114
115     @Test
116     public void testGetAttributeDictionaryEntityData() {
117         logger.info("testGetAttributeDictionaryEntityData: Entering");
118
119         MockHttpServletResponse response = new MockHttpServletResponse();
120
121         controller.getAttributeDictionaryEntityData(response);
122
123         try {
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);
129         }
130
131         logger.info("testGetAttributeDictionaryEntityData: exit");
132     }
133
134     @Test
135     public void testSaveAttributeDictionary() {
136         logger.info("testSaveAttributeDictionary: Entering");
137
138         MockHttpServletResponse response = new MockHttpServletResponse();
139         request = mock(HttpServletRequest.class);
140
141         try {
142             jsonString =
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"));
154
155         } catch (Exception e) {
156             fail("Exception: " + e);
157         }
158
159         logger.info("testSaveAttributeDictionary: exit");
160     }
161
162     @Test
163     public void testRemoveAttributeDictionary() {
164         logger.info("testRemoveAttributeDictionary: Entering");
165
166         MockHttpServletResponse response = new MockHttpServletResponse();
167         request = mock(HttpServletRequest.class);
168
169         try {
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"));
177
178         } catch (Exception e) {
179             fail("Exception: " + e);
180         }
181
182         logger.info("testRemoveAttributeDictionary: exit");
183     }
184
185     @Test
186     public void testGetOnapNameDictionaryByNameEntityData() {
187         logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
188
189         MockHttpServletResponse response = new MockHttpServletResponse();
190
191         controller.getOnapNameDictionaryByNameEntityData(response);
192
193         try {
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);
199         }
200
201         logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
202     }
203
204     @Test
205     public void testGetOnapNameDictionaryEntityData() {
206         logger.info("testGetOnapNameDictionaryEntityData: Entering");
207
208         MockHttpServletResponse response = new MockHttpServletResponse();
209
210         controller.getOnapNameDictionaryEntityData(response);
211
212         try {
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);
218         }
219
220         logger.info("testGetOnapNameDictionaryEntityData: exit");
221     }
222
223     @Test
224     public void testSaveOnapDictionary() {
225
226         logger.info("testSaveOnapDictionary: Entering");
227
228         MockHttpServletResponse response = new MockHttpServletResponse();
229         request = mock(HttpServletRequest.class);
230
231         try {
232             jsonString =
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"));
240
241         } catch (Exception e) {
242             fail("Exception: " + e);
243         }
244
245         logger.info("testSaveOnapDictionary: exit");
246     }
247
248     @Test
249     public void testRemoveOnapDictionary() {
250         logger.info("testRemoveOnapDictionary: Entering");
251
252         MockHttpServletResponse response = new MockHttpServletResponse();
253         request = mock(HttpServletRequest.class);
254
255         try {
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"));
263
264         } catch (Exception e) {
265             fail("Exception: " + e);
266         }
267
268         logger.info("testRemoveOnapDictionary: exit");
269     }
270
271 }