[POLICY-73] replace openecomp for policy-engine
[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.*;
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.json.Json;
36 import javax.json.JsonObject;
37 import javax.servlet.http.HttpServletRequest;
38
39 import org.json.JSONObject;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.common.logging.flexlogger.FlexLogger;
45 import org.onap.policy.common.logging.flexlogger.Logger;
46 import org.onap.policy.rest.adapter.PolicyRestAdapter;
47 import org.onap.policy.rest.dao.CommonClassDao;
48 import org.onap.policy.rest.jpa.Attribute;
49 import org.onap.policy.rest.jpa.MicroServiceModels;
50 import org.onap.policy.rest.jpa.PolicyEditorScopes;
51 import org.springframework.mock.web.MockHttpServletResponse;
52
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.JsonNode;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56 import com.github.fge.jackson.JsonLoader;
57
58 /**
59  * The class <code>DictionaryControllerTest</code> contains tests
60  * for the class {@link <code>DictionaryController</code>}*
61  *
62  * All JUnits are designed to run in the local development environment
63  * where they have write privileges and can execute time-sensitive
64  * tasks.
65  */
66 public class DictionaryControllerTest {
67         
68         private static Logger logger = FlexLogger.getLogger(DictionaryControllerTest.class);
69         private static CommonClassDao commonClassDao;
70         private String jsonString = null;
71         private String configBodyString = null;
72         private HttpServletRequest request = null;
73         private DictionaryController controller = null;
74         private BufferedReader br = null;
75
76         @Before
77         public void setUp() throws Exception {
78                 logger.info("setUp: Entering");
79         commonClassDao = Mockito.mock(CommonClassDao.class);
80             HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
81    
82         List<Object> microServiceModelsData = new ArrayList<Object>();
83         MicroServiceModels testData = new MicroServiceModels();
84         testData.setVersion("1707.4.1.2-Junit");        
85
86         //--- mock the getDataByColumn() call
87         List<String>  microList = new ArrayList<String>();
88         microList.add("123");
89         List<Object>  listId = new ArrayList<Object>();
90         when(commonClassDao.getDataByColumn(Attribute.class, "xacmlId")).thenReturn(microList);
91         PolicyEditorScopes editorScope = new PolicyEditorScopes();
92         doNothing().when(commonClassDao).save(editorScope);
93         doNothing().when(commonClassDao).update(editorScope);
94         
95         when(commonClassDao.getData(Attribute.class)).thenReturn(listId);
96         
97                 jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\",  \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
98                                 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
99                                 + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
100                                 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
101                                 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
102                                 + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
103                                 + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
104                                 + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
105
106                 configBodyString = "{\"service\":\"SniroPolicyEntityTest\",\"policyName\":\"someone\",\"description\":\"test\",\"templateVersion\":\"1607\",\"version\":\"HD\","
107                                 + "\"priority\":\"2\",\"content\":{\"lastPolled\":\"1\",\"boolen-test\":\"true\",\"created\":\"test\",\"retiredDate\":\"test\",\"scope\":\"SNIRO_PLACEMENT_VDHV\","
108                                 + "\"name\":\"test\",\"lastModified\":\"test\",\"state\":\"CREATED\",\"type\":\"CONFIG\",\"intent\":\"test\",\"target\":\"SNIRO\"}}";
109
110                 request = mock(HttpServletRequest.class);        
111         BufferedReader br = new BufferedReader(new StringReader(jsonString));
112         //--- mock the getReader() call
113         when(request.getReader()).thenReturn(br);   
114         
115         controller = new DictionaryController(commonClassDao);
116         
117         logger.info("setUp: exit");
118         }
119
120         @After
121         public void tearDown() throws Exception {
122         }
123
124         @Test
125         public void testGetAttributeDictionaryEntityDatabyAttributeName() {
126                 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");
127
128                 MockHttpServletResponse response =  new MockHttpServletResponse();
129
130                 controller.getAttributeDictionaryEntityDatabyAttributeName(request, response);
131                 
132                 try {
133                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
134                         logger.info("response.getContentAsString(): " + response.getContentAsString());
135                 } catch (UnsupportedEncodingException e) {
136                         fail("Exception: " + e);
137                 }
138                 
139                 logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
140         }
141
142         @Test
143         public void testGetAttributeDictionaryEntityData() {
144                 logger.info("testGetAttributeDictionaryEntityData: Entering");
145
146                 MockHttpServletResponse response =  new MockHttpServletResponse();
147
148                 controller.getAttributeDictionaryEntityData(request, response);
149                 
150                 try {
151                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
152                         logger.info("response.getContentAsString(): " + response.getContentAsString());
153                 } catch (UnsupportedEncodingException e) {
154                         fail("Exception: " + e);
155                 }
156                 
157                 logger.info("testGetAttributeDictionaryEntityData: exit");
158         }
159
160         @Test
161         public void testSaveAttributeDictionary() {
162                 logger.info("testSaveAttributeDictionary: Entering");
163
164                 MockHttpServletResponse response =  new MockHttpServletResponse();
165             request = mock(HttpServletRequest.class);  
166         
167                 try {
168                     // mock the getReader() call
169                         jsonString = "{\"attributeDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}],  \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
170                                         + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
171                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
172                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
173                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
174                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
175                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
176                                         + "     \"policyJSON\": {\"some\": \"test\",    \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
177                         
178                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
179                         when(request.getReader()).thenReturn(br);                   
180                         controller.saveAttributeDictionary(request, response);
181                         logger.info("response.getContentAsString(): " + response.getContentAsString());
182                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
183
184                 } catch (Exception e) {
185                         fail("Exception: " + e);
186                 }
187                 
188                 logger.info("testSaveAttributeDictionary: exit");
189         }
190
191         @Test
192         public void testRemoveAttributeDictionary() {
193                 logger.info("testRemoveAttributeDictionary: Entering");
194
195                 MockHttpServletResponse response =  new MockHttpServletResponse();
196             request = mock(HttpServletRequest.class);   
197         
198                 try {
199                     // mock the getReader() call
200                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
201                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
202                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
203                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
204                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
205                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
206                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
207                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
208                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
209                         when(request.getReader()).thenReturn(br);                   
210                         controller.removeAttributeDictionary(request, response);
211                         logger.info("response.getContentAsString(): " + response.getContentAsString());
212                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("attributeDictionaryDatas"));
213
214                 } catch (Exception e) {
215                         fail("Exception: " + e);
216                 }
217                 
218                 logger.info("testRemoveAttributeDictionary: exit");
219         }
220
221         @Test
222         public void testGetOnapNameDictionaryByNameEntityData() {
223                 logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");
224
225                 MockHttpServletResponse response =  new MockHttpServletResponse();
226
227                 controller.getOnapNameDictionaryByNameEntityData(request, response);
228                 
229                 try {
230                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
231                         logger.info("response.getContentAsString(): " + response.getContentAsString());
232                 } catch (UnsupportedEncodingException e) {
233                         fail("Exception: " + e);
234                 }
235                 
236                 logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
237         }
238
239         @Test
240         public void testGetOnapNameDictionaryEntityData() {
241                 logger.info("testGetOnapNameDictionaryEntityData: Entering");
242
243                 MockHttpServletResponse response =  new MockHttpServletResponse();
244
245                 controller.getOnapNameDictionaryEntityData(request, response);
246                 
247                 try {
248                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
249                         logger.info("response.getContentAsString(): " + response.getContentAsString());
250                 } catch (UnsupportedEncodingException e) {
251                         fail("Exception: " + e);
252                 }
253                 
254                 logger.info("testGetOnapNameDictionaryEntityData: exit");
255         }
256
257         @Test
258         public void testSaveOnapDictionary() {
259                 
260                 logger.info("testSaveOnapDictionary: Entering");
261
262                 MockHttpServletResponse response =  new MockHttpServletResponse();
263             request = mock(HttpServletRequest.class);  
264         
265                 try {
266                     // mock the getReader() call
267                         jsonString = "{\"onapNameDictionaryData\": {\"userDataTypeValues\": [{\"attributeValues\": \"Values1\"}, {\"attributeValues\": \"Values2\"}],   \"datatypeBean\": {\"type\": \"C\"},\"model\": {\"name\": \"testingdata\", "
268                                         + " \"subScopename\": \"\",\"userDataTypeValues\": [\"user-type\"],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
269                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
270                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
271                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
272                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
273                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
274                                         + "     \"policyJSON\": {\"some\": \"test\",    \"dmdTopic\": \"1\",\"fileId\": \"56\"}, \"userid\":\"smetest\", \"userDataTypeValues\":[\"type-one\"]}";
275                         
276                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
277                         when(request.getReader()).thenReturn(br);                   
278                         controller.saveOnapDictionary(request, response);
279                         logger.info("response.getContentAsString(): " + response.getContentAsString());
280                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
281
282                 } catch (Exception e) {
283                         fail("Exception: " + e);
284                 }
285                 
286                 logger.info("testSaveOnapDictionary: exit");            
287         }
288
289         @Test
290         public void testRemoveOnapDictionary() {
291                 logger.info("testRemoveOnapDictionary: Entering");
292
293                 MockHttpServletResponse response =  new MockHttpServletResponse();
294             request = mock(HttpServletRequest.class);   
295         
296                 try {
297                     // mock the getReader() call
298                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
299                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
300                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
301                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
302                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
303                                         + "     \"policyDescription\": \"testing input\", \"onapName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
304                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
305                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
306                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
307                         when(request.getReader()).thenReturn(br);                   
308                         controller.removeOnapDictionary(request, response);
309                         logger.info("response.getContentAsString(): " + response.getContentAsString());
310                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("onapNameDictionaryDatas"));
311
312                 } catch (Exception e) {
313                         fail("Exception: " + e);
314                 }
315                 
316                 logger.info("testRemoveOnapDictionary: exit");
317         }
318
319 }