2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.so.apihandlerinfra.infra.rest;
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.spy;
29 import java.util.HashMap;
31 import java.util.Optional;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.aai.domain.yang.ServiceInstance;
42 import org.onap.aai.domain.yang.VfModule;
43 import org.onap.aai.domain.yang.VolumeGroup;
44 import org.onap.aaiclient.client.aai.AAIObjectType;
45 import org.onap.aaiclient.client.aai.AAIResourcesClient;
46 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
47 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
48 import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
49 import org.onap.so.constants.Status;
50 import org.onap.so.db.request.client.RequestsDbClient;
51 import org.onap.so.serviceinstancebeans.CloudConfiguration;
52 import org.onap.so.serviceinstancebeans.ModelType;
53 import org.onap.so.serviceinstancebeans.RequestDetails;
54 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 @RunWith(MockitoJUnitRunner.class)
58 public class BpmnRequestBuilderTest {
60 private static final String RESOURCE_PATH = "src/test/resources/__files/infra/";
63 public ExpectedException exceptionRule = ExpectedException.none();
67 private AAIResourcesClient aaiResourcesClient;
70 private AAIDataRetrieval aaiData = spy(AAIDataRetrieval.class);
73 private RequestsDbClient requestDBClient;
76 private BpmnRequestBuilder reqBuilder = spy(BpmnRequestBuilder.class);
79 private ObjectMapper mapper = new ObjectMapper();
81 private GraphInventoryCommonObjectMapperProvider provider = new GraphInventoryCommonObjectMapperProvider();
85 // aaiData.setAaiResourcesClient(aaiResourcesClient);
89 public void test_buildServiceInstanceDeleteRequest() throws Exception {
90 ServiceInstance service =
91 provider.getMapper().readValue(new File(RESOURCE_PATH + "ServiceInstance.json"), ServiceInstance.class);
93 doReturn(service).when(aaiData).getServiceInstance("serviceId");
94 ServiceInstancesRequest expectedRequest = mapper
95 .readValue(new File(RESOURCE_PATH + "ExpectedServiceRequest.json"), ServiceInstancesRequest.class);
96 expectedRequest.getRequestDetails().getModelInfo().setModelId(null); // bad getter/setter setting multiple
98 ServiceInstancesRequest actualRequest = reqBuilder.buildServiceDeleteRequest("serviceId");
99 assertThat(actualRequest, sameBeanAs(expectedRequest));
103 public void test_buildVnfDeleteRequest() throws Exception {
104 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
106 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
107 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
109 ServiceInstancesRequest expectedRequest =
110 mapper.readValue(new File(RESOURCE_PATH + "ExpectedVnfRequest.json"), ServiceInstancesRequest.class);
111 ServiceInstancesRequest actualRequest = reqBuilder.buildVnfDeleteRequest("vnfId");
112 assertThat(actualRequest, sameBeanAs(expectedRequest));
116 public void test_buildVFModuleDeleteRequest() throws Exception {
117 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
119 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
120 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
121 VfModule vfModule = provider.getMapper().readValue(new File(RESOURCE_PATH + "VfModule.json"), VfModule.class);
123 doReturn(Optional.of(vfModule)).when(aaiResourcesClient).get(VfModule.class,
124 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "vnfId", "vfModuleId"));
126 ServiceInstancesRequest expectedRequest = mapper
127 .readValue(new File(RESOURCE_PATH + "ExpectedVfModuleRequest.json"), ServiceInstancesRequest.class);
128 ServiceInstancesRequest actualRequest =
129 reqBuilder.buildVFModuleDeleteRequest("vnfId", "vfModuleId", ModelType.vfModule);
130 assertThat(actualRequest, sameBeanAs(expectedRequest));
134 public void test_buildVolumeGroupDeleteRequest() throws Exception {
135 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
137 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
138 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
139 VolumeGroup volumeGroup =
140 provider.getMapper().readValue(new File(RESOURCE_PATH + "VolumeGroup.json"), VolumeGroup.class);
141 AAIResultWrapper wrapper = new AAIResultWrapper(volumeGroup);
142 doReturn(wrapper).when(aaiResourcesClient)
143 .get(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId")
144 .relatedTo(AAIObjectType.VOLUME_GROUP, "volumeGroupId"));
146 ServiceInstancesRequest expectedRequest = mapper
147 .readValue(new File(RESOURCE_PATH + "ExpectedVolumeGroupRequest.json"), ServiceInstancesRequest.class);
148 ServiceInstancesRequest actualRequest = reqBuilder.buildVolumeGroupDeleteRequest("vnfId", "volumeGroupId");
149 assertThat(actualRequest, sameBeanAs(expectedRequest));
153 public void test_getCloudConfigurationVfModuleReplace() throws Exception {
154 String vnfId = "vnfId";
155 String vfModuleId = "vfModuleId";
157 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
159 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
160 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
162 CloudConfiguration result = reqBuilder.getCloudConfigurationVfModuleReplace(vnfId, vfModuleId);
163 assertEquals("0422ffb57ba042c0800a29dc85ca70f8", result.getTenantId());
164 assertEquals("cloudOwner", result.getCloudOwner());
165 assertEquals("regionOne", result.getLcpCloudRegionId());
169 public void test_mapCloudConfigurationVnf() throws Exception {
170 String vnfId = "6fb01019-c3c4-41fe-b307-d1c56850b687";
171 Map<String, String[]> filters = new HashMap<>();
172 filters.put("vnfId", new String[] {"EQ", vnfId});
173 filters.put("requestStatus", new String[] {"EQ", Status.COMPLETE.toString()});
174 filters.put("action", new String[] {"EQ", "createInstance"});
176 ServiceInstancesRequest serviceRequest = new ServiceInstancesRequest();
177 CloudConfiguration cloudConfiguration = new CloudConfiguration();
178 RequestDetails requestDetails = new RequestDetails();
179 cloudConfiguration.setCloudOwner("cloudOwner");
180 cloudConfiguration.setTenantId("tenantId");
181 cloudConfiguration.setLcpCloudRegionId("lcpCloudRegionId");
182 requestDetails.setCloudConfiguration(cloudConfiguration);
183 serviceRequest.setRequestDetails(requestDetails);
185 doReturn(filters).when(reqBuilder).createQueryRequest("vnfId", vnfId);
186 doReturn(Optional.of(serviceRequest)).when(reqBuilder).findServiceInstanceRequest(filters);
189 CloudConfiguration result = reqBuilder.mapCloudConfigurationVnf(vnfId);
190 assertEquals("tenantId", result.getTenantId());
191 assertEquals("cloudOwner", result.getCloudOwner());
192 assertEquals("lcpCloudRegionId", result.getLcpCloudRegionId());