2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.dmaap;
23 import static ch.qos.logback.classic.Level.WARN;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.verifyNoMoreInteractions;
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37 import com.google.gson.JsonObject;
39 import java.io.IOException;
40 import java.nio.charset.Charset;
41 import java.util.Optional;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
47 import org.onap.ccsdk.oran.a1policymanagementservice.dmaap.DmaapRequestMessage.Operation;
48 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.web.reactive.function.client.WebClientResponseException;
54 import reactor.core.publisher.Mono;
55 import reactor.test.StepVerifier;
57 class DmaapMessageHandlerTest {
58 private static final String URL = "url";
60 private final AsyncRestClient dmaapClient = mock(AsyncRestClient.class);
61 private final AsyncRestClient pmsClient = mock(AsyncRestClient.class);
62 private DmaapMessageHandler testedObject;
63 private Gson gson = new GsonBuilder().create(); //
66 private void setUp() throws Exception {
67 testedObject = spy(new DmaapMessageHandler(dmaapClient, pmsClient));
70 JsonObject payloadAsJson() {
71 return gson.fromJson(payloadAsString(), JsonObject.class);
74 String payloadAsString() {
75 return "{\"param\":\"value\"}";
78 DmaapRequestMessage dmaapRequestMessage(Operation operation) {
79 Optional<JsonObject> payload =
80 ((operation == Operation.PUT || operation == Operation.POST) ? Optional.of(payloadAsJson())
82 return ImmutableDmaapRequestMessage.builder() //
83 .apiVersion("apiVersion") //
84 .correlationId("correlationId") //
85 .operation(operation) //
86 .originatorId("originatorId") //
88 .requestId("requestId") //
90 .timestamp("timestamp") //
95 private Mono<ResponseEntity<String>> okResponse() {
96 ResponseEntity<String> entity = new ResponseEntity<>("OK", HttpStatus.OK);
97 return Mono.just(entity);
100 private Mono<ResponseEntity<String>> notOkResponse() {
101 ResponseEntity<String> entity = new ResponseEntity<>("NOK", HttpStatus.BAD_GATEWAY);
102 return Mono.just(entity);
106 void successfulDelete() throws IOException {
107 doReturn(okResponse()).when(pmsClient).deleteForEntity(anyString());
108 doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
110 DmaapRequestMessage message = dmaapRequestMessage(Operation.DELETE);
113 .create(testedObject.createTask(message)) //
114 .expectSubscription() //
116 .verifyComplete(); //
118 verify(pmsClient).deleteForEntity(URL);
119 verifyNoMoreInteractions(pmsClient);
121 verify(dmaapClient).post(anyString(), anyString());
123 verifyNoMoreInteractions(dmaapClient);
127 void successfulGet() throws IOException {
128 doReturn(okResponse()).when(pmsClient).getForEntity(anyString());
129 doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
131 DmaapRequestMessage message = dmaapRequestMessage(Operation.GET);
133 .create(testedObject.createTask(message)) //
134 .expectSubscription() //
136 .verifyComplete(); //
138 verify(pmsClient).getForEntity(URL);
139 verifyNoMoreInteractions(pmsClient);
141 verify(dmaapClient).post(anyString(), anyString());
142 verifyNoMoreInteractions(dmaapClient);
146 void exceptionFromPmsWhenGet_thenPostError() throws IOException {
147 String errorBody = "Unavailable";
148 WebClientResponseException webClientResponseException = new WebClientResponseException(
149 HttpStatus.SERVICE_UNAVAILABLE.value(), "", (HttpHeaders) null, errorBody.getBytes(), (Charset) null);
150 doReturn(Mono.error(webClientResponseException)).when(pmsClient).getForEntity(anyString());
151 doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
153 DmaapRequestMessage message = dmaapRequestMessage(Operation.GET);
155 .create(testedObject.createTask(message)) //
156 .expectSubscription() //
157 .verifyComplete(); //
159 ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
160 verify(dmaapClient).post(anyString(), captor.capture());
161 String actualMessage = captor.getValue();
162 assertThat(actualMessage).contains(HttpStatus.SERVICE_UNAVAILABLE.toString()) //
163 .contains(errorBody);
167 void successfulPut() throws IOException {
168 doReturn(okResponse()).when(pmsClient).putForEntity(anyString(), anyString());
169 doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
171 DmaapRequestMessage message = dmaapRequestMessage(Operation.PUT);
173 .create(testedObject.createTask(message)) //
174 .expectSubscription() //
176 .verifyComplete(); //
178 verify(pmsClient).putForEntity(URL, payloadAsString());
179 verifyNoMoreInteractions(pmsClient);
181 verify(dmaapClient).post(anyString(), anyString());
182 verifyNoMoreInteractions(dmaapClient);
186 void successfulPost() throws IOException {
187 doReturn(okResponse()).when(pmsClient).postForEntity(anyString(), anyString());
188 doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
190 DmaapRequestMessage message = dmaapRequestMessage(Operation.POST);
192 .create(testedObject.createTask(message)) //
193 .expectSubscription() //
195 .verifyComplete(); //
197 verify(pmsClient).postForEntity(URL, payloadAsString());
198 verifyNoMoreInteractions(pmsClient);
200 verify(dmaapClient).post(anyString(), anyString());
201 verifyNoMoreInteractions(dmaapClient);
205 void exceptionWhenCallingPms_thenErrorResponse() throws IOException {
207 doReturn(notOkResponse()).when(pmsClient).putForEntity(anyString(), anyString());
208 doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
210 DmaapRequestMessage message = dmaapRequestMessage(Operation.PUT);
211 testedObject.createTask(message).block();
213 verify(pmsClient).putForEntity(anyString(), anyString());
214 verifyNoMoreInteractions(pmsClient);
216 ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
217 verify(dmaapClient).post(anyString(), captor.capture());
218 String actualMessage = captor.getValue();
219 assertThat(actualMessage).as("Message \"%s\" sent to DMaaP contains %s", actualMessage, HttpStatus.BAD_GATEWAY)
220 .contains(HttpStatus.BAD_GATEWAY.toString());
222 verifyNoMoreInteractions(dmaapClient);
226 void putWithoutPayload_thenNotFoundResponseWithWarning() throws Exception {
227 DmaapRequestMessage message = ImmutableDmaapRequestMessage.builder() //
228 .apiVersion("apiVersion") //
229 .correlationId("correlationId") //
230 .operation(DmaapRequestMessage.Operation.PUT) //
231 .originatorId("originatorId") //
232 .payload(Optional.empty()) //
233 .requestId("requestId") //
235 .timestamp("timestamp") //
239 final ListAppender<ILoggingEvent> logAppender =
240 LoggingUtils.getLogListAppender(DmaapMessageHandler.class, WARN);
242 testedObject.handleDmaapMsg(message);
244 assertThat(logAppender.list.get(0).getFormattedMessage())
245 .startsWith("Expected payload in message from DMAAP: ");