2118b6d81e1305a42e8769ef82b70744c72c224a
[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.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;
46
47 /**
48  * @author Waqas Ikram (waqas.ikram@est.tech)
49  *
50  */
51 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
52
53     private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
54
55     @Autowired
56     private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
57
58     @After
59     public void after() {
60         cloudRegionCacheServiceProvider.clearAll();
61     }
62
63     @Test
64     public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
65         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
66
67         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
68
69         final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
70         assertEquals(HttpStatus.OK, response.getStatusCode());
71
72         assertTrue(response.hasBody());
73
74         final CloudRegion cloudRegion = response.getBody();
75         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
76         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
77
78         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
79
80     }
81
82     @Test
83     public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
84         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
85
86         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
87
88         final ResponseEntity<CloudRegion> response =
89                 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
90         assertEquals(HttpStatus.OK, response.getStatusCode());
91
92         assertTrue(response.hasBody());
93
94         final CloudRegion cloudRegion = response.getBody();
95         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
96         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
97
98         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
99
100     }
101
102     @Test
103     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
104
105         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
106
107         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
108
109         final String relationShipUrl =
110                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, RELATIONSHIP_URL);
111
112         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
113                 TestUtils.getGenericVnfRelationShip(), Relationship.class);
114         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
115
116         final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
117         assertTrue(optional.isPresent());
118
119         final CloudRegion actual = optional.get();
120
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);
125
126         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
127         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
128
129         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
130         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
131         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
132
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());
136
137     }
138
139
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());
144     }
145
146 }