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.actor.so;
 
  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.assertSame;
 
  28 import static org.mockito.Mockito.when;
 
  31 import org.junit.Before;
 
  32 import org.junit.Test;
 
  33 import org.mockito.Mock;
 
  34 import org.mockito.MockitoAnnotations;
 
  35 import org.onap.policy.common.endpoints.http.client.HttpClient;
 
  36 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
 
  37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.Operation;
 
  39 import org.onap.policy.controlloop.actorserviceprovider.Util;
 
  40 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
 
  44 public class SoOperatorTest {
 
  45     private static final String ACTOR = "my-actor";
 
  46     private static final String OPERATION = "my-name";
 
  47     private static final String CLIENT = "my-client";
 
  48     private static final String PATH = "/my-path";
 
  49     private static final String PATH_GET = "my-path-get/";
 
  50     private static final int MAX_GETS = 3;
 
  51     private static final int WAIT_SEC_GETS = 20;
 
  52     private static final int TIMEOUT = 100;
 
  55     private HttpClient client;
 
  58     private HttpClientFactory factory;
 
  61     private SoOperator oper;
 
  64      * Initializes fields, including {@link #oper}, and resets the static fields used by
 
  69         MockitoAnnotations.initMocks(this);
 
  71         when(factory.get(CLIENT)).thenReturn(client);
 
  73         oper = new MyOperator();
 
  75         SoParams params = SoParams.builder().pathGet(PATH_GET).maxGets(MAX_GETS).waitSecGet(WAIT_SEC_GETS)
 
  76                         .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
 
  77         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
 
  78         oper.configure(paramMap);
 
  82     public void testConstructor() {
 
  83         assertEquals(ACTOR, oper.getActorName());
 
  84         assertEquals(OPERATION, oper.getName());
 
  85         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
 
  89     public void testMakeSoOperator() {
 
  90         oper = SoOperator.makeSoOperator(ACTOR, OPERATION, MyOperation::new);
 
  92         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
 
  93         ControlLoopEventContext context = new ControlLoopEventContext(event);
 
  94         ControlLoopOperationParams params =
 
  95                         ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
 
  97         Operation operation1 = oper.buildOperation(params);
 
  98         assertNotNull(operation1);
 
 100         Operation operation2 = oper.buildOperation(params);
 
 101         assertNotNull(operation2);
 
 102         assertNotSame(operation1, operation2);
 
 106     public void testDoConfigure_testGetters() {
 
 107         // should use given values
 
 108         assertSame(client, oper.getClient());
 
 109         assertEquals(PATH_GET, oper.getPathGet());
 
 110         assertEquals(MAX_GETS, oper.getMaxGets());
 
 111         assertEquals(WAIT_SEC_GETS, oper.getWaitSecGet());
 
 113         SoParams params = SoParams.builder().pathGet("unslashed").maxGets(MAX_GETS).waitSecGet(WAIT_SEC_GETS)
 
 114                         .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
 
 115         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
 
 116         oper.configure(paramMap);
 
 117         assertEquals("unslashed/", oper.getPathGet());
 
 119         // test invalid parameters
 
 120         Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, SoParams.builder().build());
 
 121         assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class);
 
 125     private class MyOperator extends SoOperator {
 
 126         public MyOperator() {
 
 127             super(ACTOR, OPERATION);
 
 131         public Operation buildOperation(ControlLoopOperationParams params) {
 
 136         protected HttpClientFactory getClientFactory() {
 
 141     private class MyOperation extends SoOperation {
 
 142         public MyOperation(ControlLoopOperationParams params, SoOperator operator) {
 
 143             super(params, operator);