Merge "Policy:Major"
[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-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
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
22 package org.onap.policy.pap.xacml.rest.controller;
23
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;
29
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;
35
36 import javax.servlet.http.HttpServletRequest;
37
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;
50
51 /**
52  * The class <code>DictionaryControllerTest</code> contains tests
53  * for the class {@link <code>DictionaryController</code>}*
54  *
55  * All JUnits are designed to run in the local development environment
56  * where they have write privileges and can execute time-sensitive
57  * tasks.
58  */
59 public class DictionaryControllerTest {
60         
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;
66
67         @Before
68         public void setUp() throws Exception {
69                 logger.info("setUp: Entering");
70         commonClassDao = Mockito.mock(CommonClassDao.class);
71
72         MicroServiceModels testData = new MicroServiceModels();
73         testData.setVersion("1707.4.1.2-Junit");        
74
75         //--- mock the getDataByColumn() call
76         List<String>  microList = new ArrayList<String>();
77         microList.add("123");
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);
86         
87         when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
88         
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");
95         }
96         
97         @Test
98         public void testGetAttributeDictionaryEntityDatabyAttributeName() {
99                 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
100
101                 MockHttpServletResponse response =  new MockHttpServletResponse();
102
103                 controller.getAttributeDictionaryEntityDatabyAttributeName(response);
104                 
105                 try {
106                         assertTrue( response.getContentAsString() != null && 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 && response.getContentAsString().contains("attributeDictionaryDatas"));
125                         logger.info("response.getContentAsString(): " + response.getContentAsString());
126                 } catch (UnsupportedEncodingException e) {
127                         fail("Exception: " + e);
128                 }
129                 
130                 logger.info("testGetAttributeDictionaryEntityData: exit");
131         }
132
133         @Test
134         public void testSaveAttributeDictionary() {
135                 logger.info("testSaveAttributeDictionary: Entering");
136
137                 MockHttpServletResponse response =  new MockHttpServletResponse();
138             request = mock(HttpServletRequest.class);  
139         
140                 try {   
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"));
147
148                 } catch (Exception e) {
149                         fail("Exception: " + e);
150                 }
151                 
152                 logger.info("testSaveAttributeDictionary: exit");
153         }
154
155         @Test
156         public void testRemoveAttributeDictionary() {
157                 logger.info("testRemoveAttributeDictionary: Entering");
158
159                 MockHttpServletResponse response =  new MockHttpServletResponse();
160             request = mock(HttpServletRequest.class);   
161         
162                 try {
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"));
169
170                 } catch (Exception e) {
171                         fail("Exception: " + e);
172                 }
173                 
174                 logger.info("testRemoveAttributeDictionary: exit");
175         }
176
177         @Test
178         public void testGetOnapNameDictionaryByNameEntityData() {
179                 logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
180
181                 MockHttpServletResponse response =  new MockHttpServletResponse();
182
183                 controller.getOnapNameDictionaryByNameEntityData(response);
184                 
185                 try {
186                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
187                         logger.info("response.getContentAsString(): " + response.getContentAsString());
188                 } catch (UnsupportedEncodingException e) {
189                         fail("Exception: " + e);
190                 }
191                 
192                 logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
193         }
194
195         @Test
196         public void testGetOnapNameDictionaryEntityData() {
197                 logger.info("testGetOnapNameDictionaryEntityData: Entering");
198
199                 MockHttpServletResponse response =  new MockHttpServletResponse();
200
201                 controller.getOnapNameDictionaryEntityData(response);
202                 
203                 try {
204                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
205                         logger.info("response.getContentAsString(): " + response.getContentAsString());
206                 } catch (UnsupportedEncodingException e) {
207                         fail("Exception: " + e);
208                 }
209                 
210                 logger.info("testGetOnapNameDictionaryEntityData: exit");
211         }
212
213         @Test
214         public void testSaveOnapDictionary() {
215                 
216                 logger.info("testSaveOnapDictionary: Entering");
217
218                 MockHttpServletResponse response =  new MockHttpServletResponse();
219             request = mock(HttpServletRequest.class);  
220         
221                 try {   
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"));
228
229                 } catch (Exception e) {
230                         fail("Exception: " + e);
231                 }
232                 
233                 logger.info("testSaveOnapDictionary: exit");            
234         }
235
236         @Test
237         public void testRemoveOnapDictionary() {
238                 logger.info("testRemoveOnapDictionary: Entering");
239
240                 MockHttpServletResponse response =  new MockHttpServletResponse();
241             request = mock(HttpServletRequest.class);   
242         
243                 try {
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"));
250
251                 } catch (Exception e) {
252                         fail("Exception: " + e);
253                 }
254                 
255                 logger.info("testRemoveOnapDictionary: exit");
256         }
257
258 }