Fixes for eclipse warnings unused variables
[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 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.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.rest.dao.CommonClassDao;
45 import org.onap.policy.rest.jpa.Attribute;
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             HttpServletRequest request = Mockito.mock(HttpServletRequest.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         PolicyEditorScopes editorScope = new PolicyEditorScopes();
81         doNothing().when(commonClassDao).save(editorScope);
82         doNothing().when(commonClassDao).update(editorScope);
83         
84         when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
85         
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\", \"onapName\": \"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\"} }";
94                 request = mock(HttpServletRequest.class);        
95         BufferedReader br = new BufferedReader(new StringReader(jsonString));
96         //--- mock the getReader() call
97         when(request.getReader()).thenReturn(br);   
98         
99         controller = new DictionaryController(commonClassDao);
100         
101         logger.info("setUp: exit");
102         }
103
104         @After
105         public void tearDown() throws Exception {
106         }
107
108         @Test
109         public void testGetAttributeDictionaryEntityDatabyAttributeName() {
110                 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
111
112                 MockHttpServletResponse response =  new MockHttpServletResponse();
113
114                 controller.getAttributeDictionaryEntityDatabyAttributeName(request, response);
115                 
116                 try {
117                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
118                         logger.info("response.getContentAsString(): " + response.getContentAsString());
119                 } catch (UnsupportedEncodingException e) {
120                         fail("Exception: " + e);
121                 }
122                 
123                 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
124         }
125
126         @Test
127         public void testGetAttributeDictionaryEntityData() {
128                 logger.info("testGetAttributeDictionaryEntityData: Entering");
129
130                 MockHttpServletResponse response =  new MockHttpServletResponse();
131
132                 controller.getAttributeDictionaryEntityData(request, response);
133                 
134                 try {
135                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
136                         logger.info("response.getContentAsString(): " + response.getContentAsString());
137                 } catch (UnsupportedEncodingException e) {
138                         fail("Exception: " + e);
139                 }
140                 
141                 logger.info("testGetAttributeDictionaryEntityData: exit");
142         }
143
144         @Test
145         public void testSaveAttributeDictionary() {
146                 logger.info("testSaveAttributeDictionary: Entering");
147
148                 MockHttpServletResponse response =  new MockHttpServletResponse();
149             request = mock(HttpServletRequest.class);  
150         
151                 try {
152                     // mock the getReader() call
153                         jsonString = "{\"attributeDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}],  \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
154                                         + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
155                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
156                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
157                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
158                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
159                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
160                                         + "     \"policyJSON\": {\"some\": \"test\",    \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
161                         
162                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
163                         when(request.getReader()).thenReturn(br);                   
164                         controller.saveAttributeDictionary(request, response);
165                         logger.info("response.getContentAsString(): " + response.getContentAsString());
166                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
167
168                 } catch (Exception e) {
169                         fail("Exception: " + e);
170                 }
171                 
172                 logger.info("testSaveAttributeDictionary: exit");
173         }
174
175         @Test
176         public void testRemoveAttributeDictionary() {
177                 logger.info("testRemoveAttributeDictionary: Entering");
178
179                 MockHttpServletResponse response =  new MockHttpServletResponse();
180             request = mock(HttpServletRequest.class);   
181         
182                 try {
183                     // mock the getReader() call
184                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
185                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
186                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
187                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
188                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
189                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
190                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
191                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
192                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
193                         when(request.getReader()).thenReturn(br);                   
194                         controller.removeAttributeDictionary(request, response);
195                         logger.info("response.getContentAsString(): " + response.getContentAsString());
196                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
197
198                 } catch (Exception e) {
199                         fail("Exception: " + e);
200                 }
201                 
202                 logger.info("testRemoveAttributeDictionary: exit");
203         }
204
205         @Test
206         public void testGetOnapNameDictionaryByNameEntityData() {
207                 logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
208
209                 MockHttpServletResponse response =  new MockHttpServletResponse();
210
211                 controller.getOnapNameDictionaryByNameEntityData(request, response);
212                 
213                 try {
214                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
215                         logger.info("response.getContentAsString(): " + response.getContentAsString());
216                 } catch (UnsupportedEncodingException e) {
217                         fail("Exception: " + e);
218                 }
219                 
220                 logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
221         }
222
223         @Test
224         public void testGetOnapNameDictionaryEntityData() {
225                 logger.info("testGetOnapNameDictionaryEntityData: Entering");
226
227                 MockHttpServletResponse response =  new MockHttpServletResponse();
228
229                 controller.getOnapNameDictionaryEntityData(request, response);
230                 
231                 try {
232                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
233                         logger.info("response.getContentAsString(): " + response.getContentAsString());
234                 } catch (UnsupportedEncodingException e) {
235                         fail("Exception: " + e);
236                 }
237                 
238                 logger.info("testGetOnapNameDictionaryEntityData: exit");
239         }
240
241         @Test
242         public void testSaveOnapDictionary() {
243                 
244                 logger.info("testSaveOnapDictionary: Entering");
245
246                 MockHttpServletResponse response =  new MockHttpServletResponse();
247             request = mock(HttpServletRequest.class);  
248         
249                 try {
250                     // mock the getReader() call
251                         jsonString = "{\"onapNameDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}],   \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
252                                         + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
253                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
254                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
255                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
256                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
257                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
258                                         + "     \"policyJSON\": {\"some\": \"test\",    \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
259                         
260                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
261                         when(request.getReader()).thenReturn(br);                   
262                         controller.saveOnapDictionary(request, response);
263                         logger.info("response.getContentAsString(): " + response.getContentAsString());
264                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
265
266                 } catch (Exception e) {
267                         fail("Exception: " + e);
268                 }
269                 
270                 logger.info("testSaveOnapDictionary: exit");            
271         }
272
273         @Test
274         public void testRemoveOnapDictionary() {
275                 logger.info("testRemoveOnapDictionary: Entering");
276
277                 MockHttpServletResponse response =  new MockHttpServletResponse();
278             request = mock(HttpServletRequest.class);   
279         
280                 try {
281                     // mock the getReader() call
282                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
283                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
284                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
285                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
286                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
287                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
288                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
289                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
290                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
291                         when(request.getReader()).thenReturn(br);                   
292                         controller.removeOnapDictionary(request, response);
293                         logger.info("response.getContentAsString(): " + response.getContentAsString());
294                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
295
296                 } catch (Exception e) {
297                         fail("Exception: " + e);
298                 }
299                 
300                 logger.info("testRemoveOnapDictionary: exit");
301         }
302
303 }