53d191e14ff651820eb461c8232043c9ec61d717
[policy/apex-pdp.git] /
1 /*-
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
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.onap.policy.apex.plugins.event.carrier.grpc;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.Mockito.spy;
25
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;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class ApexGrpcProducerTest {
40     private static final String HOST = "localhost";
41     @Mock
42     private CdsProcessorGrpcClient grpcClient;
43     private ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer());
44     @Mock
45     private EventHandlerParameters eventHandlerParameters;
46
47     /**
48      * Set up testing.
49      *
50      * @throws ApexEventException on test set up errors.
51      */
52     @Before
53     public void setUp() throws ApexEventException {
54         populateEventHandlerParameters(HOST, 5);
55     }
56
57     @Test(expected = ApexEventException.class)
58     public void testInit_fail() throws ApexEventException {
59         apexGrpcProducer.init("TestApexGrpcProducer", new EventHandlerParameters());
60     }
61
62     @Test
63     public void testInit_pass() {
64         // should not throw an exception
65         Assertions.assertThatCode(() -> apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters))
66             .doesNotThrowAnyException();
67     }
68
69     @Test
70     public void testStop() throws ApexEventException {
71         apexGrpcProducer.init("TestApexGrpcProducer", eventHandlerParameters);
72         // should not throw an exception
73         Assertions.assertThatCode(() -> apexGrpcProducer.stop()).doesNotThrowAnyException();
74     }
75
76     @Test
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");
83     }
84
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());
91         params.setHost(host);
92         params.setPort(3214);
93         params.setUsername("dummyUser");
94         params.setPassword("dummyPassword");
95         params.setTimeout(timeout);
96         eventHandlerParameters.setCarrierTechnologyParameters(params);
97     }
98 }