diff options
Diffstat (limited to 'models-interactions/model-actors/actorServiceProvider')
21 files changed, 691 insertions, 333 deletions
diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActor.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActor.java index 1e44a170c..564bbc346 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActor.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActor.java @@ -27,14 +27,23 @@ import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException; import org.onap.policy.controlloop.actorserviceprovider.Util; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicActorParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager; /** - * Actor that uses a bidirectional topic. The actor's parameters must be a - * {@link BidirectionalTopicActorParams}. + * Actor that uses a bidirectional topic. The actor's operator parameters are expected to + * be an {@link BidirectionalTopicParams}. + * + * @param <P> type of parameters */ -public class BidirectionalTopicActor extends ActorImpl implements BidirectionalTopicManager { +public class BidirectionalTopicActor<P extends BidirectionalTopicActorParams> extends ActorImpl + implements BidirectionalTopicManager { + + /** + * Class of parameters. + */ + private final Class<P> paramsClass; /** * Maps a pair of sink and source topic names to their bidirectional topic. @@ -47,8 +56,9 @@ public class BidirectionalTopicActor extends ActorImpl implements BidirectionalT * * @param name actor's name */ - public BidirectionalTopicActor(String name) { + public BidirectionalTopicActor(String name, Class<P> paramsClass) { super(name); + this.paramsClass = paramsClass; } @Override @@ -92,7 +102,7 @@ public class BidirectionalTopicActor extends ActorImpl implements BidirectionalT String actorName = getName(); // @formatter:off - return Util.translate(actorName, actorParameters, BidirectionalTopicActorParams.class) + return Util.translate(actorName, actorParameters, paramsClass) .doValidation(actorName) .makeOperationParameters(actorName); // @formatter:on diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java index d1e21f8fd..1ae80490c 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperation.java @@ -23,14 +23,13 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import lombok.Getter; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; -import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; @@ -55,34 +54,40 @@ public abstract class BidirectionalTopicOperation<Q, S> extends OperationPartial SUCCESS, FAILURE, STILL_WAITING } - // fields extracted from the operator - - private final BidirectionalTopicHandler topicHandler; - private final Forwarder forwarder; - private final BidirectionalTopicParams topicParams; - private final long timeoutMs; + /** + * Configuration for this operation. + */ + private final BidirectionalTopicConfig config; /** * Response class. */ private final Class<S> responseClass; + // fields extracted from "config" + + private final BidirectionalTopicHandler topicHandler; + private final Forwarder forwarder; + /** * Constructs the object. * * @param params operation parameters - * @param operator operator that created this operation + * @param config configuration for this operation * @param clazz response class */ - public BidirectionalTopicOperation(ControlLoopOperationParams params, BidirectionalTopicOperator operator, + public BidirectionalTopicOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config, Class<S> clazz) { - super(params, operator); - this.topicHandler = operator.getTopicHandler(); - this.forwarder = operator.getForwarder(); - this.topicParams = operator.getParams(); + super(params, config); + this.config = config; this.responseClass = clazz; - this.timeoutMs = TimeUnit.MILLISECONDS.convert(topicParams.getTimeoutSec(), TimeUnit.SECONDS); + this.forwarder = config.getForwarder(); + this.topicHandler = config.getTopicHandler(); + } + + public long getTimeoutMs() { + return config.getTimeoutMs(); } /** @@ -91,7 +96,7 @@ public abstract class BidirectionalTopicOperation<Q, S> extends OperationPartial @Override protected long getTimeoutMs(Integer timeoutSec) { // TODO move this method to the superclass - return (timeoutSec == null || timeoutSec == 0 ? this.timeoutMs : super.getTimeoutMs(timeoutSec)); + return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec)); } /** diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperator.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperator.java index 51689e49b..43c8b8872 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperator.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperator.java @@ -23,24 +23,30 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.function.BiFunction; import lombok.Getter; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager; -import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder; import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey; /** * Operator that uses a bidirectional topic. Topic operators may share a * {@link BidirectionalTopicHandler}. */ -public abstract class BidirectionalTopicOperator extends OperatorPartial { +public class BidirectionalTopicOperator extends OperatorPartial { + + /** + * Function to make an operation. + */ + @SuppressWarnings("rawtypes") + private final OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> operationMaker; /** * Manager from which to get the topic handlers. @@ -52,30 +58,26 @@ public abstract class BidirectionalTopicOperator extends OperatorPartial { */ private final List<SelectorKey> selectorKeys; - /* - * The remaining fields are initialized when configure() is invoked, thus they may - * change. - */ - /** - * Current parameters. While {@link params} may change, the values contained within it - * will not, thus operations may copy it. + * Current configuration. This is set by {@link #doConfigure(Map)}. */ @Getter - private BidirectionalTopicParams params; + private BidirectionalTopicConfig currentConfig; - /** - * Topic handler associated with the parameters. - */ - @Getter - private BidirectionalTopicHandler topicHandler; /** - * Forwarder associated with the parameters. + * Constructs the object. + * + * @param actorName name of the actor with which this operator is associated + * @param name operation name + * @param topicManager manager from which to get the topic handler + * @param selectorKeys keys used to extract the fields used to select responses for + * this operator */ - @Getter - private Forwarder forwarder; - + protected BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager, + List<SelectorKey> selectorKeys) { + this(actorName, name, topicManager, selectorKeys, null); + } /** * Constructs the object. @@ -86,75 +88,70 @@ public abstract class BidirectionalTopicOperator extends OperatorPartial { * @param selectorKeys keys used to extract the fields used to select responses for * this operator */ + // @formatter:off public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager, - List<SelectorKey> selectorKeys) { + List<SelectorKey> selectorKeys, + @SuppressWarnings("rawtypes") OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> + operationMaker) { + // @formatter:on + super(actorName, name); this.topicManager = topicManager; this.selectorKeys = selectorKeys; - } - - @Override - protected void doConfigure(Map<String, Object> parameters) { - params = Util.translate(getFullName(), parameters, BidirectionalTopicParams.class); - ValidationResult result = params.validate(getFullName()); - if (!result.isValid()) { - throw new ParameterValidationRuntimeException("invalid parameters", result); - } - - topicHandler = topicManager.getTopicHandler(params.getSinkTopic(), params.getSourceTopic()); - forwarder = topicHandler.addForwarder(selectorKeys); + this.operationMaker = operationMaker; } /** - * Makes an operator that will construct operations. + * Constructs the object. * - * @param <Q> request type - * @param <S> response type - * @param actorName actor name - * @param operation operation name + * @param actorName name of the actor with which this operator is associated + * @param name operation name * @param topicManager manager from which to get the topic handler - * @param operationMaker function to make an operation - * @param keys keys used to extract the fields used to select responses for this - * operator - * @return a new operator + * @param selectorKeys keys used to extract the fields used to select responses for + * this operator */ // @formatter:off - public static <Q, S> BidirectionalTopicOperator makeOperator(String actorName, String operation, - BidirectionalTopicManager topicManager, - BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator, - BidirectionalTopicOperation<Q, S>> operationMaker, - SelectorKey... keys) { - // @formatter:off - - return makeOperator(actorName, operation, topicManager, Arrays.asList(keys), operationMaker); + public BidirectionalTopicOperator(String actorName, String name, BidirectionalTopicManager topicManager, + @SuppressWarnings("rawtypes") OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> + operationMaker, + SelectorKey... selectorKeys) { + // @formatter:on + this(actorName, name, topicManager, Arrays.asList(selectorKeys), operationMaker); + } + + /** + * Translates the parameters to an {@link HttpParams} and then extracts the relevant + * values. + */ + @Override + protected void doConfigure(Map<String, Object> parameters) { + currentConfig = makeConfiguration(parameters); } /** - * Makes an operator that will construct operations. + * Makes a new configuration using the specified parameters. * - * @param <Q> request type - * @param <S> response type - * @param actorName actor name - * @param operation operation name - * @param topicManager manager from which to get the topic handler - * @param keys keys used to extract the fields used to select responses for - * this operator - * @param operationMaker function to make an operation - * @return a new operator + * @param parameters operator parameters + * @return a new configuration */ - // @formatter:off - public static <Q,S> BidirectionalTopicOperator makeOperator(String actorName, String operation, - BidirectionalTopicManager topicManager, - List<SelectorKey> keys, - BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator, - BidirectionalTopicOperation<Q,S>> operationMaker) { - // @formatter:on + protected BidirectionalTopicConfig makeConfiguration(Map<String, Object> parameters) { + BidirectionalTopicParams params = Util.translate(getFullName(), parameters, BidirectionalTopicParams.class); + ValidationResult result = params.validate(getFullName()); + if (!result.isValid()) { + throw new ParameterValidationRuntimeException("invalid parameters", result); + } + + return new BidirectionalTopicConfig(getBlockingExecutor(), params, topicManager, selectorKeys); + } + + @Override + public Operation buildOperation(ControlLoopOperationParams params) { + if (operationMaker == null) { + throw new UnsupportedOperationException("cannot make operation for " + getFullName()); + } + + verifyRunning(); - return new BidirectionalTopicOperator(actorName, operation, topicManager, keys) { - @Override - public synchronized Operation buildOperation(ControlLoopOperationParams params) { - return operationMaker.apply(params, this); - } - }; + return operationMaker.apply(params, currentConfig); } } diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActor.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActor.java index 28b7b3924..eb5662f23 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActor.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActor.java @@ -27,18 +27,26 @@ import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpActorPara /** * Actor that uses HTTP, where the only additional property that an operator needs is a - * URL. The actor's parameters must be an {@link HttpActorParams} and its operator - * parameters are expected to be an {@link HttpParams}. + * URL. The actor's operator parameters are expected to be an {@link HttpParams}. + * + * @param <P> type of parameters */ -public class HttpActor extends ActorImpl { +public class HttpActor<P extends HttpActorParams> extends ActorImpl { + + /** + * Class of parameters. + */ + private final Class<P> paramsClass; /** * Constructs the object. * * @param name actor's name + * @param paramsClass class of parameters */ - public HttpActor(String name) { + public HttpActor(String name, Class<P> paramsClass) { super(name); + this.paramsClass = paramsClass; } /** @@ -50,7 +58,7 @@ public class HttpActor extends ActorImpl { String actorName = getName(); // @formatter:off - return Util.translate(actorName, actorParameters, HttpActorParams.class) + return Util.translate(actorName, actorParameters, paramsClass) .doValidation(actorName) .makeOperationParameters(actorName); // @formatter:on diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java index da4887e15..4c007ea19 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java @@ -36,6 +36,7 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture; import org.onap.policy.controlloop.policy.PolicyResult; @@ -52,9 +53,9 @@ public abstract class HttpOperation<T> extends OperationPartial { private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class); /** - * Operator that created this operation. + * Configuration for this operation. */ - private final HttpOperator operator; + private final HttpConfig config; /** * Response class. @@ -66,21 +67,33 @@ public abstract class HttpOperation<T> extends OperationPartial { * Constructs the object. * * @param params operation parameters - * @param operator operator that created this operation + * @param config configuration for this operation * @param clazz response class */ - public HttpOperation(ControlLoopOperationParams params, HttpOperator operator, Class<T> clazz) { - super(params, operator); - this.operator = operator; + public HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz) { + super(params, config); + this.config = config; this.responseClass = clazz; } + public HttpClient getClient() { + return config.getClient(); + } + + public String getPath() { + return config.getPath(); + } + + public long getTimeoutMs() { + return config.getTimeoutMs(); + } + /** * If no timeout is specified, then it returns the operator's configured timeout. */ @Override protected long getTimeoutMs(Integer timeoutSec) { - return (timeoutSec == null || timeoutSec == 0 ? operator.getTimeoutMs() : super.getTimeoutMs(timeoutSec)); + return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec)); } /** @@ -99,7 +112,7 @@ public abstract class HttpOperation<T> extends OperationPartial { * @return the path URI suffix */ public String makePath() { - return operator.getPath(); + return getPath(); } /** @@ -110,7 +123,7 @@ public abstract class HttpOperation<T> extends OperationPartial { * @return the URL to which from which to get */ public String makeUrl() { - return (operator.getClient().getBaseUrl() + makePath()); + return (getClient().getBaseUrl() + makePath()); } /** diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperator.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperator.java index b4a3318e7..9e446a7c8 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperator.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperator.java @@ -21,36 +21,33 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.function.BiFunction; import lombok.Getter; -import org.onap.policy.common.endpoints.http.client.HttpClient; import org.onap.policy.common.endpoints.http.client.HttpClientFactory; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.Util; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; /** * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}. */ -@Getter -public abstract class HttpOperator extends OperatorPartial { - - private HttpClient client; +public class HttpOperator extends OperatorPartial { /** - * Default timeout, in milliseconds, if none specified in the request. + * Function to make an operation. */ - private long timeoutMs; + @SuppressWarnings("rawtypes") + private final OperationMaker<HttpConfig, HttpOperation> operationMaker; /** - * URI path for this particular operation. Includes a leading "/". + * Current configuration. This is set by {@link #doConfigure(Map)}. */ - private String path; + @Getter + private HttpConfig currentConfig; /** @@ -59,28 +56,21 @@ public abstract class HttpOperator extends OperatorPartial { * @param actorName name of the actor with which this operator is associated * @param name operation name */ - public HttpOperator(String actorName, String name) { - super(actorName, name); + protected HttpOperator(String actorName, String name) { + this(actorName, name, null); } /** - * Makes an operator that will construct operations. + * Constructs the object. * - * @param <T> response type - * @param actorName actor name - * @param operation operation name + * @param actorName name of the actor with which this operator is associated + * @param name operation name * @param operationMaker function to make an operation - * @return a new operator */ - public static <T> HttpOperator makeOperator(String actorName, String operation, - BiFunction<ControlLoopOperationParams, HttpOperator, HttpOperation<T>> operationMaker) { - - return new HttpOperator(actorName, operation) { - @Override - public Operation buildOperation(ControlLoopOperationParams params) { - return operationMaker.apply(params, this); - } - }; + public HttpOperator(String actorName, String name, + @SuppressWarnings("rawtypes") OperationMaker<HttpConfig, HttpOperation> operationMaker) { + super(actorName, name); + this.operationMaker = operationMaker; } /** @@ -89,24 +79,34 @@ public abstract class HttpOperator extends OperatorPartial { */ @Override protected void doConfigure(Map<String, Object> parameters) { + currentConfig = makeConfiguration(parameters); + } + + /** + * Makes a new configuration using the specified parameters. + * + * @param parameters operator parameters + * @return a new configuration + */ + protected HttpConfig makeConfiguration(Map<String, Object> parameters) { HttpParams params = Util.translate(getFullName(), parameters, HttpParams.class); ValidationResult result = params.validate(getFullName()); if (!result.isValid()) { throw new ParameterValidationRuntimeException("invalid parameters", result); } - doConfigure(params); + return new HttpConfig(getBlockingExecutor(), params, getClientFactory()); } - /** - * Configures the operator using the specified parameters. - * - * @param params operator parameters - */ - protected void doConfigure(HttpParams params) { - client = getClientFactory().get(params.getClientName()); - path = params.getPath(); - timeoutMs = TimeUnit.MILLISECONDS.convert(params.getTimeoutSec(), TimeUnit.SECONDS); + @Override + public Operation buildOperation(ControlLoopOperationParams params) { + if (operationMaker == null) { + throw new UnsupportedOperationException("cannot make operation for " + getFullName()); + } + + verifyRunning(); + + return operationMaker.apply(params, currentConfig); } // these may be overridden by junit tests diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationMaker.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationMaker.java new file mode 100644 index 000000000..ae95097b0 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationMaker.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.impl; + +import org.onap.policy.controlloop.actorserviceprovider.Operation; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; + +/** + * Function to make an operation. + * + * @param <C> configuration type + * @param <T> type of operation it creates + */ +public interface OperationMaker<C, T extends Operation> { + T apply(ControlLoopOperationParams params, C config); +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java index 680a56f89..e636228f6 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java @@ -35,6 +35,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import lombok.Getter; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.utils.NetLoggerUtil; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; @@ -46,6 +47,7 @@ import org.onap.policy.controlloop.actorserviceprovider.CallbackManager; import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.OperatorConfig; import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture; import org.onap.policy.controlloop.policy.PolicyResult; import org.slf4j.Logger; @@ -71,49 +73,43 @@ public abstract class OperationPartial implements Operation { public static final long DEFAULT_RETRY_WAIT_MS = 1000L; - // values extracted from the operator - - private final OperatorPartial operator; + private final OperatorConfig config; /** * Operation parameters. */ protected final ControlLoopOperationParams params; + @Getter + private final String fullName; + /** * Constructs the object. * * @param params operation parameters - * @param operator operator that created this operation + * @param config configuration for this operation */ - public OperationPartial(ControlLoopOperationParams params, OperatorPartial operator) { + public OperationPartial(ControlLoopOperationParams params, OperatorConfig config) { this.params = params; - this.operator = operator; + this.config = config; + this.fullName = params.getActor() + "." + params.getOperation(); } public Executor getBlockingExecutor() { - return operator.getBlockingExecutor(); - } - - public String getFullName() { - return operator.getFullName(); + return config.getBlockingExecutor(); } public String getActorName() { - return operator.getActorName(); + return params.getActor(); } public String getName() { - return operator.getName(); + return params.getOperation(); } @Override public final CompletableFuture<OperationOutcome> start() { - if (!operator.isAlive()) { - throw new IllegalStateException("operation is not running: " + getFullName()); - } - // allocate a controller for the entire operation final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>(); diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperatorPartial.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperatorPartial.java index 3e15c1be4..8007c24f9 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperatorPartial.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperatorPartial.java @@ -62,6 +62,17 @@ public abstract class OperatorPartial extends StartConfigPartial<Map<String, Obj } /** + * Verifies that the operator is running. + * + * @throws IllegalStateException if it is not running + */ + public void verifyRunning() { + if (!isAlive()) { + throw new IllegalStateException("operation is not running: " + getFullName()); + } + } + + /** * This method does nothing. */ @Override diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicConfig.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicConfig.java new file mode 100644 index 000000000..458314850 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicConfig.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import lombok.Getter; +import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; +import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager; +import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder; +import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey; + +/** + * Configuration for Bidirectional Topic Operators. + */ +@Getter +public class BidirectionalTopicConfig extends OperatorConfig { + + /** + * Topic handler associated with the parameters. + */ + private BidirectionalTopicHandler topicHandler; + + /** + * Forwarder associated with the parameters. + */ + private Forwarder forwarder; + + /** + * Default timeout, in milliseconds, if none specified in the request. + */ + private final long timeoutMs; + + + /** + * Constructs the object. + * + * @param blockingExecutor executor to be used for tasks that may perform blocking I/O + * @param params operator parameters + * @param topicManager manager from which to get the topic handler + * @param selectorKeys keys used to extract the fields used to select responses for + * this operator + */ + public BidirectionalTopicConfig(Executor blockingExecutor, BidirectionalTopicParams params, + BidirectionalTopicManager topicManager, List<SelectorKey> selectorKeys) { + super(blockingExecutor); + topicHandler = topicManager.getTopicHandler(params.getSinkTopic(), params.getSourceTopic()); + forwarder = topicHandler.addForwarder(selectorKeys); + timeoutMs = TimeUnit.MILLISECONDS.convert(params.getTimeoutSec(), TimeUnit.SECONDS); + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpConfig.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpConfig.java new file mode 100644 index 000000000..b3e08f707 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpConfig.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import lombok.Getter; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; + +/** + * Configuration for HTTP Operators. + */ +@Getter +public class HttpConfig extends OperatorConfig { + + private final HttpClient client; + + /** + * Default timeout, in milliseconds, if none specified in the request. + */ + private final long timeoutMs; + + /** + * URI path for this particular operation. Includes a leading "/". + */ + private final String path; + + + /** + * Constructs the object. + * + * @param blockingExecutor executor to be used for tasks that may perform blocking I/O + * @param params operator parameters + * @param clientFactory factory from which to obtain the {@link HttpClient} + */ + public HttpConfig(Executor blockingExecutor, HttpParams params, HttpClientFactory clientFactory) { + super(blockingExecutor); + client = clientFactory.get(params.getClientName()); + path = params.getPath(); + timeoutMs = TimeUnit.MILLISECONDS.convert(params.getTimeoutSec(), TimeUnit.SECONDS); + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/OperatorConfig.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/OperatorConfig.java new file mode 100644 index 000000000..e5bd5f5ea --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/OperatorConfig.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import java.util.concurrent.Executor; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +public class OperatorConfig { + + /** + * Executor to be used for tasks that may perform blocking I/O. + */ + @Getter + private final Executor blockingExecutor; +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java index e1606aeaf..4a4354195 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicActorTest.java @@ -61,7 +61,7 @@ public class BidirectionalTopicActorTest { @Mock private BidirectionalTopicHandler handler2; - private BidirectionalTopicActor actor; + private BidirectionalTopicActor<BidirectionalTopicActorParams> actor; /** @@ -157,7 +157,8 @@ public class BidirectionalTopicActorTest { public void testMakeOperatorParameters() { BidirectionalTopicActorParams params = makeParams(); - final BidirectionalTopicActor prov = new BidirectionalTopicActor(ACTOR); + final BidirectionalTopicActor<BidirectionalTopicActorParams> prov = + new BidirectionalTopicActor<>(ACTOR, BidirectionalTopicActorParams.class); Function<String, Map<String, Object>> maker = prov.makeOperatorParameters(Util.translateToMap(prov.getName(), params)); @@ -193,7 +194,7 @@ public class BidirectionalTopicActorTest { @Test public void testMakeTopicHandler() { // use a real actor - actor = new BidirectionalTopicActor(ACTOR); + actor = new BidirectionalTopicActor<>(ACTOR, BidirectionalTopicActorParams.class); handler1 = actor.getTopicHandler(MY_SINK, MY_SOURCE1); handler2 = actor.getTopicHandler(MY_SINK, MY_SOURCE2); @@ -218,10 +219,10 @@ public class BidirectionalTopicActorTest { return params; } - private class MyActor extends BidirectionalTopicActor { + private class MyActor extends BidirectionalTopicActor<BidirectionalTopicActorParams> { public MyActor() { - super(ACTOR); + super(ACTOR, BidirectionalTopicActorParams.class); } @Override diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperationTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperationTest.java index ceb63fe91..5725a6d61 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperationTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperationTest.java @@ -53,7 +53,7 @@ import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.common.utils.time.PseudoExecutor; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; -import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder; @@ -65,8 +65,6 @@ public class BidirectionalTopicOperationTest { private static final String ACTOR = "my-actor"; private static final String OPERATION = "my-operation"; private static final String REQ_ID = "my-request-id"; - private static final String MY_SINK = "my-sink"; - private static final String MY_SOURCE = "my-source"; private static final String TEXT = "some text"; private static final int TIMEOUT_SEC = 10; private static final long TIMEOUT_MS = 1000 * TIMEOUT_SEC; @@ -75,7 +73,7 @@ public class BidirectionalTopicOperationTest { private static final StandardCoder coder = new StandardCoder(); @Mock - private BidirectionalTopicOperator operator; + private BidirectionalTopicConfig config; @Mock private BidirectionalTopicHandler handler; @Mock @@ -85,7 +83,6 @@ public class BidirectionalTopicOperationTest { private ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor; private ControlLoopOperationParams params; - private BidirectionalTopicParams topicParams; private OperationOutcome outcome; private StandardCoderObject stdResponse; private String responseText; @@ -100,15 +97,9 @@ public class BidirectionalTopicOperationTest { public void setUp() throws CoderException { MockitoAnnotations.initMocks(this); - topicParams = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK) - .timeoutSec(TIMEOUT_SEC).build(); - - when(operator.getActorName()).thenReturn(ACTOR); - when(operator.getName()).thenReturn(OPERATION); - when(operator.getTopicHandler()).thenReturn(handler); - when(operator.getForwarder()).thenReturn(forwarder); - when(operator.getParams()).thenReturn(topicParams); - when(operator.isAlive()).thenReturn(true); + when(config.getTopicHandler()).thenReturn(handler); + when(config.getForwarder()).thenReturn(forwarder); + when(config.getTimeoutMs()).thenReturn(TIMEOUT_MS); when(handler.send(any())).thenReturn(true); when(handler.getSinkTopicCommInfrastructure()).thenReturn(SINK_INFRA); @@ -132,7 +123,6 @@ public class BidirectionalTopicOperationTest { assertEquals(OPERATION, oper.getName()); assertSame(handler, oper.getTopicHandler()); assertSame(forwarder, oper.getForwarder()); - assertSame(topicParams, oper.getTopicParams()); assertEquals(TIMEOUT_MS, oper.getTimeoutMs()); assertSame(MyResponse.class, oper.getResponseClass()); } @@ -334,7 +324,7 @@ public class BidirectionalTopicOperationTest { private class MyStringOperation extends BidirectionalTopicOperation<String, String> { public MyStringOperation() { - super(BidirectionalTopicOperationTest.this.params, operator, String.class); + super(BidirectionalTopicOperationTest.this.params, config, String.class); } @Override @@ -356,7 +346,7 @@ public class BidirectionalTopicOperationTest { private class MyScoOperation extends BidirectionalTopicOperation<MyRequest, StandardCoderObject> { public MyScoOperation() { - super(BidirectionalTopicOperationTest.this.params, operator, StandardCoderObject.class); + super(BidirectionalTopicOperationTest.this.params, config, StandardCoderObject.class); } @Override @@ -378,7 +368,7 @@ public class BidirectionalTopicOperationTest { private class MyOperation extends BidirectionalTopicOperation<MyRequest, MyResponse> { public MyOperation() { - super(BidirectionalTopicOperationTest.this.params, operator, MyResponse.class); + super(BidirectionalTopicOperationTest.this.params, config, MyResponse.class); } @Override diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java index 4fae782bd..ae923c0bd 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/BidirectionalTopicOperatorTest.java @@ -20,20 +20,23 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.mockito.Mockito.when; +import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicReference; -import java.util.function.BiFunction; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; @@ -86,9 +89,7 @@ public class BidirectionalTopicOperatorTest { public void testConstructor_testGetParams_testGetTopicHandler_testGetForwarder() { assertEquals(ACTOR, oper.getActorName()); assertEquals(OPERATION, oper.getName()); - assertEquals(params, oper.getParams()); - assertSame(handler, oper.getTopicHandler()); - assertSame(forwarder, oper.getForwarder()); + assertNotNull(oper.getCurrentConfig()); } @Test @@ -102,31 +103,45 @@ public class BidirectionalTopicOperatorTest { } @Test - public void testMakeOperator() { + public void testBuildOperator() { AtomicReference<ControlLoopOperationParams> paramsRef = new AtomicReference<>(); - AtomicReference<BidirectionalTopicOperator> operRef = new AtomicReference<>(); + AtomicReference<BidirectionalTopicConfig> configRef = new AtomicReference<>(); // @formatter:off - BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator, - BidirectionalTopicOperation<String, Integer>> maker = - (params, operator) -> { - paramsRef.set(params); - operRef.set(operator); - return operation; - }; + @SuppressWarnings("rawtypes") + OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation> maker = + (params, config) -> { + paramsRef.set(params); + configRef.set(config); + return operation; + }; // @formatter:on BidirectionalTopicOperator oper2 = - BidirectionalTopicOperator.makeOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey("")); + new BidirectionalTopicOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey("")); assertEquals(ACTOR, oper2.getActorName()); assertEquals(OPERATION, oper2.getName()); ControlLoopOperationParams params2 = ControlLoopOperationParams.builder().build(); + // not running yet + assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params2)); + + // configure and start it + params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC) + .build(); + oper2.configure(Util.translateToMap(OPERATION, params)); + oper2.start(); + assertSame(operation, oper2.buildOperation(params2)); assertSame(params2, paramsRef.get()); - assertSame(oper2, operRef.get()); + assertSame(oper2.getCurrentConfig(), configRef.get()); + + // with no operation-maker + BidirectionalTopicOperator oper3 = + new BidirectionalTopicOperator(ACTOR, OPERATION, mgr, Arrays.asList(new SelectorKey(""))); + assertThatThrownBy(() -> oper3.buildOperation(params2)).isInstanceOf(UnsupportedOperationException.class); } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java index 80b1d427a..dacd3a529 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpActorTest.java @@ -40,11 +40,11 @@ public class HttpActorTest { private static final String CLIENT = "my-client"; private static final int TIMEOUT = 10; - private HttpActor actor; + private HttpActor<HttpActorParams> actor; @Before public void setUp() { - actor = new HttpActor(ACTOR); + actor = new HttpActor<>(ACTOR, HttpActorParams.class); } @Test @@ -59,7 +59,7 @@ public class HttpActorTest { "operB", Map.of("path", "urlB"))); // @formatter:on - final HttpActor prov = new HttpActor(ACTOR); + final HttpActor<HttpActorParams> prov = new HttpActor<>(ACTOR, HttpActorParams.class); Function<String, Map<String, Object>> maker = prov.makeOperatorParameters(Util.translateToMap(prov.getName(), params)); diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java index 8189c74fe..2e9f58cbc 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperationTest.java @@ -29,7 +29,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.spy; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import java.util.Collections; @@ -39,6 +39,7 @@ import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -67,6 +68,7 @@ import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams.TopicParamsBuilder; import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; @@ -76,11 +78,10 @@ import org.onap.policy.common.gson.GsonMessageBodyHandler; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; -import org.onap.policy.controlloop.actorserviceprovider.Util; import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; import org.onap.policy.controlloop.policy.PolicyResult; @@ -110,9 +111,12 @@ public class HttpOperationTest { @Mock private HttpClient client; - + @Mock + private HttpClientFactory clientFactory; @Mock private Response response; + @Mock + private Executor executor; private VirtualControlLoopEvent event; private ControlLoopEventContext context; @@ -120,7 +124,7 @@ public class HttpOperationTest { private OperationOutcome outcome; private AtomicReference<InvocationCallback<Response>> callback; private Future<Response> future; - private HttpOperator operator; + private HttpConfig config; private MyGetOperation<String> oper; /** @@ -192,19 +196,9 @@ public class HttpOperationTest { callback = new AtomicReference<>(); future = new CompletableFuture<>(); - operator = new HttpOperator(ACTOR, OPERATION) { - @Override - public Operation buildOperation(ControlLoopOperationParams params) { - return null; - } + when(clientFactory.get(any())).thenReturn(client); - @Override - public HttpClient getClient() { - return client; - } - }; - - initOper(operator, HTTP_CLIENT); + initConfig(HTTP_CLIENT); oper = new MyGetOperation<>(String.class); } @@ -229,7 +223,9 @@ public class HttpOperationTest { @Test public void testMakeUrl() { // use a real client - client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT); + initRealConfig(HTTP_CLIENT); + + oper = new MyGetOperation<>(String.class); assertThat(oper.makeUrl()).endsWith("/" + BASE_URI + PATH); } @@ -243,19 +239,6 @@ public class HttpOperationTest { // should use given value assertEquals(20 * 1000L, oper.getTimeoutMs(20)); - - // indicate we have a timeout value - operator = spy(operator); - when(operator.getTimeoutMs()).thenReturn(30L); - - oper = new MyGetOperation<String>(String.class); - - // should use default - assertEquals(30L, oper.getTimeoutMs(null)); - assertEquals(30L, oper.getTimeoutMs(0)); - - // should use given value - assertEquals(40 * 1000L, oper.getTimeoutMs(40)); } /** @@ -366,7 +349,7 @@ public class HttpOperationTest { @Test public void testGet() throws Exception { // use a real client - client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT); + initRealConfig(HTTP_CLIENT); MyGetOperation<MyResponse> oper2 = new MyGetOperation<>(MyResponse.class); @@ -382,7 +365,7 @@ public class HttpOperationTest { @Test public void testDelete() throws Exception { // use a real client - client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT); + initRealConfig(HTTP_CLIENT); MyDeleteOperation oper2 = new MyDeleteOperation(); @@ -398,8 +381,7 @@ public class HttpOperationTest { @Test public void testPost() throws Exception { // use a real client - client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT); - + initRealConfig(HTTP_CLIENT); MyPostOperation oper2 = new MyPostOperation(); OperationOutcome outcome = runOperation(oper2); @@ -414,7 +396,7 @@ public class HttpOperationTest { @Test public void testPut() throws Exception { // use a real client - client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT); + initRealConfig(HTTP_CLIENT); MyPutOperation oper2 = new MyPutOperation(); @@ -454,18 +436,34 @@ public class HttpOperationTest { } /** - * Initializes the given operator. + * Initializes the configuration. * * @param operator operator to be initialized * @param clientName name of the client which it should use */ - private void initOper(HttpOperator operator, String clientName) { - operator.stop(); + private void initConfig(String clientName) { + initConfig(clientName, clientFactory); + } + /** + * Initializes the configuration with a real client. + * + * @param operator operator to be initialized + * @param clientName name of the client which it should use + */ + private void initConfig(String clientName, HttpClientFactory factory) { HttpParams params = HttpParams.builder().clientName(clientName).path(PATH).timeoutSec(1).build(); - Map<String, Object> mapParams = Util.translateToMap(OPERATION, params); - operator.configure(mapParams); - operator.start(); + config = new HttpConfig(executor, params, factory); + } + + /** + * Initializes the configuration with a real client. + * + * @param operator operator to be initialized + * @param clientName name of the client which it should use + */ + private void initRealConfig(String clientName) { + initConfig(clientName, HttpClientFactoryInstance.getClientFactory()); } /** @@ -497,7 +495,7 @@ public class HttpOperationTest { private class MyGetOperation<T> extends HttpOperation<T> { public MyGetOperation(Class<T> responseClass) { - super(HttpOperationTest.this.params, HttpOperationTest.this.operator, responseClass); + super(HttpOperationTest.this.params, HttpOperationTest.this.config, responseClass); } @Override @@ -511,14 +509,14 @@ public class HttpOperationTest { // @formatter:off return handleResponse(outcome, url, - callback -> operator.getClient().get(callback, makePath(), headers)); + callback -> getClient().get(callback, makePath(), headers)); // @formatter:on } } private class MyPostOperation extends HttpOperation<MyResponse> { public MyPostOperation() { - super(HttpOperationTest.this.params, HttpOperationTest.this.operator, MyResponse.class); + super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class); } @Override @@ -537,14 +535,14 @@ public class HttpOperationTest { // @formatter:off return handleResponse(outcome, url, - callback -> operator.getClient().post(callback, makePath(), entity, headers)); + callback -> getClient().post(callback, makePath(), entity, headers)); // @formatter:on } } private class MyPutOperation extends HttpOperation<MyResponse> { public MyPutOperation() { - super(HttpOperationTest.this.params, HttpOperationTest.this.operator, MyResponse.class); + super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class); } @Override @@ -563,14 +561,14 @@ public class HttpOperationTest { // @formatter:off return handleResponse(outcome, url, - callback -> operator.getClient().put(callback, makePath(), entity, headers)); + callback -> getClient().put(callback, makePath(), entity, headers)); // @formatter:on } } private class MyDeleteOperation extends HttpOperation<String> { public MyDeleteOperation() { - super(HttpOperationTest.this.params, HttpOperationTest.this.operator, String.class); + super(HttpOperationTest.this.params, HttpOperationTest.this.config, String.class); } @Override @@ -584,7 +582,7 @@ public class HttpOperationTest { // @formatter:off return handleResponse(outcome, url, - callback -> operator.getClient().delete(callback, makePath(), headers)); + callback -> getClient().delete(callback, makePath(), headers)); // @formatter:on } } diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperatorTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperatorTest.java index 081bb346b..873a306c2 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperatorTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperatorTest.java @@ -20,12 +20,12 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import static org.mockito.Mockito.when; import java.util.Map; @@ -42,6 +42,7 @@ import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.Util; import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; @@ -86,63 +87,65 @@ public class HttpOperatorTest { } @Test - public void testGetClient() { - assertNotNull(oper.getClient()); + public void testDoConfigureMapOfStringObject_testGetConfig() { + // start with an UNCONFIGURED operator + oper.shutdown(); + oper = new MyOperator(); + + assertNull(oper.getCurrentConfig()); + + HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + Map<String, Object> paramMap = Util.translateToMap(OPERATION, params); + oper.configure(paramMap); + + assertNotNull(oper.getCurrentConfig()); + + // test invalid parameters + paramMap.remove("path"); + assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class); } @Test - public void testMakeOperator() { - HttpOperator oper2 = HttpOperator.makeOperator(ACTOR, OPERATION, MyOperation::new); + public void testBuildOperation() { + HttpOperator oper2 = new MyOperator(); assertNotNull(oper2); + assertNotNull(oper2.getClientFactory()); VirtualControlLoopEvent event = new VirtualControlLoopEvent(); ControlLoopEventContext context = new ControlLoopEventContext(event); ControlLoopOperationParams params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build(); + // not running yet + assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params)); + + // configure and start it + HttpParams params2 = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + Map<String, Object> paramMap = Util.translateToMap(OPERATION, params2); + oper2.configure(paramMap); + oper2.start(); + Operation operation1 = oper2.buildOperation(params); assertNotNull(operation1); Operation operation2 = oper2.buildOperation(params); assertNotNull(operation2); assertNotSame(operation1, operation2); + + // with no operation-maker + HttpOperator oper3 = new HttpOperator(ACTOR, OPERATION); + assertThatThrownBy(() -> oper3.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class); } @Test - public void testDoConfigureMapOfStringObject_testGetClient_testGetPath_testGetTimeoutMs() { - // start with an UNCONFIGURED operator - oper.shutdown(); - oper = new MyOperator(); - - assertNull(oper.getClient()); - assertNull(oper.getPath()); - - // no timeout yet - assertEquals(0L, oper.getTimeoutMs()); - - HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); - Map<String, Object> paramMap = Util.translateToMap(OPERATION, params); - oper.configure(paramMap); - - assertSame(client, oper.getClient()); - assertEquals(PATH, oper.getPath()); - - // should use given value - assertEquals(TIMEOUT * 1000, oper.getTimeoutMs()); - - // test invalid parameters - paramMap.remove("path"); - assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class); + public void testGetClientFactory() { + HttpOperator oper2 = new HttpOperator(ACTOR, OPERATION); + assertNotNull(oper2.getClientFactory()); } private class MyOperator extends HttpOperator { public MyOperator() { - super(ACTOR, OPERATION); - } - - @Override - public Operation buildOperation(ControlLoopOperationParams params) { - return null; + super(ACTOR, OPERATION, MyOperation::new); } @Override @@ -152,8 +155,8 @@ public class HttpOperatorTest { } private class MyOperation extends HttpOperation<String> { - public MyOperation(ControlLoopOperationParams params, HttpOperator operator) { - super(params, operator, String.class); + public MyOperation(ControlLoopOperationParams params, HttpConfig config) { + super(params, config, String.class); } @Override diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java index 67ac27c8d..39564a443 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartialTest.java @@ -21,7 +21,6 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -41,7 +40,6 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executor; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -70,6 +68,7 @@ import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.OperatorConfig; import org.onap.policy.controlloop.policy.PolicyResult; import org.slf4j.LoggerFactory; @@ -111,7 +110,7 @@ public class OperationPartialTest { private OperationOutcome opstart; private OperationOutcome opend; - private OperatorPartial operator; + private OperatorConfig config; /** * Attaches the appender to the logger. @@ -150,20 +149,7 @@ public class OperationPartialTest { .executor(executor).actor(ACTOR).operation(OPERATION).timeoutSec(TIMEOUT) .startCallback(this::starter).targetEntity(MY_SINK).build(); - operator = new OperatorPartial(ACTOR, OPERATION) { - @Override - public Executor getBlockingExecutor() { - return executor; - } - - @Override - public Operation buildOperation(ControlLoopOperationParams params) { - return null; - } - }; - - operator.configure(null); - operator.start(); + config = new OperatorConfig(executor); oper = new MyOper(); @@ -197,35 +183,12 @@ public class OperationPartialTest { assertNull(future.get(5, TimeUnit.SECONDS)); } - /** - * Exercises the doXxx() methods. - */ - @Test - public void testDoXxx() { - assertThatCode(() -> operator.doConfigure(null)).doesNotThrowAnyException(); - assertThatCode(() -> operator.doStart()).doesNotThrowAnyException(); - assertThatCode(() -> operator.doStop()).doesNotThrowAnyException(); - assertThatCode(() -> operator.doShutdown()).doesNotThrowAnyException(); - - } - @Test public void testStart() { verifyRun("testStart", 1, 1, PolicyResult.SUCCESS); } /** - * Tests startOperation() when the operator is not running. - */ - @Test - public void testStartNotRunning() { - // stop the operator - operator.stop(); - - assertThatIllegalStateException().isThrownBy(() -> oper.start()); - } - - /** * Tests startOperation() when the operation has a preprocessor. */ @Test @@ -392,7 +355,7 @@ public class OperationPartialTest { /* * Use an operation that doesn't override doOperation(). */ - OperationPartial oper2 = new OperationPartial(params, operator) {}; + OperationPartial oper2 = new OperationPartial(params, config) {}; oper2.start(); assertTrue(executor.runAll(MAX_REQUESTS)); @@ -1101,7 +1064,7 @@ public class OperationPartialTest { @Test public void testGetRetryWait() { // need an operator that doesn't override the retry time - OperationPartial oper2 = new OperationPartial(params, operator) {}; + OperationPartial oper2 = new OperationPartial(params, config) {}; assertEquals(OperationPartial.DEFAULT_RETRY_WAIT_MS, oper2.getRetryWaitMs()); } @@ -1257,7 +1220,7 @@ public class OperationPartialTest { public MyOper() { - super(OperationPartialTest.this.params, operator); + super(OperationPartialTest.this.params, config); } @Override diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicConfigTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicConfigTest.java new file mode 100644 index 000000000..7c6a98fde --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicConfigTest.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Executor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; +import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager; +import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder; +import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey; + +public class BidirectionalTopicConfigTest { + private static final String MY_SINK = "my-sink"; + private static final String MY_SOURCE = "my-source"; + private static final int TIMEOUT_SEC = 10; + + @Mock + private BidirectionalTopicManager topicManager; + @Mock + private BidirectionalTopicHandler topicHandler; + @Mock + private Forwarder forwarder; + @Mock + private Executor executor; + + private BidirectionalTopicConfig config; + + /** + * Sets up. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + List<SelectorKey> keys = Arrays.asList(new SelectorKey("")); + + when(topicManager.getTopicHandler(MY_SINK, MY_SOURCE)).thenReturn(topicHandler); + when(topicHandler.addForwarder(keys)).thenReturn(forwarder); + + BidirectionalTopicParams params = BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE) + .timeoutSec(TIMEOUT_SEC).build(); + config = new BidirectionalTopicConfig(executor, params, topicManager, keys); + } + + @Test + public void test() { + assertSame(executor, config.getBlockingExecutor()); + assertSame(topicHandler, config.getTopicHandler()); + assertSame(forwarder, config.getForwarder()); + assertEquals(1000L * TIMEOUT_SEC, config.getTimeoutMs()); + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpConfigTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpConfigTest.java new file mode 100644 index 000000000..309b30fd5 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpConfigTest.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.when; + +import java.util.concurrent.Executor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; + +public class HttpConfigTest { + private static final String MY_CLIENT = "my-client"; + private static final String MY_PATH = "my-path"; + private static final int TIMEOUT_SEC = 10; + + @Mock + private HttpClient client; + @Mock + private HttpClientFactory factory; + @Mock + private Executor executor; + + private HttpConfig config; + + /** + * Sets up. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(factory.get(MY_CLIENT)).thenReturn(client); + + HttpParams params = HttpParams.builder().clientName(MY_CLIENT).path(MY_PATH).timeoutSec(TIMEOUT_SEC).build(); + config = new HttpConfig(executor, params, factory); + } + + @Test + public void test() { + assertSame(executor, config.getBlockingExecutor()); + assertSame(client, config.getClient()); + assertEquals(MY_PATH, config.getPath()); + assertEquals(1000L * TIMEOUT_SEC, config.getTimeoutMs()); + } +} |