2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.controlloop.actor.test;
 
  23 import static org.junit.Assert.assertEquals;
 
  26 import java.util.UUID;
 
  27 import java.util.concurrent.CompletableFuture;
 
  28 import java.util.concurrent.Executor;
 
  29 import javax.ws.rs.core.Response;
 
  30 import org.mockito.Mock;
 
  31 import org.mockito.MockitoAnnotations;
 
  32 import org.onap.policy.common.utils.coder.Coder;
 
  33 import org.onap.policy.common.utils.coder.CoderException;
 
  34 import org.onap.policy.common.utils.coder.StandardCoder;
 
  35 import org.onap.policy.common.utils.resources.ResourceUtils;
 
  36 import org.onap.policy.common.utils.time.PseudoExecutor;
 
  37 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  42  * Superclass for various Operation tests.
 
  44 public class BasicOperation {
 
  45     protected static final UUID REQ_ID = UUID.randomUUID();
 
  46     protected static final String SUB_REQ_ID = "my-sub-request-id";
 
  47     protected static final String DEFAULT_ACTOR = "default-Actor";
 
  48     protected static final String DEFAULT_OPERATION = "default-Operation";
 
  49     protected static final String TARGET_ENTITY = "my-target";
 
  50     protected static final String CL_NAME = "my-closed-loop";
 
  51     protected static final String EVENT_POLICY_NAME = "my-event-policy-name";
 
  52     protected static final String EVENT_POLICY_VERSION = "my-event-policy-version";
 
  53     protected static final String EVENT_VERSION = "my-event-version";
 
  55     protected static final Executor blockingExecutor = command -> {
 
  56         Thread thread = new Thread(command);
 
  57         thread.setDaemon(true);
 
  61     protected final String actorName;
 
  62     protected final String operationName;
 
  63     protected Coder coder = new StandardCoder();
 
  66     protected ActorService service;
 
  68     protected CompletableFuture<Response> future;
 
  69     protected ControlLoopOperationParams params;
 
  70     protected OperationOutcome outcome;
 
  71     protected PseudoExecutor executor;
 
  74      * Constructs the object using a default actor and operation name.
 
  76     public BasicOperation() {
 
  77         this.actorName = DEFAULT_ACTOR;
 
  78         this.operationName = DEFAULT_OPERATION;
 
  82      * Constructs the object.
 
  84      * @param actor actor name
 
  85      * @param operation operation name
 
  87     public BasicOperation(String actor, String operation) {
 
  88         this.actorName = actor;
 
  89         this.operationName = operation;
 
  93      * Initializes mocks and sets up.
 
  95     public void setUpBasic() {
 
  96         MockitoAnnotations.initMocks(this);
 
  98         future = new CompletableFuture<>();
 
 100         executor = new PseudoExecutor();
 
 104         // get a fresh outcome
 
 105         outcome = params.makeOutcome();
 
 109      * Reinitializes {@link #params}.
 
 111      * Note: {@link #params} is configured to use {@link #executor}.
 
 113     protected void makeContext() {
 
 114         params = ControlLoopOperationParams.builder().executor(executor).requestId(REQ_ID).actorService(service)
 
 115                         .actor(actorName).operation(operationName).payload(makePayload())
 
 121      * Makes payload data.
 
 123      * @return payload data
 
 125     protected Map<String, Object> makePayload() {
 
 130      * Pretty-prints a request and verifies that the result matches the expected JSON.
 
 132      * @param <R> request type
 
 133      * @param expectedJsonFile name of the file containing the expected JSON
 
 134      * @param request request to verify
 
 135      * @param ignore names of fields to be ignored, because they change with each request
 
 136      * @throws CoderException if the request cannot be pretty-printed
 
 138     protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
 
 139         String json = coder.encode(request, true);
 
 140         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
 
 142         // strip various items, because they change for each request
 
 143         for (String stripper : ignore) {
 
 145             json = json.replaceAll(stripper, "");
 
 146             expected = expected.replaceAll(stripper, "");
 
 150         expected = expected.trim();
 
 152         assertEquals(expected, json);