09dbf92fe6b1b00b373a3ef199d9cdb4d716f2a5
[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 static final String URL = "url";
50     private static final String PAYLOAD = "payload";
51
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() //
57         .serializeNulls() //
58         .create(); //
59
60     @BeforeEach
61     private void setUp() throws Exception {
62         testedObject = spy(new DmaapMessageHandler(dmaapClient, agentClient));
63     }
64
65     DmaapRequestMessage dmaapRequestMessage(Operation operation) {
66         return ImmutableDmaapRequestMessage.builder().apiVersion("apiVersion") //
67             .correlationId("correlationId") //
68             .operation(operation) //
69             .originatorId("originatorId") //
70             .payload(PAYLOAD) //
71             .requestId("requestId") //
72             .target("target") //
73             .timestamp("timestamp") //
74             .type("type") //
75             .url(URL) //
76             .build();
77     }
78
79     private String dmaapInputMessage(Operation operation) {
80         return gson.toJson(dmaapRequestMessage(operation));
81     }
82
83     @Test
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();
88
89         StepVerifier //
90             .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
91             .expectSubscription() //
92             .expectNext("OK") //
93             .verifyComplete(); //
94
95         verify(agentClient, times(1)).delete(URL);
96         verifyNoMoreInteractions(agentClient);
97
98         verify(dmaapClient, times(1)).send(anyString());
99         verify(dmaapClient, times(1)).sendBatchWithResponse();
100         verifyNoMoreInteractions(dmaapClient);
101     }
102
103     @Test
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();
108
109         StepVerifier //
110             .create(testedObject.createTask(dmaapInputMessage(Operation.GET))) //
111             .expectSubscription() //
112             .expectNext("OK") //
113             .verifyComplete(); //
114
115         verify(agentClient, times(1)).get(URL);
116         verifyNoMoreInteractions(agentClient);
117
118         verify(dmaapClient, times(1)).send(anyString());
119         verify(dmaapClient, times(1)).sendBatchWithResponse();
120         verifyNoMoreInteractions(dmaapClient);
121     }
122
123     @Test
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();
128
129         StepVerifier //
130             .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
131             .expectSubscription() //
132             .expectNext("OK") //
133             .verifyComplete(); //
134
135         verify(agentClient, times(1)).put(URL, PAYLOAD);
136         verifyNoMoreInteractions(agentClient);
137
138         verify(dmaapClient, times(1)).send(anyString());
139         verify(dmaapClient, times(1)).sendBatchWithResponse();
140         verifyNoMoreInteractions(dmaapClient);
141     }
142
143     @Test
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();
148
149         StepVerifier //
150             .create(testedObject.createTask(dmaapInputMessage(Operation.POST))) //
151             .expectSubscription() //
152             .expectNext("OK") //
153             .verifyComplete(); //
154
155         verify(agentClient, times(1)).post(URL, PAYLOAD);
156         verifyNoMoreInteractions(agentClient);
157
158         verify(dmaapClient, times(1)).send(anyString());
159         verify(dmaapClient, times(1)).sendBatchWithResponse();
160         verifyNoMoreInteractions(dmaapClient);
161     }
162
163     @Test
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();
168         StepVerifier //
169             .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
170             .expectSubscription() //
171             .verifyComplete(); //
172
173         verify(agentClient, times(1)).put(URL, PAYLOAD);
174         verifyNoMoreInteractions(agentClient);
175
176         // Error response
177         verify(dmaapClient, times(1)).send(anyString());
178         verify(dmaapClient, times(1)).sendBatchWithResponse();
179         verifyNoMoreInteractions(dmaapClient);
180     }
181
182 }