Testcases for cds actor
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperatorTest.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.actorserviceprovider.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNotSame;
28 import static org.junit.Assert.assertNull;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Map;
32 import java.util.concurrent.CompletableFuture;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.common.endpoints.http.client.HttpClient;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actorserviceprovider.Operation;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.actorserviceprovider.Util;
43 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
48
49 public class HttpOperatorTest {
50
51     private static final String ACTOR = "my-actor";
52     private static final String OPERATION = "my-name";
53     private static final String HTTP_CLIENT = "my-client";
54     private static final String PATH = "/my-path";
55     private static final int TIMEOUT = 100;
56
57     @Mock
58     private HttpClient client;
59
60     @Mock
61     private HttpClientFactory factory;
62
63     private MyOperator oper;
64
65     /**
66      * Initializes fields, including {@link #oper}, and resets the static fields used by
67      * the REST server.
68      */
69     @Before
70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72
73         when(factory.get(HTTP_CLIENT)).thenReturn(client);
74
75         oper = new MyOperator();
76
77         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
78         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
79         oper.configure(paramMap);
80     }
81
82     @Test
83     public void testHttpOperator() {
84         assertEquals(ACTOR, oper.getActorName());
85         assertEquals(OPERATION, oper.getName());
86         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
87     }
88
89     @Test
90     public void testDoConfigureMapOfStringObject_testGetConfig() {
91         // start with an UNCONFIGURED operator
92         oper.shutdown();
93         oper = new MyOperator();
94
95         assertNull(oper.getCurrentConfig());
96
97         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
98         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
99         oper.configure(paramMap);
100
101         assertNotNull(oper.getCurrentConfig());
102
103         // test invalid parameters
104         paramMap.remove("path");
105         assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
106     }
107
108     @Test
109     public void testBuildOperation() {
110         HttpOperator oper2 = new MyOperator();
111         assertNotNull(oper2);
112         assertNotNull(oper2.getClientFactory());
113
114         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
115         ControlLoopEventContext context = new ControlLoopEventContext(event);
116         ControlLoopOperationParams params =
117                         ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
118
119         // configure and start it
120         HttpParams params2 = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
121         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params2);
122         oper2.configure(paramMap);
123
124         // not running yet
125         assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params));
126
127         oper2.start();
128
129         Operation operation1 = oper2.buildOperation(params);
130         assertNotNull(operation1);
131
132         Operation operation2 = oper2.buildOperation(params);
133         assertNotNull(operation2);
134         assertNotSame(operation1, operation2);
135
136         // with no operation-maker
137         HttpOperator oper3 = new HttpOperator(ACTOR, OPERATION);
138         assertThatThrownBy(() -> oper3.buildOperation(params)).isInstanceOf(UnsupportedOperationException.class);
139     }
140
141     @Test
142     public void testGetClientFactory() {
143         HttpOperator oper2 = new HttpOperator(ACTOR, OPERATION);
144         assertNotNull(oper2.getClientFactory());
145     }
146
147     private class MyOperator extends HttpOperator {
148         public MyOperator() {
149             super(ACTOR, OPERATION, MyOperation::new);
150         }
151
152         @Override
153         protected HttpClientFactory getClientFactory() {
154             return factory;
155         }
156     }
157
158     private class MyOperation extends HttpOperation<String> {
159         public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
160             super(params, config, String.class);
161         }
162
163         @Override
164         protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
165             return null;
166         }
167     }
168 }