2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
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;
32 import java.util.concurrent.CompletableFuture;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.common.endpoints.http.client.HttpClient;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actorserviceprovider.Operation;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.actorserviceprovider.Util;
43 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
49 public class HttpOperatorTest {
51 private static final String ACTOR = "my-actor";
52 private static final String OPERATION = "my-name";
53 private static final String HTTP_CLIENT = "my-client";
54 private static final String PATH = "/my-path";
55 private static final int TIMEOUT = 100;
58 private HttpClient client;
61 private HttpClientFactory factory;
63 private MyOperator oper;
66 * Initializes fields, including {@link #oper}, and resets the static fields used by
71 MockitoAnnotations.initMocks(this);
73 when(factory.get(HTTP_CLIENT)).thenReturn(client);
75 oper = new MyOperator();
77 HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
78 Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
79 oper.configure(paramMap);
83 public void testHttpOperator() {
84 assertEquals(ACTOR, oper.getActorName());
85 assertEquals(OPERATION, oper.getName());
86 assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
90 public void testDoConfigureMapOfStringObject_testGetConfig() {
91 // start with an UNCONFIGURED operator
93 oper = new MyOperator();
95 assertNull(oper.getCurrentConfig());
97 HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
98 Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
99 oper.configure(paramMap);
101 assertNotNull(oper.getCurrentConfig());
103 // test invalid parameters
104 paramMap.remove("path");
105 assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
109 public void testBuildOperation() {
110 HttpOperator oper2 = new MyOperator();
111 assertNotNull(oper2);
112 assertNotNull(oper2.getClientFactory());
114 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
115 ControlLoopEventContext context = new ControlLoopEventContext(event);
116 ControlLoopOperationParams params =
117 ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
120 assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params));
122 // configure and start it
123 HttpParams params2 = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
124 Map<String, Object> paramMap = Util.translateToMap(OPERATION, params2);
125 oper2.configure(paramMap);
128 Operation operation1 = oper2.buildOperation(params);
129 assertNotNull(operation1);
131 Operation operation2 = oper2.buildOperation(params);
132 assertNotNull(operation2);
133 assertNotSame(operation1, operation2);
135 // with no operation-maker
136 HttpOperator oper3 = new HttpOperator(ACTOR, OPERATION);
137 assertThatThrownBy(() -> oper3.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);
141 public void testGetClientFactory() {
142 HttpOperator oper2 = new HttpOperator(ACTOR, OPERATION);
143 assertNotNull(oper2.getClientFactory());
146 private class MyOperator extends HttpOperator {
147 public MyOperator() {
148 super(ACTOR, OPERATION, MyOperation::new);
152 protected HttpClientFactory getClientFactory() {
157 private class MyOperation extends HttpOperation<String> {
158 public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
159 super(params, config, String.class);
163 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {