31be69e3a9510f9fbf62bd264c6340f123ebc112
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  *  Modifications Copyright (C) 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  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.event.carrier.grpc;
24
25 import static org.mockito.Mockito.spy;
26
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import org.assertj.core.api.Assertions;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.policy.apex.service.engine.event.ApexEventException;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
37 import org.onap.policy.cds.client.CdsProcessorGrpcClient;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class ApexGrpcProducerTest {
41     private static final String PRODUCER_NAME = "TestApexGrpcProducer";
42     private static final String HOST = "localhost";
43     @Mock
44     private CdsProcessorGrpcClient grpcClient;
45     private ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer());
46     @Mock
47     private EventHandlerParameters eventHandlerParameters;
48
49     /**
50      * Set up testing.
51      *
52      * @throws ApexEventException on test set up errors.
53      */
54     @Before
55     public void setUp() throws ApexEventException {
56         populateEventHandlerParameters(HOST, 5);
57     }
58
59     @Test(expected = ApexEventException.class)
60     public void testInit_fail() throws ApexEventException {
61         apexGrpcProducer.init(PRODUCER_NAME, new EventHandlerParameters());
62     }
63
64     @Test
65     public void testInit_pass() {
66         // should not throw an exception
67         Assertions.assertThatCode(() -> apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters))
68             .doesNotThrowAnyException();
69     }
70
71     @Test
72     public void testStop() throws ApexEventException {
73         apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters);
74         // should not throw an exception
75         Assertions.assertThatCode(() -> apexGrpcProducer.stop()).doesNotThrowAnyException();
76     }
77
78     @Test
79     public void testSendEvent() throws ApexEventException {
80         apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters);
81         Assertions
82             .assertThatCode(() -> apexGrpcProducer.sendEvent(123, null, "grpcEvent",
83                 Files.readString(Paths.get("src/test/resources/executionServiceInputEvent.json"))))
84             .doesNotThrowAnyException();
85     }
86
87     private void populateEventHandlerParameters(String host, int timeout) {
88         eventHandlerParameters = new EventHandlerParameters();
89         GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters();
90         params.setLabel("GRPC");
91         params.setEventProducerPluginClass(ApexGrpcProducer.class.getName());
92         params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName());
93         params.setHost(host);
94         params.setPort(3214);
95         params.setUsername("dummyUser");
96         params.setPassword("dummyPassword");
97         params.setTimeout(timeout);
98         eventHandlerParameters.setCarrierTechnologyParameters(params);
99     }
100 }