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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.controller;
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.TestConstants.CLOUD_OWNER_NAME;
27 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
28 import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Optional;
32 import org.junit.After;
33 import org.junit.Test;
34 import org.onap.aai.domain.yang.CloudRegion;
35 import org.onap.aai.domain.yang.RelatedToProperty;
36 import org.onap.aai.domain.yang.Relationship;
37 import org.onap.aai.domain.yang.RelationshipData;
38 import org.onap.so.aaisimulator.models.CloudRegionKey;
39 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
40 import org.onap.so.aaisimulator.utils.Constants;
41 import org.onap.so.aaisimulator.utils.TestConstants;
42 import org.onap.so.aaisimulator.utils.TestUtils;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
48 * @author Waqas Ikram (waqas.ikram@est.tech)
51 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
53 private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
56 private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
60 cloudRegionCacheServiceProvider.clearAll();
64 public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
65 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
67 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
69 final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
70 assertEquals(HttpStatus.OK, response.getStatusCode());
72 assertTrue(response.hasBody());
74 final CloudRegion cloudRegion = response.getBody();
75 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
76 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
78 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
83 public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
84 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
86 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
88 final ResponseEntity<CloudRegion> response =
89 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
90 assertEquals(HttpStatus.OK, response.getStatusCode());
92 assertTrue(response.hasBody());
94 final CloudRegion cloudRegion = response.getBody();
95 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
96 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
98 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
103 public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
105 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
107 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
109 final String relationShipUrl =
110 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, RELATIONSHIP_URL);
112 final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
113 TestUtils.getGenericVnfRelationShip(), Relationship.class);
114 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
116 final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
117 assertTrue(optional.isPresent());
119 final CloudRegion actual = optional.get();
121 assertNotNull(actual.getRelationshipList());
122 final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
123 assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
124 final Relationship relationship = relationshipList.get(0);
126 assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
127 assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
129 final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
130 assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
131 assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
133 final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
134 assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
135 assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
140 private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
141 final ResponseEntity<Void> responseEntity =
142 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
143 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());