daa0affec9d0dfdf780f12f7b207f170d4fa161e
[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.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.Map;
33 import java.util.TreeMap;
34 import java.util.function.Consumer;
35 import java.util.function.Function;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.parameters.ValidationResult;
39
40 public class HttpActorParamsTest {
41
42     private static final String CONTAINER = "my-container";
43     private static final String CLIENT = "my-client";
44     private static final int TIMEOUT = 10;
45
46     private static final String PATH1 = "path #1";
47     private static final String PATH2 = "path #2";
48     private static final String URI1 = "uri #1";
49     private static final String URI2 = "uri #2";
50
51     private Map<String, String> paths;
52     private HttpActorParams params;
53
54     /**
55      * Initializes {@link #paths} with two items and {@link params} with a fully populated
56      * object.
57      */
58     @Before
59     public void setUp() {
60         paths = new TreeMap<>();
61         paths.put(PATH1, URI1);
62         paths.put(PATH2, URI2);
63
64         params = makeHttpActorParams();
65     }
66
67     @Test
68     public void testMakeOperationParameters() {
69         Function<String, Map<String, Object>> maker = params.makeOperationParameters(CONTAINER);
70         assertNull(maker.apply("unknown-operation"));
71
72         Map<String, Object> subparam = maker.apply(PATH1);
73         assertNotNull(subparam);
74         assertEquals("{clientName=my-client, path=uri #1, timeoutSec=10}", new TreeMap<>(subparam).toString());
75
76         subparam = maker.apply(PATH2);
77         assertNotNull(subparam);
78         assertEquals("{clientName=my-client, path=uri #2, timeoutSec=10}", new TreeMap<>(subparam).toString());
79     }
80
81     @Test
82     public void testDoValidation() {
83         assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException();
84
85         // invalid param
86         params.setClientName(null);
87         assertThatThrownBy(() -> params.doValidation(CONTAINER))
88                         .isInstanceOf(ParameterValidationRuntimeException.class);
89     }
90
91     @Test
92     public void testValidate() {
93         assertTrue(params.validate(CONTAINER).isValid());
94
95         testValidateField("clientName", "null", params2 -> params2.setClientName(null));
96         testValidateField("path", "null", params2 -> params2.setPath(null));
97         testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
98
99         // check edge cases
100         params.setTimeoutSec(0);
101         assertTrue(params.validate(CONTAINER).isValid());
102
103         params.setTimeoutSec(1);
104         assertTrue(params.validate(CONTAINER).isValid());
105
106         // one path value is null
107         testValidateField(PATH2, "null", params2 -> paths.put(PATH2, null));
108     }
109
110     private void testValidateField(String fieldName, String expected, Consumer<HttpActorParams> makeInvalid) {
111
112         // original params should be valid
113         ValidationResult result = params.validate(CONTAINER);
114         assertTrue(fieldName, result.isValid());
115
116         // make invalid params
117         HttpActorParams params2 = makeHttpActorParams();
118         makeInvalid.accept(params2);
119         result = params2.validate(CONTAINER);
120         assertFalse(fieldName, result.isValid());
121         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
122     }
123
124     private HttpActorParams makeHttpActorParams() {
125         HttpActorParams params2 = new HttpActorParams();
126         params2.setClientName(CLIENT);
127         params2.setTimeoutSec(TIMEOUT);
128         params2.setPath(paths);
129
130         return params2;
131     }
132 }