Fix some sonars in policy-models
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpActorTest.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.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26
27 import java.util.Map;
28 import java.util.TreeMap;
29 import java.util.function.Function;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.controlloop.actorserviceprovider.Util;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpActorParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
35
36 public class HttpActorTest {
37
38     private static final String ACTOR = "my-actor";
39     private static final String UNKNOWN = "unknown";
40     private static final String CLIENT = "my-client";
41     private static final int TIMEOUT = 10;
42
43     private HttpActor<HttpActorParams> actor;
44
45     @Before
46     public void setUp() {
47         actor = new HttpActor<>(ACTOR, HttpActorParams.class);
48     }
49
50     @Test
51     public void testMakeOperatorParameters() {
52         HttpActorParams params = new HttpActorParams();
53         params.setClientName(CLIENT);
54         params.setTimeoutSec(TIMEOUT);
55
56         // @formatter:off
57         params.setOperations(Map.of(
58                         "operA", Map.of("path", "urlA"),
59                         "operB", Map.of("path", "urlB")));
60         // @formatter:on
61
62         final HttpActor<HttpActorParams> prov = new HttpActor<>(ACTOR, HttpActorParams.class);
63         Function<String, Map<String, Object>> maker =
64                         prov.makeOperatorParameters(Util.translateToMap(prov.getName(), params));
65
66         assertNull(maker.apply(UNKNOWN));
67
68         // use a TreeMap to ensure the properties are sorted
69         assertEquals("{clientName=my-client, path=urlA, timeoutSec=10}",
70                         new TreeMap<>(maker.apply("operA")).toString());
71
72         assertEquals("{clientName=my-client, path=urlB, timeoutSec=10}",
73                         new TreeMap<>(maker.apply("operB")).toString());
74
75         // with invalid actor parameters
76         params.setOperations(null);
77         Map<String, Object> map = Util.translateToMap(prov.getName(), params);
78         assertThatThrownBy(() -> prov.makeOperatorParameters(map))
79                         .isInstanceOf(ParameterValidationRuntimeException.class);
80     }
81
82     @Test
83     public void testHttpActor() {
84         assertEquals(ACTOR, actor.getName());
85         assertEquals(ACTOR, actor.getFullName());
86     }
87 }