Merge "Sonar fix too many method param"
[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 java.io.File;
27 import java.util.Optional;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Spy;
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.uri.AAIUriFactory;
44 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
45 import org.onap.so.db.request.client.RequestsDbClient;
46 import org.onap.so.serviceinstancebeans.ModelType;
47 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class BpmnRequestBuilderTest {
52
53     private static final String RESOURCE_PATH = "src/test/resources/__files/infra/";
54
55     @Rule
56     public ExpectedException exceptionRule = ExpectedException.none();
57
58     @InjectMocks
59     @Spy
60     BpmnRequestBuilder reqBuilder;
61
62     @Mock
63     private RequestsDbClient requestDBClient;
64
65     @Mock
66     private AAIResourcesClient aaiResourcesClient;
67
68     private ObjectMapper mapper = new ObjectMapper();
69
70     private GraphInventoryCommonObjectMapperProvider provider = new GraphInventoryCommonObjectMapperProvider();
71
72     @Before
73     public void setup() {
74         reqBuilder.setAaiResourcesClient(aaiResourcesClient);
75
76     }
77
78     @Test
79     public void test_buildServiceInstanceDeleteRequest() throws Exception {
80         ServiceInstance service =
81                 provider.getMapper().readValue(new File(RESOURCE_PATH + "ServiceInstance.json"), ServiceInstance.class);
82
83         doReturn(service).when(reqBuilder).getServiceInstance("serviceId");
84         ServiceInstancesRequest expectedRequest = mapper
85                 .readValue(new File(RESOURCE_PATH + "ExpectedServiceRequest.json"), ServiceInstancesRequest.class);
86         expectedRequest.getRequestDetails().getModelInfo().setModelId(null);
87         // bad getter/setter setting multiple fields
88         ServiceInstancesRequest actualRequest = reqBuilder.buildServiceDeleteRequest("serviceId");
89         assertThat(actualRequest, sameBeanAs(expectedRequest));
90     }
91
92     @Test
93     public void test_buildVnfDeleteRequest() throws Exception {
94         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
95
96         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
97                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
98
99         ServiceInstancesRequest expectedRequest =
100                 mapper.readValue(new File(RESOURCE_PATH + "ExpectedVnfRequest.json"), ServiceInstancesRequest.class);
101         ServiceInstancesRequest actualRequest = reqBuilder.buildVnfDeleteRequest("vnfId");
102         assertThat(actualRequest, sameBeanAs(expectedRequest));
103     }
104
105     @Test
106     public void test_buildVFModuleDeleteRequest() throws Exception {
107         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
108
109         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
110                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
111         VfModule vfModule = provider.getMapper().readValue(new File(RESOURCE_PATH + "VfModule.json"), VfModule.class);
112
113         doReturn(Optional.of(vfModule)).when(aaiResourcesClient).get(VfModule.class,
114                 AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, "vnfId", "vfModuleId"));
115
116         ServiceInstancesRequest expectedRequest = mapper
117                 .readValue(new File(RESOURCE_PATH + "ExpectedVfModuleRequest.json"), ServiceInstancesRequest.class);
118         ServiceInstancesRequest actualRequest =
119                 reqBuilder.buildVFModuleDeleteRequest("vnfId", "vfModuleId", ModelType.vfModule);
120         assertThat(actualRequest, sameBeanAs(expectedRequest));
121     }
122
123     @Test
124     public void test_buildVolumeGroupDeleteRequest() throws Exception {
125         GenericVnf vnf = provider.getMapper().readValue(new File(RESOURCE_PATH + "Vnf.json"), GenericVnf.class);
126
127         doReturn(Optional.of(vnf)).when(aaiResourcesClient).get(GenericVnf.class,
128                 AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId"));
129         VolumeGroup volumeGroup =
130                 provider.getMapper().readValue(new File(RESOURCE_PATH + "VolumeGroup.json"), VolumeGroup.class);
131
132         doReturn(Optional.of(volumeGroup)).when(aaiResourcesClient).get(VolumeGroup.class, AAIUriFactory
133                 .createResourceUri(AAIObjectType.VOLUME_GROUP, "cloudOwner", "regionOne", "volumeGroupId"));
134
135         ServiceInstancesRequest expectedRequest = mapper
136                 .readValue(new File(RESOURCE_PATH + "ExpectedVolumeGroupRequest.json"), ServiceInstancesRequest.class);
137         ServiceInstancesRequest actualRequest = reqBuilder.buildVolumeGroupDeleteRequest("vnfId", "volumeGroupId");
138         assertThat(actualRequest, sameBeanAs(expectedRequest));
139     }
140 }