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;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import java.io.IOException;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dmaap.mr.client.MRBatchingPublisher;
36 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
37 import org.oransc.policyagent.clients.AsyncRestClient;
38 import org.oransc.policyagent.configuration.ApplicationConfig;
39 import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
43 public class DmaapMessageHandlerTest {
45 private ApplicationConfig appConfig = mock(ApplicationConfig.class);
46 private final MRBatchingPublisher dmaapClient = mock(MRBatchingPublisher.class);
47 private final AsyncRestClient agentClient = mock(AsyncRestClient.class);
48 private DmaapMessageHandler testedObject;
49 private static Gson gson = new GsonBuilder() //
54 private void setUp() throws Exception {
55 testedObject = spy(new DmaapMessageHandler(dmaapClient, appConfig, agentClient));
58 ImmutableDmaapRequestMessage dmaapRequestMessage(Operation operation) {
59 return ImmutableDmaapRequestMessage.builder().apiVersion("apiVersion") //
60 .correlationId("correlationId") //
61 .operation(operation) //
62 .originatorId("originatorId") //
63 .payload("payload") //
64 .requestId("requestId") //
66 .timestamp("timestamp") //
72 private String dmaapInputMessage(Operation operation) {
73 return gson.toJson(dmaapRequestMessage(operation));
77 public void successfulCase() throws IOException {
78 doReturn(Mono.just("OK")).when(agentClient).delete("url");
79 doReturn(1).when(dmaapClient).send(anyString());
80 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
83 .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
84 .expectSubscription() //
88 verify(agentClient, times(1)).delete("url");
89 verifyNoMoreInteractions(agentClient);
91 verify(dmaapClient, times(1)).send(anyString());
92 verify(dmaapClient, times(1)).sendBatchWithResponse();
93 verifyNoMoreInteractions(dmaapClient);
97 public void errorCase() throws IOException {
98 doReturn(Mono.error(new Exception("Refused"))).when(agentClient).put("url", "payload");
99 doReturn(1).when(dmaapClient).send(anyString());
100 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
102 .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
103 .expectSubscription() //
104 .verifyComplete(); //
106 verify(agentClient, times(1)).put("url", "payload");
107 verifyNoMoreInteractions(agentClient);
110 verify(dmaapClient, times(1)).send(anyString());
111 verify(dmaapClient, times(1)).sendBatchWithResponse();
112 verifyNoMoreInteractions(dmaapClient);