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