2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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=========================================================
21 package org.onap.so.adapters.vnfmadapter.rest;
23 import static org.hamcrest.Matchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertThat;
28 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
29 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
31 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
33 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
34 import com.google.gson.Gson;
36 import java.net.URISyntaxException;
37 import java.security.GeneralSecurityException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Objects;
41 import java.util.UUID;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.onap.so.adapters.vnfmadapter.Constants;
46 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
47 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.LinkSelf;
48 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmNotificationsFilter;
49 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
50 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002;
51 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
52 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthentication;
53 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter;
54 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter.NotificationTypesEnum;
55 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilterVnfProductsFromProviders;
56 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsLinks;
57 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinksSelf;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.beans.factory.annotation.Qualifier;
60 import org.springframework.boot.test.context.SpringBootTest;
61 import org.springframework.cache.Cache;
62 import org.springframework.cache.CacheManager;
63 import org.springframework.http.HttpHeaders;
64 import org.springframework.http.HttpMethod;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.MediaType;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.test.context.ActiveProfiles;
69 import org.springframework.test.context.junit4.SpringRunner;
70 import org.springframework.test.web.client.MockRestServiceServer;
71 import org.springframework.web.client.RestTemplate;
74 * @author Ronan Kenny (ronan.kenny@est.tech)
75 * @author Gareth Roper (gareth.roper@est.tech)
77 @RunWith(SpringRunner.class)
78 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
79 @ActiveProfiles("test")
80 public class Sol003PackageManagementSubscriptionControllerTest {
82 private final Gson gson = new Gson();
83 private final URI msbEndpoint = URI.create("http://msb-iag.onap:80/api/vnfpkgm/v1/subscriptions");
85 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
86 private RestTemplate testRestTemplate;
87 private MockRestServiceServer mockRestServer;
89 private CacheManager cacheServiceProvider;
91 private Sol003PackageManagementSubscriptionController sol003PackageManagementSubscriptionController;
95 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
96 final Cache cache = cacheServiceProvider.getCache(Constants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE);
101 public void testSuccessPostSubscription() throws GeneralSecurityException, URISyntaxException {
102 final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest();
103 final ResponseEntity<InlineResponse2002> response =
104 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
105 .postSubscriptionRequest(pkgmSubscriptionRequest);
107 final HttpHeaders headers = buildHttpHeaders(Objects.requireNonNull(response.getBody()).getCallbackUri());
109 SubscriptionsLinks subscriptionsLinks = new SubscriptionsLinks();
110 VnfPackagesLinksSelf vnfPackagesLinksSelf = new VnfPackagesLinksSelf();
111 vnfPackagesLinksSelf.setHref("https://so-vnfm-adapter.onap:30406" + PACKAGE_MANAGEMENT_BASE_URL
112 + "/subscriptions/" + response.getBody().getId());
113 subscriptionsLinks.setSelf(vnfPackagesLinksSelf);
115 assertEquals(pkgmSubscriptionRequest.getFilter(), response.getBody().getFilter());
116 assertEquals(subscriptionsLinks, response.getBody().getLinks());
117 assertEquals(response.getBody().getFilter(), pkgmSubscriptionRequest.getFilter());
118 assert (response.getHeaders().equals(headers));
119 assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
120 assertNotNull(response.getBody().getCallbackUri());
124 public void testFailPostSubscriptionAlreadyExists() throws GeneralSecurityException {
125 final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest();
127 final ResponseEntity<InlineResponse2002> response =
128 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
129 .postSubscriptionRequest(pkgmSubscriptionRequest);
131 // Create duplicate entry
132 final PkgmSubscriptionRequest pkgmSubscriptionRequest2 = buildPkgmSubscriptionRequest();
134 final ResponseEntity<InlineResponse2002> response2002 =
135 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
136 .postSubscriptionRequest(pkgmSubscriptionRequest2);
138 assertEquals(HttpStatus.SEE_OTHER, response2002.getStatusCode());
142 public void testSuccessGetSubscriptionWithSubscriptionId() throws GeneralSecurityException, URISyntaxException {
143 final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest();
145 final ResponseEntity<InlineResponse2002> response =
146 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
147 .postSubscriptionRequest(pkgmSubscriptionRequest);
148 final String subscriptionId = response.getBody().getId();
150 final ResponseEntity<InlineResponse2002> response2002 =
151 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
152 .getSubscription(subscriptionId);
154 final HttpHeaders headers = buildHttpHeaders(response.getBody().getCallbackUri());
156 assertEquals(response.getBody().getFilter(), pkgmSubscriptionRequest.getFilter());
157 assertEquals(response.getHeaders(), headers);
158 assertEquals(HttpStatus.OK, response2002.getStatusCode());
159 assertEquals(pkgmSubscriptionRequest.getFilter(), response.getBody().getFilter());
160 // Ensure CallBackUri is set to new URI
161 assertNotEquals(pkgmSubscriptionRequest.getCallbackUri(), response.getBody().getCallbackUri());
165 public void testFailGetSubscriptionWithInvalidSubscriptionId() {
166 final ResponseEntity<InlineResponse2002> response =
167 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
168 .getSubscription("invalidSubscriptionId");
169 assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
173 public void testSuccessGetSubscriptions() throws GeneralSecurityException {
174 final PkgmSubscription pkgmSubscription = buildPkgmSubscription();
175 final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest();
177 mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST))
178 .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON));
180 sol003PackageManagementSubscriptionController.postSubscriptionRequest(pkgmSubscriptionRequest);
181 ResponseEntity<List<InlineResponse2002>> response =
182 sol003PackageManagementSubscriptionController.getSubscriptions();
184 List<InlineResponse2002> subscriptionsList = response.getBody();
186 assertEquals(Objects.requireNonNull(response.getBody()).get(0).getFilter(),
187 pkgmSubscriptionRequest.getFilter());
188 assert (subscriptionsList != null);
189 assertNotEquals('0', subscriptionsList.size());
190 assertEquals(HttpStatus.OK, response.getStatusCode());
194 public void testSuccessDeleteSubscriptionWithSubscriptionId() throws GeneralSecurityException {
195 final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest();
196 final PkgmSubscription pkgmSubscription = buildPkgmSubscription();
197 final String subscriptionId = pkgmSubscription.getId();
199 mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST))
200 .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON));
201 mockRestServer.expect(requestTo(msbEndpoint + "/" + subscriptionId)).andExpect(method(HttpMethod.DELETE))
202 .andRespond(withStatus(HttpStatus.NO_CONTENT));
204 final ResponseEntity<InlineResponse2002> responsePost =
205 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
206 .postSubscriptionRequest(pkgmSubscriptionRequest);
208 final ResponseEntity responseDelete =
209 sol003PackageManagementSubscriptionController.deleteSubscription(subscriptionId);
211 // Attempt to retrieve the subscription after delete
212 final ResponseEntity<InlineResponse2002> responseGetSubscription =
213 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
214 .getSubscription(subscriptionId);
216 assertEquals(HttpStatus.NOT_FOUND, responseGetSubscription.getStatusCode());
217 assertEquals(HttpStatus.NO_CONTENT, responseDelete.getStatusCode());
221 public void testFailDeleteSubscriptionWithInvalidSubscriptionId() throws URISyntaxException, InterruptedException {
222 final ResponseEntity<Void> responseDelete = (ResponseEntity<Void>) sol003PackageManagementSubscriptionController
223 .deleteSubscription("invalidSubscriptionId");
224 assertEquals(HttpStatus.NOT_FOUND, responseDelete.getStatusCode());
227 private PkgmSubscriptionRequest buildPkgmSubscriptionRequest() {
228 final PkgmSubscriptionRequest pkgmSubscriptionRequest = new PkgmSubscriptionRequest();
229 final SubscriptionsFilter sub = buildSubscriptionsFilter();
230 final SubscriptionsAuthentication auth = new SubscriptionsAuthentication();
231 pkgmSubscriptionRequest.setFilter(sub);
232 pkgmSubscriptionRequest.setCallbackUri(msbEndpoint.toString());
233 pkgmSubscriptionRequest.setAuthentication(auth);
234 return pkgmSubscriptionRequest;
237 private SubscriptionsFilter buildSubscriptionsFilter() {
238 final SubscriptionsFilter sub = new SubscriptionsFilter();
239 final List<String> vnfdIdList = new ArrayList();
240 final List<String> vnfPkgIdList = new ArrayList();
241 final List<NotificationTypesEnum> notificationTypes = new ArrayList<>();
242 final SubscriptionsFilterVnfProductsFromProviders subscriptionsFilterVnfProductsFromProviders =
243 new SubscriptionsFilterVnfProductsFromProviders();
244 final List<SubscriptionsFilterVnfProductsFromProviders> vnfProductsFromProviders = new ArrayList<>();
246 vnfProductsFromProviders.add(subscriptionsFilterVnfProductsFromProviders);
247 sub.setVnfdId(vnfdIdList);
248 sub.setNotificationTypes(notificationTypes);
249 sub.setVnfPkgId(vnfPkgIdList);
250 sub.setVnfProductsFromProviders(vnfProductsFromProviders);
254 private PkgmSubscription buildPkgmSubscription() {
255 PkgmSubscription pkgmSubscription = new PkgmSubscription();
256 PkgmNotificationsFilter pkgmNotificationsFilter = new PkgmNotificationsFilter();
257 LinkSelf linkSelf = new LinkSelf();
258 String id = UUID.randomUUID().toString();
259 pkgmSubscription.setId(id);
260 pkgmSubscription.setCallbackUri(msbEndpoint + "/" + pkgmSubscription.getId().toString());
261 pkgmSubscription.setFilter(pkgmNotificationsFilter);
262 pkgmSubscription.setLinks(linkSelf);
263 return pkgmSubscription;
266 private PkgmSubscriptionRequest postSubscriptionForTest() {
267 final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest();
268 final PkgmSubscription pkgmSubscription = buildPkgmSubscription();
270 mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST))
271 .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON));
272 return pkgmSubscriptionRequest;
275 private HttpHeaders buildHttpHeaders(String uri) throws URISyntaxException {
276 final HttpHeaders headers = new HttpHeaders();
277 URI myUri = new URI(uri);
278 headers.setLocation(myUri);