f4b6141f89ab18db3393dac3aab705b8fbd47fb2
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / BasicSdncOperation.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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
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;
30
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeoutException;
34 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
36 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
42 import org.onap.policy.sdnc.SdncRequest;
43 import org.onap.policy.sdnc.SdncResponse;
44 import org.onap.policy.sdnc.SdncResponseOutput;
45 import org.onap.policy.simulators.Util;
46
47 /**
48  * Superclass for various operator tests.
49  */
50 public abstract class BasicSdncOperation extends BasicHttpOperation {
51     /**
52      * Fields to be ignored when comparing requests with JSON.
53      */
54     protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
55
56     protected SdncResponse response;
57
58     /**
59      * Constructs the object using a default actor and operation name.
60      */
61     public BasicSdncOperation() {
62         super();
63     }
64
65     /**
66      * Constructs the object.
67      *
68      * @param actor actor name
69      * @param operation operation name
70      */
71     public BasicSdncOperation(String actor, String operation) {
72         super(actor, operation);
73     }
74
75     /**
76      * Starts the simulator.
77      */
78     protected static void initBeforeClass() throws Exception {
79         Util.buildSdncSim();
80
81         BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("restconf/operations/")
82                         .hostname("localhost").managed(true).port(Util.SDNCSIM_SERVER_PORT).build();
83         HttpClientFactoryInstance.getClientFactory().build(clientParams);
84     }
85
86     protected static void destroyAfterClass() {
87         HttpClientFactoryInstance.getClientFactory().destroy();
88         HttpServletServerFactoryInstance.getServerFactory().destroy();
89     }
90
91     /**
92      * Initializes mocks and sets up.
93      */
94     public void setUp() throws Exception {
95         super.setUpBasic();
96
97         response = new SdncResponse();
98
99         SdncResponseOutput output = new SdncResponseOutput();
100         response.setResponseOutput(output);
101         output.setResponseCode("200");
102
103         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
104     }
105
106     /**
107      * Runs the operation and verifies that the response is successful.
108      *
109      * @param operation operation to run
110      * @return the request that was posted
111      */
112     protected SdncRequest verifyOperation(SdncOperation operation)
113                     throws InterruptedException, ExecutionException, TimeoutException, CoderException {
114
115         CompletableFuture<OperationOutcome> future2 = operation.start();
116         executor.runAll(100);
117         assertFalse(future2.isDone());
118
119         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
120         callbackCaptor.getValue().completed(rawResponse);
121
122         executor.runAll(100);
123         assertTrue(future2.isDone());
124
125         outcome = future2.get();
126         assertEquals(OperationResult.SUCCESS, outcome.getResult());
127         assertTrue(outcome.getResponse() instanceof SdncResponse);
128
129         assertNotNull(outcome.getSubRequestId());
130
131         String reqText = requestCaptor.getValue().getEntity();
132
133         return coder.decode(reqText, SdncRequest.class);
134     }
135 }