Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / HttpActorParamsTest.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 public class HttpActorParamsTest {
36
37     private static final String CONTAINER = "my-container";
38     private static final String CLIENT = "my-client";
39     private static final int TIMEOUT = 10;
40
41     private static final String PATH1 = "path #1";
42     private static final String PATH2 = "path #2";
43     private static final String URI1 = "uri #1";
44     private static final String URI2 = "uri #2";
45
46     private Map<String, Map<String, Object>> operations;
47     private HttpActorParams params;
48
49     /**
50      * Initializes {@link #operations} with two items and {@link params} with a fully
51      * populated object.
52      */
53     @Before
54     public void setUp() {
55         operations = new TreeMap<>();
56         operations.put(PATH1, Map.of("path", URI1));
57         operations.put(PATH2, Map.of("path", URI2));
58
59         params = makeHttpActorParams();
60     }
61
62     @Test
63     public void testValidate() {
64         assertTrue(params.validate(CONTAINER).isValid());
65
66         // only a few fields are required
67         HttpActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations, "timeoutSec", 1),
68                         HttpActorParams.class);
69         assertTrue(sparse.validate(CONTAINER).isValid());
70
71         testValidateField("operation", "null", params2 -> params2.setOperation(null));
72         testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
73
74         // check edge cases
75         params.setTimeoutSec(0);
76         assertFalse(params.validate(CONTAINER).isValid());
77
78         params.setTimeoutSec(1);
79         assertTrue(params.validate(CONTAINER).isValid());
80     }
81
82     private void testValidateField(String fieldName, String expected, Consumer<HttpActorParams> makeInvalid) {
83
84         // original params should be valid
85         ValidationResult result = params.validate(CONTAINER);
86         assertTrue(fieldName, result.isValid());
87
88         // make invalid params
89         HttpActorParams params2 = makeHttpActorParams();
90         makeInvalid.accept(params2);
91         result = params2.validate(CONTAINER);
92         assertFalse(fieldName, result.isValid());
93         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
94     }
95
96     private HttpActorParams makeHttpActorParams() {
97         HttpActorParams params2 = new HttpActorParams();
98         params2.setClientName(CLIENT);
99         params2.setTimeoutSec(TIMEOUT);
100         params2.setOperation(operations);
101
102         return params2;
103     }
104 }