Merge "Convert models to JUnit 5"
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / HttpPollingParamsTest.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.HttpPollingParams.HttpPollingParamsBuilder;
34
35 class HttpPollingParamsTest {
36     private static final String CONTAINER = "my-container";
37     private static final String CLIENT = "my-client";
38     private static final String PATH = "my-path";
39     private static final String POLL_PATH = "my-poll-path";
40     private static final int MAX_POLLS = 3;
41     private static final int POLL_WAIT_SEC = 20;
42     private static final int TIMEOUT = 10;
43
44     private HttpPollingParams params;
45
46     @BeforeEach
47      void setUp() {
48         params = HttpPollingParams.builder().pollPath(POLL_PATH).maxPolls(MAX_POLLS).pollWaitSec(POLL_WAIT_SEC)
49                         .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
50     }
51
52     @Test
53      void testValidate() {
54         assertTrue(params.validate(CONTAINER).isValid());
55
56         testValidateField("pollPath", "null", bldr -> bldr.pollPath(null));
57         testValidateField("maxPolls", "minimum", bldr -> bldr.maxPolls(-1));
58         testValidateField("pollWaitSec", "minimum", bldr -> bldr.pollWaitSec(-1));
59
60         // validate one of the superclass fields
61         testValidateField("clientName", "null", bldr -> bldr.clientName(null));
62
63         // check edge cases
64         assertTrue(params.toBuilder().maxPolls(0).build().validate(CONTAINER).isValid());
65         assertFalse(params.toBuilder().pollWaitSec(0).build().validate(CONTAINER).isValid());
66         assertTrue(params.toBuilder().pollWaitSec(1).build().validate(CONTAINER).isValid());
67     }
68
69     @Test
70      void testBuilder_testToBuilder() {
71         assertEquals(CLIENT, params.getClientName());
72
73         assertEquals(POLL_PATH, params.getPollPath());
74         assertEquals(MAX_POLLS, params.getMaxPolls());
75         assertEquals(POLL_WAIT_SEC, params.getPollWaitSec());
76
77         assertEquals(params, params.toBuilder().build());
78     }
79
80     private void testValidateField(String fieldName, String expected,
81                     Function<HttpPollingParamsBuilder<?, ?>, HttpPollingParamsBuilder<?, ?>> makeInvalid) {
82
83         // original params should be valid
84         ValidationResult result = params.validate(CONTAINER);
85         assertTrue(result.isValid(), fieldName);
86
87         // make invalid params
88         result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER);
89         assertFalse(result.isValid(), fieldName);
90         assertThat(result.getResult()).contains(fieldName).contains(expected);
91     }
92 }