2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actorserviceprovider.parameters;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import java.util.function.Function;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams.HttpParamsBuilder;
35 class HttpParamsTest {
37 private static final String CONTAINER = "my-container";
38 private static final String CLIENT = "my-client";
39 private static final String PATH = "my-path";
40 private static final int TIMEOUT = 10;
42 private HttpParams params;
46 params = HttpParams.builder().clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
51 assertTrue(params.validate(CONTAINER).isValid());
53 testValidateField("clientName", "null", bldr -> bldr.clientName(null));
54 testValidateField("path", "null", bldr -> bldr.path(null));
55 testValidateField("timeoutSec", "minimum", bldr -> bldr.timeoutSec(-1));
58 assertFalse(params.toBuilder().clientName("").build().validate(CONTAINER).isValid());
60 assertTrue(params.toBuilder().path("").build().validate(CONTAINER).isValid());
62 assertFalse(params.toBuilder().timeoutSec(0).build().validate(CONTAINER).isValid());
63 assertTrue(params.toBuilder().timeoutSec(1).build().validate(CONTAINER).isValid());
67 void testBuilder_testToBuilder() {
68 assertEquals(CLIENT, params.getClientName());
69 assertEquals(PATH, params.getPath());
70 assertEquals(TIMEOUT, params.getTimeoutSec());
72 assertEquals(params, params.toBuilder().build());
75 private void testValidateField(String fieldName, String expected,
76 @SuppressWarnings("rawtypes") Function<HttpParamsBuilder, HttpParamsBuilder> makeInvalid) {
78 // original params should be valid
79 ValidationResult result = params.validate(CONTAINER);
80 assertTrue(result.isValid(), fieldName);
82 // make invalid params
83 result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER);
84 assertFalse(result.isValid(), fieldName);
85 assertThat(result.getResult()).contains(fieldName).contains(expected);