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.actor.so;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
28 import java.util.TreeMap;
29 import java.util.function.Consumer;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.controlloop.actorserviceprovider.Util;
36 public class SoActorParamsTest {
37 private static final String CONTAINER = "my-container";
38 private static final String CLIENT = "my-client";
39 private static final String PATH_GET = "my-path-get";
40 private static final int MAX_GETS = 3;
41 private static final int WAIT_SEC_GETS = 20;
42 private static final int TIMEOUT = 10;
44 private static final String PATH1 = "path #1";
45 private static final String PATH2 = "path #2";
46 private static final String URI1 = "uri #1";
47 private static final String URI2 = "uri #2";
49 private Map<String, Map<String, Object>> operations;
50 private SoActorParams params;
53 * Initializes {@link #operations} with two items and {@link params} with a fully
58 operations = new TreeMap<>();
59 operations.put(PATH1, Map.of("path", URI1));
60 operations.put(PATH2, Map.of("path", URI2));
62 params = makeSoActorParams();
66 public void testValidate() {
67 assertTrue(params.validate(CONTAINER).isValid());
69 // only a few fields are required
70 SoActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations), SoActorParams.class);
71 assertTrue(sparse.validate(CONTAINER).isValid());
73 testValidateField("maxGets", "minimum", params2 -> params2.setMaxGets(-1));
74 testValidateField("waitSecGet", "minimum", params2 -> params2.setWaitSecGet(0));
76 // check fields from superclass
77 testValidateField("operation", "null", params2 -> params2.setOperation(null));
78 testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
82 assertTrue(params.validate(CONTAINER).isValid());
83 params.setMaxGets(MAX_GETS);
85 params.setWaitSecGet(1);
86 assertTrue(params.validate(CONTAINER).isValid());
87 params.setWaitSecGet(WAIT_SEC_GETS);
90 private void testValidateField(String fieldName, String expected, Consumer<SoActorParams> makeInvalid) {
92 // original params should be valid
93 ValidationResult result = params.validate(CONTAINER);
94 assertTrue(fieldName, result.isValid());
96 // make invalid params
97 SoActorParams params2 = makeSoActorParams();
98 makeInvalid.accept(params2);
99 result = params2.validate(CONTAINER);
100 assertFalse(fieldName, result.isValid());
101 assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
104 private SoActorParams makeSoActorParams() {
105 SoActorParams params2 = new SoActorParams();
106 params2.setClientName(CLIENT);
107 params2.setTimeoutSec(TIMEOUT);
108 params2.setOperation(operations);
110 params2.setWaitSecGet(WAIT_SEC_GETS);
111 params2.setMaxGets(MAX_GETS);
112 params2.setPathGet(PATH_GET);