2fd54e40c9764f66dab189070a8449864455da51
[policy/models.git] / models-interactions / model-actors / actor.cds / src / test / java / org / onap / policy / controlloop / actor / cds / GrpcOperatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Bell Canada. All rights reserved.
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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.controlloop.actor.cds;
20
21 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25
26 import java.util.Map;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.MockitoAnnotations;
30 import org.onap.policy.cds.properties.CdsServerProperties;
31 import org.onap.policy.controlloop.VirtualControlLoopEvent;
32 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
33 import org.onap.policy.controlloop.actorserviceprovider.Operation;
34 import org.onap.policy.controlloop.actorserviceprovider.Util;
35 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
36 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
38
39 public class GrpcOperatorTest {
40
41     GrpcOperator operation;
42     Map<String, Object> paramMap;
43     Map<String, Object> invalidParamMap;
44
45     /**
46      * Initializes fields, including {@link #operation}.
47      */
48     @Before
49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51         operation = new GrpcOperator(CdsActorConstants.CDS_ACTOR, GrpcOperation.NAME, GrpcOperation::new);
52
53         CdsServerProperties props = new CdsServerProperties();
54         props.setHost("grpcHost");
55         props.setPort(1234);
56         props.setUsername("grpcUsername");
57         props.setPassword("grpcPassword");
58         props.setTimeout(30);
59
60         paramMap = Util.translateToMap(GrpcOperation.NAME, props);
61         props.setHost(null);
62         invalidParamMap = Util.translateToMap(GrpcOperation.NAME, props);
63     }
64
65     @Test
66     public void testGrpcOperator() {
67         assertEquals(CdsActorConstants.CDS_ACTOR, operation.getActorName());
68         assertEquals(GrpcOperation.NAME, operation.getName());
69         assertEquals(CdsActorConstants.CDS_ACTOR + "." + GrpcOperation.NAME, operation.getFullName());
70     }
71
72
73     @Test
74     public void testDoConfigure() {
75
76         operation.doConfigure(paramMap);
77         assertEquals(30000, operation.getCurrentConfig().getTimeoutMs());
78
79         // use invalidParamsMap
80         assertThatExceptionOfType(ParameterValidationRuntimeException.class)
81                         .isThrownBy(() -> operation.makeConfiguration(invalidParamMap));
82     }
83
84     @Test
85     public void testBuildOperation() {
86         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
87         ControlLoopEventContext context = new ControlLoopEventContext(event);
88         ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
89                         .operation(GrpcOperation.NAME).context(context).build();
90
91         // not configured yet
92         assertThatIllegalStateException().isThrownBy(() -> operation.buildOperation(params));
93
94         operation.configure(paramMap);
95
96         // not running yet
97         assertThatIllegalStateException().isThrownBy(() -> operation.buildOperation(params));
98
99         operation.start();
100         Operation operation1 = operation.buildOperation(params);
101         assertEquals(GrpcOperation.NAME, operation1.getName());
102
103         // with no operation-maker
104         GrpcOperator oper2 = new GrpcOperator(CdsActorConstants.CDS_ACTOR, GrpcOperation.NAME);
105         assertThatThrownBy(() -> oper2.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);
106     }
107 }