More actor clean-up
[policy/models.git] / models-interactions / model-actors / actor.test / src / main / java / org / onap / policy / controlloop / actor / test / BasicOperation.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.test;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.Map;
26 import java.util.TreeMap;
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
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.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42
43 /**
44  * Superclass for various Operation tests.
45  */
46 public class BasicOperation {
47     protected static final Coder coder = new StandardCoder();
48     protected static final UUID REQ_ID = UUID.randomUUID();
49     protected static final String DEFAULT_ACTOR = "default-actor";
50     protected static final String DEFAULT_OPERATION = "default-operation";
51     protected static final String TARGET_ENTITY = "my-target";
52
53     protected final String actorName;
54     protected final String operationName;
55
56     @Mock
57     protected ActorService service;
58
59     protected CompletableFuture<Response> future;
60     protected ControlLoopOperationParams params;
61     protected Map<String, String> enrichment;
62     protected VirtualControlLoopEvent event;
63     protected ControlLoopEventContext context;
64     protected OperationOutcome outcome;
65     protected PseudoExecutor executor;
66
67     /**
68      * Constructs the object using a default actor and operation name.
69      */
70     public BasicOperation() {
71         this.actorName = DEFAULT_ACTOR;
72         this.operationName = DEFAULT_OPERATION;
73     }
74
75     /**
76      * Constructs the object.
77      *
78      * @param actor actor name
79      * @param operation operation name
80      */
81     public BasicOperation(String actor, String operation) {
82         this.actorName = actor;
83         this.operationName = operation;
84     }
85
86     /**
87      * Initializes mocks and sets up.
88      */
89     public void setUpBasic() {
90         MockitoAnnotations.initMocks(this);
91
92         future = new CompletableFuture<>();
93
94         executor = new PseudoExecutor();
95
96         makeContext();
97
98         outcome = params.makeOutcome();
99     }
100
101     /**
102      * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
103      * {@link #params}.
104      * <p/>
105      * Note: {@link #params} is configured to use {@link #executor}.
106      */
107     protected void makeContext() {
108         enrichment = new TreeMap<>(makeEnrichment());
109
110         event = new VirtualControlLoopEvent();
111         event.setRequestId(REQ_ID);
112         event.setAai(enrichment);
113
114         context = new ControlLoopEventContext(event);
115
116         params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
117                         .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
118                         .build();
119     }
120
121     /**
122      * Makes enrichment data.
123      *
124      * @return enrichment data
125      */
126     protected Map<String, String> makeEnrichment() {
127         return new TreeMap<>();
128     }
129
130
131     /**
132      * Makes payload data.
133      *
134      * @return payload data
135      */
136     protected Map<String, String> makePayload() {
137         return null;
138     }
139
140     /**
141      * Pretty-prints a request and verifies that the result matches the expected JSON.
142      *
143      * @param <R> request type
144      * @param expectedJsonFile name of the file containing the expected JSON
145      * @param request request to verify
146      * @param ignore names of fields to be ignored, because they change with each request
147      * @throws CoderException if the request cannot be pretty-printed
148      */
149     protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
150         String json = coder.encode(request, true);
151         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
152
153         // strip various items, because they change for each request
154         for (String stripper : ignore) {
155             stripper += "[^,]*";
156             json = json.replaceAll(stripper, "");
157             expected = expected.replaceAll(stripper, "");
158         }
159
160         json = json.trim();
161         expected = expected.trim();
162
163         assertEquals(expected, json);
164     }
165 }