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;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.TimeoutException;
35 import java.util.function.BiFunction;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44 import org.onap.policy.sdnc.SdncRequest;
45 import org.onap.policy.sdnc.SdncResponse;
46 import org.onap.policy.sdnc.SdncResponseOutput;
47 import org.powermock.reflect.Whitebox;
50 * Superclass for various operator tests.
52 public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest> {
54 protected SdncResponse response;
57 * Constructs the object using a default actor and operation name.
59 public BasicSdncOperation() {
64 * Constructs the object.
66 * @param actor actor name
67 * @param operation operation name
69 public BasicSdncOperation(String actor, String operation) {
70 super(actor, operation);
74 * Initializes mocks and sets up.
76 public void setUp() throws Exception {
79 response = new SdncResponse();
81 SdncResponseOutput output = new SdncResponseOutput();
82 response.setResponseOutput(output);
83 output.setResponseCode("200");
85 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
89 * Runs the operation and verifies that the response is successful.
91 * @param operation operation to run
92 * @return the request that was posted
94 protected SdncRequest verifyOperation(SdncOperation operation)
95 throws InterruptedException, ExecutionException, TimeoutException {
97 CompletableFuture<OperationOutcome> future2 = operation.start();
99 assertFalse(future2.isDone());
101 verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
102 callbackCaptor.getValue().completed(rawResponse);
104 executor.runAll(100);
105 assertTrue(future2.isDone());
107 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
109 return requestCaptor.getValue().getEntity();
113 * Pretty-prints a request and verifies that the result matches the expected JSON.
115 * @param <T> request type
116 * @param expectedJsonFile name of the file containing the expected JSON
117 * @param request request to verify
118 * @throws CoderException if the request cannot be pretty-printed
120 protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
121 String json = new StandardCoder().encode(request, true);
122 String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
124 // strip request id, because it changes each time
125 final String stripper = "svc-request-id[^,]*";
126 json = json.replaceFirst(stripper, "").trim();
127 expected = expected.replaceFirst(stripper, "").trim();
129 assertEquals(expected, json);
133 * Verifies that an exception is thrown if a field is missing from the enrichment
136 * @param fieldName name of the field to be removed from the enrichment data
137 * @param expectedText text expected in the exception message
139 protected void verifyMissing(String fieldName, String expectedText,
140 BiFunction<ControlLoopOperationParams,HttpOperator,SdncOperation> maker) {
143 enrichment.remove(fieldName);
145 SdncOperation oper = maker.apply(params, operator);
147 assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
148 .withMessageContaining("missing").withMessageContaining(expectedText);
151 protected abstract Map<String, String> makeEnrichment();