Fixing SO build
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-adapter / src / test / java / org / onap / so / adapters / etsi / sol003 / adapter / packagemanagement / rest / EtsiSubscriptionNotificationControllerTest.java
1 /*-
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
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
21 package org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest;
22
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;
34 import java.net.URI;
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;
77
78 /**
79  * @author Andrew Lamb (andrew.a.lamb@est.tech)
80  *
81  */
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 {
87
88     @LocalServerPort
89     private int port;
90
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();
108
109     private BasicHttpHeadersProvider basicHttpHeadersProvider;
110     private final Gson gson = new GsonBuilder().create();;
111
112     @Autowired
113     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
114     private RestTemplate restTemplate;
115     private MockRestServiceServer mockRestServiceServer;
116
117     @Autowired
118     private TestRestTemplate testRestTemplate;
119
120     @Autowired
121     private CacheManager cacheServiceProvider;
122     private Cache cache;
123
124     @Before
125     public void setUp() {
126         mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
127         basicHttpHeadersProvider = new BasicHttpHeadersProvider();
128         cache = cacheServiceProvider.getCache(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE);
129         cache.clear();
130     }
131
132     @After
133     public void tearDown() {
134         mockRestServiceServer.reset();
135         cache.clear();
136     }
137
138     @Test
139     public void testSubscriptionNotificationEndPoint_ReturnsNoContent() {
140         final ResponseEntity<?> response = sendHttpGet(NOTIFICATION_BASE_URL);
141         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
142     }
143
144     @Test
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);
151
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
156                                 .toString()))
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());
163
164         final ResponseEntity<?> response = sendHttpPost(notificationString);
165
166         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
167     }
168
169     @Test
170     public void testOnboardingNotificationNotSentOnToVnfmCallbackUri_SubscriptionRequestNotInCache_Fail() {
171         final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
172         final String notificationString = gson.toJson(notification);
173         final ResponseEntity<?> response = sendHttpPost(notificationString);
174
175         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
176         assertTrue(response.getBody() instanceof ProblemDetails);
177
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.";
181
182         assertEquals(errorMessage, problemDetails.getDetail());
183     }
184
185     @Test
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);
192
193         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
194                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
195
196         final ResponseEntity<?> response = sendHttpPost(notificationString);
197
198         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
199         assertTrue(response.getBody() instanceof ProblemDetails);
200
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;
204
205         assertEquals(errorMessage, problemDetails.getDetail());
206     }
207
208     @Test
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);
215
216         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
217                 .andRespond(withStatus(HttpStatus.MOVED_PERMANENTLY));
218
219         final ResponseEntity<?> response = sendHttpPost(notificationString);
220
221         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
222         assertTrue(response.getBody() instanceof ProblemDetails);
223
224         final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
225         final String errorMessage = "An error occurred.  Sending of notification to VNFM failed.";
226
227         assertEquals(errorMessage, problemDetails.getDetail());
228     }
229
230     @Test
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);
237
238         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
239                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
240
241         final ResponseEntity<?> response = sendHttpPost(notificationString);
242
243         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
244         assertTrue(response.getBody() instanceof ProblemDetails);
245
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;
249
250         assertEquals(errorMessage, problemDetails.getDetail());
251     }
252
253     @Test
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);
260
261         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
262                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
263
264         final ResponseEntity<?> response = sendHttpPost(notificationString);
265
266         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
267         assertTrue(response.getBody() instanceof ProblemDetails);
268
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: "
272                 + CALLBACK_URI;
273
274         assertEquals(errorMessage, problemDetails.getDetail());
275     }
276
277     @Test
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);
284
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()))
293                 .andExpect(
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());
299
300         final ResponseEntity<?> response = sendHttpPost(notificationString);
301
302         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
303     }
304
305     @Test
306     public void testChangeNotificationNotSentOnToVnfmCallbackUri_SubscriptionRequestNotInCache_Fail() {
307         final PkgChangeNotification notification = buildPkgChangeNotification();
308         final String notificationString = gson.toJson(notification);
309         final ResponseEntity<?> response = sendHttpPost(notificationString);
310
311         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
312         assertTrue(response.getBody() instanceof ProblemDetails);
313
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.";
317
318         assertEquals(errorMessage, problemDetails.getDetail());
319     }
320
321     @Test
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);
328
329         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
330                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
331
332         final ResponseEntity<?> response = sendHttpPost(notificationString);
333
334         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
335         assertTrue(response.getBody() instanceof ProblemDetails);
336
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;
340
341         assertEquals(errorMessage, problemDetails.getDetail());
342     }
343
344     @Test
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);
351
352         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
353                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
354
355         final ResponseEntity<?> response = sendHttpPost(notificationString);
356
357         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
358         assertTrue(response.getBody() instanceof ProblemDetails);
359
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;
363
364         assertEquals(errorMessage, problemDetails.getDetail());
365     }
366
367     @Test
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);
374
375         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
376                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
377
378         final ResponseEntity<?> response = sendHttpPost(notificationString);
379
380         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
381         assertTrue(response.getBody() instanceof ProblemDetails);
382
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: "
386                 + CALLBACK_URI;
387
388         assertEquals(errorMessage, problemDetails.getDetail());
389     }
390
391     @Test
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);
398
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
403                                 .toString()))
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());
410
411         final ResponseEntity<?> response = sendHttpPost(notificationString);
412
413         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
414     }
415
416     @Test
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);
423
424         mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
425                 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION))
426                 .andRespond(withStatus(HttpStatus.UNAUTHORIZED));
427
428         final ResponseEntity<?> response = sendHttpPost(notificationString);
429
430         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
431         assertTrue(response.getBody() instanceof ProblemDetails);
432
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;
436
437         assertEquals(errorMessage, problemDetails.getDetail());
438     }
439
440     @Test
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);
447
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));
451
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()))
461                 .andExpect(
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());
466
467         final ResponseEntity<?> response = sendHttpPost(notificationString);
468
469         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
470     }
471
472     @Test
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);
479
480         mockRestServiceServer.expect(requestTo(TOKEN_ENDPOINT)).andExpect(method(HttpMethod.POST))
481                 .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());
482
483         final ResponseEntity<?> response = sendHttpPost(notificationString);
484
485         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
486         assertTrue(response.getBody() instanceof ProblemDetails);
487
488         final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
489         final String errorMessage = "An error occurred.  Unable to retrieve OAuth Token from VNFM for notification.";
490
491         assertEquals(errorMessage, problemDetails.getDetail());
492     }
493
494     @Test
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);
501
502         final ResponseEntity<?> response = sendHttpPost(notificationString);
503
504         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
505         assertTrue(response.getBody() instanceof ProblemDetails);
506
507         final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
508         final String errorMessage = "An error occurred.  Authentication type "
509                 + subscriptionRequest.getAuthentication().getAuthType().toString() + " not currently supported.";
510
511         assertEquals(errorMessage, problemDetails.getDetail());
512     }
513
514     private PkgOnboardingNotification buildPkgOnboardingNotification() {
515         final PkgOnboardingNotification notification = new PkgOnboardingNotification();
516         notification.setId(NOTIFICATION_ID);
517         notification
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());
524         return notification;
525     }
526
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());
538         return notification;
539     }
540
541     private PkgmLinks buildPkgmLinks() {
542         final PkgmLinks pkgmLinks = new PkgmLinks();
543
544         final NOTIFICATIONLINKSERIALIZER subscriptionLinkSerializer = new NOTIFICATIONLINKSERIALIZER();
545         subscriptionLinkSerializer.setHref("subscription_href");
546         pkgmLinks.setSubscription(subscriptionLinkSerializer);
547
548         final NOTIFICATIONLINKSERIALIZER vnfPackageLinkSerializer = new NOTIFICATIONLINKSERIALIZER();
549         vnfPackageLinkSerializer.setHref("vnf_package_href");
550         pkgmLinks.setVnfPackage(vnfPackageLinkSerializer);
551
552         return pkgmLinks;
553     }
554
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;
561     }
562
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);
580         } else {
581             final SubscriptionsAuthenticationParamsBasic basicParams =
582                     new SubscriptionsAuthenticationParamsBasic().userName(USERNAME).password(PASSWORD);
583             subscriptionsAuthentication.setParamsBasic(basicParams);
584         }
585
586         return subscriptionsAuthentication;
587     }
588
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);
594     }
595
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);
600     }
601
602 }