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.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.util.function.Function;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams.HttpPollingParamsBuilder;
34 public class HttpPollingParamsTest {
35 private static final String CONTAINER = "my-container";
36 private static final String CLIENT = "my-client";
37 private static final String PATH = "my-path";
38 private static final String POLL_PATH = "my-poll-path";
39 private static final int MAX_POLLS = 3;
40 private static final int POLL_WAIT_SEC = 20;
41 private static final int TIMEOUT = 10;
43 private HttpPollingParams params;
47 params = HttpPollingParams.builder().pollPath(POLL_PATH).maxPolls(MAX_POLLS).pollWaitSec(POLL_WAIT_SEC)
48 .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
52 public void testValidate() {
53 assertTrue(params.validate(CONTAINER).isValid());
55 testValidateField("pollPath", "null", bldr -> bldr.pollPath(null));
56 testValidateField("maxPolls", "minimum", bldr -> bldr.maxPolls(-1));
57 testValidateField("pollWaitSec", "minimum", bldr -> bldr.pollWaitSec(-1));
59 // validate one of the superclass fields
60 testValidateField("clientName", "null", bldr -> bldr.clientName(null));
63 assertTrue(params.toBuilder().maxPolls(0).build().validate(CONTAINER).isValid());
64 assertFalse(params.toBuilder().pollWaitSec(0).build().validate(CONTAINER).isValid());
65 assertTrue(params.toBuilder().pollWaitSec(1).build().validate(CONTAINER).isValid());
69 public void testBuilder_testToBuilder() {
70 assertEquals(CLIENT, params.getClientName());
72 assertEquals(POLL_PATH, params.getPollPath());
73 assertEquals(MAX_POLLS, params.getMaxPolls());
74 assertEquals(POLL_WAIT_SEC, params.getPollWaitSec());
76 assertEquals(params, params.toBuilder().build());
79 private void testValidateField(String fieldName, String expected,
80 Function<HttpPollingParamsBuilder<?,?>, HttpPollingParamsBuilder<?,?>> makeInvalid) {
82 // original params should be valid
83 ValidationResult result = params.validate(CONTAINER);
84 assertTrue(fieldName, result.isValid());
86 // make invalid params
87 result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER);
88 assertFalse(fieldName, result.isValid());
89 assertThat(result.getResult()).contains(fieldName).contains(expected);