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