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;
31 import java.util.Collections;
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;
50 public class HttpOperatorTest {
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;
59 private HttpClient client;
62 private HttpClientFactory factory;
64 private MyOperator oper;
67 * Initializes fields, including {@link #oper}, and resets the static fields used by
72 MockitoAnnotations.initMocks(this);
74 when(factory.get(HTTP_CLIENT)).thenReturn(client);
76 oper = new MyOperator();
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);
84 public void testHttpOperator() {
85 assertEquals(ACTOR, oper.getActorName());
86 assertEquals(OPERATION, oper.getName());
87 assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
91 public void testDoConfigureMapOfStringObject_testGetConfig() {
92 // start with an UNCONFIGURED operator
94 oper = new MyOperator();
96 assertNull(oper.getCurrentConfig());
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);
102 assertNotNull(oper.getCurrentConfig());
104 // test invalid parameters
105 paramMap.remove("path");
106 assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
110 public void testBuildOperation() {
111 HttpOperator oper2 = new MyOperator();
112 assertNotNull(oper2);
113 assertNotNull(oper2.getClientFactory());
115 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
116 ControlLoopEventContext context = new ControlLoopEventContext(event);
117 ControlLoopOperationParams params =
118 ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
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);
126 assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params));
130 Operation operation1 = oper2.buildOperation(params);
131 assertNotNull(operation1);
133 Operation operation2 = oper2.buildOperation(params);
134 assertNotNull(operation2);
135 assertNotSame(operation1, operation2);
137 // with no operation-maker
138 HttpOperator oper3 = new HttpOperator(ACTOR, OPERATION);
139 assertThatThrownBy(() -> oper3.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);
143 public void testGetClientFactory() {
144 HttpOperator oper2 = new HttpOperator(ACTOR, OPERATION);
145 assertNotNull(oper2.getClientFactory());
148 private class MyOperator extends HttpOperator {
149 public MyOperator() {
150 super(ACTOR, OPERATION, MyOperation::new);
154 protected HttpClientFactory getClientFactory() {
159 private class MyOperation extends HttpOperation<String> {
160 public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
161 super(params, config, String.class, Collections.emptyList());
165 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {