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.AAIResourcesClient;
45 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
46 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
47 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
48 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
49 import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
50 import org.onap.so.constants.Status;
51 import org.onap.so.db.request.client.RequestsDbClient;
52 import org.onap.so.serviceinstancebeans.CloudConfiguration;
53 import org.onap.so.serviceinstancebeans.ModelType;
54 import org.onap.so.serviceinstancebeans.RequestDetails;
55 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
56 import com.fasterxml.jackson.databind.ObjectMapper;
58 @RunWith(MockitoJUnitRunner.class)
59 public class BpmnRequestBuilderTest {
61 private static final String RESOURCE_PATH = "src/test/resources/__files/infra/";
64 public ExpectedException exceptionRule = ExpectedException.none();
68 private AAIResourcesClient aaiResourcesClient;
71 private AAIDataRetrieval aaiData = spy(AAIDataRetrieval.class);
74 private RequestsDbClient requestDBClient;
77 private BpmnRequestBuilder reqBuilder = spy(BpmnRequestBuilder.class);
80 private ObjectMapper mapper = new ObjectMapper();
82 private GraphInventoryCommonObjectMapperProvider provider = new GraphInventoryCommonObjectMapperProvider();
86 // aaiData.setAaiResourcesClient(aaiResourcesClient);
90 public void test_buildServiceInstanceDeleteRequest() throws Exception {
91 ServiceInstance service =
92 provider.getMapper().readValue(new File(RESOURCE_PATH + "ServiceInstance.json"), ServiceInstance.class);
94 doReturn(service).when(aaiData).getServiceInstance("serviceId");
95 ServiceInstancesRequest expectedRequest = mapper
96 .readValue(new File(RESOURCE_PATH + "ExpectedServiceRequest.json"), ServiceInstancesRequest.class);
97 expectedRequest.getRequestDetails().getModelInfo().setModelId(null); // bad getter/setter setting multiple
99 ServiceInstancesRequest actualRequest = reqBuilder.buildServiceDeleteRequest("serviceId");
100 assertThat(actualRequest, sameBeanAs(expectedRequest));
104 public void test_buildVnfDeleteRequest() throws Exception {
105 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
107 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
108 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("vnfId")));
110 ServiceInstancesRequest expectedRequest =
111 mapper.readValue(new File(RESOURCE_PATH + "ExpectedVnfRequest.json"), ServiceInstancesRequest.class);
112 ServiceInstancesRequest actualRequest = reqBuilder.buildVnfDeleteRequest("vnfId");
113 assertThat(actualRequest, sameBeanAs(expectedRequest));
117 public void test_buildVFModuleDeleteRequest() throws Exception {
118 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
120 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
121 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("vnfId")));
122 VfModule vfModule = provider.getMapper().readValue(new File(RESOURCE_PATH + "VfModule.json"), VfModule.class);
124 doReturn(Optional.of(vfModule)).when(aaiResourcesClient).get(VfModule.class, AAIUriFactory
125 .createResourceUri(AAIFluentTypeBuilder.network().genericVnf("vnfId").vfModule("vfModuleId")));
127 ServiceInstancesRequest expectedRequest = mapper
128 .readValue(new File(RESOURCE_PATH + "ExpectedVfModuleRequest.json"), ServiceInstancesRequest.class);
129 ServiceInstancesRequest actualRequest =
130 reqBuilder.buildVFModuleDeleteRequest("vnfId", "vfModuleId", ModelType.vfModule);
131 assertThat(actualRequest, sameBeanAs(expectedRequest));
135 public void test_buildVolumeGroupDeleteRequest() throws Exception {
136 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
138 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
139 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("vnfId")));
140 VolumeGroup volumeGroup =
141 provider.getMapper().readValue(new File(RESOURCE_PATH + "VolumeGroup.json"), VolumeGroup.class);
142 AAIResultWrapper wrapper = new AAIResultWrapper(volumeGroup);
143 doReturn(wrapper).when(aaiResourcesClient)
144 .get(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf("vnfId"))
145 .relatedTo(Types.VOLUME_GROUP.getFragment("volumeGroupId")));
147 ServiceInstancesRequest expectedRequest = mapper
148 .readValue(new File(RESOURCE_PATH + "ExpectedVolumeGroupRequest.json"), ServiceInstancesRequest.class);
149 ServiceInstancesRequest actualRequest = reqBuilder.buildVolumeGroupDeleteRequest("vnfId", "volumeGroupId");
150 assertThat(actualRequest, sameBeanAs(expectedRequest));
154 public void test_getCloudConfigurationVfModuleReplace() throws Exception {
155 String vnfId = "vnfId";
156 String vfModuleId = "vfModuleId";
158 GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
160 doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
161 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)));
163 CloudConfiguration result = reqBuilder.getCloudConfigurationVfModuleReplace(vnfId, vfModuleId);
164 assertEquals("0422ffb57ba042c0800a29dc85ca70f8", result.getTenantId());
165 assertEquals("cloudOwner", result.getCloudOwner());
166 assertEquals("regionOne", result.getLcpCloudRegionId());
170 public void test_mapCloudConfigurationVnf() throws Exception {
171 String vnfId = "6fb01019-c3c4-41fe-b307-d1c56850b687";
172 Map<String, String[]> filters = new HashMap<>();
173 filters.put("vnfId", new String[] {"EQ", vnfId});
174 filters.put("requestStatus", new String[] {"EQ", Status.COMPLETE.toString()});
175 filters.put("action", new String[] {"EQ", "createInstance"});
177 ServiceInstancesRequest serviceRequest = new ServiceInstancesRequest();
178 CloudConfiguration cloudConfiguration = new CloudConfiguration();
179 RequestDetails requestDetails = new RequestDetails();
180 cloudConfiguration.setCloudOwner("cloudOwner");
181 cloudConfiguration.setTenantId("tenantId");
182 cloudConfiguration.setLcpCloudRegionId("lcpCloudRegionId");
183 requestDetails.setCloudConfiguration(cloudConfiguration);
184 serviceRequest.setRequestDetails(requestDetails);
186 doReturn(filters).when(reqBuilder).createQueryRequest("vnfId", vnfId);
187 doReturn(Optional.of(serviceRequest)).when(reqBuilder).findServiceInstanceRequest(filters);
190 CloudConfiguration result = reqBuilder.mapCloudConfigurationVnf(vnfId);
191 assertEquals("tenantId", result.getTenantId());
192 assertEquals("cloudOwner", result.getCloudOwner());
193 assertEquals("lcpCloudRegionId", result.getLcpCloudRegionId());