Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-event-notifier / src / test / java / org / o / ran / oam / nf / oam / adopter / event / notifier / NotificationProviderTest.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.event.notifier;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
24
25 import io.reactivex.rxjava3.observers.TestObserver;
26 import java.util.Collections;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.Future;
29 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
30 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
31 import org.apache.hc.core5.concurrent.FutureCallback;
32 import org.apache.hc.core5.http.ContentType;
33 import org.apache.hc.core5.http.HttpHost;
34 import org.apache.hc.core5.http.HttpStatus;
35 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
36 import org.apache.hc.core5.http.nio.AsyncRequestProducer;
37 import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
38 import org.apache.hc.core5.http.nio.HandlerFactory;
39 import org.apache.hc.core5.http.protocol.HttpContext;
40 import org.junit.jupiter.api.Test;
41 import org.mockito.stubbing.Answer;
42 import org.o.ran.oam.nf.oam.adopter.api.CommonEventFormat302ONAP;
43 import org.o.ran.oam.nf.oam.adopter.api.Event;
44 import org.o.ran.oam.nf.oam.adopter.api.VesEventNotifier;
45 import org.o.ran.oam.nf.oam.adopter.event.notifier.properties.VesCollectorProperties;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50
51 @SpringBootTest(classes = {NotificationProvider.class, VesCollectorProperties.class})
52 @EnableConfigurationProperties
53 class NotificationProviderTest {
54
55     @Autowired
56     VesEventNotifier vesEventNotifier;
57
58     @MockBean
59     CloseableHttpAsyncClientMock client;
60
61     @Test
62     void testNotifySingleEvent() {
63         final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
64         final Future<SimpleHttpResponse> completableFuture = CompletableFuture.completedFuture(response);
65         when(client.doExecute(any(), any(), any(), any(), any(), any()))
66                 .thenAnswer((Answer<Future<SimpleHttpResponse>>) invocation -> completableFuture);
67
68         final CommonEventFormat302ONAP commonEvent = new CommonEventFormat302ONAP();
69         commonEvent.setEvent(new Event());
70         final TestObserver<Void> observer = vesEventNotifier.notifyEvents(commonEvent).test();
71         observer.assertComplete();
72     }
73
74     @Test
75     void testNotifySingleEventFail() {
76         final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_BAD_REQUEST);
77         response.setBody("some error", ContentType.APPLICATION_JSON);
78         final Future<SimpleHttpResponse> completableFuture = CompletableFuture.completedFuture(response);
79         when(client.doExecute(any(), any(), any(), any(), any(), any()))
80                 .thenAnswer((Answer<Future<SimpleHttpResponse>>) invocation -> completableFuture);
81         final CommonEventFormat302ONAP commonEvent = new CommonEventFormat302ONAP();
82         commonEvent.setEvent(new Event());
83         final TestObserver<Void> observer = vesEventNotifier.notifyEvents(commonEvent).test();
84         observer.assertError(throwable -> throwable.getMessage()
85                                                   .equals("Failed to post: HTTP/1.1 400 Bad Request some error"));
86     }
87
88     @Test
89     void testNotifyEventBatch() {
90         final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
91         final Future<SimpleHttpResponse> completableFuture = CompletableFuture.completedFuture(response);
92         when(client.doExecute(any(), any(), any(), any(), any(), any()))
93                 .thenAnswer((Answer<Future<SimpleHttpResponse>>) invocation -> completableFuture);
94
95         final CommonEventFormat302ONAP commonEvent = new CommonEventFormat302ONAP();
96         commonEvent.setEventList(Collections.singletonList(new Event()));
97         final TestObserver<Void> observer = vesEventNotifier.notifyEvents(commonEvent).test();
98         observer.assertComplete();
99     }
100
101     private abstract static class CloseableHttpAsyncClientMock extends CloseableHttpAsyncClient {
102
103         protected abstract <T> Future<T> doExecute(HttpHost var1, AsyncRequestProducer var2,
104                 AsyncResponseConsumer<T> var3, HandlerFactory<AsyncPushConsumer> var4, HttpContext var5,
105                 FutureCallback<T> var6);
106     }
107 }