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.assertThatThrownBy;
 
  24 import static org.junit.Assert.assertEquals;
 
  25 import static org.junit.Assert.assertNotNull;
 
  26 import static org.junit.Assert.assertNotSame;
 
  27 import static org.junit.Assert.assertNull;
 
  28 import static org.junit.Assert.assertSame;
 
  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.HttpParams;
 
  46 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
 
  48 public class HttpOperatorTest {
 
  50     private static final String ACTOR = "my-actor";
 
  51     private static final String OPERATION = "my-name";
 
  52     private static final String HTTP_CLIENT = "my-client";
 
  53     private static final String PATH = "/my-path";
 
  54     private static final int TIMEOUT = 100;
 
  57     private HttpClient client;
 
  60     private HttpClientFactory factory;
 
  62     private MyOperator oper;
 
  65      * Initializes fields, including {@link #oper}, and resets the static fields used by
 
  70         MockitoAnnotations.initMocks(this);
 
  72         when(factory.get(HTTP_CLIENT)).thenReturn(client);
 
  74         oper = new MyOperator();
 
  76         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
 
  77         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
 
  78         oper.configure(paramMap);
 
  82     public void testHttpOperator() {
 
  83         assertEquals(ACTOR, oper.getActorName());
 
  84         assertEquals(OPERATION, oper.getName());
 
  85         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
 
  89     public void testGetClient() {
 
  90         assertNotNull(oper.getClient());
 
  94     public void testMakeOperator() {
 
  95         HttpOperator oper2 = HttpOperator.makeOperator(ACTOR, OPERATION, MyOperation::new);
 
  98         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
 
  99         ControlLoopEventContext context = new ControlLoopEventContext(event);
 
 100         ControlLoopOperationParams params =
 
 101                         ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
 
 103         Operation operation1 = oper2.buildOperation(params);
 
 104         assertNotNull(operation1);
 
 106         Operation operation2 = oper2.buildOperation(params);
 
 107         assertNotNull(operation2);
 
 108         assertNotSame(operation1, operation2);
 
 112     public void testDoConfigureMapOfStringObject_testGetClient_testGetPath_testGetTimeoutMs() {
 
 113         // start with an UNCONFIGURED operator
 
 115         oper = new MyOperator();
 
 117         assertNull(oper.getClient());
 
 118         assertNull(oper.getPath());
 
 121         assertEquals(0L, oper.getTimeoutMs());
 
 123         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
 
 124         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
 
 125         oper.configure(paramMap);
 
 127         assertSame(client, oper.getClient());
 
 128         assertEquals(PATH, oper.getPath());
 
 130         // should use given value
 
 131         assertEquals(TIMEOUT * 1000, oper.getTimeoutMs());
 
 133         // test invalid parameters
 
 134         paramMap.remove("path");
 
 135         assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
 
 138     private class MyOperator extends HttpOperator {
 
 139         public MyOperator() {
 
 140             super(ACTOR, OPERATION);
 
 144         public Operation buildOperation(ControlLoopOperationParams params) {
 
 149         protected HttpClientFactory getClientFactory() {
 
 154     private class MyOperation extends HttpOperation<String> {
 
 155         public MyOperation(ControlLoopOperationParams params, HttpOperator operator) {
 
 156             super(params, operator, String.class);
 
 160         protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {