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.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;
54 * @author waqas.ikram@ericsson.com
57 public class OwningEntityControllerTest extends AbstractSpringBootTest {
59 private static final String OWN_ENTITY_ID_VALUE = "oe_1";
60 private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
63 private OwnEntityCacheServiceProvider cacheServiceProvider;
66 private CustomerCacheServiceProvider customerCacheServiceProvider;
70 cacheServiceProvider.clearAll();
71 customerCacheServiceProvider.clearAll();
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);
80 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
82 final ResponseEntity<OwningEntity> actualResponse =
83 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
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());
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);
100 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
102 final ResponseEntity<Results> actualResponse = testRestTemplateService
103 .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
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));
114 public void test_putOwningEntityRelationShip_successfullyAddedToCache() throws Exception {
115 addCustomerAndServiceInstance();
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());
122 final String owningEntityRelationshipUrl = url + RELATIONSHIP_LIST_RELATIONSHIP_URL;
124 final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(owningEntityRelationshipUrl,
125 TestUtils.getOwningEntityRelationship(), Void.class);
127 assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
129 final ResponseEntity<OwningEntity> actualResponse =
130 testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
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));
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());
147 final RelationshipData globalRelationshipData =
148 getRelationshipData(relationshipDataList, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
149 assertNotNull(globalRelationshipData);
150 assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
152 final RelationshipData serviceSubscriptionRelationshipData =
153 getRelationshipData(relationshipDataList, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
154 assertNotNull(serviceSubscriptionRelationshipData);
155 assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
157 final RelationshipData serviceInstanceRelationshipData =
158 getRelationshipData(relationshipDataList, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
159 assertNotNull(serviceInstanceRelationshipData);
160 assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
162 final Optional<ServiceInstance> optional =
163 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
164 assertTrue(optional.isPresent());
166 final ServiceInstance serviceInstance = optional.get();
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());
176 final List<RelationshipData> serviceRelationshipDataList = serviceRelationshipList.get(0).getRelationshipData();
177 assertFalse(serviceRelationshipDataList.isEmpty());
178 assertEquals(1, serviceRelationshipDataList.size());
180 final RelationshipData owningEntityRelationshipData =
181 getRelationshipData(serviceRelationshipDataList, Constants.OWNING_ENTITY_OWNING_ENTITY_ID);
182 assertNotNull(owningEntityRelationshipData);
183 assertEquals(OWN_ENTITY_ID_VALUE, owningEntityRelationshipData.getRelationshipValue());
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());
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());