2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actorserviceprovider.impl;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.Mockito.when;
30 import java.util.Collections;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.TestInstance;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.onap.policy.common.endpoints.http.client.HttpClient;
40 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
41 import org.onap.policy.controlloop.actorserviceprovider.Util;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
48 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
49 @ExtendWith(MockitoExtension.class)
50 class HttpPollingOperatorTest {
51 private static final String ACTOR = "my-actor";
52 private static final String OPERATION = "my-name";
53 private static final String CLIENT = "my-client";
54 private static final String PATH = "/my-path";
55 private static final String POLL_PATH = "my-path-get/";
56 private static final int MAX_POLLS = 3;
57 private static final int POLL_WAIT_SEC = 20;
58 private static final int TIMEOUT = 100;
61 private HttpClient client;
63 private HttpClientFactory factory;
65 private HttpPollingOperator oper;
68 * Initializes fields, including {@link #oper}, and resets the static fields used by
73 when(factory.get(CLIENT)).thenReturn(client);
75 oper = new MyOperator();
77 HttpPollingParams params = HttpPollingParams.builder().pollPath(POLL_PATH).maxPolls(MAX_POLLS)
78 .pollWaitSec(POLL_WAIT_SEC).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
79 Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
80 oper.configure(paramMap);
84 void testConstructor() {
85 assertEquals(ACTOR, oper.getActorName());
86 assertEquals(OPERATION, oper.getName());
87 assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
91 void testDoConfigure_testGetters() {
92 assertTrue(oper.getCurrentConfig() instanceof HttpPollingConfig);
94 // test invalid parameters
95 Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, HttpPollingParams.builder().build());
96 assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class);
100 void testGetClientFactory() {
101 HttpPollingOperator oper2 = new HttpPollingOperator(ACTOR, OPERATION);
102 assertNotNull(oper2.getClientFactory());
106 private class MyOperator extends HttpPollingOperator {
107 public MyOperator() {
108 super(ACTOR, OPERATION, MyOperation::new);
112 protected HttpClientFactory getClientFactory() {
117 private static class MyOperation extends HttpOperation<String> {
118 public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
119 super(params, config, String.class, Collections.emptyList());