Clean up and enhancement of Actor re-design
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / BasicOperator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.sdnc;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25
26 import java.util.Map;
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;
34
35 /**
36  * Superclass for various operator tests.
37  */
38 public abstract class BasicOperator {
39     protected static final UUID REQ_ID = UUID.randomUUID();
40     protected static final String ACTOR = "my-actor";
41
42     protected Map<String, String> enrichment;
43     protected VirtualControlLoopEvent event;
44     protected ControlLoopEventContext context;
45
46     /**
47      * Pretty-prints a request and verifies that the result matches the expected JSON.
48      *
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
53      */
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);
57
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();
62
63         assertEquals(expected, json);
64     }
65
66     /**
67      * Verifies that an exception is thrown if a field is missing from the enrichment
68      * data.
69      *
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
73      */
74     protected void verifyMissing(SdncOperator oper, String fieldName, String expectedText) {
75         makeContext();
76         enrichment.remove(fieldName);
77
78         assertThatIllegalArgumentException().isThrownBy(() -> oper.constructRequest(context))
79                         .withMessageContaining("missing").withMessageContaining(expectedText);
80     }
81
82     protected void makeContext() {
83         // need a mutable map, so make a copy
84         enrichment = new TreeMap<>(makeEnrichment());
85
86         event = new VirtualControlLoopEvent();
87         event.setRequestId(REQ_ID);
88         event.setAai(enrichment);
89
90         context = new ControlLoopEventContext(event);
91     }
92
93     protected abstract Map<String, String> makeEnrichment();
94 }