2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.oransc.policyagent.dmaap;
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
34 import java.io.IOException;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.dmaap.mr.client.MRBatchingPublisher;
39 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
40 import org.oransc.policyagent.clients.AsyncRestClient;
41 import org.oransc.policyagent.configuration.ApplicationConfig;
42 import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
47 public class DmaapMessageHandlerTest {
49 private static final String URL = "url";
50 private static final String PAYLOAD = "payload";
52 private ApplicationConfig appConfig = mock(ApplicationConfig.class);
53 private final MRBatchingPublisher dmaapClient = mock(MRBatchingPublisher.class);
54 private final AsyncRestClient agentClient = mock(AsyncRestClient.class);
55 private DmaapMessageHandler testedObject;
56 private static Gson gson = new GsonBuilder() //
61 private void setUp() throws Exception {
62 testedObject = spy(new DmaapMessageHandler(dmaapClient, agentClient));
65 DmaapRequestMessage dmaapRequestMessage(Operation operation) {
66 return ImmutableDmaapRequestMessage.builder().apiVersion("apiVersion") //
67 .correlationId("correlationId") //
68 .operation(operation) //
69 .originatorId("originatorId") //
71 .requestId("requestId") //
73 .timestamp("timestamp") //
79 private String dmaapInputMessage(Operation operation) {
80 return gson.toJson(dmaapRequestMessage(operation));
84 public void successfulDelete() throws IOException {
85 doReturn(Mono.just("OK")).when(agentClient).delete(anyString());
86 doReturn(1).when(dmaapClient).send(anyString());
87 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
90 .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
91 .expectSubscription() //
95 verify(agentClient, times(1)).delete(URL);
96 verifyNoMoreInteractions(agentClient);
98 verify(dmaapClient, times(1)).send(anyString());
99 verify(dmaapClient, times(1)).sendBatchWithResponse();
100 verifyNoMoreInteractions(dmaapClient);
104 public void successfulGet() throws IOException {
105 doReturn(Mono.just("OK")).when(agentClient).get(anyString());
106 doReturn(1).when(dmaapClient).send(anyString());
107 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
110 .create(testedObject.createTask(dmaapInputMessage(Operation.GET))) //
111 .expectSubscription() //
113 .verifyComplete(); //
115 verify(agentClient, times(1)).get(URL);
116 verifyNoMoreInteractions(agentClient);
118 verify(dmaapClient, times(1)).send(anyString());
119 verify(dmaapClient, times(1)).sendBatchWithResponse();
120 verifyNoMoreInteractions(dmaapClient);
124 public void successfulPut() throws IOException {
125 doReturn(Mono.just("OK")).when(agentClient).put(anyString(), anyString());
126 doReturn(1).when(dmaapClient).send(anyString());
127 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
130 .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
131 .expectSubscription() //
133 .verifyComplete(); //
135 verify(agentClient, times(1)).put(URL, PAYLOAD);
136 verifyNoMoreInteractions(agentClient);
138 verify(dmaapClient, times(1)).send(anyString());
139 verify(dmaapClient, times(1)).sendBatchWithResponse();
140 verifyNoMoreInteractions(dmaapClient);
144 public void successfulPost() throws IOException {
145 doReturn(Mono.just("OK")).when(agentClient).post(anyString(), anyString());
146 doReturn(1).when(dmaapClient).send(anyString());
147 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
150 .create(testedObject.createTask(dmaapInputMessage(Operation.POST))) //
151 .expectSubscription() //
153 .verifyComplete(); //
155 verify(agentClient, times(1)).post(URL, PAYLOAD);
156 verifyNoMoreInteractions(agentClient);
158 verify(dmaapClient, times(1)).send(anyString());
159 verify(dmaapClient, times(1)).sendBatchWithResponse();
160 verifyNoMoreInteractions(dmaapClient);
164 public void errorCase() throws IOException {
165 doReturn(Mono.error(new Exception("Refused"))).when(agentClient).put(anyString(), anyString());
166 doReturn(1).when(dmaapClient).send(anyString());
167 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
169 .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
170 .expectSubscription() //
171 .verifyComplete(); //
173 verify(agentClient, times(1)).put(URL, PAYLOAD);
174 verifyNoMoreInteractions(agentClient);
177 verify(dmaapClient, times(1)).send(anyString());
178 verify(dmaapClient, times(1)).sendBatchWithResponse();
179 verifyNoMoreInteractions(dmaapClient);