Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / ExternalSystemEsrControllerTest.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.ESR_PASSWORD;
29 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SERVICE_URL;
30 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_ID;
31 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_LIST_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_TYPE;
33 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_TYEP;
34 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_USERNAME;
35 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_VENDOR;
36 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_VIM_ID;
37 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_VNFM_ID;
38 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_VNFM_URL;
39 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
40 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
41 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
42 import static org.onap.so.aaisimulator.utils.TestConstants.SYSTEM_NAME;
43 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
44 import java.io.IOException;
45 import java.util.List;
46 import java.util.Optional;
47 import org.junit.After;
48 import org.junit.Test;
49 import org.onap.aai.domain.yang.EsrSystemInfo;
50 import org.onap.aai.domain.yang.EsrSystemInfoList;
51 import org.onap.aai.domain.yang.EsrVnfm;
52 import org.onap.aai.domain.yang.EsrVnfmList;
53 import org.onap.aai.domain.yang.GenericVnf;
54 import org.onap.aai.domain.yang.Relationship;
55 import org.onap.aai.domain.yang.RelationshipData;
56 import org.onap.aai.domain.yang.RelationshipList;
57 import org.onap.so.aaisimulator.service.providers.ExternalSystemCacheServiceProvider;
58 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
59 import org.onap.so.aaisimulator.utils.Constants;
60 import org.onap.so.aaisimulator.utils.TestConstants;
61 import org.onap.so.aaisimulator.utils.TestUtils;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65
66 /**
67  * @author Waqas Ikram (waqas.ikram@est.tech)
68  *
69  */
70 public class ExternalSystemEsrControllerTest extends AbstractSpringBootTest {
71
72     @Autowired
73     private ExternalSystemCacheServiceProvider externalSystemCacheServiceProvider;
74
75
76     @Autowired
77     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
78
79     @After
80     public void after() {
81         externalSystemCacheServiceProvider.clearAll();
82         genericVnfCacheServiceProvider.clearAll();
83     }
84
85     @Test
86     public void test_putEsrVnfm_successfullyAddedToCache() throws Exception {
87         final String esrVnfmUrl = getUrl(ESR_VNFM_URL, ESR_VNFM_ID);
88         addEsrVnfmAndAssertResponse(esrVnfmUrl);
89
90         final ResponseEntity<EsrVnfm> response = testRestTemplateService.invokeHttpGet(esrVnfmUrl, EsrVnfm.class);
91         assertEquals(HttpStatus.OK, response.getStatusCode());
92
93         assertTrue(response.hasBody());
94
95         final EsrVnfm actualEsrVnfm = response.getBody();
96         assertEquals(ESR_VNFM_ID, actualEsrVnfm.getVnfmId());
97         assertEquals(ESR_VIM_ID, actualEsrVnfm.getVimId());
98
99     }
100
101     @Test
102     public void test_getEsrVnfmList_getAllEsrVnfmsFromCache() throws Exception {
103         final String esrVnfmUrl = getUrl(ESR_VNFM_URL, ESR_VNFM_ID);
104         addEsrVnfmAndAssertResponse(esrVnfmUrl);
105
106         final String esrVnfmListUrl = getUrl(TestConstants.EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL);
107         final ResponseEntity<EsrVnfmList> response =
108                 testRestTemplateService.invokeHttpGet(esrVnfmListUrl, EsrVnfmList.class);
109
110         assertTrue(response.hasBody());
111
112         final EsrVnfmList actualEsrVnfmList = response.getBody();
113
114         final List<EsrVnfm> esrVnfmList = actualEsrVnfmList.getEsrVnfm();
115         assertNotNull(esrVnfmList);
116         assertEquals(1, esrVnfmList.size());
117         final EsrVnfm actualEsrVnfm = esrVnfmList.get(0);
118         assertEquals(ESR_VNFM_ID, actualEsrVnfm.getVnfmId());
119         assertEquals(ESR_VIM_ID, actualEsrVnfm.getVimId());
120
121     }
122
123     @Test
124     public void test_putEsrSystemInfo_successfullyAddedToCache() throws Exception {
125         final String esrVnfmUrl = getUrl(ESR_VNFM_URL, ESR_VNFM_ID);
126         addEsrVnfmAndAssertResponse(esrVnfmUrl);
127         final String esrSystemInfoListUrl = getUrl(ESR_VNFM_URL, ESR_VNFM_ID, ESR_SYSTEM_INFO_LIST_URL);
128
129         final String esrSystemInfoUrl = esrSystemInfoListUrl + "/esr-system-info/" + ESR_SYSTEM_INFO_ID;
130         final ResponseEntity<Void> esrSystemInfoResponse =
131                 testRestTemplateService.invokeHttpPut(esrSystemInfoUrl, TestUtils.getEsrSystemInfo(), Void.class);
132         assertEquals(HttpStatus.ACCEPTED, esrSystemInfoResponse.getStatusCode());
133
134         final ResponseEntity<EsrSystemInfoList> response =
135                 testRestTemplateService.invokeHttpGet(esrSystemInfoListUrl, EsrSystemInfoList.class);
136         assertEquals(HttpStatus.OK, response.getStatusCode());
137
138         assertTrue(response.hasBody());
139         final EsrSystemInfoList actualEsrSystemInfoList = response.getBody();
140
141         final List<EsrSystemInfo> esrSystemInfoList = actualEsrSystemInfoList.getEsrSystemInfo();
142         assertNotNull(esrSystemInfoList);
143         assertEquals(1, esrSystemInfoList.size());
144
145         final EsrSystemInfo esrSystemInfo = esrSystemInfoList.get(0);
146         assertEquals(ESR_SYSTEM_INFO_ID, esrSystemInfo.getEsrSystemInfoId());
147         assertEquals(SYSTEM_NAME, esrSystemInfo.getSystemName());
148         assertEquals(ESR_TYEP, esrSystemInfo.getType());
149         assertEquals(ESR_VENDOR, esrSystemInfo.getVendor());
150         assertEquals(ESR_SERVICE_URL, esrSystemInfo.getServiceUrl());
151         assertEquals(ESR_USERNAME, esrSystemInfo.getUserName());
152         assertEquals(ESR_PASSWORD, esrSystemInfo.getPassword());
153         assertEquals(ESR_SYSTEM_TYPE, esrSystemInfo.getSystemType());
154
155
156     }
157
158     @Test
159     public void test_putEsrRelationToGenericVnfm_successfullyAddedToCache() throws Exception {
160         final String esrVnfmUrl = getUrl(ESR_VNFM_URL, ESR_VNFM_ID);
161
162         addEsrVnfmAndAssertResponse(esrVnfmUrl);
163         addCustomerServiceAndGenericVnf();
164
165         final String relationShipUrl = esrVnfmUrl + RELATIONSHIP_LIST_RELATIONSHIP_URL;
166
167         final ResponseEntity<Void> response = testRestTemplateService.invokeHttpPut(relationShipUrl,
168                 TestUtils.getGenericVnfRelatedLink(), Void.class);
169         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
170
171         final Optional<EsrVnfm> optional = externalSystemCacheServiceProvider.getEsrVnfm(ESR_VNFM_ID);
172         assertTrue(optional.isPresent());
173
174         final EsrVnfm actualEsrVnfm = optional.get();
175         final RelationshipList relationshipList = actualEsrVnfm.getRelationshipList();
176         assertNotNull(relationshipList);
177         assertFalse(relationshipList.getRelationship().isEmpty());
178
179         final Relationship relationship = relationshipList.getRelationship().get(0);
180
181         assertEquals(Constants.DEPENDS_ON, relationship.getRelationshipLabel());
182         assertFalse(relationship.getRelationshipData().isEmpty());
183         assertEquals(1, relationship.getRelationshipData().size());
184
185         final RelationshipData relationshipData =
186                 getRelationshipData(relationship.getRelationshipData(), Constants.GENERIC_VNF_VNF_ID);
187         assertNotNull(relationshipData);
188         assertEquals(VNF_ID, relationshipData.getRelationshipValue());
189
190         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
191         assertTrue(genericVnfOptional.isPresent());
192         final GenericVnf actualGenericVnf = genericVnfOptional.get();
193         final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
194         assertNotNull(relationshipListGenericVnf);
195         assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
196
197         final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
198
199         assertEquals(Constants.DEPENDS_ON, relationshipGenericVnf.getRelationshipLabel());
200         assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
201         assertEquals(1, relationshipGenericVnf.getRelationshipData().size());
202
203         final RelationshipData esrRelationshipData =
204                 getRelationshipData(relationshipGenericVnf.getRelationshipData(), Constants.ESR_VNFM_VNFM_ID);
205         assertNotNull(esrRelationshipData);
206         assertEquals(ESR_VNFM_ID, esrRelationshipData.getRelationshipValue());
207
208
209     }
210
211     private void addEsrVnfmAndAssertResponse(final String esrVnfmUrl) throws IOException {
212         final ResponseEntity<Void> esrVnfmResponse =
213                 testRestTemplateService.invokeHttpPut(esrVnfmUrl, TestUtils.getEsrVnfm(), Void.class);
214         assertEquals(HttpStatus.ACCEPTED, esrVnfmResponse.getStatusCode());
215     }
216
217     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
218         final ResponseEntity<Void> customerResponse =
219                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
220         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
221
222         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
223         final ResponseEntity<Void> serviceInstanceResponse =
224                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
225         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
226
227         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
228         final ResponseEntity<Void> genericVnfResponse =
229                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
230         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
231
232     }
233 }