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.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;
35 public class HttpActorParamsTest {
37 private static final String CONTAINER = "my-container";
38 private static final String CLIENT = "my-client";
39 private static final int TIMEOUT = 10;
41 private static final String PATH1 = "path #1";
42 private static final String PATH2 = "path #2";
43 private static final String URI1 = "uri #1";
44 private static final String URI2 = "uri #2";
46 private Map<String, Map<String, Object>> operations;
47 private HttpActorParams params;
50 * Initializes {@link #operations} with two items and {@link params} with a fully
55 operations = new TreeMap<>();
56 operations.put(PATH1, Map.of("path", URI1));
57 operations.put(PATH2, Map.of("path", URI2));
59 params = makeHttpActorParams();
63 public void testValidate() {
64 assertTrue(params.validate(CONTAINER).isValid());
66 // only a few fields are required
67 HttpActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations, "timeoutSec", 1),
68 HttpActorParams.class);
69 assertTrue(sparse.validate(CONTAINER).isValid());
71 testValidateField("operation", "null", params2 -> params2.setOperation(null));
72 testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
75 params.setTimeoutSec(0);
76 assertFalse(params.validate(CONTAINER).isValid());
78 params.setTimeoutSec(1);
79 assertTrue(params.validate(CONTAINER).isValid());
82 private void testValidateField(String fieldName, String expected, Consumer<HttpActorParams> makeInvalid) {
84 // original params should be valid
85 ValidationResult result = params.validate(CONTAINER);
86 assertTrue(fieldName, result.isValid());
88 // make invalid params
89 HttpActorParams params2 = makeHttpActorParams();
90 makeInvalid.accept(params2);
91 result = params2.validate(CONTAINER);
92 assertFalse(fieldName, result.isValid());
93 assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
96 private HttpActorParams makeHttpActorParams() {
97 HttpActorParams params2 = new HttpActorParams();
98 params2.setClientName(CLIENT);
99 params2.setTimeoutSec(TIMEOUT);
100 params2.setOperation(operations);