Clean up and enhancement of Actor re-design
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperatorTest.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.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Map;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.common.endpoints.http.client.HttpClient;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
38 import org.onap.policy.controlloop.actorserviceprovider.Util;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
41
42 public class HttpOperatorTest {
43
44     private static final String ACTOR = "my-actor";
45     private static final String OPERATION = "my-name";
46     private static final String CLIENT = "my-client";
47     private static final String PATH = "my-path";
48     private static final long TIMEOUT = 100;
49
50     @Mock
51     private HttpClient client;
52
53     private HttpOperator oper;
54
55     /**
56      * Initializes fields, including {@link #oper}.
57      */
58     @Before
59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61
62         oper = new HttpOperator(ACTOR, OPERATION);
63     }
64
65     @Test
66     public void testDoConfigureMapOfStringObject_testGetClient_testGetPath_testGetTimeoutSec() {
67         assertNull(oper.getClient());
68         assertNull(oper.getPath());
69         assertEquals(0L, oper.getTimeoutSec());
70
71         oper = new HttpOperator(ACTOR, OPERATION) {
72             @Override
73             protected HttpClientFactory getClientFactory() {
74                 HttpClientFactory factory = mock(HttpClientFactory.class);
75                 when(factory.get(CLIENT)).thenReturn(client);
76                 return factory;
77             }
78         };
79
80         HttpParams params = HttpParams.builder().clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
81         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
82         oper.configure(paramMap);
83
84         assertSame(client, oper.getClient());
85         assertEquals(PATH, oper.getPath());
86         assertEquals(TIMEOUT, oper.getTimeoutSec());
87
88         // test invalid parameters
89         paramMap.remove("path");
90         assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
91     }
92
93     @Test
94     public void testHttpOperator() {
95         assertEquals(ACTOR, oper.getActorName());
96         assertEquals(OPERATION, oper.getName());
97         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
98     }
99
100     @Test
101     public void testGetClient() {
102         assertNotNull(oper.getClientFactory());
103     }
104 }