Adding tenant 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.TestConstants.CLOUD_OWNER_NAME;
28 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
29 import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT;
30 import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Optional;
34 import org.junit.After;
35 import org.junit.Test;
36 import org.onap.aai.domain.yang.CloudRegion;
37 import org.onap.aai.domain.yang.RelatedToProperty;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.RelationshipData;
40 import org.onap.aai.domain.yang.Tenant;
41 import org.onap.so.aaisimulator.models.CloudRegionKey;
42 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
43 import org.onap.so.aaisimulator.utils.Constants;
44 import org.onap.so.aaisimulator.utils.TestConstants;
45 import org.onap.so.aaisimulator.utils.TestUtils;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49
50 /**
51  * @author Waqas Ikram (waqas.ikram@est.tech)
52  *
53  */
54 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
55
56     private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
57
58     @Autowired
59     private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
60
61     @After
62     public void after() {
63         cloudRegionCacheServiceProvider.clearAll();
64     }
65
66     @Test
67     public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
68         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
69
70         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
71
72         final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
73         assertEquals(HttpStatus.OK, response.getStatusCode());
74
75         assertTrue(response.hasBody());
76
77         final CloudRegion cloudRegion = response.getBody();
78         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
79         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
80
81         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
82
83     }
84
85     @Test
86     public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() 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 =
92                 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
93         assertEquals(HttpStatus.OK, response.getStatusCode());
94
95         assertTrue(response.hasBody());
96
97         final CloudRegion cloudRegion = response.getBody();
98         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
99         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
100
101         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
102
103     }
104
105     @Test
106     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
107
108         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
109
110         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
111
112         final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
113                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
114
115         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
116                 TestUtils.getGenericVnfRelationShip(), Relationship.class);
117         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
118
119         final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
120         assertTrue(optional.isPresent());
121
122         final CloudRegion actual = optional.get();
123
124         assertNotNull(actual.getRelationshipList());
125         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
126         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
127         final Relationship relationship = relationshipList.get(0);
128
129         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
130         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
131
132         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
133         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
134         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
135
136         final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
137         assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
138         assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
139
140     }
141
142     @Test
143     public void test_putTenant_successfullyAddedToCache() throws Exception {
144         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
145
146         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
147
148         final String tenantUrl =
149                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
150         final ResponseEntity<Void> responseEntity =
151                 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
152         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
153
154         final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
155         assertEquals(HttpStatus.OK, response.getStatusCode());
156
157         assertTrue(response.hasBody());
158
159         final Tenant tenant = response.getBody();
160         assertEquals(TENANT_ID, tenant.getTenantId());
161         assertEquals("admin", tenant.getTenantName());
162
163         assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
164
165     }
166
167
168     private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
169         final ResponseEntity<Void> responseEntity =
170                 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
171         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
172     }
173
174 }