71c850033ba6b04056aaed407e9e38e350df1513
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / dmaap / DmaapMessageHandlerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.oransc.policyagent.dmaap;
22
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
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33
34 import java.io.IOException;
35
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;
43
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
46
47 public class DmaapMessageHandlerTest {
48
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() //
54         .serializeNulls() //
55         .create(); //
56
57     @BeforeEach
58     private void setUp() throws Exception {
59         testedObject = spy(new DmaapMessageHandler(dmaapClient, appConfig, agentClient));
60     }
61
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") //
69             .target("target") //
70             .timestamp("timestamp") //
71             .type("type") //
72             .url("url") //
73             .build();
74     }
75
76     private String dmaapInputMessage(Operation operation) {
77         return gson.toJson(dmaapRequestMessage(operation));
78     }
79
80     @Test
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();
85
86         StepVerifier //
87             .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
88             .expectSubscription() //
89             .expectNext("OK") //
90             .verifyComplete(); //
91
92         verify(agentClient, times(1)).delete("url");
93         verifyNoMoreInteractions(agentClient);
94
95         verify(dmaapClient, times(1)).send(anyString());
96         verify(dmaapClient, times(1)).sendBatchWithResponse();
97         verifyNoMoreInteractions(dmaapClient);
98     }
99
100     @Test
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();
105         StepVerifier //
106             .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
107             .expectSubscription() //
108             .verifyComplete(); //
109
110         verify(agentClient, times(1)).put("url", "payload");
111         verifyNoMoreInteractions(agentClient);
112
113         // Error response
114         verify(dmaapClient, times(1)).send(anyString());
115         verify(dmaapClient, times(1)).sendBatchWithResponse();
116         verifyNoMoreInteractions(dmaapClient);
117     }
118
119 }