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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.controlloop.actor.cds;
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;
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.TargetType;
36 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
41 public class GrpcOperatorTest {
43 GrpcOperator operation;
44 Map<String, Object> paramMap;
45 Map<String, Object> invalidParamMap;
48 * Initializes fields, including {@link #operation}.
52 MockitoAnnotations.initMocks(this);
53 operation = new GrpcOperator(CdsActorConstants.CDS_ACTOR, GrpcOperation.NAME, GrpcOperation::new);
55 CdsServerProperties props = new CdsServerProperties();
56 props.setHost("grpcHost");
58 props.setUsername("grpcUsername");
59 props.setPassword("grpcPassword");
62 paramMap = Util.translateToMap(GrpcOperation.NAME, props);
64 invalidParamMap = Util.translateToMap(GrpcOperation.NAME, props);
68 public void testGrpcOperator() {
69 assertEquals(CdsActorConstants.CDS_ACTOR, operation.getActorName());
70 assertEquals(GrpcOperation.NAME, operation.getName());
71 assertEquals(CdsActorConstants.CDS_ACTOR + "." + GrpcOperation.NAME, operation.getFullName());
76 public void testDoConfigure() {
78 operation.doConfigure(paramMap);
79 assertEquals(30000, operation.getCurrentConfig().getTimeoutMs());
81 // use invalidParamsMap
82 assertThatExceptionOfType(ParameterValidationRuntimeException.class)
83 .isThrownBy(() -> operation.makeConfiguration(invalidParamMap));
87 public void testBuildOperation() {
88 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
89 ControlLoopEventContext context = new ControlLoopEventContext(event);
90 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(CdsActorConstants.CDS_ACTOR)
91 .operation(GrpcOperation.NAME).context(context).targetType(TargetType.VM).build();
94 assertThatIllegalStateException().isThrownBy(() -> operation.buildOperation(params));
96 operation.configure(paramMap);
99 assertThatIllegalStateException().isThrownBy(() -> operation.buildOperation(params));
102 Operation operation1 = operation.buildOperation(params);
103 assertEquals(GrpcOperation.NAME, operation1.getName());
105 // with no operation-maker
106 GrpcOperator oper2 = new GrpcOperator(CdsActorConstants.CDS_ACTOR, GrpcOperation.NAME);
107 assertThatThrownBy(() -> oper2.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);