Add SO actor
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoOperatorTest.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.so;
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.assertSame;
28 import static org.mockito.Mockito.when;
29
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.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.actorserviceprovider.Operation;
39 import org.onap.policy.controlloop.actorserviceprovider.Util;
40 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
43
44 public class SoOperatorTest {
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 PATH_GET = "my-path-get/";
50     private static final int MAX_GETS = 3;
51     private static final int WAIT_SEC_GETS = 20;
52     private static final int TIMEOUT = 100;
53
54     @Mock
55     private HttpClient client;
56
57     @Mock
58     private HttpClientFactory factory;
59
60
61     private SoOperator oper;
62
63     /**
64      * Initializes fields, including {@link #oper}, and resets the static fields used by
65      * the REST server.
66      */
67     @Before
68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70
71         when(factory.get(CLIENT)).thenReturn(client);
72
73         oper = new MyOperator();
74
75         SoParams params = SoParams.builder().pathGet(PATH_GET).maxGets(MAX_GETS).waitSecGet(WAIT_SEC_GETS)
76                         .clientName(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 testConstructor() {
83         assertEquals(ACTOR, oper.getActorName());
84         assertEquals(OPERATION, oper.getName());
85         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
86     }
87
88     @Test
89     public void testMakeSoOperator() {
90         oper = SoOperator.makeSoOperator(ACTOR, OPERATION, MyOperation::new);
91
92         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
93         ControlLoopEventContext context = new ControlLoopEventContext(event);
94         ControlLoopOperationParams params =
95                         ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
96
97         Operation operation1 = oper.buildOperation(params);
98         assertNotNull(operation1);
99
100         Operation operation2 = oper.buildOperation(params);
101         assertNotNull(operation2);
102         assertNotSame(operation1, operation2);
103     }
104
105     @Test
106     public void testDoConfigure_testGetters() {
107         // should use given values
108         assertSame(client, oper.getClient());
109         assertEquals(PATH_GET, oper.getPathGet());
110         assertEquals(MAX_GETS, oper.getMaxGets());
111         assertEquals(WAIT_SEC_GETS, oper.getWaitSecGet());
112
113         SoParams params = SoParams.builder().pathGet("unslashed").maxGets(MAX_GETS).waitSecGet(WAIT_SEC_GETS)
114                         .clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
115         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
116         oper.configure(paramMap);
117         assertEquals("unslashed/", oper.getPathGet());
118
119         // test invalid parameters
120         Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, SoParams.builder().build());
121         assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class);
122     }
123
124
125     private class MyOperator extends SoOperator {
126         public MyOperator() {
127             super(ACTOR, OPERATION);
128         }
129
130         @Override
131         public Operation buildOperation(ControlLoopOperationParams params) {
132             return null;
133         }
134
135         @Override
136         protected HttpClientFactory getClientFactory() {
137             return factory;
138         }
139     }
140
141     private class MyOperation extends SoOperation {
142         public MyOperation(ControlLoopOperationParams params, SoOperator operator) {
143             super(params, operator);
144         }
145     }
146 }