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