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 ApplicationConfig appConfig = mock(ApplicationConfig.class);
50 private final MRBatchingPublisher dmaapClient = mock(MRBatchingPublisher.class);
51 private final AsyncRestClient agentClient = mock(AsyncRestClient.class);
52 private DmaapMessageHandler testedObject;
53 private static Gson gson = new GsonBuilder() //
58 private void setUp() throws Exception {
59 testedObject = spy(new DmaapMessageHandler(dmaapClient, appConfig, agentClient));
62 ImmutableDmaapRequestMessage dmaapRequestMessage(Operation operation) {
63 return ImmutableDmaapRequestMessage.builder().apiVersion("apiVersion") //
64 .correlationId("correlationId") //
65 .operation(operation) //
66 .originatorId("originatorId") //
67 .payload("payload") //
68 .requestId("requestId") //
70 .timestamp("timestamp") //
76 private String dmaapInputMessage(Operation operation) {
77 return gson.toJson(dmaapRequestMessage(operation));
81 public void successfulCase() throws IOException {
82 doReturn(Mono.just("OK")).when(agentClient).delete("url");
83 doReturn(1).when(dmaapClient).send(anyString());
84 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
87 .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
88 .expectSubscription() //
92 verify(agentClient, times(1)).delete("url");
93 verifyNoMoreInteractions(agentClient);
95 verify(dmaapClient, times(1)).send(anyString());
96 verify(dmaapClient, times(1)).sendBatchWithResponse();
97 verifyNoMoreInteractions(dmaapClient);
101 public void errorCase() throws IOException {
102 doReturn(Mono.error(new Exception("Refused"))).when(agentClient).put("url", "payload");
103 doReturn(1).when(dmaapClient).send(anyString());
104 doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
106 .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
107 .expectSubscription() //
108 .verifyComplete(); //
110 verify(agentClient, times(1)).put("url", "payload");
111 verifyNoMoreInteractions(agentClient);
114 verify(dmaapClient, times(1)).send(anyString());
115 verify(dmaapClient, times(1)).sendBatchWithResponse();
116 verifyNoMoreInteractions(dmaapClient);