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.slf4j.Logger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.web.client.TestRestTemplate;
39 import org.springframework.boot.web.server.LocalServerPort;
40 import org.springframework.http.HttpEntity;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.test.context.ActiveProfiles;
46 import org.springframework.test.context.junit4.SpringRunner;
47 import org.springframework.test.web.client.MockRestServiceServer;
48 import org.springframework.web.client.RestTemplate;
49 import java.time.LocalDateTime;
51 import static org.junit.Assert.assertEquals;
52 import static org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter.NotificationTypesEnum.VNFPACKAGECHANGENOTIFICATION;
53 import static org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsFilter.NotificationTypesEnum.VNFPACKAGEONBOARDINGNOTIFICATION;
54 import static org.onap.so.svnfm.simulator.config.SslBasedRestTemplateConfiguration.SSL_BASED_CONFIGURABLE_REST_TEMPLATE;
55 import static org.onap.so.svnfm.simulator.constants.Constant.NOTIFICATION_ENDPOINT;
56 import static org.onap.so.svnfm.simulator.constants.Constant.PACKAGE_MANAGEMENT_BASE_URL;
57 import static org.onap.so.svnfm.simulator.constants.Constant.SUBSCRIPTION_ENDPOINT;
58 import static org.onap.so.svnfm.simulator.constants.Constant.VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT;
59 import static org.onap.so.svnfm.simulator.utils.TestUtils.getBaseUrl;
60 import static org.onap.so.svnfm.simulator.utils.TestUtils.getBasicAuth;
61 import static org.onap.so.svnfm.simulator.utils.TestUtils.getHttpHeaders;
62 import static org.onap.so.svnfm.simulator.utils.TestUtils.getNotification;
63 import static org.onap.so.svnfm.simulator.utils.TestUtils.getSubscriptionRequest;
64 import static org.slf4j.LoggerFactory.getLogger;
65 import static org.springframework.http.MediaType.APPLICATION_JSON;
66 import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
67 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
68 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
69 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
72 * @author Andrew Lamb (andrew.a.lamb@est.tech)
74 @RunWith(SpringRunner.class)
75 @SpringBootTest(classes = SvnfmApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
76 @ActiveProfiles("test")
77 public class TestSubscriptionNotificationController {
79 private static final Logger LOGGER = getLogger(TestSubscriptionNotificationController.class);
84 @Autowired @Qualifier(SSL_BASED_CONFIGURABLE_REST_TEMPLATE)
85 private RestTemplate restTemplate;
86 private MockRestServiceServer mockRestServiceServer;
89 private TestRestTemplate testRestTemplate;
92 private String vnfmSimulatorCallbackUrl;
96 mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
97 gson = new GsonBuilder().create();
98 vnfmSimulatorCallbackUrl = getBaseUrl(port) + PACKAGE_MANAGEMENT_BASE_URL + NOTIFICATION_ENDPOINT;
102 public void teardown() {
103 mockRestServiceServer.reset();
107 public void testGetFromEndpoint_Success() {
108 final ResponseEntity<?> responseEntity = checkGetFromTestEndpoint();
109 assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
113 public void testPostOnboardingSubscriptionRequest_Success() throws Exception {
114 final PkgmSubscriptionRequest pkgmSubscriptionRequest =
115 gson.fromJson(getSubscriptionRequest(VNFPACKAGEONBOARDINGNOTIFICATION), PkgmSubscriptionRequest.class);
116 pkgmSubscriptionRequest.setCallbackUri(vnfmSimulatorCallbackUrl);
117 final InlineResponse201 inlineResponse =
118 new InlineResponse201().id("subscriptionId").filter(new SubscriptionsFilter1())
119 .callbackUri("callbackUri");
121 mockRestServiceServer.expect(requestTo(VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT)).andExpect(method(HttpMethod.POST))
122 .andExpect(content().json(gson.toJson(pkgmSubscriptionRequest)))
123 .andRespond(withSuccess(gson.toJson(inlineResponse), APPLICATION_JSON));
125 final ResponseEntity<?> responseEntity = postSubscriptionRequest(pkgmSubscriptionRequest);
126 final InlineResponse201 responseEntityBody = (InlineResponse201) responseEntity.getBody();
127 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
128 assertEquals(inlineResponse, responseEntityBody);
132 public void testPostChangeSubscriptionRequest_Success() throws Exception {
133 final PkgmSubscriptionRequest pkgmSubscriptionRequest =
134 gson.fromJson(getSubscriptionRequest(VNFPACKAGECHANGENOTIFICATION), PkgmSubscriptionRequest.class);
135 pkgmSubscriptionRequest.setCallbackUri(vnfmSimulatorCallbackUrl);
136 final InlineResponse201 inlineResponse =
137 new InlineResponse201().id("subscriptionId").filter(new SubscriptionsFilter1())
138 .callbackUri("callbackUri");
140 mockRestServiceServer.expect(requestTo(VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT)).andExpect(method(HttpMethod.POST))
141 .andExpect(content().json(gson.toJson(pkgmSubscriptionRequest)))
142 .andRespond(withSuccess(gson.toJson(inlineResponse), APPLICATION_JSON));
144 final ResponseEntity<?> responseEntity = postSubscriptionRequest(pkgmSubscriptionRequest);
145 final InlineResponse201 responseEntityBody = (InlineResponse201) responseEntity.getBody();
146 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
147 assertEquals(inlineResponse, responseEntityBody);
151 public void testNotificationEndpoint_Success() throws Exception {
152 final VnfPackageOnboardingNotification vnfPackageOnboardingNotification =
153 gson.fromJson(getNotification(VNFPACKAGEONBOARDINGNOTIFICATION),
154 VnfPackageOnboardingNotification.class);
155 final LocalDateTime timestamp = LocalDateTime.of(2020, 1, 1, 1, 1, 1, 1);
156 vnfPackageOnboardingNotification.setTimeStamp(timestamp);
157 final ResponseEntity<?> responseEntity = postNotification(vnfPackageOnboardingNotification);
158 assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
161 private ResponseEntity<Void> checkGetFromTestEndpoint() {
162 LOGGER.info("checkGetFromTestEndpoint() method...");
163 final String vnfmSimulatorTestEndpoint = getBaseUrl(port) + PACKAGE_MANAGEMENT_BASE_URL;
164 final String authHeader = getBasicAuth("vnfm", "password1$");
165 final HttpHeaders headers = getHttpHeaders(authHeader);
166 final HttpEntity<?> request = new HttpEntity<>(headers);
168 LOGGER.info("sending request {} to: {}", request, vnfmSimulatorTestEndpoint);
169 return testRestTemplate.exchange(vnfmSimulatorTestEndpoint, HttpMethod.GET, request, Void.class);
172 private ResponseEntity<?> postSubscriptionRequest(final PkgmSubscriptionRequest subscriptionRequest) {
173 LOGGER.info("postSubscriptionRequest() method...");
174 final String vnfmSimulatorSubscribeEndpoint =
175 getBaseUrl(port) + PACKAGE_MANAGEMENT_BASE_URL + SUBSCRIPTION_ENDPOINT;
176 final String authHeader = getBasicAuth("vnfm", "password1$");
177 final HttpHeaders headers = getHttpHeaders(authHeader);
178 final HttpEntity<?> request = new HttpEntity<>(subscriptionRequest, headers);
180 LOGGER.info("sending request {} to: {}", request, vnfmSimulatorSubscribeEndpoint);
181 return testRestTemplate
182 .exchange(vnfmSimulatorSubscribeEndpoint, HttpMethod.POST, request, InlineResponse201.class);
185 private ResponseEntity<?> postNotification(final Object notification) {
186 LOGGER.info("postNotification method...");
187 final String authHeader = getBasicAuth("vnfm", "password1$");
188 final HttpHeaders headers = getHttpHeaders(authHeader);
189 final HttpEntity<?> request = new HttpEntity<>(notification, headers);
191 LOGGER.info("sending request {} to: {}", request, vnfmSimulatorCallbackUrl);
192 return testRestTemplate.exchange(vnfmSimulatorCallbackUrl, HttpMethod.POST, request, Void.class);