2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
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;
33 import java.util.TreeMap;
34 import java.util.function.Consumer;
35 import java.util.function.Function;
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;
42 public class CommonActorParamsTest {
44 private static final String CONTAINER = "my-container";
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";
54 private Map<String, Map<String, Object>> operations;
55 private CommonActorParams params;
58 * Initializes {@link #operations} with two items and {@link params} with a fully
63 operations = new TreeMap<>();
64 operations.put(PATH1, Map.of("path", URI1));
65 operations.put(PATH2, Map.of("path", URI2, "text2", TEXT2B));
67 params = makeCommonActorParams();
71 public void testMakeOperationParameters() {
72 Function<String, Map<String, Object>> maker = params.makeOperationParameters(CONTAINER);
73 assertNull(maker.apply("unknown-operation"));
75 Map<String, Object> subparam = maker.apply(PATH1);
76 assertNotNull(subparam);
77 assertEquals("{path=uri #1, text1=hello, text2=world}", new TreeMap<>(subparam).toString());
79 subparam = maker.apply(PATH2);
80 assertNotNull(subparam);
81 assertEquals("{path=uri #2, text1=hello, text2=bye}", new TreeMap<>(subparam).toString());
85 public void testDoValidation() {
86 assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException();
89 params.setOperations(null);
90 assertThatThrownBy(() -> params.doValidation(CONTAINER))
91 .isInstanceOf(ParameterValidationRuntimeException.class);
95 public void testValidate() {
96 assertTrue(params.validate(CONTAINER).isValid());
98 // only a few fields are required
99 CommonActorParams sparse = Util.translate(CONTAINER,
100 Map.of(CommonActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1),
101 CommonActorParams.class);
102 assertTrue(sparse.validate(CONTAINER).isValid());
104 testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null));
107 private void testValidateField(String fieldName, String expected, Consumer<CommonActorParams> makeInvalid) {
109 // original params should be valid
110 ValidationResult result = params.validate(CONTAINER);
111 assertTrue(fieldName, result.isValid());
113 // make invalid params
114 CommonActorParams params2 = makeCommonActorParams();
115 makeInvalid.accept(params2);
116 result = params2.validate(CONTAINER);
117 assertFalse(fieldName, result.isValid());
118 assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
121 private CommonActorParams makeCommonActorParams() {
122 MyParams params2 = new MyParams();
123 params2.setOperations(operations);
124 params2.setText1(TEXT1);
125 params2.setText2(TEXT2);
131 public static class MyParams extends CommonActorParams {
132 @SuppressWarnings("unused")
133 private String text1;
135 @SuppressWarnings("unused")
136 private String text2;