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