Add more code to facilitate actor implementation
[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.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNotSame;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
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.HttpParams;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
47
48 public class HttpOperatorTest {
49
50     private static final String ACTOR = "my-actor";
51     private static final String OPERATION = "my-name";
52     private static final String HTTP_CLIENT = "my-client";
53     private static final String PATH = "/my-path";
54     private static final int TIMEOUT = 100;
55
56     @Mock
57     private HttpClient client;
58
59     @Mock
60     private HttpClientFactory factory;
61
62     private MyOperator oper;
63
64     /**
65      * Initializes fields, including {@link #oper}, and resets the static fields used by
66      * the REST server.
67      */
68     @Before
69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71
72         when(factory.get(HTTP_CLIENT)).thenReturn(client);
73
74         oper = new MyOperator();
75
76         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
77         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
78         oper.configure(paramMap);
79     }
80
81     @Test
82     public void testHttpOperator() {
83         assertEquals(ACTOR, oper.getActorName());
84         assertEquals(OPERATION, oper.getName());
85         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
86     }
87
88     @Test
89     public void testGetClient() {
90         assertNotNull(oper.getClient());
91     }
92
93     @Test
94     public void testMakeOperator() {
95         HttpOperator oper2 = HttpOperator.makeOperator(ACTOR, OPERATION, MyOperation::new);
96         assertNotNull(oper2);
97
98         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
99         ControlLoopEventContext context = new ControlLoopEventContext(event);
100         ControlLoopOperationParams params =
101                         ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
102
103         Operation operation1 = oper2.buildOperation(params);
104         assertNotNull(operation1);
105
106         Operation operation2 = oper2.buildOperation(params);
107         assertNotNull(operation2);
108         assertNotSame(operation1, operation2);
109     }
110
111     @Test
112     public void testDoConfigureMapOfStringObject_testGetClient_testGetPath_testGetTimeoutMs() {
113         // start with an UNCONFIGURED operator
114         oper.shutdown();
115         oper = new MyOperator();
116
117         assertNull(oper.getClient());
118         assertNull(oper.getPath());
119
120         // no timeout yet
121         assertEquals(0L, oper.getTimeoutMs());
122
123         HttpParams params = HttpParams.builder().clientName(HTTP_CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
124         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
125         oper.configure(paramMap);
126
127         assertSame(client, oper.getClient());
128         assertEquals(PATH, oper.getPath());
129
130         // should use given value
131         assertEquals(TIMEOUT * 1000, oper.getTimeoutMs());
132
133         // test invalid parameters
134         paramMap.remove("path");
135         assertThatThrownBy(() -> oper.configure(paramMap)).isInstanceOf(ParameterValidationRuntimeException.class);
136     }
137
138     private class MyOperator extends HttpOperator {
139         public MyOperator() {
140             super(ACTOR, OPERATION);
141         }
142
143         @Override
144         public Operation buildOperation(ControlLoopOperationParams params) {
145             return null;
146         }
147
148         @Override
149         protected HttpClientFactory getClientFactory() {
150             return factory;
151         }
152     }
153
154     private class MyOperation extends HttpOperation<String> {
155         public MyOperation(ControlLoopOperationParams params, HttpOperator operator) {
156             super(params, operator, String.class);
157         }
158
159         @Override
160         protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
161             return null;
162         }
163     }
164 }