Initial commit of validation framework to APIH
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / infra / rest / BpmnRequestBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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 package org.onap.so.apihandlerinfra.infra.rest;
22
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.spy;
27 import java.io.File;
28 import java.util.Optional;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.aai.domain.yang.GenericVnf;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.VfModule;
40 import org.onap.aai.domain.yang.VolumeGroup;
41 import org.onap.so.client.aai.AAIObjectType;
42 import org.onap.so.client.aai.AAIResourcesClient;
43 import org.onap.so.client.aai.entities.AAIResultWrapper;
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
45 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
46 import org.onap.so.db.request.client.RequestsDbClient;
47 import org.onap.so.serviceinstancebeans.ModelType;
48 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class BpmnRequestBuilderTest {
53
54     private static final String RESOURCE_PATH = "src/test/resources/__files/infra/";
55
56     @Rule
57     public ExpectedException exceptionRule = ExpectedException.none();
58
59
60     @Mock
61     private AAIResourcesClient aaiResourcesClient;
62
63     @InjectMocks
64     private AAIDataRetrieval aaiData = spy(AAIDataRetrieval.class);
65
66     @Mock
67     private RequestsDbClient requestDBClient;
68
69     @InjectMocks
70     private BpmnRequestBuilder reqBuilder = spy(BpmnRequestBuilder.class);
71
72
73     private ObjectMapper mapper = new ObjectMapper();
74
75     private GraphInventoryCommonObjectMapperProvider provider = new GraphInventoryCommonObjectMapperProvider();
76
77     @Before
78     public void setup() {
79         // aaiData.setAaiResourcesClient(aaiResourcesClient);
80     }
81
82     @Test
83     public void test_buildServiceInstanceDeleteRequest() throws Exception {
84         ServiceInstance service =
85                 provider.getMapper().readValue(new File(RESOURCE_PATH + "ServiceInstance.json"), ServiceInstance.class);
86
87         doReturn(service).when(aaiData).getServiceInstance("serviceId");
88         ServiceInstancesRequest expectedRequest = mapper
89                 .readValue(new File(RESOURCE_PATH + "ExpectedServiceRequest.json"), ServiceInstancesRequest.class);
90         expectedRequest.getRequestDetails().getModelInfo().setModelId(null); // bad getter/setter setting multiple
91                                                                              // fields
92         ServiceInstancesRequest actualRequest = reqBuilder.buildServiceDeleteRequest("serviceId");
93         assertThat(actualRequest, sameBeanAs(expectedRequest));
94     }
95
96     @Test
97     public void test_buildVnfDeleteRequest() throws Exception {
98         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
99
100         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
101                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
102
103         ServiceInstancesRequest expectedRequest =
104                 mapper.readValue(new File(RESOURCE_PATH + "ExpectedVnfRequest.json"), ServiceInstancesRequest.class);
105         ServiceInstancesRequest actualRequest = reqBuilder.buildVnfDeleteRequest("vnfId");
106         assertThat(actualRequest, sameBeanAs(expectedRequest));
107     }
108
109     @Test
110     public void test_buildVFModuleDeleteRequest() throws Exception {
111         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
112
113         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
114                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
115         VfModule vfModule = provider.getMapper().readValue(new File(RESOURCE_PATH + "VfModule.json"), VfModule.class);
116
117         doReturn(Optional.of(vfModule)).when(aaiResourcesClient).get(VfModule.class,
118                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "vnfId", "vfModuleId"));
119
120         ServiceInstancesRequest expectedRequest = mapper
121                 .readValue(new File(RESOURCE_PATH + "ExpectedVfModuleRequest.json"), ServiceInstancesRequest.class);
122         ServiceInstancesRequest actualRequest =
123                 reqBuilder.buildVFModuleDeleteRequest("vnfId", "vfModuleId", ModelType.vfModule);
124         assertThat(actualRequest, sameBeanAs(expectedRequest));
125     }
126
127     @Test
128     public void test_buildVolumeGroupDeleteRequest() throws Exception {
129         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
130
131         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
132                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
133         VolumeGroup volumeGroup =
134                 provider.getMapper().readValue(new File(RESOURCE_PATH + "VolumeGroup.json"), VolumeGroup.class);
135         AAIResultWrapper wrapper = new AAIResultWrapper(volumeGroup);
136         doReturn(wrapper).when(aaiResourcesClient)
137                 .get(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId")
138                         .relatedTo(AAIObjectType.VOLUME_GROUP, "volumeGroupId"));
139
140         ServiceInstancesRequest expectedRequest = mapper
141                 .readValue(new File(RESOURCE_PATH + "ExpectedVolumeGroupRequest.json"), ServiceInstancesRequest.class);
142         ServiceInstancesRequest actualRequest = reqBuilder.buildVolumeGroupDeleteRequest("vnfId", "volumeGroupId");
143         assertThat(actualRequest, sameBeanAs(expectedRequest));
144     }
145
146 }
147