2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.UUID;
34 import java.util.concurrent.CompletableFuture;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.policy.common.endpoints.http.client.HttpClient;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
42 import org.onap.policy.controlloop.actorserviceprovider.Operation;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
44 import org.onap.policy.controlloop.actorserviceprovider.Util;
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 @RunWith(MockitoJUnitRunner.class)
51 public class HttpOperatorTest {
53 private static final String ACTOR = "my-actor";
54 private static final String OPERATION = "my-name";
55 private static final String HTTP_CLIENT = "my-client";
56 private static final String PATH = "/my-path";
57 private static final int TIMEOUT = 100;
60 private HttpClient client;
63 private HttpClientFactory factory;
65 private MyOperator oper;
68 * Initializes fields, including {@link #oper}, and resets the static fields used by
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 ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION)
115 .requestId(UUID.randomUUID()).build();
117 // configure and start it
118 HttpParams params2 = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
119 Map<String, Object> paramMap = Util.translateToMap(OPERATION, params2);
120 oper2.configure(paramMap);
123 assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params));
127 Operation operation1 = oper2.buildOperation(params);
128 assertNotNull(operation1);
130 Operation operation2 = oper2.buildOperation(params);
131 assertNotNull(operation2);
132 assertNotSame(operation1, operation2);
134 // with no operation-maker
135 HttpOperator oper3 = new HttpOperator(ACTOR, OPERATION);
136 assertThatThrownBy(() -> oper3.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);
140 public void testGetClientFactory() {
141 HttpOperator oper2 = new HttpOperator(ACTOR, OPERATION);
142 assertNotNull(oper2.getClientFactory());
145 private class MyOperator extends HttpOperator {
146 public MyOperator() {
147 super(ACTOR, OPERATION, MyOperation::new);
151 protected HttpClientFactory getClientFactory() {
156 private class MyOperation extends HttpOperation<String> {
157 public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
158 super(params, config, String.class, Collections.emptyList());
162 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {