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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertThat;
27 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
28 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
30 import java.security.GeneralSecurityException;
32 import java.net.URISyntaxException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Objects;
36 import java.util.UUID;
37 import com.google.gson.Gson;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.onap.so.adapters.vnfmadapter.Constants;
42 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
43 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.LinkSelf;
44 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmNotificationsFilter;
45 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
46 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002;
47 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
48 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthentication;
49 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter;
50 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter.NotificationTypesEnum;
51 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilterVnfProductsFromProviders;
52 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsLinks;
53 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinksSelf;
54 import org.onap.so.utils.CryptoUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.beans.factory.annotation.Qualifier;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.cache.Cache;
59 import org.springframework.cache.CacheManager;
60 import org.springframework.http.HttpHeaders;
61 import org.springframework.http.HttpStatus;
62 import org.springframework.http.MediaType;
63 import org.springframework.http.ResponseEntity;
64 import org.springframework.test.context.ActiveProfiles;
65 import org.springframework.test.context.junit4.SpringRunner;
66 import org.springframework.test.web.client.MockRestServiceServer;
67 import org.springframework.web.client.RestTemplate;
68 import org.springframework.http.HttpMethod;
69 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
70 import static org.hamcrest.Matchers.is;
71 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
74 * @author Ronan Kenny (ronan.kenny@est.tech)
75 * @author Gareth Roper (gareth.roper@est.tech)
78 @RunWith(SpringRunner.class)
79 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
80 @ActiveProfiles("test")
81 @SuppressWarnings("unchecked")
82 public class Sol003PackageManagementSubscriptionControllerTest {
84 private static String subscriptionId;
85 private final Gson gson = new Gson();
88 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
89 private RestTemplate testRestTemplate;
91 private MockRestServiceServer mockRestServer;
94 private CacheManager cacheServiceProvider;
95 private final URI msbEndpoint = URI.create("http://msb-iag.onap:80/api/vnfpkgm/v1/subscriptions");
98 private Sol003PackageManagementSubscriptionController sol003PackageManagementSubscriptionController;
101 public void setUp() {
102 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
103 final Cache cache = cacheServiceProvider.getCache(Constants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE);
108 public void testSuccessPostSubscription() throws GeneralSecurityException, URISyntaxException {
109 final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest();
110 final ResponseEntity<InlineResponse2002> response =
111 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
112 .postSubscriptionRequest(pkgmSubscriptionRequest);
114 final HttpHeaders headers = buildHttpHeaders(Objects.requireNonNull(response.getBody()).getCallbackUri());
116 SubscriptionsLinks subscriptionsLinks = new SubscriptionsLinks();
117 VnfPackagesLinksSelf vnfPackagesLinksSelf = new VnfPackagesLinksSelf();
118 vnfPackagesLinksSelf.setHref("https://so-vnfm-adapter.onap:30406" + PACKAGE_MANAGEMENT_BASE_URL
119 + "/subscriptions/" + response.getBody().getId());
120 subscriptionsLinks.setSelf(vnfPackagesLinksSelf);
122 assertEquals(pkgmSubscriptionRequest.getFilter(), response.getBody().getFilter());
123 assertEquals(subscriptionsLinks, response.getBody().getLinks());
124 assertEquals(response.getBody().getFilter(), pkgmSubscriptionRequest.getFilter());
125 assert (response.getHeaders().equals(headers));
126 assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
127 assertNotNull(response.getBody().getCallbackUri());
131 public void testFailPostSubscriptionAlreadyExists() throws GeneralSecurityException {
132 final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest();
134 final ResponseEntity<InlineResponse2002> response =
135 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
136 .postSubscriptionRequest(pkgmSubscriptionRequest);
137 subscriptionId = Objects.requireNonNull(response.getBody()).getId();
139 // Create duplicate entry
140 final PkgmSubscriptionRequest pkgmSubscriptionRequest2 = buildPkgmSubscriptionRequest();
142 final ResponseEntity<InlineResponse2002> response2002 =
143 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
144 .postSubscriptionRequest(pkgmSubscriptionRequest2);
146 assertEquals(HttpStatus.SEE_OTHER, response2002.getStatusCode());
150 public void testSuccessGetSubscriptionWithSubscriptionId() throws GeneralSecurityException, URISyntaxException {
151 final PkgmSubscriptionRequest pkgmSubscriptionRequest = postSubscriptionForTest();
153 final ResponseEntity<InlineResponse2002> response =
154 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
155 .postSubscriptionRequest(pkgmSubscriptionRequest);
156 subscriptionId = Objects.requireNonNull(response.getBody()).getId();
158 final ResponseEntity<InlineResponse2002> response2002 =
159 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
160 .getSubscription(subscriptionId);
162 final HttpHeaders headers = buildHttpHeaders(response.getBody().getCallbackUri());
165 assertEquals(response.getBody().getFilter(), pkgmSubscriptionRequest.getFilter());
166 assert (response.getHeaders().equals(headers));
167 assertEquals(HttpStatus.OK, response2002.getStatusCode());
168 assertEquals(pkgmSubscriptionRequest.getFilter(), response.getBody().getFilter());
169 // Ensure CallBackUri is set to new URI
170 assertNotEquals(pkgmSubscriptionRequest.getCallbackUri(), response.getBody().getCallbackUri());
174 public void testFailGetSubscriptionWithInvalidSubscriptionId() {
175 final ResponseEntity<InlineResponse2002> response =
176 (ResponseEntity<InlineResponse2002>) sol003PackageManagementSubscriptionController
177 .getSubscription("invalidSubscriptionId");
178 assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
182 public void testSuccessGetSubscriptions() throws GeneralSecurityException {
183 final PkgmSubscription pkgmSubscription = buildPkgmSubscription();
184 final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest();
186 mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST))
187 .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON));
189 sol003PackageManagementSubscriptionController.postSubscriptionRequest(pkgmSubscriptionRequest);
190 ResponseEntity<List<InlineResponse2002>> response =
191 sol003PackageManagementSubscriptionController.getSubscriptions();
193 List<InlineResponse2002> subscriptionsList = response.getBody();
195 assertEquals(Objects.requireNonNull(response.getBody()).get(0).getFilter(),
196 pkgmSubscriptionRequest.getFilter());
197 assert (subscriptionsList != null);
198 assertNotEquals('0', subscriptionsList.size());
199 assertEquals(HttpStatus.OK, response.getStatusCode());
202 private PkgmSubscriptionRequest buildPkgmSubscriptionRequest() {
203 final PkgmSubscriptionRequest pkgmSubscriptionRequest = new PkgmSubscriptionRequest();
204 final SubscriptionsFilter sub = buildSubscriptionsFilter();
205 final SubscriptionsAuthentication auth = new SubscriptionsAuthentication();
206 pkgmSubscriptionRequest.setFilter(sub);
207 pkgmSubscriptionRequest.setCallbackUri(msbEndpoint.toString());
208 pkgmSubscriptionRequest.setAuthentication(auth);
209 return pkgmSubscriptionRequest;
212 private SubscriptionsFilter buildSubscriptionsFilter() {
213 final SubscriptionsFilter sub = new SubscriptionsFilter();
214 final List<String> vnfdIdList = new ArrayList();
215 final List<String> vnfPkgIdList = new ArrayList();
216 final List<NotificationTypesEnum> notificationTypes = new ArrayList<>();
217 final SubscriptionsFilterVnfProductsFromProviders subscriptionsFilterVnfProductsFromProviders =
218 new SubscriptionsFilterVnfProductsFromProviders();
219 final List<SubscriptionsFilterVnfProductsFromProviders> vnfProductsFromProviders = new ArrayList<>();
221 vnfProductsFromProviders.add(subscriptionsFilterVnfProductsFromProviders);
222 sub.setVnfdId(vnfdIdList);
223 sub.setNotificationTypes(notificationTypes);
224 sub.setVnfPkgId(vnfPkgIdList);
225 sub.setVnfProductsFromProviders(vnfProductsFromProviders);
229 private PkgmSubscription buildPkgmSubscription() {
230 PkgmSubscription pkgmSubscription = new PkgmSubscription();
231 PkgmNotificationsFilter pkgmNotificationsFilter = new PkgmNotificationsFilter();
232 LinkSelf linkSelf = new LinkSelf();
233 String id = UUID.randomUUID().toString();
234 pkgmSubscription.setId(id);
235 pkgmSubscription.setCallbackUri(msbEndpoint + "/" + pkgmSubscription.getId().toString());
236 pkgmSubscription.setFilter(pkgmNotificationsFilter);
237 pkgmSubscription.setLinks(linkSelf);
238 return pkgmSubscription;
241 private PkgmSubscriptionRequest postSubscriptionForTest() {
242 final PkgmSubscriptionRequest pkgmSubscriptionRequest = buildPkgmSubscriptionRequest();
243 final PkgmSubscription pkgmSubscription = buildPkgmSubscription();
245 mockRestServer.expect(requestTo(msbEndpoint)).andExpect(method(HttpMethod.POST))
246 .andRespond(withSuccess(gson.toJson(pkgmSubscription), MediaType.APPLICATION_JSON));
247 return pkgmSubscriptionRequest;
250 private HttpHeaders buildHttpHeaders(String uri) throws URISyntaxException {
251 final HttpHeaders headers = new HttpHeaders();
252 URI myUri = new URI(uri);
253 headers.setLocation(myUri);