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