2ec3664c2faa731bf4f043fbb9a96502551bc81f
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.dmaap;
22
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;
31
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
34
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37 import com.google.gson.JsonObject;
38
39 import java.io.IOException;
40 import java.nio.charset.Charset;
41
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.DisplayName;
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;
53
54 import reactor.core.publisher.Mono;
55 import reactor.test.StepVerifier;
56
57 class DmaapMessageHandlerTest {
58     private static final String URL = "url";
59
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(); //
64
65     @BeforeEach
66     private void setUp() throws Exception {
67         testedObject = spy(new DmaapMessageHandler(dmaapClient, pmsClient));
68     }
69
70     JsonObject payloadAsJson() {
71         return gson.fromJson(payloadAsString(), JsonObject.class);
72     }
73
74     String payloadAsString() {
75         return "{\"param\":\"value\"}";
76     }
77
78     DmaapRequestMessage dmaapRequestMessage(Operation operation) {
79         JsonObject payload = ((operation == Operation.PUT || operation == Operation.POST) ? payloadAsJson() : null);
80         return DmaapRequestMessage.builder() //
81                 .apiVersion("apiVersion") //
82                 .correlationId("correlationId") //
83                 .operation(operation) //
84                 .originatorId("originatorId") //
85                 .payload(payload) //
86                 .requestId("requestId") //
87                 .target("target") //
88                 .timestamp("timestamp") //
89                 .url(URL) //
90                 .build();
91     }
92
93     private Mono<ResponseEntity<String>> okResponse() {
94         ResponseEntity<String> entity = new ResponseEntity<>("OK", HttpStatus.OK);
95         return Mono.just(entity);
96     }
97
98     private Mono<ResponseEntity<String>> notOkResponse() {
99         ResponseEntity<String> entity = new ResponseEntity<>("NOK", HttpStatus.BAD_GATEWAY);
100         return Mono.just(entity);
101     }
102
103     @Test
104     @DisplayName("test successful Delete")
105     void successfulDelete() throws IOException {
106         doReturn(okResponse()).when(pmsClient).deleteForEntity(anyString());
107         doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
108
109         DmaapRequestMessage message = dmaapRequestMessage(Operation.DELETE);
110
111         StepVerifier //
112                 .create(testedObject.handleDmaapMsg(message)) //
113                 .expectSubscription() //
114                 .expectNext("OK") //
115                 .verifyComplete(); //
116
117         verify(pmsClient).deleteForEntity(URL);
118         verifyNoMoreInteractions(pmsClient);
119
120         verify(dmaapClient).post(anyString(), anyString());
121
122         verifyNoMoreInteractions(dmaapClient);
123     }
124
125     @Test
126     @DisplayName("test successful Get")
127     void successfulGet() throws IOException {
128         doReturn(okResponse()).when(pmsClient).getForEntity(anyString());
129         doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
130
131         DmaapRequestMessage message = dmaapRequestMessage(Operation.GET);
132         StepVerifier //
133                 .create(testedObject.handleDmaapMsg(message)) //
134                 .expectSubscription() //
135                 .expectNext("OK") //
136                 .verifyComplete(); //
137
138         verify(pmsClient).getForEntity(URL);
139         verifyNoMoreInteractions(pmsClient);
140
141         verify(dmaapClient).post(anyString(), anyString());
142         verifyNoMoreInteractions(dmaapClient);
143     }
144
145     @Test
146     @DisplayName("test exception From Pms When Get then Post Error")
147     void exceptionFromPmsWhenGet_thenPostError() throws IOException {
148         String errorBody = "Unavailable";
149         WebClientResponseException webClientResponseException = new WebClientResponseException(
150                 HttpStatus.SERVICE_UNAVAILABLE.value(), "", (HttpHeaders) null, errorBody.getBytes(), (Charset) null);
151         doReturn(Mono.error(webClientResponseException)).when(pmsClient).getForEntity(anyString());
152         doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
153
154         DmaapRequestMessage message = dmaapRequestMessage(Operation.GET);
155         StepVerifier //
156                 .create(testedObject.handleDmaapMsg(message)) //
157                 .expectSubscription() //
158                 .verifyComplete(); //
159
160         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
161         verify(dmaapClient).post(anyString(), captor.capture());
162         String actualMessage = captor.getValue();
163         assertThat(actualMessage).contains(HttpStatus.SERVICE_UNAVAILABLE.toString()) //
164                 .contains(errorBody);
165     }
166
167     @Test
168     @DisplayName("test successful Put")
169     void successfulPut() throws IOException {
170         doReturn(okResponse()).when(pmsClient).putForEntity(anyString(), anyString());
171         doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
172
173         DmaapRequestMessage message = dmaapRequestMessage(Operation.PUT);
174         StepVerifier //
175                 .create(testedObject.handleDmaapMsg(message)) //
176                 .expectSubscription() //
177                 .expectNext("OK") //
178                 .verifyComplete(); //
179
180         verify(pmsClient).putForEntity(URL, payloadAsString());
181         verifyNoMoreInteractions(pmsClient);
182
183         verify(dmaapClient).post(anyString(), anyString());
184         verifyNoMoreInteractions(dmaapClient);
185     }
186
187     @Test
188     @DisplayName("test successful Post")
189     void successfulPost() throws IOException {
190         doReturn(okResponse()).when(pmsClient).postForEntity(anyString(), anyString());
191         doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
192
193         DmaapRequestMessage message = dmaapRequestMessage(Operation.POST);
194         StepVerifier //
195                 .create(testedObject.handleDmaapMsg(message)) //
196                 .expectSubscription() //
197                 .expectNext("OK") //
198                 .verifyComplete(); //
199
200         verify(pmsClient).postForEntity(URL, payloadAsString());
201         verifyNoMoreInteractions(pmsClient);
202
203         verify(dmaapClient).post(anyString(), anyString());
204         verifyNoMoreInteractions(dmaapClient);
205     }
206
207     @Test
208     @DisplayName("test exception When Calling Pms then Error Response")
209     void exceptionWhenCallingPms_thenErrorResponse() throws IOException {
210
211         doReturn(notOkResponse()).when(pmsClient).putForEntity(anyString(), anyString());
212         doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString());
213
214         DmaapRequestMessage message = dmaapRequestMessage(Operation.PUT);
215         testedObject.handleDmaapMsg(message).block();
216
217         verify(pmsClient).putForEntity(anyString(), anyString());
218         verifyNoMoreInteractions(pmsClient);
219
220         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
221         verify(dmaapClient).post(anyString(), captor.capture());
222         String actualMessage = captor.getValue();
223         assertThat(actualMessage).as("Message \"%s\" sent to DMaaP contains %s", actualMessage, HttpStatus.BAD_GATEWAY)
224                 .contains(HttpStatus.BAD_GATEWAY.toString());
225
226         verifyNoMoreInteractions(dmaapClient);
227     }
228
229     @Test
230     @DisplayName("test put Without Payload then Not Found Response With Warning")
231     void putWithoutPayload_thenNotFoundResponseWithWarning() throws Exception {
232         DmaapRequestMessage message = DmaapRequestMessage.builder() //
233                 .apiVersion("apiVersion") //
234                 .correlationId("correlationId") //
235                 .operation(DmaapRequestMessage.Operation.PUT) //
236                 .originatorId("originatorId") //
237                 .payload(null) //
238                 .requestId("requestId") //
239                 .target("target") //
240                 .timestamp("timestamp") //
241                 .url(URL) //
242                 .build();
243
244         final ListAppender<ILoggingEvent> logAppender =
245                 LoggingUtils.getLogListAppender(DmaapMessageHandler.class, WARN);
246
247         doReturn(notOkResponse()).when(pmsClient).putForEntity(anyString(), anyString());
248         testedObject.handleDmaapMsg(message).block();
249
250         assertThat(logAppender.list.get(0).getFormattedMessage())
251                 .startsWith("Expected payload in message from DMAAP: ");
252     }
253
254 }