Changes for Checkstyle 8.32
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / HttpPollingActorParamsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Map;
28 import java.util.TreeMap;
29 import java.util.function.Consumer;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.controlloop.actorserviceprovider.Util;
34
35
36 public class HttpPollingActorParamsTest {
37     private static final String CONTAINER = "my-container";
38     private static final String CLIENT = "my-client";
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 static final String PATH1 = "path #1";
45     private static final String PATH2 = "path #2";
46     private static final String URI1 = "uri #1";
47     private static final String URI2 = "uri #2";
48
49     private Map<String, Map<String, Object>> operations;
50     private HttpPollingActorParams params;
51
52     /**
53      * Initializes {@link #operations} with two items and {@link params} with a fully
54      * populated object.
55      */
56     @Before
57     public void setUp() {
58         operations = new TreeMap<>();
59         operations.put(PATH1, Map.of("path", URI1));
60         operations.put(PATH2, Map.of("path", URI2));
61
62         params = makeHttpPollingActorParams();
63     }
64
65     @Test
66     public void testValidate() {
67         assertTrue(params.validate(CONTAINER).isValid());
68
69         // only a few fields are required
70         HttpPollingActorParams sparse = Util.translate(CONTAINER, Map.of(ActorParams.OPERATIONS_FIELD, operations),
71                         HttpPollingActorParams.class);
72         assertTrue(sparse.validate(CONTAINER).isValid());
73
74         testValidateField("maxPolls", "minimum", params2 -> params2.setMaxPolls(-1));
75         testValidateField("pollWaitSec", "minimum", params2 -> params2.setPollWaitSec(0));
76
77         // check fields from superclass
78         testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null));
79         testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
80
81         // check edge cases
82         params.setMaxPolls(0);
83         assertTrue(params.validate(CONTAINER).isValid());
84         params.setMaxPolls(MAX_POLLS);
85
86         params.setPollWaitSec(1);
87         assertTrue(params.validate(CONTAINER).isValid());
88         params.setPollWaitSec(POLL_WAIT_SEC);
89     }
90
91     private void testValidateField(String fieldName, String expected, Consumer<HttpPollingActorParams> makeInvalid) {
92
93         // original params should be valid
94         ValidationResult result = params.validate(CONTAINER);
95         assertTrue(fieldName, result.isValid());
96
97         // make invalid params
98         HttpPollingActorParams params2 = makeHttpPollingActorParams();
99         makeInvalid.accept(params2);
100         result = params2.validate(CONTAINER);
101         assertFalse(fieldName, result.isValid());
102         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
103     }
104
105     private HttpPollingActorParams makeHttpPollingActorParams() {
106         HttpPollingActorParams params2 = new HttpPollingActorParams();
107         params2.setClientName(CLIENT);
108         params2.setTimeoutSec(TIMEOUT);
109         params2.setOperations(operations);
110
111         params2.setPollWaitSec(POLL_WAIT_SEC);
112         params2.setMaxPolls(MAX_POLLS);
113         params2.setPollPath(POLL_PATH);
114
115         return params2;
116     }
117 }