Add property lists to Actors
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpPollingOperatorTest.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.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Collections;
30 import java.util.Map;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.common.endpoints.http.client.HttpClient;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
37 import org.onap.policy.controlloop.actorserviceprovider.Util;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
43
44 public class HttpPollingOperatorTest {
45     private static final String ACTOR = "my-actor";
46     private static final String OPERATION = "my-name";
47     private static final String CLIENT = "my-client";
48     private static final String PATH = "/my-path";
49     private static final String POLL_PATH = "my-path-get/";
50     private static final int MAX_POLLS = 3;
51     private static final int POLL_WAIT_SEC = 20;
52     private static final int TIMEOUT = 100;
53
54     @Mock
55     private HttpClient client;
56     @Mock
57     private HttpClientFactory factory;
58
59     private HttpPollingOperator oper;
60
61     /**
62      * Initializes fields, including {@link #oper}, and resets the static fields used by
63      * the REST server.
64      */
65     @Before
66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68
69         when(factory.get(CLIENT)).thenReturn(client);
70
71         oper = new MyOperator();
72
73         HttpPollingParams params = HttpPollingParams.builder().pollPath(POLL_PATH).maxPolls(MAX_POLLS)
74                         .pollWaitSec(POLL_WAIT_SEC).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
75         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
76         oper.configure(paramMap);
77     }
78
79     @Test
80     public void testConstructor() {
81         assertEquals(ACTOR, oper.getActorName());
82         assertEquals(OPERATION, oper.getName());
83         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
84     }
85
86     @Test
87     public void testDoConfigure_testGetters() {
88         assertTrue(oper.getCurrentConfig() instanceof HttpPollingConfig);
89
90         // test invalid parameters
91         Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, HttpPollingParams.builder().build());
92         assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class);
93     }
94
95     @Test
96     public void testGetClientFactory() {
97         HttpPollingOperator oper2 = new HttpPollingOperator(ACTOR, OPERATION);
98         assertNotNull(oper2.getClientFactory());
99     }
100
101
102     private class MyOperator extends HttpPollingOperator {
103         public MyOperator() {
104             super(ACTOR, OPERATION, MyOperation::new);
105         }
106
107         @Override
108         protected HttpClientFactory getClientFactory() {
109             return factory;
110         }
111     }
112
113     private static class MyOperation extends HttpOperation<String> {
114         public MyOperation(ControlLoopOperationParams params, HttpConfig config) {
115             super(params, config, String.class, Collections.emptyList());
116         }
117     }
118 }