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.etsi.sol003.adapter.packagemanagement.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.onap.so.adapters.etsi.sol003.adapter.common.CommonConstants.ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL;
26 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
27 import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_CLASS;
28 import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
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;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.UUID;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.NOTIFICATIONLINKSERIALIZER;
43 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgChangeNotification;
44 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgOnboardingNotification;
45 import org.onap.so.adapters.etsi.sol003.adapter.etsicatalog.notification.model.PkgmLinks;
46 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.extclients.vnfm.notification.model.VnfPackageChangeNotification;
47 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.extclients.vnfm.notification.model.VnfPackageOnboardingNotification;
48 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.PackageManagementConstants;
49 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.extclients.etsicatalog.model.ProblemDetails;
50 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.PkgmSubscriptionRequest;
51 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthentication;
52 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthenticationParamsBasic;
53 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthenticationParamsOauth2ClientCredentials;
54 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
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.boot.test.web.client.TestRestTemplate;
59 import org.springframework.boot.web.server.LocalServerPort;
60 import org.springframework.cache.Cache;
61 import org.springframework.cache.CacheManager;
62 import org.springframework.http.HttpEntity;
63 import org.springframework.http.HttpMethod;
64 import org.springframework.http.HttpStatus;
65 import org.springframework.http.MediaType;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.test.annotation.DirtiesContext;
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;
72 import org.threeten.bp.LocalDateTime;
73 import org.threeten.bp.OffsetDateTime;
74 import org.threeten.bp.ZoneOffset;
75 import com.google.gson.Gson;
76 import com.google.gson.GsonBuilder;
79 * @author Andrew Lamb (andrew.a.lamb@est.tech)
82 @RunWith(SpringRunner.class)
83 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
84 @ActiveProfiles("test")
85 @DirtiesContext(classMode = BEFORE_CLASS)
86 public class EtsiSubscriptionNotificationControllerTest {
91 private static final URI CALLBACK_URI = URI.create("http://test_callback_uri/notification");
92 private static final String TOKEN_ENDPOINT = "http://test_token_endpoint_uri/";
93 private static final String TOKEN = "dXNlcm5hbWU6cGFzc3dvcmQ=......";
94 private static final String JSON_TOKEN = "{\"access_token\":\"" + TOKEN + "\"}";
95 private static final String LOCALHOST_URL = "http://localhost:";
96 private static final String NOTIFICATION_BASE_URL =
97 ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL + "/notification";
98 private static final String USERNAME = "username";
99 private static final String PASSWORD = "password";
100 private static final String EXPECTED_BASIC_AUTHORIZATION = "Basic dXNlcm5hbWU6cGFzc3dvcmQ=";
101 private static final String EXPECTED_OAUTH_AUTHORIZATION = "Bearer " + TOKEN;
102 private static final String NOTIFICATION_ID = "NOTIFICATION_ID";
103 private static final String SUBSCRIPTION_ID = "SUBSCRIPTION_ID";
104 private static final OffsetDateTime TIMESTAMP =
105 OffsetDateTime.of(LocalDateTime.of(2020, 1, 1, 1, 1, 1, 1), ZoneOffset.ofHours(1));
106 private static final String VNFPKG_ID = UUID.randomUUID().toString();
107 private static final String VNFD_ID = UUID.randomUUID().toString();
109 private BasicHttpHeadersProvider basicHttpHeadersProvider;
110 private final Gson gson = new GsonBuilder().create();;
113 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
114 private RestTemplate restTemplate;
115 private MockRestServiceServer mockRestServiceServer;
118 private TestRestTemplate testRestTemplate;
121 private CacheManager cacheServiceProvider;
125 public void setUp() {
126 mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
127 basicHttpHeadersProvider = new BasicHttpHeadersProvider();
128 cache = cacheServiceProvider.getCache(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE);
133 public void tearDown() {
134 mockRestServiceServer.reset();
139 public void testSubscriptionNotificationEndPoint_ReturnsNoContent() {
140 final ResponseEntity<?> response = sendHttpGet(NOTIFICATION_BASE_URL);
141 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
145 public void testOnboardingNotificationSentOnToVnfmCallbackUri_SubscriptionRequestInCache_Success() {
146 final PkgmSubscriptionRequest subscriptionRequest =
147 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
148 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
149 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
150 final String notificationString = gson.toJson(notification);
152 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
153 .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
154 .andExpect(jsonPath("$.notificationType")
155 .value(VnfPackageOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION
157 .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
158 .andExpect(jsonPath("$.timeStamp").value(TIMESTAMP.toString()))
159 .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID.toString()))
160 .andExpect(jsonPath("$.vnfdId").value(VNFD_ID.toString()))
161 .andExpect(jsonPath("$._links").value(buildPkgmLinks()))
162 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());
164 final ResponseEntity<?> response = sendHttpPost(notificationString);
166 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
170 public void testOnboardingNotificationNotSentOnToVnfmCallbackUri_SubscriptionRequestNotInCache_Fail() {
171 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
172 final String notificationString = gson.toJson(notification);
173 final ResponseEntity<?> response = sendHttpPost(notificationString);
175 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
176 assertTrue(response.getBody() instanceof ProblemDetails);
178 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
179 final String errorMessage = "No subscription found with subscriptionId " + SUBSCRIPTION_ID
180 + ". Unable to forward notification to subscriber.";
182 assertEquals(errorMessage, problemDetails.getDetail());
186 public void testOnboardingNotificationSentOnToVnfmCallbackUri_BadRequestResponseFromCallbackUri_Fail() {
187 final PkgmSubscriptionRequest subscriptionRequest =
188 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
189 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
190 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
191 final String notificationString = gson.toJson(notification);
193 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
194 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
196 final ResponseEntity<?> response = sendHttpPost(notificationString);
198 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
199 assertTrue(response.getBody() instanceof ProblemDetails);
201 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
202 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
203 + HttpStatus.BAD_REQUEST + ".\n" + "No result found for given url: " + CALLBACK_URI;
205 assertEquals(errorMessage, problemDetails.getDetail());
209 public void testOnboardingNotificationSentOnToVnfmCallbackUri_301MovedPermanentlyResponseFromCallbackUri_Fail() {
210 final PkgmSubscriptionRequest subscriptionRequest =
211 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
212 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
213 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
214 final String notificationString = gson.toJson(notification);
216 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
217 .andRespond(withStatus(HttpStatus.MOVED_PERMANENTLY));
219 final ResponseEntity<?> response = sendHttpPost(notificationString);
221 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
222 assertTrue(response.getBody() instanceof ProblemDetails);
224 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
225 final String errorMessage = "An error occurred. Sending of notification to VNFM failed.";
227 assertEquals(errorMessage, problemDetails.getDetail());
231 public void testOnboardingNotificationSentOnToVnfmCallbackUri_NotFoundResponseFromCallbackUri_Fail() {
232 final PkgmSubscriptionRequest subscriptionRequest =
233 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
234 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
235 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
236 final String notificationString = gson.toJson(notification);
238 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
239 .andRespond(withStatus(HttpStatus.NOT_FOUND));
241 final ResponseEntity<?> response = sendHttpPost(notificationString);
243 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
244 assertTrue(response.getBody() instanceof ProblemDetails);
246 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
247 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
248 + HttpStatus.NOT_FOUND + ".\n" + "No result found for given url: " + CALLBACK_URI;
250 assertEquals(errorMessage, problemDetails.getDetail());
254 public void testOnboardingNotificationSentOnToVnfmCallbackUri_InternalServerErrorResponseFromCallbackUri_Fail() {
255 final PkgmSubscriptionRequest subscriptionRequest =
256 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
257 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
258 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
259 final String notificationString = gson.toJson(notification);
261 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
262 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
264 final ResponseEntity<?> response = sendHttpPost(notificationString);
266 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
267 assertTrue(response.getBody() instanceof ProblemDetails);
269 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
270 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
271 + HttpStatus.INTERNAL_SERVER_ERROR.value() + ".\n" + "Unable to invoke HTTP POST using URL: "
274 assertEquals(errorMessage, problemDetails.getDetail());
278 public void testChangeNotificationSentOnToVnfmCallbackUri_SubscriptionRequestInCache_Success() {
279 final PkgmSubscriptionRequest subscriptionRequest =
280 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
281 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
282 final PkgChangeNotification notification = buildPkgChangeNotification();
283 final String notificationString = gson.toJson(notification);
285 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
286 .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
287 .andExpect(jsonPath("$.notificationType").value(
288 VnfPackageChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION.getValue()))
289 .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
290 .andExpect(jsonPath("$.timeStamp").value(TIMESTAMP.toString()))
291 .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID.toString()))
292 .andExpect(jsonPath("$.vnfdId").value(VNFD_ID.toString()))
294 jsonPath("$.changeType").value(PkgChangeNotification.ChangeTypeEnum.OP_STATE_CHANGE.toString()))
295 .andExpect(jsonPath("$.operationalState")
296 .value(PkgChangeNotification.OperationalStateEnum.ENABLED.toString()))
297 .andExpect(jsonPath("$._links").value(buildPkgmLinks()))
298 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());
300 final ResponseEntity<?> response = sendHttpPost(notificationString);
302 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
306 public void testChangeNotificationNotSentOnToVnfmCallbackUri_SubscriptionRequestNotInCache_Fail() {
307 final PkgChangeNotification notification = buildPkgChangeNotification();
308 final String notificationString = gson.toJson(notification);
309 final ResponseEntity<?> response = sendHttpPost(notificationString);
311 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
312 assertTrue(response.getBody() instanceof ProblemDetails);
314 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
315 final String errorMessage = "No subscription found with subscriptionId " + SUBSCRIPTION_ID
316 + ". Unable to forward notification to subscriber.";
318 assertEquals(errorMessage, problemDetails.getDetail());
322 public void testChangeNotificationSentOnToVnfmCallbackUri_BadRequestResponseFromCallbackUri_Fail() {
323 final PkgmSubscriptionRequest subscriptionRequest =
324 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
325 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
326 final PkgChangeNotification notification = buildPkgChangeNotification();
327 final String notificationString = gson.toJson(notification);
329 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
330 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
332 final ResponseEntity<?> response = sendHttpPost(notificationString);
334 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
335 assertTrue(response.getBody() instanceof ProblemDetails);
337 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
338 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
339 + HttpStatus.BAD_REQUEST + ".\n" + "No result found for given url: " + CALLBACK_URI;
341 assertEquals(errorMessage, problemDetails.getDetail());
345 public void testChangeNotificationSentOnToVnfmCallbackUri_NotFoundResponseFromCallbackUri_Fail() {
346 final PkgmSubscriptionRequest subscriptionRequest =
347 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
348 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
349 final PkgChangeNotification notification = buildPkgChangeNotification();
350 final String notificationString = gson.toJson(notification);
352 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
353 .andRespond(withStatus(HttpStatus.NOT_FOUND));
355 final ResponseEntity<?> response = sendHttpPost(notificationString);
357 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
358 assertTrue(response.getBody() instanceof ProblemDetails);
360 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
361 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
362 + HttpStatus.NOT_FOUND + ".\n" + "No result found for given url: " + CALLBACK_URI;
364 assertEquals(errorMessage, problemDetails.getDetail());
368 public void testChangeNotificationSentOnToVnfmCallbackUri_InternalServerErrorResponseFromCallbackUri_Fail() {
369 final PkgmSubscriptionRequest subscriptionRequest =
370 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
371 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
372 final PkgChangeNotification notification = buildPkgChangeNotification();
373 final String notificationString = gson.toJson(notification);
375 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
376 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
378 final ResponseEntity<?> response = sendHttpPost(notificationString);
380 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
381 assertTrue(response.getBody() instanceof ProblemDetails);
383 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
384 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
385 + HttpStatus.INTERNAL_SERVER_ERROR.value() + ".\n" + "Unable to invoke HTTP POST using URL: "
388 assertEquals(errorMessage, problemDetails.getDetail());
392 public void testNotificationSentOnToVnfm_BasicAuthUserPasswordAuthorized_Success() {
393 final PkgmSubscriptionRequest subscriptionRequest =
394 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
395 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
396 final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
397 final String notificationString = gson.toJson(notification);
399 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
400 .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
401 .andExpect(jsonPath("$.notificationType")
402 .value(VnfPackageOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION
404 .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
405 .andExpect(jsonPath("$.timeStamp").value(TIMESTAMP.toString()))
406 .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID.toString()))
407 .andExpect(jsonPath("$.vnfdId").value(VNFD_ID.toString()))
408 .andExpect(jsonPath("$._links").value(buildPkgmLinks()))
409 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());
411 final ResponseEntity<?> response = sendHttpPost(notificationString);
413 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
417 public void testNotificationSentOnToVnfm_BasicAuthUserPasswordNotAuthorized_Fail() {
418 final PkgmSubscriptionRequest subscriptionRequest =
419 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
420 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
421 final PkgChangeNotification notification = buildPkgChangeNotification();
422 final String notificationString = gson.toJson(notification);
424 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
425 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION))
426 .andRespond(withStatus(HttpStatus.UNAUTHORIZED));
428 final ResponseEntity<?> response = sendHttpPost(notificationString);
430 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
431 assertTrue(response.getBody() instanceof ProblemDetails);
433 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
434 final String errorMessage = "An error occurred. Sending of notification to VNFM failed with response: "
435 + HttpStatus.UNAUTHORIZED.value() + ".\n" + "Unable to invoke HTTP POST using URL: " + CALLBACK_URI;
437 assertEquals(errorMessage, problemDetails.getDetail());
441 public void testNotificationSentOnToVnfm_OAuthAuthorized_Success() {
442 final PkgmSubscriptionRequest subscriptionRequest =
443 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS);
444 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
445 final PkgChangeNotification notification = buildPkgChangeNotification();
446 final String notificationString = gson.toJson(notification);
448 mockRestServiceServer.expect(requestTo(TOKEN_ENDPOINT)).andExpect(method(HttpMethod.POST))
449 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION))
450 .andRespond(withSuccess(JSON_TOKEN, MediaType.APPLICATION_JSON));
452 mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
453 .andExpect(header("Authorization", EXPECTED_OAUTH_AUTHORIZATION))
454 .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
455 .andExpect(jsonPath("$.notificationType").value(
456 VnfPackageChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION.toString()))
457 .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
458 .andExpect(jsonPath("$.timeStamp").value(TIMESTAMP.toString()))
459 .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID.toString()))
460 .andExpect(jsonPath("$.vnfdId").value(VNFD_ID.toString()))
462 jsonPath("$.changeType").value(PkgChangeNotification.ChangeTypeEnum.OP_STATE_CHANGE.toString()))
463 .andExpect(jsonPath("$.operationalState")
464 .value(PkgChangeNotification.OperationalStateEnum.ENABLED.toString()))
465 .andExpect(jsonPath("$._links").value(buildPkgmLinks())).andRespond(withSuccess());
467 final ResponseEntity<?> response = sendHttpPost(notificationString);
469 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
473 public void testNotificationSentOnToVnfm_OAuthTokenNotReceived_Fail() {
474 final PkgmSubscriptionRequest subscriptionRequest =
475 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS);
476 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
477 final PkgChangeNotification notification = buildPkgChangeNotification();
478 final String notificationString = gson.toJson(notification);
480 mockRestServiceServer.expect(requestTo(TOKEN_ENDPOINT)).andExpect(method(HttpMethod.POST))
481 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());
483 final ResponseEntity<?> response = sendHttpPost(notificationString);
485 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
486 assertTrue(response.getBody() instanceof ProblemDetails);
488 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
489 final String errorMessage = "An error occurred. Unable to retrieve OAuth Token from VNFM for notification.";
491 assertEquals(errorMessage, problemDetails.getDetail());
495 public void testNotificationSentOnToVnfm_TLSCertNotYetSupported_Fail() {
496 final PkgmSubscriptionRequest subscriptionRequest =
497 buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT);
498 cache.put(SUBSCRIPTION_ID, subscriptionRequest);
499 final PkgChangeNotification notification = buildPkgChangeNotification();
500 final String notificationString = gson.toJson(notification);
502 final ResponseEntity<?> response = sendHttpPost(notificationString);
504 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
505 assertTrue(response.getBody() instanceof ProblemDetails);
507 final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
508 final String errorMessage = "An error occurred. Authentication type "
509 + subscriptionRequest.getAuthentication().getAuthType().toString() + " not currently supported.";
511 assertEquals(errorMessage, problemDetails.getDetail());
514 private PkgOnboardingNotification buildPkgOnboardingNotification() {
515 final PkgOnboardingNotification notification = new PkgOnboardingNotification();
516 notification.setId(NOTIFICATION_ID);
518 .setNotificationType(PkgOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION);
519 notification.setSubscriptionId(SUBSCRIPTION_ID);
520 notification.setTimeStamp(TIMESTAMP);
521 notification.setVnfPkgId(VNFPKG_ID);
522 notification.setVnfdId(VNFD_ID);
523 notification.setLinks(buildPkgmLinks());
527 private PkgChangeNotification buildPkgChangeNotification() {
528 final PkgChangeNotification notification = new PkgChangeNotification();
529 notification.setId(NOTIFICATION_ID);
530 notification.setNotificationType(PkgChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION);
531 notification.setSubscriptionId(SUBSCRIPTION_ID);
532 notification.setTimeStamp(TIMESTAMP);
533 notification.setVnfPkgId(VNFPKG_ID);
534 notification.setVnfdId(VNFD_ID);
535 notification.setChangeType(PkgChangeNotification.ChangeTypeEnum.OP_STATE_CHANGE);
536 notification.setOperationalState(PkgChangeNotification.OperationalStateEnum.ENABLED);
537 notification.setLinks(buildPkgmLinks());
541 private PkgmLinks buildPkgmLinks() {
542 final PkgmLinks pkgmLinks = new PkgmLinks();
544 final NOTIFICATIONLINKSERIALIZER subscriptionLinkSerializer = new NOTIFICATIONLINKSERIALIZER();
545 subscriptionLinkSerializer.setHref("subscription_href");
546 pkgmLinks.setSubscription(subscriptionLinkSerializer);
548 final NOTIFICATIONLINKSERIALIZER vnfPackageLinkSerializer = new NOTIFICATIONLINKSERIALIZER();
549 vnfPackageLinkSerializer.setHref("vnf_package_href");
550 pkgmLinks.setVnfPackage(vnfPackageLinkSerializer);
555 private PkgmSubscriptionRequest buildPkgmSubscriptionRequest(
556 final SubscriptionsAuthentication.AuthTypeEnum authTypeEnum) {
557 final PkgmSubscriptionRequest subscriptionRequest = new PkgmSubscriptionRequest();
558 subscriptionRequest.setCallbackUri(CALLBACK_URI.toString());
559 subscriptionRequest.setAuthentication(buildSubscriptionsAuthentication(authTypeEnum));
560 return subscriptionRequest;
563 // TODO update for auth types other than basicAuth
564 private SubscriptionsAuthentication buildSubscriptionsAuthentication(
565 final SubscriptionsAuthentication.AuthTypeEnum authTypeEnum) {
566 final SubscriptionsAuthentication subscriptionsAuthentication = new SubscriptionsAuthentication();
567 final List<SubscriptionsAuthentication.AuthTypeEnum> authTypes = new ArrayList<>();
568 authTypes.add(authTypeEnum);
569 subscriptionsAuthentication.setAuthType(authTypes);
570 if (authTypeEnum == SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT) {
571 // TODO: remove basic params and code for TLS
572 final SubscriptionsAuthenticationParamsBasic basicParams =
573 new SubscriptionsAuthenticationParamsBasic().userName(USERNAME).password(PASSWORD);
574 subscriptionsAuthentication.setParamsBasic(basicParams);
575 } else if (authTypeEnum == SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS) {
576 final SubscriptionsAuthenticationParamsOauth2ClientCredentials oathParams =
577 new SubscriptionsAuthenticationParamsOauth2ClientCredentials().clientId(USERNAME)
578 .clientPassword(PASSWORD).tokenEndpoint(TOKEN_ENDPOINT);
579 subscriptionsAuthentication.setParamsOauth2ClientCredentials(oathParams);
581 final SubscriptionsAuthenticationParamsBasic basicParams =
582 new SubscriptionsAuthenticationParamsBasic().userName(USERNAME).password(PASSWORD);
583 subscriptionsAuthentication.setParamsBasic(basicParams);
586 return subscriptionsAuthentication;
589 private <T> ResponseEntity<ProblemDetails> sendHttpPost(final T notification) {
590 final String testURL = LOCALHOST_URL + port + NOTIFICATION_BASE_URL;
591 final HttpEntity<?> request = new HttpEntity<>(notification, basicHttpHeadersProvider.getHttpHeaders());
592 return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.POST, request,
593 ProblemDetails.class);
596 private ResponseEntity<Void> sendHttpGet(final String url) {
597 final String testURL = LOCALHOST_URL + port + url;
598 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
599 return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, Void.class);