389ff28dbdfb1de7b55dc1af196d83ce27e3f637
[so.git] /
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.junit.Assert.assertEquals;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.spy;
28 import java.io.File;
29 import java.util.HashMap;
30 import java.util.Map;
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;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class BpmnRequestBuilderTest {
59
60     private static final String RESOURCE_PATH = "src/test/resources/__files/infra/";
61
62     @Rule
63     public ExpectedException exceptionRule = ExpectedException.none();
64
65
66     @Mock
67     private AAIResourcesClient aaiResourcesClient;
68
69     @InjectMocks
70     private AAIDataRetrieval aaiData = spy(AAIDataRetrieval.class);
71
72     @Mock
73     private RequestsDbClient requestDBClient;
74
75     @InjectMocks
76     private BpmnRequestBuilder reqBuilder = spy(BpmnRequestBuilder.class);
77
78
79     private ObjectMapper mapper = new ObjectMapper();
80
81     private GraphInventoryCommonObjectMapperProvider provider = new GraphInventoryCommonObjectMapperProvider();
82
83     @Before
84     public void setup() {
85         // aaiData.setAaiResourcesClient(aaiResourcesClient);
86     }
87
88     @Test
89     public void test_buildServiceInstanceDeleteRequest() throws Exception {
90         ServiceInstance service =
91                 provider.getMapper().readValue(new File(RESOURCE_PATH + "ServiceInstance.json"), ServiceInstance.class);
92
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
97                                                                              // fields
98         ServiceInstancesRequest actualRequest = reqBuilder.buildServiceDeleteRequest("serviceId");
99         assertThat(actualRequest, sameBeanAs(expectedRequest));
100     }
101
102     @Test
103     public void test_buildVnfDeleteRequest() throws Exception {
104         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
105
106         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
107                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
108
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));
113     }
114
115     @Test
116     public void test_buildVFModuleDeleteRequest() throws Exception {
117         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
118
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);
122
123         doReturn(Optional.of(vfModule)).when(aaiResourcesClient).get(VfModule.class,
124                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "vnfId", "vfModuleId"));
125
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));
131     }
132
133     @Test
134     public void test_buildVolumeGroupDeleteRequest() throws Exception {
135         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
136
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"));
145
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));
150     }
151
152     @Test
153     public void test_getCloudConfigurationVfModuleReplace() throws Exception {
154         String vnfId = "vnfId";
155         String vfModuleId = "vfModuleId";
156
157         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
158
159         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
160                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
161
162         CloudConfiguration result = reqBuilder.getCloudConfigurationVfModuleReplace(vnfId, vfModuleId);
163         assertEquals("0422ffb57ba042c0800a29dc85ca70f8", result.getTenantId());
164         assertEquals("cloudOwner", result.getCloudOwner());
165         assertEquals("regionOne", result.getLcpCloudRegionId());
166     }
167
168     @Test
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"});
175
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);
184
185         doReturn(filters).when(reqBuilder).createQueryRequest("vnfId", vnfId);
186         doReturn(Optional.of(serviceRequest)).when(reqBuilder).findServiceInstanceRequest(filters);
187
188
189         CloudConfiguration result = reqBuilder.mapCloudConfigurationVnf(vnfId);
190         assertEquals("tenantId", result.getTenantId());
191         assertEquals("cloudOwner", result.getCloudOwner());
192         assertEquals("lcpCloudRegionId", result.getLcpCloudRegionId());
193     }
194
195 }
196