2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20 package org.openecomp.mso.apihandlerinfra;
 
  22 import org.openecomp.mso.apihandler.common.ValidationException;
 
  23 import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.ServiceInstancesRequest;
 
  24 import org.apache.commons.io.IOUtils;
 
  25 import org.codehaus.jackson.JsonParseException;
 
  26 import org.codehaus.jackson.map.JsonMappingException;
 
  27 import org.codehaus.jackson.map.ObjectMapper;
 
  28 import org.junit.Test;
 
  30 import java.io.IOException;
 
  31 import java.util.HashMap;
 
  33 import static org.junit.Assert.*;
 
  35 public class MsoRequestTest {
 
  40         public void testParseOrchestration() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
  41                         ObjectMapper mapper = new ObjectMapper();
 
  42                         String requestJSON = " {\"requestDetails\": {\"requestInfo\": { \"source\": \"VID\", \"requestorId\": \"zz9999\"}}}";
 
  43                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
  44                         MsoRequest msoRequest = new MsoRequest ("1234");
 
  45                         msoRequest.parseOrchestration(sir);
 
  46                         assertEquals(msoRequest.getRequestInfo().getSource(),"VID");
 
  47                         assertEquals(msoRequest.getRequestInfo().getRequestorId(),"zz9999");
 
  51         @Test(expected = ValidationException.class)
 
  52         public void testParseOrchestrationFailure() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
  53                         ObjectMapper mapper = new ObjectMapper();
 
  54                         String requestJSON = " {\"requestDetails\": {\"requestInfo\": { \"source\": \"VID\"}}}";
 
  55                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
  56                         MsoRequest msoRequest = new MsoRequest ("1234");
 
  57                         msoRequest.parseOrchestration(sir);
 
  62         public void testParseV3VnfCreate() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
  65                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3VnfCreate.json"));
 
  67                 } catch (IOException e) {
 
  68                     fail ("Exception caught");
 
  72                         ObjectMapper mapper = new ObjectMapper();
 
  73                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
  74                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
  75                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
  76                         MsoRequest msoRequest = new MsoRequest ("1234");
 
  77                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v3");
 
  78                         assertEquals(msoRequest.getRequestInfo().getSource(),"VID");
 
  79                         assertFalse(msoRequest.getALaCarteFlag());
 
  80                         assertEquals(msoRequest.getReqVersion(),3);
 
  81                         boolean testIsALaCarteSet = msoRequest.getServiceInstancesRequest().getRequestDetails().getRequestParameters().isALaCarteSet();
 
  82                         assertFalse(testIsALaCarteSet);
 
  86         @Test(expected = ValidationException.class)
 
  87         public void testParseV3VolumeGroupFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
  90                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3VolumeGroupBad.json"));
 
  92                 } catch (IOException e) {
 
  93                     fail ("Exception caught");
 
  97                         ObjectMapper mapper = new ObjectMapper();
 
  98                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
  99                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 100                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
 101                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 102                         msoRequest.parse(sir, instanceIdMap, Action.updateInstance, "v3");
 
 107         public void testParseV3UpdateNetwork() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
 110                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3UpdateNetwork.json"));
 
 112                 } catch (IOException e) {
 
 113                     fail ("Exception caught");
 
 114                     e.printStackTrace ();
 
 117                         ObjectMapper mapper = new ObjectMapper();
 
 118                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
 119                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 120                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
 121                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 122                         msoRequest.parse(sir, instanceIdMap, Action.updateInstance, "v3");
 
 126         @Test(expected = ValidationException.class)
 
 127         public void testParseV3UpdateNetworkFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
 130                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3UpdateNetworkBad.json"));
 
 132                 } catch (IOException e) {
 
 133                     fail ("Exception caught");
 
 134                     e.printStackTrace ();
 
 137                         ObjectMapper mapper = new ObjectMapper();
 
 138                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
 139                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 140                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
 141                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 142                         msoRequest.parse(sir, instanceIdMap, Action.updateInstance, "v3");
 
 147         public void testParseV3DeleteNetwork() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
 150                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteNetwork.json"));
 
 152                 } catch (IOException e) {
 
 153                     fail ("Exception caught");
 
 154                     e.printStackTrace ();
 
 157                         ObjectMapper mapper = new ObjectMapper();
 
 158                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
 159                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 160                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
 161                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 162                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
 
 166         public void testParseV3ServiceInstanceDelete() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
 167                 String requestJSON1, requestJSON2;
 
 169                           requestJSON1 = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstance.json"));
 
 170                           requestJSON2 = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstanceALaCarte.json"));
 
 172                 } catch (IOException e) {
 
 173                     fail ("Exception caught");
 
 174                     e.printStackTrace ();
 
 177                         ObjectMapper mapper = new ObjectMapper();
 
 178                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
 179                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 180                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON1, ServiceInstancesRequest.class);
 
 181                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 182                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
 
 183                         boolean testIsALaCarteSet = msoRequest.getServiceInstancesRequest().getRequestDetails().getRequestParameters().isALaCarteSet();
 
 184                         assertTrue(testIsALaCarteSet);
 
 185                         assertFalse(msoRequest.getALaCarteFlag());
 
 186                         sir  = mapper.readValue(requestJSON2, ServiceInstancesRequest.class);
 
 187                         msoRequest = new MsoRequest ("12345");
 
 188                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");
 
 189                         testIsALaCarteSet = msoRequest.getServiceInstancesRequest().getRequestDetails().getRequestParameters().isALaCarteSet();
 
 190                         assertTrue(testIsALaCarteSet);
 
 191                         assertTrue(msoRequest.getALaCarteFlag());
 
 195         @Test(expected = ValidationException.class)
 
 196         public void testParseV3ServiceInstanceCreateFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
 199                           requestJSON2 = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstanceALaCarte.json"));
 
 201                 } catch (IOException e) {
 
 202                     fail ("Exception caught");
 
 203                     e.printStackTrace ();
 
 206                         ObjectMapper mapper = new ObjectMapper();
 
 207                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
 208                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 209                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON2, ServiceInstancesRequest.class);
 
 210                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 211                         msoRequest.parse(sir, instanceIdMap, Action.createInstance, "v3");
 
 215         @Test(expected = ValidationException.class)
 
 216         public void testParseV3ServiceInstanceDeleteMacroFail() throws JsonParseException, JsonMappingException, IOException, ValidationException{
 
 219                           requestJSON = IOUtils.toString (ClassLoader.class.getResourceAsStream ("/v3DeleteServiceInstanceBad.json"));
 
 221                 } catch (IOException e) {
 
 222                     fail ("Exception caught");
 
 223                     e.printStackTrace ();
 
 226                         ObjectMapper mapper = new ObjectMapper();
 
 227                          HashMap<String, String> instanceIdMap = new HashMap<String,String>();
 
 228                         instanceIdMap.put("serviceInstanceId", "3eecada1-83a4-4f33-9ed2-7937e7b8dbbc");
 
 229                         ServiceInstancesRequest sir  = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
 230                         MsoRequest msoRequest = new MsoRequest ("1234");
 
 231                         msoRequest.parse(sir, instanceIdMap, Action.deleteInstance, "v3");