cfdec987d169776c442343e3092c42b19babe61a
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.sdnc;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.verify;
30
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeoutException;
34 import org.mockito.Mockito;
35 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
43 import org.onap.policy.sdnc.SdncRequest;
44 import org.onap.policy.sdnc.SdncResponse;
45 import org.onap.policy.sdnc.SdncResponseOutput;
46 import org.onap.policy.simulators.Util;
47
48 /**
49  * Superclass for various operator tests.
50  */
51 abstract class BasicSdncOperation extends BasicHttpOperation {
52     /**
53      * Fields to be ignored when comparing requests with JSON.
54      */
55     protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
56
57     protected SdncResponse response;
58
59     /**
60      * Constructs the object using a default actor and operation name.
61      */
62     BasicSdncOperation() {
63         super();
64     }
65
66     /**
67      * Constructs the object.
68      *
69      * @param actor actor name
70      * @param operation operation name
71      */
72     BasicSdncOperation(String actor, String operation) {
73         super(actor, operation);
74     }
75
76     /**
77      * Starts the simulator.
78      */
79     protected static void initBeforeClass() throws Exception {
80         var testServer = Util.buildSdncSim();
81         assertNotNull(testServer);
82
83         BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("restconf/operations/")
84                         .hostname("localhost").managed(true).port(Util.SDNCSIM_SERVER_PORT).build();
85         HttpClientFactoryInstance.getClientFactory().build(clientParams);
86     }
87
88     protected static void destroyAfterClass() {
89         HttpClientFactoryInstance.getClientFactory().destroy();
90         HttpServletServerFactoryInstance.getServerFactory().destroy();
91     }
92
93     /**
94      * Initializes mocks and sets up.
95      */
96     void setUp() throws Exception {
97         super.setUpBasic();
98
99         response = new SdncResponse();
100
101         SdncResponseOutput output = new SdncResponseOutput();
102         response.setResponseOutput(output);
103         output.setResponseCode("200");
104
105         Mockito.lenient().when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
106     }
107
108     /**
109      * Runs the operation and verifies that the response is successful.
110      *
111      * @param operation operation to run
112      * @return the request that was posted
113      */
114     protected SdncRequest verifyOperation(SdncOperation operation)
115                     throws InterruptedException, ExecutionException, TimeoutException, CoderException {
116
117         CompletableFuture<OperationOutcome> future2 = operation.start();
118         executor.runAll(100);
119         assertFalse(future2.isDone());
120
121         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
122         callbackCaptor.getValue().completed(rawResponse);
123
124         executor.runAll(100);
125         assertTrue(future2.isDone());
126
127         outcome = future2.get();
128         assertEquals(OperationResult.SUCCESS, outcome.getResult());
129         assertTrue(outcome.getResponse() instanceof SdncResponse);
130
131         assertNotNull(outcome.getSubRequestId());
132
133         String reqText = requestCaptor.getValue().getEntity();
134
135         return coder.decode(reqText, SdncRequest.class);
136     }
137 }