bd4103f928d3f4c4094cf54a7a94e672ff02c626
[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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import org.assertj.core.api.Assertions;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.service.engine.event.ApexEventException;
31 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
32 import org.onap.policy.apex.service.engine.event.PeeredReference;
33 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
35
36 public class ApexGrpcConsumerTest {
37     private static final String CONSUMER_NAME = "TestApexGrpcConsumer";
38     private ApexGrpcConsumer grpcConsumer = null;
39     private ApexGrpcProducer grpcProducer = null;
40     private EventHandlerParameters consumerParameters = null;
41     private ApexEventReceiver incomingEventReceiver = null;
42
43     /**
44      * Set up testing.
45      *
46      * @throws ApexEventException on test set up errors.
47      */
48     @Before
49     public void setUp() throws ApexEventException {
50         grpcConsumer = new ApexGrpcConsumer();
51         grpcProducer = new ApexGrpcProducer();
52     }
53
54     @Test
55     public void testInit() {
56         consumerParameters = populateConsumerParameters(true, true);
57         Assertions.assertThatCode(() -> grpcConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver))
58             .doesNotThrowAnyException();
59     }
60
61     @Test
62     public void testInit_invalidPeeredMode() {
63         consumerParameters = populateConsumerParameters(true, false);
64         assertThatThrownBy(() -> grpcConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver))
65             .hasMessageContaining(
66                 "gRPC consumer (" + CONSUMER_NAME + ") must run in peered requestor mode with a gRPC producer");
67     }
68
69     @Test
70     public void testGetName() {
71         assertEquals(null, new ApexGrpcConsumer().getName());
72     }
73
74     @Test
75     public void testPeeredReference() throws ApexEventException {
76         consumerParameters = populateConsumerParameters(true, true);
77         grpcConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR,
78             new PeeredReference(EventHandlerPeeredMode.REQUESTOR, grpcConsumer, grpcProducer));
79         grpcConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver);
80         PeeredReference peeredReference = grpcConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR);
81         assertNotNull(peeredReference);
82         assertEquals(grpcConsumer, peeredReference.getPeeredConsumer());
83         assertEquals(grpcProducer, peeredReference.getPeeredProducer());
84     }
85
86     private EventHandlerParameters populateConsumerParameters(boolean isConsumer, boolean isPeeredMode) {
87         consumerParameters = new EventHandlerParameters();
88         GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters();
89         params.setLabel("GRPC");
90         params.setEventProducerPluginClass(ApexGrpcProducer.class.getName());
91         params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName());
92         if (!isConsumer) {
93             params.setHost("hostname");
94             params.setPort(3214);
95             params.setUsername("dummyUser");
96             params.setPassword("dummyPassword");
97             params.setTimeout(1);
98         }
99         consumerParameters.setCarrierTechnologyParameters(params);
100         if (isPeeredMode) {
101             consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
102             consumerParameters.setPeer(EventHandlerPeeredMode.REQUESTOR, "requestorPeerName");
103         }
104         return consumerParameters;
105     }
106 }