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