aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-sol003-adapter-pkgm/so-etsi-sol003-adapter-pkgm-service/src/test/java/org/onap/so/adapters/etsisol003adapter/pkgm/rest/EtsiSubscriptionNotificationControllerTest.java
blob: efe6891f1e5096f8ba329339465a381933aaf0d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.etsisol003adapter.pkgm.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.onap.so.adapters.etsi.sol003.adapter.common.CommonConstants.ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL;
import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_CLASS;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import java.net.URI;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.so.adapters.etsisol003adapter.etsicatalog.notification.model.NOTIFICATIONLINKSERIALIZER;
import org.onap.so.adapters.etsisol003adapter.etsicatalog.notification.model.PkgChangeNotification;
import org.onap.so.adapters.etsisol003adapter.etsicatalog.notification.model.PkgOnboardingNotification;
import org.onap.so.adapters.etsisol003adapter.etsicatalog.notification.model.PkgmLinks;
import org.onap.so.adapters.etsisol003adapter.pkgm.JSON;
import org.onap.so.adapters.etsisol003adapter.pkgm.PackageManagementConstants;
import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.ProblemDetails;
import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.vnfm.notification.model.VnfPackageChangeNotification;
import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.vnfm.notification.model.VnfPackageOnboardingNotification;
import org.onap.so.adapters.etsisol003adapter.pkgm.model.PkgmSubscriptionRequest;
import org.onap.so.adapters.etsisol003adapter.pkgm.model.SubscriptionsAuthentication;
import org.onap.so.adapters.etsisol003adapter.pkgm.model.SubscriptionsAuthenticationParamsBasic;
import org.onap.so.adapters.etsisol003adapter.pkgm.model.SubscriptionsAuthenticationParamsOauth2ClientCredentials;
import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import com.google.gson.Gson;

