2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.grpc;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.Mockito.spy;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import org.assertj.core.api.Assertions;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.onap.policy.apex.service.engine.event.ApexEventException;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
36 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
38 @RunWith(MockitoJUnitRunner.class)
39 public class ApexGrpcProducerTest {
40 private static final String HOST = "localhost";
42 private CdsProcessorGrpcClient grpcClient;
43 private ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer());
45 private EventHandlerParameters eventHandlerParameters;
50 * @throws ApexEventException on test set up errors.
53 public void setUp() throws ApexEventException {
54 populateEventHandlerParameters(HOST, 5);
57 @Test(expected = ApexEventException.class)
58 public void testInit_fail() throws ApexEventException {
59 apexGrpcProducer.init("TestApexGrpcProducer", new EventHandlerParameters());
63 public void testInit_pass() {
64 // should not throw an exception
65 Assertions.assertThatCode(() -> apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters))
66 .doesNotThrowAnyException();
70 public void testStop() throws ApexEventException {
71 apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters);
72 // should not throw an exception
73 Assertions.assertThatCode(() -> apexGrpcProducer.stop()).doesNotThrowAnyException();
77 public void testSendEvent() throws ApexEventException {
78 apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters);
79 assertThatThrownBy(() -> {
80 apexGrpcProducer.sendEvent(123, null, "grpcEvent",
81 Files.readString(Paths.get("src/test/resources/executionServiceInputEvent.json")));
82 }).hasMessageContaining("UNAVAILABLE: io exception");
85 private void populateEventHandlerParameters(String host, int timeout) {
86 eventHandlerParameters = new EventHandlerParameters();
87 GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters();
88 params.setLabel("GRPC");
89 params.setEventProducerPluginClass(ApexGrpcProducer.class.getName());
90 params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName());
93 params.setUsername("dummyUser");
94 params.setPassword("dummyPassword");
95 params.setTimeout(timeout);
96 eventHandlerParameters.setCarrierTechnologyParameters(params);