13ecaba6a6df976917cad42230e84d25e5a1ee59
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.grpc;
23
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 PRODUCER_NAME = "TestApexGrpcProducer";
41     private static final String HOST = "localhost";
42     @Mock
43     private CdsProcessorGrpcClient grpcClient;
44     private ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer());
45     @Mock
46     private EventHandlerParameters eventHandlerParameters;
47
48     /**
49      * Set up testing.
50      *
51      * @throws ApexEventException on test set up errors.
52      */
53     @Before
54     public void setUp() throws ApexEventException {
55         populateEventHandlerParameters(HOST, 5);
56     }
57
58     @Test(expected = ApexEventException.class)
59     public void testInit_fail() throws ApexEventException {
60         apexGrpcProducer.init(PRODUCER_NAME, new EventHandlerParameters());
61     }
62
63     @Test
64     public void testInit_pass() {
65         // should not throw an exception
66         Assertions.assertThatCode(() -> apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters))
67             .doesNotThrowAnyException();
68     }
69
70     @Test
71     public void testStop() throws ApexEventException {
72         apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters);
73         // should not throw an exception
74         Assertions.assertThatCode(() -> apexGrpcProducer.stop()).doesNotThrowAnyException();
75     }
76
77     @Test
78     public void testSendEvent() throws ApexEventException {
79         apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters);
80         Assertions
81             .assertThatCode(() -> apexGrpcProducer.sendEvent(123, null, "grpcEvent",
82                 Files.readString(Paths.get("src/test/resources/executionServiceInputEvent.json"))))
83             .doesNotThrowAnyException();
84     }
85
86     private void populateEventHandlerParameters(String host, int timeout) {
87         eventHandlerParameters = new EventHandlerParameters();
88         GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters();
89         params.setLabel("GRPC");
90         params.setEventProducerPluginClass(ApexGrpcProducer.class.getName());
91         params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName());
92         params.setHost(host);
93         params.setPort(3214);
94         params.setUsername("dummyUser");
95         params.setPassword("dummyPassword");
96         params.setTimeout(timeout);
97         eventHandlerParameters.setCarrierTechnologyParameters(params);
98     }
99 }