X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ves-nf-oam-adopter%2Fves-nf-oam-adopter-event-notifier%2Fsrc%2Ftest%2Fjava%2Forg%2Fo%2Fran%2Foam%2Fnf%2Foam%2Fadopter%2Fevent%2Fnotifier%2FNotificationProviderTest.java;fp=ves-nf-oam-adopter%2Fves-nf-oam-adopter-event-notifier%2Fsrc%2Ftest%2Fjava%2Forg%2Fo%2Fran%2Foam%2Fnf%2Foam%2Fadopter%2Fevent%2Fnotifier%2FNotificationProviderTest.java;h=080eeadabba29ae8702cc40c88f26ac69bd21bce;hb=09c5bdc9bb6c40351bf14b6ebce9e0c4855ab35a;hp=0000000000000000000000000000000000000000;hpb=daa8211f9ef3029bde5fe2843431136a695913f1;p=oam%2Fnf-oam-adopter.git diff --git a/ves-nf-oam-adopter/ves-nf-oam-adopter-event-notifier/src/test/java/org/o/ran/oam/nf/oam/adopter/event/notifier/NotificationProviderTest.java b/ves-nf-oam-adopter/ves-nf-oam-adopter-event-notifier/src/test/java/org/o/ran/oam/nf/oam/adopter/event/notifier/NotificationProviderTest.java new file mode 100644 index 0000000..080eead --- /dev/null +++ b/ves-nf-oam-adopter/ves-nf-oam-adopter-event-notifier/src/test/java/org/o/ran/oam/nf/oam/adopter/event/notifier/NotificationProviderTest.java @@ -0,0 +1,107 @@ +/* + * ============LICENSE_START======================================================= + * O-RAN-SC + * ================================================================================ + * Copyright © 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.o.ran.oam.nf.oam.adopter.event.notifier; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import io.reactivex.rxjava3.observers.TestObserver; +import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.nio.AsyncPushConsumer; +import org.apache.hc.core5.http.nio.AsyncRequestProducer; +import org.apache.hc.core5.http.nio.AsyncResponseConsumer; +import org.apache.hc.core5.http.nio.HandlerFactory; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.junit.jupiter.api.Test; +import org.mockito.stubbing.Answer; +import org.o.ran.oam.nf.oam.adopter.api.CommonEventFormat302ONAP; +import org.o.ran.oam.nf.oam.adopter.api.Event; +import org.o.ran.oam.nf.oam.adopter.api.VesEventNotifier; +import org.o.ran.oam.nf.oam.adopter.event.notifier.properties.VesCollectorProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +@SpringBootTest(classes = {NotificationProvider.class, VesCollectorProperties.class}) +@EnableConfigurationProperties +public class NotificationProviderTest { + + @Autowired + VesEventNotifier vesEventNotifier; + + @MockBean + CloseableHttpAsyncClientMock client; + + @Test + void testNotifySingleEvent() { + final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK); + final Future completableFuture = CompletableFuture.completedFuture(response); + when(client.doExecute(any(), any(), any(), any(), any(), any())) + .thenAnswer((Answer>) invocation -> completableFuture); + + final CommonEventFormat302ONAP commonEvent = new CommonEventFormat302ONAP(); + commonEvent.setEvent(new Event()); + final TestObserver observer = vesEventNotifier.notifyEvents(commonEvent).test(); + observer.assertComplete(); + } + + @Test + void testNotifySingleEventFail() { + final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_BAD_REQUEST); + response.setBody("some error", ContentType.APPLICATION_JSON); + final Future completableFuture = CompletableFuture.completedFuture(response); + when(client.doExecute(any(), any(), any(), any(), any(), any())) + .thenAnswer((Answer>) invocation -> completableFuture); + final CommonEventFormat302ONAP commonEvent = new CommonEventFormat302ONAP(); + commonEvent.setEvent(new Event()); + final TestObserver observer = vesEventNotifier.notifyEvents(commonEvent).test(); + observer.assertError(throwable -> throwable.getMessage() + .equals("Failed to post: HTTP/1.1 400 Bad Request some error")); + } + + @Test + void testNotifyEventBatch() { + final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK); + final Future completableFuture = CompletableFuture.completedFuture(response); + when(client.doExecute(any(), any(), any(), any(), any(), any())) + .thenAnswer((Answer>) invocation -> completableFuture); + + final CommonEventFormat302ONAP commonEvent = new CommonEventFormat302ONAP(); + commonEvent.setEventList(Collections.singletonList(new Event())); + final TestObserver observer = vesEventNotifier.notifyEvents(commonEvent).test(); + observer.assertComplete(); + } + + private abstract static class CloseableHttpAsyncClientMock extends CloseableHttpAsyncClient { + + protected abstract Future doExecute(HttpHost var1, AsyncRequestProducer var2, + AsyncResponseConsumer var3, HandlerFactory var4, HttpContext var5, + FutureCallback var6); + } +} \ No newline at end of file