Fixing project and owning entity relationship endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / CloudRegionsControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
28 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME;
29 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
30 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
32 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
35 import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT;
36 import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID;
37 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
38 import java.io.IOException;
39 import java.util.List;
40 import java.util.Optional;
41 import org.junit.After;
42 import org.junit.Test;
43 import org.onap.aai.domain.yang.CloudRegion;
44 import org.onap.aai.domain.yang.GenericVnf;
45 import org.onap.aai.domain.yang.RelatedToProperty;
46 import org.onap.aai.domain.yang.Relationship;
47 import org.onap.aai.domain.yang.RelationshipData;
48 import org.onap.aai.domain.yang.RelationshipList;
49 import org.onap.aai.domain.yang.Tenant;
50 import org.onap.so.aaisimulator.models.CloudRegionKey;
51 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
52 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
53 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
54 import org.onap.so.aaisimulator.utils.Constants;
55 import org.onap.so.aaisimulator.utils.TestConstants;
56 import org.onap.so.aaisimulator.utils.TestUtils;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.http.HttpStatus;
59 import org.springframework.http.ResponseEntity;
60
61 /**
62  * @author Waqas Ikram (waqas.ikram@est.tech)
63  *
64  */
65 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
66
67     private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
68
69     @Autowired
70     private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
71
72     @Autowired
73     private CustomerCacheServiceProvider customerCacheServiceProvider;
74
75     @Autowired
76     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
77
78     @After
79     public void after() {
80         cloudRegionCacheServiceProvider.clearAll();
81         customerCacheServiceProvider.clearAll();
82         genericVnfCacheServiceProvider.clearAll();
83     }
84
85     @Test
86     public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
87         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
88
89         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
90
91         final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
92         assertEquals(HttpStatus.OK, response.getStatusCode());
93
94         assertTrue(response.hasBody());
95
96         final CloudRegion cloudRegion = response.getBody();
97         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
98         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
99
100         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
101
102     }
103
104     @Test
105     public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
106         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
107
108         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
109
110         final ResponseEntity<CloudRegion> response =
111                 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
112         assertEquals(HttpStatus.OK, response.getStatusCode());
113
114         assertTrue(response.hasBody());
115
116         final CloudRegion cloudRegion = response.getBody();
117         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
118         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
119
120         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
121
122     }
123
124     @Test
125     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
126
127         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
128
129         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
130
131         final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
132                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
133
134         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
135                 TestUtils.getGenericVnfRelationShip(), Relationship.class);
136         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
137
138         final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
139         assertTrue(optional.isPresent());
140
141         final CloudRegion actual = optional.get();
142
143         assertNotNull(actual.getRelationshipList());
144         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
145         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
146         final Relationship relationship = relationshipList.get(0);
147
148         assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
149
150         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
151         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
152
153         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
154         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
155         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
156
157         final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
158         assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
159         assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
160
161     }
162
163     @Test
164     public void test_putTenant_successfullyAddedToCache() throws Exception {
165         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
166
167         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
168
169         final String tenantUrl =
170                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
171         addTenantAndAssertResponse(tenantUrl);
172
173         final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
174         assertEquals(HttpStatus.OK, response.getStatusCode());
175
176         assertTrue(response.hasBody());
177
178         final Tenant tenant = response.getBody();
179         assertEquals(TENANT_ID, tenant.getTenantId());
180         assertEquals("admin", tenant.getTenantName());
181
182         assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
183
184     }
185
186     @Test
187     public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
188
189         addCustomerServiceAndGenericVnf();
190
191         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
192         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
193
194         final String tenantUrl =
195                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID);
196         addTenantAndAssertResponse(tenantUrl);
197
198         final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
199                 TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
200
201         final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
202                 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
203         assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
204
205         final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
206
207         assertTrue(optional.isPresent());
208         final Tenant actualTenant = optional.get();
209         final RelationshipList relationshipList = actualTenant.getRelationshipList();
210         assertNotNull(relationshipList);
211         assertFalse(relationshipList.getRelationship().isEmpty());
212
213         final Relationship relationship = relationshipList.getRelationship().get(0);
214
215         assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel());
216         assertFalse(relationship.getRelationshipData().isEmpty());
217         assertEquals(1, relationship.getRelationshipData().size());
218
219         final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
220
221         final RelationshipData relationshipData =
222                 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
223         assertNotNull(relationshipData);
224         assertEquals(VNF_ID, relationshipData.getRelationshipValue());
225
226         final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
227
228         final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
229         assertNotNull(property);
230         assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
231
232         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
233         assertTrue(genericVnfOptional.isPresent());
234         final GenericVnf actualGenericVnf = genericVnfOptional.get();
235         final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
236         assertNotNull(relationshipListGenericVnf);
237         assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
238
239         final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
240
241         assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
242         assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
243         assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
244
245     }
246
247     private void addTenantAndAssertResponse(final String tenantUrl) throws IOException {
248         final ResponseEntity<Void> responseEntity =
249                 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
250         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
251     }
252
253     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
254         final ResponseEntity<Void> customerResponse =
255                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
256         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
257
258         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
259         final ResponseEntity<Void> serviceInstanceResponse =
260                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
261         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
262
263         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
264         final ResponseEntity<Void> genericVnfResponse =
265                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
266         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
267
268     }
269
270     private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
271         final ResponseEntity<Void> responseEntity =
272                 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
273         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
274     }
275
276 }