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