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.assertSame;
 
  26 import static org.mockito.Mockito.when;
 
  28 import java.util.List;
 
  29 import java.util.concurrent.atomic.AtomicReference;
 
  30 import java.util.function.BiFunction;
 
  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.controlloop.actorserviceprovider.Operation;
 
  36 import org.onap.policy.controlloop.actorserviceprovider.Util;
 
  37 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
 
  40 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
 
  41 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
 
  42 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
 
  43 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
 
  45 public class BidirectionalTopicOperatorTest {
 
  46     private static final String ACTOR = "my-actor";
 
  47     private static final String OPERATION = "my-operation";
 
  48     private static final String MY_SOURCE = "my-source";
 
  49     private static final String MY_SINK = "my-target";
 
  50     private static final int TIMEOUT_SEC = 10;
 
  53     private BidirectionalTopicManager mgr;
 
  55     private BidirectionalTopicHandler handler;
 
  57     private Forwarder forwarder;
 
  59     private BidirectionalTopicOperation<String, Integer> operation;
 
  61     private List<SelectorKey> keys;
 
  62     private BidirectionalTopicParams params;
 
  63     private MyOperator oper;
 
  70         MockitoAnnotations.initMocks(this);
 
  72         keys = List.of(new SelectorKey(""));
 
  74         when(mgr.getTopicHandler(MY_SINK, MY_SOURCE)).thenReturn(handler);
 
  75         when(handler.addForwarder(keys)).thenReturn(forwarder);
 
  77         oper = new MyOperator(keys);
 
  79         params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
 
  81         oper.configure(Util.translateToMap(OPERATION, params));
 
  86     public void testConstructor_testGetParams_testGetTopicHandler_testGetForwarder() {
 
  87         assertEquals(ACTOR, oper.getActorName());
 
  88         assertEquals(OPERATION, oper.getName());
 
  89         assertEquals(params, oper.getParams());
 
  90         assertSame(handler, oper.getTopicHandler());
 
  91         assertSame(forwarder, oper.getForwarder());
 
  95     public void testDoConfigure() {
 
  99         params.setSourceTopic(null);
 
 100         assertThatThrownBy(() -> oper.configure(Util.translateToMap(OPERATION, params)))
 
 101                         .isInstanceOf(ParameterValidationRuntimeException.class);
 
 105     public void testMakeOperator() {
 
 106         AtomicReference<ControlLoopOperationParams> paramsRef = new AtomicReference<>();
 
 107         AtomicReference<BidirectionalTopicOperator> operRef = new AtomicReference<>();
 
 110         BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator,
 
 111                     BidirectionalTopicOperation<String, Integer>> maker =
 
 112                         (params, operator) -> {
 
 113                             paramsRef.set(params);
 
 114                             operRef.set(operator);
 
 119         BidirectionalTopicOperator oper2 =
 
 120                         BidirectionalTopicOperator.makeOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey(""));
 
 122         assertEquals(ACTOR, oper2.getActorName());
 
 123         assertEquals(OPERATION, oper2.getName());
 
 125         ControlLoopOperationParams params2 = ControlLoopOperationParams.builder().build();
 
 127         assertSame(operation, oper2.buildOperation(params2));
 
 128         assertSame(params2, paramsRef.get());
 
 129         assertSame(oper2, operRef.get());
 
 133     private class MyOperator extends BidirectionalTopicOperator {
 
 134         public MyOperator(List<SelectorKey> selectorKeys) {
 
 135             super(ACTOR, OPERATION, mgr, selectorKeys);
 
 139         public Operation buildOperation(ControlLoopOperationParams params) {