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.oransc.policyagent.clients.AsyncRestClient;
40 import org.oransc.policyagent.configuration.ApplicationConfig;
41 import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
43 import reactor.core.publisher.Mono;
44 import reactor.test.StepVerifier;
46 public class DmaapMessageHandlerTest {
48 private ApplicationConfig appConfig = mock(ApplicationConfig.class);
49 private final MRBatchingPublisher dmaapClient = mock(MRBatchingPublisher.class);
50 private final AsyncRestClient agentClient = mock(AsyncRestClient.class);
51 private DmaapMessageHandler testedObject;
52 private static Gson gson = new GsonBuilder() //
57 private void setUp() throws Exception {
58 testedObject = spy(new DmaapMessageHandler(dmaapClient, appConfig, agentClient));
61 ImmutableDmaapRequestMessage dmaapRequestMessage(Operation operation) {
62 return ImmutableDmaapRequestMessage.builder().apiVersion("apiVersion") //
63 .correlationId("correlationId") //
64 .operation(operation) //
65 .originatorId("originatorId") //
66 .payload("payload") //
67 .requestId("requestId") //
69 .timestamp("timestamp") //
75 private String dmaapInputMessage(Operation operation) {
76 return gson.toJson(dmaapRequestMessage(operation));
80 public void successfulCase() throws IOException {
81 doReturn(Mono.just("OK")).when(agentClient).delete("url");
82 doReturn(1).when(dmaapClient).send(anyString());
85 .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
86 .expectSubscription() //
90 verify(agentClient, times(1)).delete("url");
91 verifyNoMoreInteractions(agentClient);
93 verify(dmaapClient, times(1)).send(anyString());
94 verifyNoMoreInteractions(dmaapClient);
98 public void errorCase() throws IOException {
99 doReturn(Mono.error(new Exception("Refused"))).when(agentClient).put("url", "payload");
100 doReturn(1).when(dmaapClient).send(anyString());
103 .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
104 .expectSubscription() //
105 .verifyComplete(); //
107 verify(agentClient, times(1)).put("url", "payload");
108 verifyNoMoreInteractions(agentClient);
111 verify(dmaapClient, times(1)).send(anyString());
112 verifyNoMoreInteractions(dmaapClient);