diff options
author | Jim Hahn <jrh3@att.com> | 2020-02-18 15:28:39 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-02-18 16:39:14 -0500 |
commit | 4ccc26577b51545b4b4db6823c6d926bc0ffc5a4 (patch) | |
tree | 81e84b3579d4ac7af81dcc25649db5e3d926c8e5 /models-interactions/model-actors/actor.test/src/main | |
parent | ca409ee108f3f653d66be804e8a9f95f794e26d4 (diff) |
Validate request content of various actors
Created common junit superclass, BasicOperation, containing
verifyRequest() method, among others.
Issue-ID: POLICY-2363
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: I7ff4428e94bfd301ff4194e1766ef61906714dcf
Diffstat (limited to 'models-interactions/model-actors/actor.test/src/main')
3 files changed, 172 insertions, 131 deletions
diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java index 14c7ef576..e10e94142 100644 --- a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java @@ -22,51 +22,31 @@ package org.onap.policy.controlloop.actor.test; import static org.mockito.Mockito.when; -import java.util.Map; -import java.util.TreeMap; -import java.util.UUID; import java.util.function.BiConsumer; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; -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.VirtualControlLoopEvent; -import org.onap.policy.controlloop.actorserviceprovider.ActorService; -import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; -import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; -import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder; /** * Superclass for various BidirectionalTopicOperation tests. */ -public class BasicBidirectionalTopicOperation { - protected static final UUID REQ_ID = UUID.randomUUID(); - protected static final String DEFAULT_ACTOR = "default-actor"; - protected static final String DEFAULT_OPERATION = "default-operation"; +public class BasicBidirectionalTopicOperation extends BasicOperation { protected static final String MY_SINK = "my-sink"; protected static final String MY_SOURCE = "my-source"; - protected static final String TARGET_ENTITY = "my-target"; - protected static final Coder coder = new StandardCoder(); protected static final int TIMEOUT = 10; - protected final String actorName; - protected final String operationName; - @Captor protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor; @Mock - protected ActorService service; - @Mock protected BidirectionalTopicHandler topicHandler; @Mock protected Forwarder forwarder; @@ -74,19 +54,12 @@ public class BasicBidirectionalTopicOperation { protected BidirectionalTopicOperator operator; protected BidirectionalTopicParams topicParams; - protected ControlLoopOperationParams params; - protected Map<String, String> enrichment; - protected VirtualControlLoopEvent event; - protected ControlLoopEventContext context; - protected OperationOutcome outcome; - protected PseudoExecutor executor; /** * Constructs the object using a default actor and operation name. */ public BasicBidirectionalTopicOperation() { - this.actorName = DEFAULT_ACTOR; - this.operationName = DEFAULT_OPERATION; + super(); } /** @@ -96,8 +69,7 @@ public class BasicBidirectionalTopicOperation { * @param operation operation name */ public BasicBidirectionalTopicOperation(String actor, String operation) { - this.actorName = actor; - this.operationName = operation; + super(actor, operation); } /** @@ -118,30 +90,6 @@ public class BasicBidirectionalTopicOperation { } /** - * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and - * {@link #params}. - * <p/> - * Note: {@link #params} is configured to use {@link #executor}. - */ - protected void makeContext() { - enrichment = new TreeMap<>(makeEnrichment()); - - event = new VirtualControlLoopEvent(); - event.setRequestId(REQ_ID); - event.setAai(enrichment); - - context = new ControlLoopEventContext(event); - - params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service) - .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload()) - .build(); - } - - protected Map<String, String> makePayload() { - return null; - } - - /** * Initializes an operator so that it is "alive" and has the given names. */ protected void initOperator() { @@ -155,15 +103,6 @@ public class BasicBidirectionalTopicOperation { } /** - * Makes enrichment data. - * - * @return enrichment data - */ - protected Map<String, String> makeEnrichment() { - return new TreeMap<>(); - } - - /** * Provides a response to the topic {@link #listenerCaptor}. * * @param listener listener to which to provide the response diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicHttpOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicHttpOperation.java index 492929296..05fa2260b 100644 --- a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicHttpOperation.java +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicHttpOperation.java @@ -23,8 +23,6 @@ package org.onap.policy.controlloop.actor.test; import static org.mockito.Mockito.when; import java.util.Map; -import java.util.TreeMap; -import java.util.UUID; import java.util.concurrent.CompletableFuture; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -32,34 +30,20 @@ import javax.ws.rs.core.Response; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.mockito.stubbing.Answer; import org.onap.policy.common.endpoints.http.client.HttpClient; import org.onap.policy.common.endpoints.http.client.HttpClientFactory; -import org.onap.policy.common.utils.time.PseudoExecutor; -import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.actorserviceprovider.ActorService; -import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; -import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; -import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; /** * Superclass for various HttpOperation tests. * * @param <Q> request type */ -public class BasicHttpOperation<Q> { - protected static final UUID REQ_ID = UUID.randomUUID(); - protected static final String DEFAULT_ACTOR = "default-actor"; - protected static final String DEFAULT_OPERATION = "default-operation"; +public class BasicHttpOperation<Q> extends BasicOperation { protected static final String MY_CLIENT = "my-client"; protected static final String BASE_URI = "/base-uri"; protected static final String PATH = "/my-path"; - protected static final String TARGET_ENTITY = "my-target"; - - protected final String actorName; - protected final String operationName; @Captor protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor; @@ -71,9 +55,6 @@ public class BasicHttpOperation<Q> { protected ArgumentCaptor<Map<String, Object>> headerCaptor; @Mock - protected ActorService service; - - @Mock protected HttpClient client; @Mock @@ -85,20 +66,12 @@ public class BasicHttpOperation<Q> { @Mock protected HttpOperator operator; - protected CompletableFuture<Response> future; - protected ControlLoopOperationParams params; - protected Map<String, String> enrichment; - protected VirtualControlLoopEvent event; - protected ControlLoopEventContext context; - protected OperationOutcome outcome; - protected PseudoExecutor executor; /** * Constructs the object using a default actor and operation name. */ public BasicHttpOperation() { - this.actorName = DEFAULT_ACTOR; - this.operationName = DEFAULT_OPERATION; + super(); } /** @@ -108,52 +81,25 @@ public class BasicHttpOperation<Q> { * @param operation operation name */ public BasicHttpOperation(String actor, String operation) { - this.actorName = actor; - this.operationName = operation; + super(actor, operation); } /** * Initializes mocks and sets up. */ public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + super.setUp(); when(factory.get(MY_CLIENT)).thenReturn(client); when(rawResponse.getStatus()).thenReturn(200); - future = new CompletableFuture<>(); when(client.getBaseUrl()).thenReturn(BASE_URI); - executor = new PseudoExecutor(); - - makeContext(); - - outcome = params.makeOutcome(); - initOperator(); } /** - * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and - * {@link #params}. - * <p/> - * Note: {@link #params} is configured to use {@link #executor}. - */ - protected void makeContext() { - enrichment = new TreeMap<>(makeEnrichment()); - - event = new VirtualControlLoopEvent(); - event.setRequestId(REQ_ID); - event.setAai(enrichment); - - context = new ControlLoopEventContext(event); - - params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service) - .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).build(); - } - - /** * Initializes an operator so that it is "alive" and has the given names. */ protected void initOperator() { @@ -166,15 +112,6 @@ public class BasicHttpOperation<Q> { } /** - * Makes enrichment data. - * - * @return enrichment data - */ - protected Map<String, String> makeEnrichment() { - return new TreeMap<>(); - } - - /** * Provides a response to an asynchronous HttpClient call. * * @param response response to be provided to the call diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java new file mode 100644 index 000000000..dbdd553a9 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java @@ -0,0 +1,165 @@ +/*- + * ============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.actor.test; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; +import java.util.TreeMap; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.core.Response; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.common.utils.time.PseudoExecutor; +import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.ActorService; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; + +/** + * Superclass for various Operation tests. + */ +public class BasicOperation { + protected static final Coder coder = new StandardCoder(); + protected static final UUID REQ_ID = UUID.randomUUID(); + protected static final String DEFAULT_ACTOR = "default-actor"; + protected static final String DEFAULT_OPERATION = "default-operation"; + protected static final String TARGET_ENTITY = "my-target"; + + protected final String actorName; + protected final String operationName; + + @Mock + protected ActorService service; + + protected CompletableFuture<Response> future; + protected ControlLoopOperationParams params; + protected Map<String, String> enrichment; + protected VirtualControlLoopEvent event; + protected ControlLoopEventContext context; + protected OperationOutcome outcome; + protected PseudoExecutor executor; + + /** + * Constructs the object using a default actor and operation name. + */ + public BasicOperation() { + this.actorName = DEFAULT_ACTOR; + this.operationName = DEFAULT_OPERATION; + } + + /** + * Constructs the object. + * + * @param actor actor name + * @param operation operation name + */ + public BasicOperation(String actor, String operation) { + this.actorName = actor; + this.operationName = operation; + } + + /** + * Initializes mocks and sets up. + */ + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + future = new CompletableFuture<>(); + + executor = new PseudoExecutor(); + + makeContext(); + + outcome = params.makeOutcome(); + } + + /** + * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and + * {@link #params}. + * <p/> + * Note: {@link #params} is configured to use {@link #executor}. + */ + protected void makeContext() { + enrichment = new TreeMap<>(makeEnrichment()); + + event = new VirtualControlLoopEvent(); + event.setRequestId(REQ_ID); + event.setAai(enrichment); + + context = new ControlLoopEventContext(event); + + params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service) + .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload()) + .build(); + } + + /** + * Makes enrichment data. + * + * @return enrichment data + */ + protected Map<String, String> makeEnrichment() { + return new TreeMap<>(); + } + + + /** + * Makes payload data. + * + * @return payload data + */ + protected Map<String, String> makePayload() { + return null; + } + + /** + * Pretty-prints a request and verifies that the result matches the expected JSON. + * + * @param <R> request type + * @param expectedJsonFile name of the file containing the expected JSON + * @param request request to verify + * @param ignore names of fields to be ignored, because they change with each request + * @throws CoderException if the request cannot be pretty-printed + */ + protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException { + String json = coder.encode(request, true); + String expected = ResourceUtils.getResourceAsString(expectedJsonFile); + + // strip various items, because they change for each request + for (String stripper : ignore) { + stripper += "[^,]*"; + json = json.replaceAll(stripper, ""); + expected = expected.replaceAll(stripper, ""); + } + + json = json.trim(); + expected = expected.trim(); + + assertEquals(expected, json); + } +} |