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