Convert models to JUnit 5
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / HttpParamsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actorserviceprovider.parameters;
23
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;
28
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;
34
35 class HttpParamsTest {
36
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;
41
42     private HttpParams params;
43
44     @BeforeEach
45      void setUp() {
46         params = HttpParams.builder().clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
47     }
48
49     @Test
50      void testValidate() {
51         assertTrue(params.validate(CONTAINER).isValid());
52
53         testValidateField("clientName", "null", bldr -> bldr.clientName(null));
54         testValidateField("path", "null", bldr -> bldr.path(null));
55         testValidateField("timeoutSec", "minimum", bldr -> bldr.timeoutSec(-1));
56
57         // check edge cases
58         assertFalse(params.toBuilder().clientName("").build().validate(CONTAINER).isValid());
59
60         assertTrue(params.toBuilder().path("").build().validate(CONTAINER).isValid());
61
62         assertFalse(params.toBuilder().timeoutSec(0).build().validate(CONTAINER).isValid());
63         assertTrue(params.toBuilder().timeoutSec(1).build().validate(CONTAINER).isValid());
64     }
65
66     @Test
67      void testBuilder_testToBuilder() {
68         assertEquals(CLIENT, params.getClientName());
69         assertEquals(PATH, params.getPath());
70         assertEquals(TIMEOUT, params.getTimeoutSec());
71
72         assertEquals(params, params.toBuilder().build());
73     }
74
75     private void testValidateField(String fieldName, String expected,
76                     @SuppressWarnings("rawtypes") Function<HttpParamsBuilder, HttpParamsBuilder> makeInvalid) {
77
78         // original params should be valid
79         ValidationResult result = params.validate(CONTAINER);
80         assertTrue(result.isValid(), fieldName);
81
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);
86     }
87 }