/**
 * @author Andrew Lamb (andrew.a.lamb@est.tech)
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@DirtiesContext(classMode = BEFORE_CLASS)
public class EtsiSubscriptionNotificationControllerTest {

    @LocalServerPort
    private int port;

    private static final URI CALLBACK_URI = URI.create("http://test_callback_uri/notification");
    private static final String TOKEN_ENDPOINT = "http://test_token_endpoint_uri/";
    private static final String TOKEN = "dXNlcm5hbWU6cGFzc3dvcmQ=......";
    private static final String JSON_TOKEN = "{\"access_token\":\"" + TOKEN + "\"}";
    private static final String LOCALHOST_URL = "http://localhost:";
    private static final String NOTIFICATION_BASE_URL =
            ETSI_SUBSCRIPTION_NOTIFICATION_CONTROLLER_BASE_URL + "/notification";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final String EXPECTED_BASIC_AUTHORIZATION = "Basic dXNlcm5hbWU6cGFzc3dvcmQ=";
    private static final String EXPECTED_OAUTH_AUTHORIZATION = "Bearer " + TOKEN;
    private static final String NOTIFICATION_ID = "NOTIFICATION_ID";
    private static final String SUBSCRIPTION_ID = "SUBSCRIPTION_ID";
    private static final String TIME_STAMP_STRING_EXPECTED_FROM_ETSI_CATALOG = "2020-01-01 01:01:01";
    private static final java.time.LocalDateTime TIMESTAMP = java.time.LocalDateTime.of(2020, 1, 1, 1, 1, 1, 1);
    private static final String VNFPKG_ID = UUID.randomUUID().toString();
    private static final String VNFD_ID = UUID.randomUUID().toString();
    private static final String EXPECTED_VNF_PACKAGE_HREF =
            "https://so-vnfm-adapter.onap:30406/so/vnfm-adapter/v1/vnfpkgm/v1/vnf_packages/" + VNFPKG_ID;
    private static final String EXPECTED_SUBSCRIPTION_HREF =
            "https://so-vnfm-adapter.onap:30406/so/vnfm-adapter/v1/vnfpkgm/v1/subscriptions/" + SUBSCRIPTION_ID;


    private BasicHttpHeadersProvider basicHttpHeadersProvider;

    @Autowired
    @Qualifier(CONFIGURABLE_REST_TEMPLATE)
    private RestTemplate restTemplate;
    private MockRestServiceServer mockRestServiceServer;

    private TestRestTemplate testRestTemplate;

    @Autowired
    private CacheManager cacheServiceProvider;
    private Cache cache;

    @Before
    public void setUp() {
        mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
        basicHttpHeadersProvider = new BasicHttpHeadersProvider();
        cache = cacheServiceProvider.getCache(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE);
        cache.clear();

        final Gson gson = JSON.createGson().registerTypeAdapter(LocalDateTime.class,
                new EtsiSubscriptionNotificationController.LocalDateTimeTypeAdapter()).create();
        testRestTemplate = new TestRestTemplate(
                new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
    }


    @After
    public void tearDown() {
        mockRestServiceServer.reset();
        cache.clear();
    }

    @Test
    public void testSubscriptionNotificationEndPoint_ReturnsNoContent() {
        final ResponseEntity<?> response = sendHttpGet(NOTIFICATION_BASE_URL);
        assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
    }

    @Test
    public void testOnboardingNotificationSentOnToVnfmCallbackUri_SubscriptionRequestInCache_Success() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
                .andExpect(jsonPath("$.notificationType")
                        .value(VnfPackageOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION
                                .toString()))
                .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
                .andExpect(jsonPath("$.timeStamp").value(TIME_STAMP_STRING_EXPECTED_FROM_ETSI_CATALOG))
                .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID)).andExpect(jsonPath("$.vnfdId").value(VNFD_ID))
                .andExpect(jsonPath("$._links")
                        .value(buildPkgmLinks(EXPECTED_VNF_PACKAGE_HREF, EXPECTED_SUBSCRIPTION_HREF)))
                .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
    }

    @Test
    public void testOnboardingNotificationNotSentOnToVnfmCallbackUri_SubscriptionRequestNotInCache_Fail() {
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();
        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "No subscription found with subscriptionId " + SUBSCRIPTION_ID
                + ". Unable to forward notification to subscriber.";

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testOnboardingNotificationSentOnToVnfmCallbackUri_BadRequestResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.BAD_REQUEST));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.BAD_REQUEST + ".\n" + "No result found for given url: " + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testOnboardingNotificationSentOnToVnfmCallbackUri_301MovedPermanentlyResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.MOVED_PERMANENTLY));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed.";

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testOnboardingNotificationSentOnToVnfmCallbackUri_NotFoundResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.NOT_FOUND));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.NOT_FOUND + ".\n" + "No result found for given url: " + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testOnboardingNotificationSentOnToVnfmCallbackUri_InternalServerErrorResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.INTERNAL_SERVER_ERROR.value() + ".\n" + "Unable to invoke HTTP POST using URL: "
                + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testChangeNotificationSentOnToVnfmCallbackUri_SubscriptionRequestInCache_Success() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
                .andExpect(jsonPath("$.notificationType").value(
                        VnfPackageChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION.getValue()))
                .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
                .andExpect(jsonPath("$.timeStamp").value(TIME_STAMP_STRING_EXPECTED_FROM_ETSI_CATALOG))
                .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID)).andExpect(jsonPath("$.vnfdId").value(VNFD_ID))
                .andExpect(
                        jsonPath("$.changeType").value(PkgChangeNotification.ChangeTypeEnum.OP_STATE_CHANGE.toString()))
                .andExpect(jsonPath("$.operationalState")
                        .value(PkgChangeNotification.OperationalStateEnum.ENABLED.toString()))
                .andExpect(jsonPath("$._links")
                        .value(buildPkgmLinks(EXPECTED_VNF_PACKAGE_HREF, EXPECTED_SUBSCRIPTION_HREF)))
                .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
    }

    @Test
    public void testChangeNotificationNotSentOnToVnfmCallbackUri_SubscriptionRequestNotInCache_Fail() {
        final PkgChangeNotification notification = buildPkgChangeNotification();
        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "No subscription found with subscriptionId " + SUBSCRIPTION_ID
                + ". Unable to forward notification to subscriber.";

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testChangeNotificationSentOnToVnfmCallbackUri_BadRequestResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.BAD_REQUEST));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.BAD_REQUEST + ".\n" + "No result found for given url: " + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testChangeNotificationSentOnToVnfmCallbackUri_NotFoundResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.NOT_FOUND));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.NOT_FOUND + ".\n" + "No result found for given url: " + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testChangeNotificationSentOnToVnfmCallbackUri_InternalServerErrorResponseFromCallbackUri_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.INTERNAL_SERVER_ERROR.value() + ".\n" + "Unable to invoke HTTP POST using URL: "
                + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testNotificationSentOnToVnfm_BasicAuthUserPasswordAuthorized_Success() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgOnboardingNotification notification = buildPkgOnboardingNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
                .andExpect(jsonPath("$.notificationType")
                        .value(VnfPackageOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION
                                .toString()))
                .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
                .andExpect(jsonPath("$.timeStamp").value(TIME_STAMP_STRING_EXPECTED_FROM_ETSI_CATALOG))
                .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID)).andExpect(jsonPath("$.vnfdId").value(VNFD_ID))
                .andExpect(jsonPath("$._links")
                        .value(buildPkgmLinks(EXPECTED_VNF_PACKAGE_HREF, EXPECTED_SUBSCRIPTION_HREF)))
                .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
    }

    @Test
    public void testNotificationSentOnToVnfm_BasicAuthUserPasswordNotAuthorized_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.BASIC);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION))
                .andRespond(withStatus(HttpStatus.UNAUTHORIZED));

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Sending of notification to VNFM failed with response: "
                + HttpStatus.UNAUTHORIZED.value() + ".\n" + "Unable to invoke HTTP POST using URL: " + CALLBACK_URI;

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testNotificationSentOnToVnfm_OAuthAuthorized_Success() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(TOKEN_ENDPOINT)).andExpect(method(HttpMethod.POST))
                .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION))
                .andRespond(withSuccess(JSON_TOKEN, MediaType.APPLICATION_JSON));

        mockRestServiceServer.expect(requestTo(CALLBACK_URI)).andExpect(method(HttpMethod.POST))
                .andExpect(header("Authorization", EXPECTED_OAUTH_AUTHORIZATION))
                .andExpect(jsonPath("$.id").value(NOTIFICATION_ID))
                .andExpect(jsonPath("$.notificationType").value(
                        VnfPackageChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION.toString()))
                .andExpect(jsonPath("$.subscriptionId").value(SUBSCRIPTION_ID))
                .andExpect(jsonPath("$.timeStamp").value(TIME_STAMP_STRING_EXPECTED_FROM_ETSI_CATALOG))
                .andExpect(jsonPath("$.vnfPkgId").value(VNFPKG_ID)).andExpect(jsonPath("$.vnfdId").value(VNFD_ID))
                .andExpect(
                        jsonPath("$.changeType").value(PkgChangeNotification.ChangeTypeEnum.OP_STATE_CHANGE.toString()))
                .andExpect(jsonPath("$.operationalState")
                        .value(PkgChangeNotification.OperationalStateEnum.ENABLED.toString()))
                .andExpect(jsonPath("$._links")
                        .value(buildPkgmLinks(EXPECTED_VNF_PACKAGE_HREF, EXPECTED_SUBSCRIPTION_HREF)))
                .andRespond(withSuccess());

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
    }

    @Test
    public void testNotificationSentOnToVnfm_OAuthTokenNotReceived_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        mockRestServiceServer.expect(requestTo(TOKEN_ENDPOINT)).andExpect(method(HttpMethod.POST))
                .andExpect(header("Authorization", EXPECTED_BASIC_AUTHORIZATION)).andRespond(withSuccess());

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Unable to retrieve OAuth Token from VNFM for notification.";

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    @Test
    public void testNotificationSentOnToVnfm_TLSCertNotYetSupported_Fail() {
        final PkgmSubscriptionRequest subscriptionRequest =
                buildPkgmSubscriptionRequest(SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT);
        cache.put(SUBSCRIPTION_ID, subscriptionRequest);
        final PkgChangeNotification notification = buildPkgChangeNotification();

        final ResponseEntity<?> response = sendHttpPost(notification);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
        assertTrue(response.getBody() instanceof ProblemDetails);

        final ProblemDetails problemDetails = (ProblemDetails) response.getBody();
        final String errorMessage = "An error occurred.  Authentication type "
                + subscriptionRequest.getAuthentication().getAuthType().toString() + " not currently supported.";

        assertEquals(errorMessage, problemDetails.getDetail());
    }

    private PkgOnboardingNotification buildPkgOnboardingNotification() {
        final PkgOnboardingNotification notification = new PkgOnboardingNotification();
        notification.setId(NOTIFICATION_ID);
        notification
                .setNotificationType(PkgOnboardingNotification.NotificationTypeEnum.VNFPACKAGEONBOARDINGNOTIFICATION);
        notification.setSubscriptionId(SUBSCRIPTION_ID);
        notification.setTimeStamp(TIMESTAMP);
        notification.setVnfPkgId(VNFPKG_ID);
        notification.setVnfdId(VNFD_ID);
        notification.setLinks(buildPkgmLinks());
        return notification;
    }

    private PkgChangeNotification buildPkgChangeNotification() {
        final PkgChangeNotification notification = new PkgChangeNotification();
        notification.setId(NOTIFICATION_ID);
        notification.setNotificationType(PkgChangeNotification.NotificationTypeEnum.VNFPACKAGECHANGENOTIFICATION);
        notification.setSubscriptionId(SUBSCRIPTION_ID);
        notification.setTimeStamp(TIMESTAMP);
        notification.setVnfPkgId(VNFPKG_ID);
        notification.setVnfdId(VNFD_ID);
        notification.setChangeType(PkgChangeNotification.ChangeTypeEnum.OP_STATE_CHANGE);
        notification.setOperationalState(PkgChangeNotification.OperationalStateEnum.ENABLED);
        notification.setLinks(buildPkgmLinks());
        return notification;
    }

    private PkgmLinks buildPkgmLinks() {
        return buildPkgmLinks("vnf_package_href", "subscription_href");
    }

    private PkgmLinks buildPkgmLinks(final String vnfPkgHref, final String subscriptionHref) {
        return new PkgmLinks().vnfPackage(new NOTIFICATIONLINKSERIALIZER().href(vnfPkgHref))
                .subscription(new NOTIFICATIONLINKSERIALIZER().href(subscriptionHref));
    }

    private PkgmSubscriptionRequest buildPkgmSubscriptionRequest(
            final SubscriptionsAuthentication.AuthTypeEnum authTypeEnum) {
        final PkgmSubscriptionRequest subscriptionRequest = new PkgmSubscriptionRequest();
        subscriptionRequest.setCallbackUri(CALLBACK_URI.toString());
        subscriptionRequest.setAuthentication(buildSubscriptionsAuthentication(authTypeEnum));
        return subscriptionRequest;
    }

    // TODO update for auth types other than basicAuth
    private SubscriptionsAuthentication buildSubscriptionsAuthentication(
            final SubscriptionsAuthentication.AuthTypeEnum authTypeEnum) {
        final SubscriptionsAuthentication subscriptionsAuthentication = new SubscriptionsAuthentication();
        final List<SubscriptionsAuthentication.AuthTypeEnum> authTypes = new ArrayList<>();
        authTypes.add(authTypeEnum);
        subscriptionsAuthentication.setAuthType(authTypes);
        if (authTypeEnum == SubscriptionsAuthentication.AuthTypeEnum.TLS_CERT) {
            // TODO: remove basic params and code for TLS
            final SubscriptionsAuthenticationParamsBasic basicParams =
                    new SubscriptionsAuthenticationParamsBasic().userName(USERNAME).password(PASSWORD);
            subscriptionsAuthentication.setParamsBasic(basicParams);
        } else if (authTypeEnum == SubscriptionsAuthentication.AuthTypeEnum.OAUTH2_CLIENT_CREDENTIALS) {
            final SubscriptionsAuthenticationParamsOauth2ClientCredentials oathParams =
                    new SubscriptionsAuthenticationParamsOauth2ClientCredentials().clientId(USERNAME)
                            .clientPassword(PASSWORD).tokenEndpoint(TOKEN_ENDPOINT);
            subscriptionsAuthentication.setParamsOauth2ClientCredentials(oathParams);
        } else {
            final SubscriptionsAuthenticationParamsBasic basicParams =
                    new SubscriptionsAuthenticationParamsBasic().userName(USERNAME).password(PASSWORD);
            subscriptionsAuthentication.setParamsBasic(basicParams);
        }

        return subscriptionsAuthentication;
    }

    private <T> ResponseEntity<ProblemDetails> sendHttpPost(final T notification) {
        final String testURL = LOCALHOST_URL + port + NOTIFICATION_BASE_URL;
        final HttpEntity<?> request = new HttpEntity<>(notification, basicHttpHeadersProvider.getHttpHeaders());
        return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.POST, request,
                ProblemDetails.class);
    }

    private ResponseEntity<Void> sendHttpGet(final String url) {
        final String testURL = LOCALHOST_URL + port + url;
        final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
        return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, Void.class);
    }

}