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