Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / CommonActorParamsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.Map;
33 import java.util.TreeMap;
34 import java.util.function.Consumer;
35 import java.util.function.Function;
36 import lombok.Setter;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.common.parameters.ValidationResult;
40 import org.onap.policy.controlloop.actorserviceprovider.Util;
41
42 public class CommonActorParamsTest {
43
44     private static final String CONTAINER = "my-container";
45
46     private static final String PATH1 = "path #1";
47     private static final String PATH2 = "path #2";
48     private static final String URI1 = "uri #1";
49     private static final String URI2 = "uri #2";
50     private static final String TEXT1 = "hello";
51     private static final String TEXT2 = "world";
52     private static final String TEXT2B = "bye";
53
54     private Map<String, Map<String, Object>> operations;
55     private CommonActorParams params;
56
57     /**
58      * Initializes {@link #operations} with two items and {@link params} with a fully
59      * populated object.
60      */
61     @Before
62     public void setUp() {
63         operations = new TreeMap<>();
64         operations.put(PATH1, Map.of("path", URI1));
65         operations.put(PATH2, Map.of("path", URI2, "text2", TEXT2B));
66
67         params = makeCommonActorParams();
68     }
69
70     @Test
71     public void testMakeOperationParameters() {
72         Function<String, Map<String, Object>> maker = params.makeOperationParameters(CONTAINER);
73         assertNull(maker.apply("unknown-operation"));
74
75         Map<String, Object> subparam = maker.apply(PATH1);
76         assertNotNull(subparam);
77         assertEquals("{path=uri #1, text1=hello, text2=world}", new TreeMap<>(subparam).toString());
78
79         subparam = maker.apply(PATH2);
80         assertNotNull(subparam);
81         assertEquals("{path=uri #2, text1=hello, text2=bye}", new TreeMap<>(subparam).toString());
82     }
83
84     @Test
85     public void testDoValidation() {
86         assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException();
87
88         // invalid param
89         params.setOperation(null);
90         assertThatThrownBy(() -> params.doValidation(CONTAINER))
91                         .isInstanceOf(ParameterValidationRuntimeException.class);
92     }
93
94     @Test
95     public void testValidate() {
96         assertTrue(params.validate(CONTAINER).isValid());
97
98         // only a few fields are required
99         CommonActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations, "timeoutSec", 1),
100                         CommonActorParams.class);
101         assertTrue(sparse.validate(CONTAINER).isValid());
102
103         testValidateField("operation", "null", params2 -> params2.setOperation(null));
104     }
105
106     private void testValidateField(String fieldName, String expected, Consumer<CommonActorParams> makeInvalid) {
107
108         // original params should be valid
109         ValidationResult result = params.validate(CONTAINER);
110         assertTrue(fieldName, result.isValid());
111
112         // make invalid params
113         CommonActorParams params2 = makeCommonActorParams();
114         makeInvalid.accept(params2);
115         result = params2.validate(CONTAINER);
116         assertFalse(fieldName, result.isValid());
117         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
118     }
119
120     private CommonActorParams makeCommonActorParams() {
121         MyParams params2 = new MyParams();
122         params2.setOperation(operations);
123         params2.setText1(TEXT1);
124         params2.setText2(TEXT2);
125
126         return params2;
127     }
128
129     @Setter
130     public static class MyParams extends CommonActorParams {
131         @SuppressWarnings("unused")
132         private String text1;
133
134         @SuppressWarnings("unused")
135         private String text2;
136     }
137 }