Rerealese a1-controller
[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 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;
42
43 public class DmaapMessageHandlerTest {
44
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() //
50         .serializeNulls() //
51         .create(); //
52
53     @BeforeEach
54     private void setUp() throws Exception {
55         testedObject = spy(new DmaapMessageHandler(dmaapClient, appConfig, agentClient));
56     }
57
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") //
65             .target("target") //
66             .timestamp("timestamp") //
67             .type("type") //
68             .url("url") //
69             .build();
70     }
71
72     private String dmaapInputMessage(Operation operation) {
73         return gson.toJson(dmaapRequestMessage(operation));
74     }
75
76     @Test
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();
81
82         StepVerifier //
83             .create(testedObject.createTask(dmaapInputMessage(Operation.DELETE))) //
84             .expectSubscription() //
85             .expectNext("OK") //
86             .verifyComplete(); //
87
88         verify(agentClient, times(1)).delete("url");
89         verifyNoMoreInteractions(agentClient);
90
91         verify(dmaapClient, times(1)).send(anyString());
92         verify(dmaapClient, times(1)).sendBatchWithResponse();
93         verifyNoMoreInteractions(dmaapClient);
94     }
95
96     @Test
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();
101         StepVerifier //
102             .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) //
103             .expectSubscription() //
104             .verifyComplete(); //
105
106         verify(agentClient, times(1)).put("url", "payload");
107         verifyNoMoreInteractions(agentClient);
108
109         // Error response
110         verify(dmaapClient, times(1)).send(anyString());
111         verify(dmaapClient, times(1)).sendBatchWithResponse();
112         verifyNoMoreInteractions(dmaapClient);
113     }
114
115 }