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.svnfm.simulator.controllers;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse201;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter1;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.notification.model.VnfPackageOnboardingNotification;
33 import org.onap.so.svnfm.simulator.config.SvnfmApplication;
34 import org.onap.so.svnfm.simulator.controller.SubscriptionNotificationController;
35 import org.slf4j.Logger;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.web.client.TestRestTemplate;
40 import org.springframework.boot.web.client.RestTemplateBuilder;
41 import org.springframework.boot.web.server.LocalServerPort;
42 import org.springframework.http.HttpEntity;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.http.converter.json.GsonHttpMessageConverter;
48 import org.springframework.test.context.ActiveProfiles;
49 import org.springframework.test.context.junit4.SpringRunner;
50 import org.springframework.test.web.client.MockRestServiceServer;
51 import org.springframework.web.client.RestTemplate;
52 import java.time.LocalDateTime;
54 import static org.junit.Assert.assertEquals;
55 import static org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter.NotificationTypesEnum.VNFPACKAGECHANGENOTIFICATION;
56 import static org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter.NotificationTypesEnum.VNFPACKAGEONBOARDINGNOTIFICATION;
57 import static org.onap.so.svnfm.simulator.config.SslBasedRestTemplateConfiguration.SSL_BASED_CONFIGURABLE_REST_TEMPLATE;
58 import static org.onap.so.svnfm.simulator.constants.Constant.NOTIFICATION_ENDPOINT;
59 import static org.onap.so.svnfm.simulator.constants.Constant.PACKAGE_MANAGEMENT_BASE_URL;
60 import static org.onap.so.svnfm.simulator.constants.Constant.SUBSCRIPTION_ENDPOINT;
61 import static org.onap.so.svnfm.simulator.constants.Constant.VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT;
62 import static org.onap.so.svnfm.simulator.utils.TestUtils.getBaseUrl;
63 import static org.onap.so.svnfm.simulator.utils.TestUtils.getBasicAuth;
64 import static org.onap.so.svnfm.simulator.utils.TestUtils.getHttpHeaders;
65 import static org.onap.so.svnfm.simulator.utils.TestUtils.getNotification;
66 import static org.onap.so.svnfm.simulator.utils.TestUtils.getSubscriptionRequest;
67 import static org.slf4j.LoggerFactory.getLogger;
68 import static org.springframework.http.MediaType.APPLICATION_JSON;
69 import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
70 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
71 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
72 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
75 * @author Andrew Lamb (andrew.a.lamb@est.tech)
77 @RunWith(SpringRunner.class)
78 @SpringBootTest(classes = SvnfmApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
79 @ActiveProfiles("test")
80 public class TestSubscriptionNotificationController {
82 private static final Logger LOGGER = getLogger(TestSubscriptionNotificationController.class);
83 private static final String SOL003_SUBSCRIPTION_URL = "http://so-vnfm-adapter.onap:9092" + VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT;
88 @Autowired @Qualifier(SSL_BASED_CONFIGURABLE_REST_TEMPLATE)
89 private RestTemplate restTemplate;
90 private MockRestServiceServer mockRestServiceServer;
92 private TestRestTemplate testRestTemplate;
95 private String vnfmSimulatorCallbackUrl;
99 mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
100 gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class,
101 new SubscriptionNotificationController.LocalDateTimeTypeAdapter()).create();
102 vnfmSimulatorCallbackUrl = getBaseUrl(port) + PACKAGE_MANAGEMENT_BASE_URL + NOTIFICATION_ENDPOINT;
103 testRestTemplate = new TestRestTemplate(
104 new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
108 public void teardown() {
109 mockRestServiceServer.reset();
113 public void testGetFromEndpoint_Success() {
114 final ResponseEntity<?> responseEntity = checkGetFromTestEndpoint();
115 assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
119 public void testPostOnboardingSubscriptionRequest_Success() throws Exception {
120 final PkgmSubscriptionRequest pkgmSubscriptionRequest =
121 gson.fromJson(getSubscriptionRequest(VNFPACKAGEONBOARDINGNOTIFICATION), PkgmSubscriptionRequest.class);
122 pkgmSubscriptionRequest.setCallbackUri(vnfmSimulatorCallbackUrl);
123 final InlineResponse201 inlineResponse =
124 new InlineResponse201().id("subscriptionId").filter(new SubscriptionsFilter1())
125 .callbackUri("callbackUri");
127 mockRestServiceServer.expect(requestTo(SOL003_SUBSCRIPTION_URL)).andExpect(method(HttpMethod.POST))
128 .andExpect(content().json(gson.toJson(pkgmSubscriptionRequest)))
129 .andRespond(withSuccess(gson.toJson(inlineResponse), APPLICATION_JSON));
131 final ResponseEntity<?> responseEntity = postSubscriptionRequest(pkgmSubscriptionRequest);
132 final InlineResponse201 responseEntityBody = (InlineResponse201) responseEntity.getBody();
133 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
134 assertEquals(inlineResponse, responseEntityBody);
138 public void testPostChangeSubscriptionRequest_Success() throws Exception {
139 final PkgmSubscriptionRequest pkgmSubscriptionRequest =
140 gson.fromJson(getSubscriptionRequest(VNFPACKAGECHANGENOTIFICATION), PkgmSubscriptionRequest.class);
141 pkgmSubscriptionRequest.setCallbackUri(vnfmSimulatorCallbackUrl);
142 final InlineResponse201 inlineResponse =
143 new InlineResponse201().id("subscriptionId").filter(new SubscriptionsFilter1())
144 .callbackUri("callbackUri");
146 mockRestServiceServer.expect(requestTo(SOL003_SUBSCRIPTION_URL)).andExpect(method(HttpMethod.POST))
147 .andExpect(content().json(gson.toJson(pkgmSubscriptionRequest)))
148 .andRespond(withSuccess(gson.toJson(inlineResponse), APPLICATION_JSON));
150 final ResponseEntity<?> responseEntity = postSubscriptionRequest(pkgmSubscriptionRequest);
151 final InlineResponse201 responseEntityBody = (InlineResponse201) responseEntity.getBody();
152 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
153 assertEquals(inlineResponse, responseEntityBody);
157 public void testNotificationEndpoint_Success() throws Exception {
158 final VnfPackageOnboardingNotification vnfPackageOnboardingNotification =
159 gson.fromJson(getNotification(VNFPACKAGEONBOARDINGNOTIFICATION),
160 VnfPackageOnboardingNotification.class);
161 final ResponseEntity<?> responseEntity = postNotification(vnfPackageOnboardingNotification);
162 assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
165 private ResponseEntity<Void> checkGetFromTestEndpoint() {
166 LOGGER.info("checkGetFromTestEndpoint() method...");
167 final String vnfmSimulatorTestEndpoint = getBaseUrl(port) + PACKAGE_MANAGEMENT_BASE_URL;
168 final String authHeader = getBasicAuth("vnfm", "password1$");
169 final HttpHeaders headers = getHttpHeaders(authHeader);
170 final HttpEntity<?> request = new HttpEntity<>(headers);
172 LOGGER.info("sending request {} to: {}", request, vnfmSimulatorTestEndpoint);
173 return testRestTemplate.exchange(vnfmSimulatorTestEndpoint, HttpMethod.GET, request, Void.class);
176 private ResponseEntity<?> postSubscriptionRequest(final PkgmSubscriptionRequest subscriptionRequest) {
177 LOGGER.info("postSubscriptionRequest() method...");
178 final String vnfmSimulatorSubscribeEndpoint =
179 getBaseUrl(port) + PACKAGE_MANAGEMENT_BASE_URL + SUBSCRIPTION_ENDPOINT;
180 final String authHeader = getBasicAuth("vnfm", "password1$");
181 final HttpHeaders headers = getHttpHeaders(authHeader);
182 final HttpEntity<?> request = new HttpEntity<>(subscriptionRequest, headers);
184 LOGGER.info("sending request {} to: {}", request, vnfmSimulatorSubscribeEndpoint);
185 return testRestTemplate
186 .exchange(vnfmSimulatorSubscribeEndpoint, HttpMethod.POST, request, InlineResponse201.class);
189 private ResponseEntity<?> postNotification(final Object notification) {
190 LOGGER.info("postNotification method...");
191 final String authHeader = getBasicAuth("vnfm", "password1$");
192 final HttpHeaders headers = getHttpHeaders(authHeader);
193 final HttpEntity<?> request = new HttpEntity<>(notification, headers);
195 LOGGER.info("sending request {} to: {}", request, vnfmSimulatorCallbackUrl);
196 return testRestTemplate.exchange(vnfmSimulatorCallbackUrl, HttpMethod.POST, request, Void.class);