Merge "Take CSIT preparations to separate script"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / OwningEntityControllerTest.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.RELATIONSHIP_LIST_RELATIONSHIP_URL;
27 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
28 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
29 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
30 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Optional;
36 import org.junit.After;
37 import org.junit.Test;
38 import org.onap.aai.domain.yang.OwningEntity;
39 import org.onap.aai.domain.yang.Relationship;
40 import org.onap.aai.domain.yang.RelationshipData;
41 import org.onap.aai.domain.yang.ServiceInstance;
42 import org.onap.so.aaisimulator.models.Format;
43 import org.onap.so.aaisimulator.models.Results;
44 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
45 import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider;
46 import org.onap.so.aaisimulator.utils.Constants;
47 import org.onap.so.aaisimulator.utils.TestUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.http.ResponseEntity;
51
52 /**
53  * @author waqas.ikram@ericsson.com
54  *
55  */
56 public class OwningEntityControllerTest extends AbstractSpringBootTest {
57
58     private static final String OWN_ENTITY_ID_VALUE = "oe_1";
59     private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
60
61     @Autowired
62     private OwnEntityCacheServiceProvider cacheServiceProvider;
63
64     @Autowired
65     private CustomerCacheServiceProvider customerCacheServiceProvider;
66
67     @After
68     public void after() {
69         cacheServiceProvider.clearAll();
70         customerCacheServiceProvider.clearAll();
71     }
72
73     @Test
74     public void test_putOwningEntity_successfullyAddedToCache() throws Exception {
75         final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
76         final ResponseEntity<Void> actual =
77                 testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
78
79         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
80
81         final ResponseEntity<OwningEntity> actualResponse =
82                 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
83
84         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
85         assertTrue(actualResponse.hasBody());
86         final OwningEntity actualOwningEntity = actualResponse.getBody();
87         assertEquals(OWN_ENTITY_ID_VALUE, actualOwningEntity.getOwningEntityId());
88         assertEquals(OWN_ENTITY_NAME_VALUE, actualOwningEntity.getOwningEntityName());
89         assertNotNull(actualOwningEntity.getResourceVersion());
90
91     }
92
93     @Test
94     public void test_getOwningEntityCount_correctResult() throws Exception {
95         final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
96         final ResponseEntity<Void> actual =
97                 testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
98
99         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
100
101         final ResponseEntity<Results> actualResponse = testRestTemplateService
102                 .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
103
104         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
105         assertTrue(actualResponse.hasBody());
106         final Results result = actualResponse.getBody();
107         assertNotNull(result.getValues());
108         assertFalse(result.getValues().isEmpty());
109         assertEquals(1, result.getValues().get(0).get(Constants.OWNING_ENTITY));
110     }
111
112     @Test
113     public void test_putOwningEntityRelationShip_successfullyAddedToCache() throws Exception {
114         addCustomerAndServiceInstance();
115
116         final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
117         final ResponseEntity<Void> actual =
118                 testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
119         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
120
121         final String owningEntityRelationshipUrl = url + RELATIONSHIP_LIST_RELATIONSHIP_URL;
122
123         final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(owningEntityRelationshipUrl,
124                 TestUtils.getOwningEntityRelationship(), Void.class);
125
126         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
127
128         final ResponseEntity<OwningEntity> actualResponse =
129                 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
130
131         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
132         assertTrue(actualResponse.hasBody());
133         final OwningEntity actualOwningEntity = actualResponse.getBody();
134         assertEquals(OWN_ENTITY_ID_VALUE, actualOwningEntity.getOwningEntityId());
135         assertEquals(OWN_ENTITY_NAME_VALUE, actualOwningEntity.getOwningEntityName());
136         assertNotNull(actualOwningEntity.getRelationshipList());
137         assertFalse(actualOwningEntity.getRelationshipList().getRelationship().isEmpty());
138         assertNotNull(actualOwningEntity.getRelationshipList().getRelationship().get(0));
139
140         final Relationship actualRelationship = actualOwningEntity.getRelationshipList().getRelationship().get(0);
141         final List<RelationshipData> relationshipDataList = actualRelationship.getRelationshipData();
142         assertEquals(Constants.BELONGS_TO, actualRelationship.getRelationshipLabel());
143         assertFalse(relationshipDataList.isEmpty());
144         assertEquals(3, relationshipDataList.size());
145
146         final RelationshipData globalRelationshipData =
147                 getRelationshipData(relationshipDataList, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
148         assertNotNull(globalRelationshipData);
149         assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
150
151         final RelationshipData serviceSubscriptionRelationshipData =
152                 getRelationshipData(relationshipDataList, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
153         assertNotNull(serviceSubscriptionRelationshipData);
154         assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
155
156         final RelationshipData serviceInstanceRelationshipData =
157                 getRelationshipData(relationshipDataList, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
158         assertNotNull(serviceInstanceRelationshipData);
159         assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
160
161         final Optional<ServiceInstance> optional =
162                 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
163         assertTrue(optional.isPresent());
164
165         final ServiceInstance serviceInstance = optional.get();
166
167         assertNotNull(serviceInstance.getRelationshipList());
168         final List<Relationship> serviceRelationshipList = serviceInstance.getRelationshipList().getRelationship();
169         assertFalse(serviceRelationshipList.isEmpty());
170         assertEquals(1, serviceRelationshipList.size());
171         final Relationship relationship = serviceRelationshipList.get(0);
172         assertEquals(Constants.BELONGS_TO, relationship.getRelationshipLabel());
173         assertEquals(Constants.OWNING_ENTITY_URL + OWN_ENTITY_ID_VALUE, relationship.getRelatedLink());
174
175         final List<RelationshipData> serviceRelationshipDataList = serviceRelationshipList.get(0).getRelationshipData();
176         assertFalse(serviceRelationshipDataList.isEmpty());
177         assertEquals(1, serviceRelationshipDataList.size());
178
179         final RelationshipData owningEntityRelationshipData =
180                 getRelationshipData(serviceRelationshipDataList, Constants.OWNING_ENTITY_OWNING_ENTITY_ID);
181         assertNotNull(owningEntityRelationshipData);
182         assertEquals(OWN_ENTITY_ID_VALUE, owningEntityRelationshipData.getRelationshipValue());
183
184     }
185
186     private void addCustomerAndServiceInstance() throws Exception, IOException {
187         final ResponseEntity<Void> customerResponse =
188                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
189         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
190
191         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
192         final ResponseEntity<Void> serviceInstanceResponse =
193                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
194         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
195
196     }
197
198 }