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.sdnc;
 
  23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
 
  24 import static org.junit.Assert.assertEquals;
 
  27 import java.util.TreeMap;
 
  28 import java.util.UUID;
 
  29 import org.onap.policy.common.utils.coder.CoderException;
 
  30 import org.onap.policy.common.utils.coder.StandardCoder;
 
  31 import org.onap.policy.common.utils.resources.ResourceUtils;
 
  32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  33 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  36  * Superclass for various operator tests.
 
  38 public abstract class BasicOperator {
 
  39     protected static final UUID REQ_ID = UUID.randomUUID();
 
  40     protected static final String ACTOR = "my-actor";
 
  42     protected Map<String, String> enrichment;
 
  43     protected VirtualControlLoopEvent event;
 
  44     protected ControlLoopEventContext context;
 
  47      * Pretty-prints a request and verifies that the result matches the expected JSON.
 
  49      * @param <T> request type
 
  50      * @param expectedJsonFile name of the file containing the expected JSON
 
  51      * @param request request to verify
 
  52      * @throws CoderException if the request cannot be pretty-printed
 
  54     protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
 
  55         String json = new StandardCoder().encode(request, true);
 
  56         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
 
  58         // strip request id, because it changes each time
 
  59         final String stripper = "svc-request-id[^,]*";
 
  60         json = json.replaceFirst(stripper, "").trim();
 
  61         expected = expected.replaceFirst(stripper, "").trim();
 
  63         assertEquals(expected, json);
 
  67      * Verifies that an exception is thrown if a field is missing from the enrichment
 
  70      * @param oper operator to construct the request
 
  71      * @param fieldName name of the field to be removed from the enrichment data
 
  72      * @param expectedText text expected in the exception message
 
  74     protected void verifyMissing(SdncOperator oper, String fieldName, String expectedText) {
 
  76         enrichment.remove(fieldName);
 
  78         assertThatIllegalArgumentException().isThrownBy(() -> oper.constructRequest(context))
 
  79                         .withMessageContaining("missing").withMessageContaining(expectedText);
 
  82     protected void makeContext() {
 
  83         // need a mutable map, so make a copy
 
  84         enrichment = new TreeMap<>(makeEnrichment());
 
  86         event = new VirtualControlLoopEvent();
 
  87         event.setRequestId(REQ_ID);
 
  88         event.setAai(enrichment);
 
  90         context = new ControlLoopEventContext(event);
 
  93     protected abstract Map<String, String> makeEnrichment();