Add property lists to Actors
[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.assertThatIllegalStateException;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNotSame;
28 import static org.junit.Assert.assertNull;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Collections;
32 import java.util.Map;
33 import java.util.concurrent.CompletableFuture;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.common.endpoints.http.client.HttpClient;
39 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.actorserviceprovider.Operation;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
43 import org.onap.policy.controlloop.actorserviceprovider.Util;
44 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
49
50 public class HttpOperatorTest {
51
52     private static final String ACTOR = "my-actor";
53     private static final String OPERATION = "my-name";
54     private static final String HTTP_CLIENT = "my-client";
55     private static final String PATH = "/my-path";
56     private static final int TIMEOUT = 100;
57
58     @Mock
59     private HttpClient client;
60
61     @Mock
62     private HttpClientFactory factory;
63
64     private MyOperator oper;
65
66     /**
67      * Initializes fields, including {@link #oper}, and resets the static fields used by
68      * the REST server.
69      */
70     @Before
71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73
74         when(factory.get(HTTP_CLIENT)).thenReturn(client);
75
76         oper = new MyOperator();
77
78         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
79         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
80         oper.configure(paramMap);
81     }
82
83     @Test
84     public void testHttpOperator() {
85         assertEquals(ACTOR, oper.getActorName());
86         assertEquals(OPERATION, oper.getName());
87         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
88     }
89
90     @Test
91     public void testDoConfigureMapOfStringObject_testGetConfig() {
92         // start with an UNCONFIGURED operator
93         oper.shutdown();
94         oper = new MyOperator();
95
96         assertNull(oper.getCurrentConfig());
97
98         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
99         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
100         oper.configure(paramMap);
101
102         assertNotNull(oper.getCurrentConfig());
103
104         // test invalid parameters
105         paramMap.remove("path");
106         assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
107     }
108
109     @Test
110     public void testBuildOperation() {
111         HttpOperator oper2 = new MyOperator();
112         assertNotNull(oper2);
113         assertNotNull(oper2.getClientFactory());
114
115         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
116         ControlLoopEventContext context = new ControlLoopEventContext(event);
117         ControlLoopOperationParams params =
118                         ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
119
120         // configure and start it
121         HttpParams params2 = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
122         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params2);
123         oper2.configure(paramMap);
124
125         // not running yet
126         assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params));
127
128         oper2.start();
129
130         Operation operation1 = oper2.buildOperation(params);
131         assertNotNull(operation1);
132
133         Operation operation2 = oper2.buildOperation(params);
134         assertNotNull(operation2);
135         assertNotSame(operation1, operation2);
136
137         // with no operation-maker
138         HttpOperator oper3 = new HttpOperator(ACTOR, OPERATION);
139         assertThatThrownBy(() -> oper3.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);
140     }
141
142     @Test
143     public void testGetClientFactory() {
144         HttpOperator oper2 = new HttpOperator(ACTOR, OPERATION);
145         assertNotNull(oper2.getClientFactory());
146     }
147
148     private class MyOperator extends HttpOperator {
149         public MyOperator() {
150             super(ACTOR, OPERATION, MyOperation::new);
151         }
152
153         @Override
154         protected HttpClientFactory getClientFactory() {
155             return factory;
156         }
157     }
158
159     private class MyOperation extends HttpOperation<String> {
160         public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
161             super(params, config, String.class, Collections.emptyList());
162         }
163
164         @Override
165         protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
166             return null;
167         }
168     }
169 }