Change the header to SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / openecomp / mso / apihandlerinfra / MsoRequestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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 package org.openecomp.mso.apihandlerinfra;
21
22 import org.apache.commons.io.IOUtils;
23 import org.codehaus.jackson.JsonParseException;
24 import org.codehaus.jackson.map.JsonMappingException;
25 import org.codehaus.jackson.map.ObjectMapper;
26 import org.junit.Test;
27
28 import static org.junit.Assert.assertEquals;
29
30
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertTrue;
33 import static org.junit.Assert.fail;
34
35 import java.io.IOException;
36 import java.util.HashMap;
37
38 import org.openecomp.mso.apihandler.common.ValidationException;
39 import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.ServiceInstancesRequest;
40
41 public class MsoRequestTest {
42
43
44
45         @Test
46         public void testParseOrchestration() throws JsonParseException, JsonMappingException, IOException, ValidationException{
47                         ObjectMapper mapper = new ObjectMapper();
48                         String requestJSON = " {\"requestDetails\": {\"requestInfo\": { \"source\": \"VID\", \"requestorId\": \"zz9999\"}}}";
49                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
50                         MsoRequest msoRequest = new MsoRequest ("1234");
51                         msoRequest.parseOrchestration(sir);
52                         assertEquals(msoRequest.getRequestInfo().getSource(),"VID");
53                         assertEquals(msoRequest.getRequestInfo().getRequestorId(),"zz9999");
54
55         }
56
57         @Test(expected = ValidationException.class)
58         public void testParseOrchestrationFailure() throws JsonParseException, JsonMappingException, IOException, ValidationException{
59                         ObjectMapper mapper = new ObjectMapper();
60                         String requestJSON = " {\"requestDetails\": {\"requestInfo\": { \"source\": \"VID\"}}}";
61                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
62                         MsoRequest msoRequest = new MsoRequest ("1234");
63                         msoRequest.parseOrchestration(sir);
64
65         }
66
67         @Test
68         public void testParseV3VnfCreate() throws JsonParseException, JsonMappingException, IOException, ValidationException{
69                 String requestJSON;
70                  try {
71                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3VnfCreate.json"));
72
73                 } catch (IOException e) {
74                     fail ("Exception caught");
75                     e.printStackTrace ();
76                     return;
77                 }
78                         ObjectMapper mapper = new ObjectMapper();
79                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
80                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
81                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
82                         MsoRequest msoRequest = new MsoRequest ("1234");
83                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v3");
84                         assertEquals(msoRequest.getRequestInfo().getSource(),"VID");
85                         assertFalse(msoRequest.getALaCarteFlag());
86                         assertEquals(msoRequest.getReqVersion(),3);
87                         boolean testIsALaCarteSet = msoRequest.getServiceInstancesRequest().getRequestDetails().getRequestParameters().isALaCarteSet();
88                         assertFalse(testIsALaCarteSet);
89
90         }
91
92         @Test(expected = ValidationException.class)
93         public void testParseV3VolumeGroupFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
94                 String requestJSON;
95                  try {
96                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3VolumeGroupBad.json"));
97
98                 } catch (IOException e) {
99                     fail ("Exception caught");
100                     e.printStackTrace ();
101                     return;
102                 }
103                         ObjectMapper mapper = new ObjectMapper();
104                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
105                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
106                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
107                         MsoRequest msoRequest = new MsoRequest ("1234");
108                         msoRequest.parse(sir, instanceIdMap, Action.updateInstance, "v3");
109
110         }
111
112         @Test
113         public void testParseV3UpdateNetwork() throws JsonParseException, JsonMappingException, IOException, ValidationException{
114                 String requestJSON;
115                  try {
116                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3UpdateNetwork.json"));
117
118                 } catch (IOException e) {
119                     fail ("Exception caught");
120                     e.printStackTrace ();
121                     return;
122                 }
123                         ObjectMapper mapper = new ObjectMapper();
124                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
125                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
126                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
127                         MsoRequest msoRequest = new MsoRequest ("1234");
128                         msoRequest.parse(sir, instanceIdMap, Action.updateInstance, "v3");
129
130         }
131
132         @Test(expected = ValidationException.class)
133         public void testParseV3UpdateNetworkFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
134                 String requestJSON;
135                  try {
136                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3UpdateNetworkBad.json"));
137
138                 } catch (IOException e) {
139                     fail ("Exception caught");
140                     e.printStackTrace ();
141                     return;
142                 }
143                         ObjectMapper mapper = new ObjectMapper();
144                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
145                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
146                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
147                         MsoRequest msoRequest = new MsoRequest ("1234");
148                         msoRequest.parse(sir, instanceIdMap, Action.updateInstance, "v3");
149
150         }
151
152         @Test
153         public void testParseV3DeleteNetwork() throws JsonParseException, JsonMappingException, IOException, ValidationException{
154                 String requestJSON;
155                  try {
156                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteNetwork.json"));
157
158                 } catch (IOException e) {
159                     fail ("Exception caught");
160                     e.printStackTrace ();
161                     return;
162                 }
163                         ObjectMapper mapper = new ObjectMapper();
164                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
165                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
166                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
167                         MsoRequest msoRequest = new MsoRequest ("1234");
168                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
169         }
170
171         @Test
172         public void testParseV3ServiceInstanceDelete() throws JsonParseException, JsonMappingException, IOException, ValidationException{
173                 String requestJSON1, requestJSON2;
174                  try {
175                           requestJSON1 = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstance.json"));
176                           requestJSON2 = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstanceALaCarte.json"));
177
178                 } catch (IOException e) {
179                     fail ("Exception caught");
180                     e.printStackTrace ();
181                     return;
182                 }
183                         ObjectMapper mapper = new ObjectMapper();
184                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
185                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
186                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON1, ServiceInstancesRequest.class);
187                         MsoRequest msoRequest = new MsoRequest ("1234");
188                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
189                         boolean testIsALaCarteSet = msoRequest.getServiceInstancesRequest().getRequestDetails().getRequestParameters().isALaCarteSet();
190                         assertTrue(testIsALaCarteSet);
191                         assertFalse(msoRequest.getALaCarteFlag());
192                         sir  = mapper.readValue(requestJSON2, ServiceInstancesRequest.class);
193                         msoRequest = new MsoRequest ("12345");
194                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
195                         testIsALaCarteSet = msoRequest.getServiceInstancesRequest().getRequestDetails().getRequestParameters().isALaCarteSet();
196                         assertTrue(testIsALaCarteSet);
197                         assertTrue(msoRequest.getALaCarteFlag());
198
199         }
200
201         @Test(expected = ValidationException.class)
202         public void testParseV3ServiceInstanceCreateFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
203                 String requestJSON2;
204                  try {
205                           requestJSON2 = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstanceALaCarte.json"));
206
207                 } catch (IOException e) {
208                     fail ("Exception caught");
209                     e.printStackTrace ();
210                     return;
211                 }
212                         ObjectMapper mapper = new ObjectMapper();
213                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
214                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
215                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON2, ServiceInstancesRequest.class);
216                         MsoRequest msoRequest = new MsoRequest ("1234");
217                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v3");
218
219         }
220
221         @Test(expected = ValidationException.class)
222         public void testParseV3ServiceInstanceDeleteMacroFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
223                 String requestJSON;
224                  try {
225                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstanceBad.json"));
226
227                 } catch (IOException e) {
228                     fail ("Exception caught");
229                     e.printStackTrace ();
230                     return;
231                 }
232                         ObjectMapper mapper = new ObjectMapper();
233                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
234                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
235                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
236                         MsoRequest msoRequest = new MsoRequest ("1234");
237                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
238
239         }
240
241         @Test
242         public void testVfModuleV4UsePreLoad() throws JsonParseException, JsonMappingException, IOException, ValidationException {
243                 String requestJSON;
244                  try {
245                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v4CreateVfModule.json"));
246                    
247                 } catch (IOException e) {
248                     fail ("Exception caught");
249                     e.printStackTrace ();
250                     return;
251                 }
252                  
253                         ObjectMapper mapper = new ObjectMapper();
254                         HashMap<String, String> instanceIdMap = new HashMap<String,String>();
255                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
256                         instanceIdMap.put("vnfInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
257                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
258                         MsoRequest msoRequest = new MsoRequest ("1234");
259                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v4");
260                         
261                         
262                         
263                  try {
264                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v4CreateVfModuleNoCustomizationId.json"));
265                    
266                 } catch (IOException e) {
267                     fail ("Exception caught");
268                     e.printStackTrace ();
269                     return;
270                 }
271                  
272                         mapper = new ObjectMapper();
273                         instanceIdMap = new HashMap<String,String>();
274                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
275                         instanceIdMap.put("vnfInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
276                         sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
277                         msoRequest = new MsoRequest ("1234");
278                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v4");
279         }
280         
281         @Test(expected = ValidationException.class)
282         public void testV4UsePreLoadMissingModelCustomizationId() throws JsonParseException, JsonMappingException, IOException, ValidationException {
283                 String requestJSON;
284                  try {
285                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v4CreateVfModuleMissingModelCustomizationId.json"));
286                    
287                 } catch (IOException e) {
288                     fail ("Exception caught");
289                     e.printStackTrace ();
290                     return;
291                 }
292                  
293                         ObjectMapper mapper = new ObjectMapper();
294                         HashMap<String, String> instanceIdMap = new HashMap<String,String>();
295                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
296                         instanceIdMap.put("vnfInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
297                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
298                         MsoRequest msoRequest = new MsoRequest ("1234");
299                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v4");
300         }
301 }