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;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.parameters.ValidationResult;
40 public class HttpActorParamsTest {
42 private static final String CONTAINER = "my-container";
43 private static final String CLIENT = "my-client";
44 private static final long TIMEOUT = 10;
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";
51 private Map<String, String> paths;
52 private HttpActorParams params;
55 * Initializes {@link #paths} with two items and {@link params} with a fully populated
60 paths = new TreeMap<>();
61 paths.put(PATH1, URI1);
62 paths.put(PATH2, URI2);
64 params = makeHttpActorParams();
68 public void testMakeOperationParameters() {
69 Function<String, Map<String, Object>> maker = params.makeOperationParameters(CONTAINER);
70 assertNull(maker.apply("unknown-operation"));
72 Map<String, Object> subparam = maker.apply(PATH1);
73 assertNotNull(subparam);
74 assertEquals("{clientName=my-client, path=uri #1, timeoutSec=10}", new TreeMap<>(subparam).toString());
76 subparam = maker.apply(PATH2);
77 assertNotNull(subparam);
78 assertEquals("{clientName=my-client, path=uri #2, timeoutSec=10}", new TreeMap<>(subparam).toString());
82 public void testDoValidation() {
83 assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException();
86 params.setClientName(null);
87 assertThatThrownBy(() -> params.doValidation(CONTAINER))
88 .isInstanceOf(ParameterValidationRuntimeException.class);
92 public void testValidate() {
93 assertTrue(params.validate(CONTAINER).isValid());
95 testValidateField("clientName", "null", params2 -> params2.setClientName(null));
96 testValidateField("path", "null", params2 -> params2.setPath(null));
97 testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
100 params.setTimeoutSec(0);
101 assertTrue(params.validate(CONTAINER).isValid());
103 params.setTimeoutSec(1);
104 assertTrue(params.validate(CONTAINER).isValid());
106 // one path value is null
107 testValidateField(PATH2, "null", params2 -> paths.put(PATH2, null));
110 private void testValidateField(String fieldName, String expected, Consumer<HttpActorParams> makeInvalid) {
112 // original params should be valid
113 ValidationResult result = params.validate(CONTAINER);
114 assertTrue(fieldName, result.isValid());
116 // make invalid params
117 HttpActorParams params2 = makeHttpActorParams();
118 makeInvalid.accept(params2);
119 result = params2.validate(CONTAINER);
120 assertFalse(fieldName, result.isValid());
121 assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
124 private HttpActorParams makeHttpActorParams() {
125 HttpActorParams params2 = new HttpActorParams();
126 params2.setClientName(CLIENT);
127 params2.setTimeoutSec(TIMEOUT);
128 params2.setPath(paths);