summaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java
blob: cf07d3b2be88432b111e2e9baa5d41f510285fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.controlloop.actorserviceprovider.impl;

import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.parameters.ObjectValidationResult;
import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
import org.onap.policy.controlloop.actorserviceprovider.Operator;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;

public class ActorImplTest {
    private static final String EXPECTED_EXCEPTION = "expected exception";
    private static final String ACTOR_NAME = "my-actor";
    private static final String OPER1 = "add";
    private static final String OPER2 = "subtract";
    private static final String OPER3 = "multiply";
    private static final String OPER4 = "divide";

    private MyOper oper1;
    private MyOper oper2;
    private MyOper oper3;
    private MyOper oper4;

    private Map<String, Object> sub1;
    private Map<String, Object> sub2;
    private Map<String, Object> sub3;
    private Map<String, Object> sub4;
    private Map<String, Object> params;

    private ActorImpl actor;


    /**
     * Initializes the fields, including a fully populated {@link #actor}.
     */
    @Before
    public void setUp() {
        oper1 = spy(new MyOper(OPER1));
        oper2 = spy(new MyOper(OPER2));
        oper3 = spy(new MyOper(OPER3));
        oper4 = spy(new MyOper(OPER4));

        sub1 = Map.of("sub A", "value A");
        sub2 = Map.of("sub B", "value B");
        sub3 = Map.of("sub C", "value C");
        sub4 = Map.of("sub D", "value D");

        params = Map.of(ActorParams.OPERATIONS_FIELD, Map.of(OPER1, sub1, OPER2, sub2, OPER3, sub3, OPER4, sub4));

        actor = makeActor(oper1, oper2, oper3, oper4);
    }

    @Test
    public void testActorImpl_testGetName() {
        assertEquals(ACTOR_NAME, actor.getName());
        assertEquals(4, actor.getOperationNames().size());
        assertEquals(0, actor.getSequenceNumber());
    }

    @Test
    public void testDoStart() {
        actor.configure(params);
        assertEquals(4, actor.getOperationNames().size());

        /*
         * arrange for second operator to be unconfigured and the third operator to throw
         * an exception
         */
        Iterator<Operator> iter = actor.getOperators().iterator();
        iter.next();
        when(iter.next().isConfigured()).thenReturn(false);
        when(iter.next().start()).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));

        /*
         * Start the actor.
         */
        actor.start();
        assertTrue(actor.isAlive());

        iter = actor.getOperators().iterator();
        verify(iter.next()).start();
        // this one isn't configured, so shouldn't attempt to start it
        verify(iter.next(), never()).start();
        // this one threw an exception
        iter.next();
        verify(iter.next()).start();

        // no other types of operations
        verify(oper1, never()).stop();
        verify(oper1, never()).shutdown();
    }

    @Test
    public void testDoStop() {
        actor.configure(params);
        actor.start();
        assertEquals(4, actor.getOperationNames().size());

        // arrange for second operator to throw an exception
        Iterator<Operator> iter = actor.getOperators().iterator();
        iter.next();
        when(iter.next().stop()).thenThrow(new IllegalStateException(EXPECTED_EXCEPTION));

        /*
         * Stop the actor.
         */
        actor.stop();
        assertFalse(actor.isAlive());

        iter = actor.getOperators().iterator();
        verify(iter.next()).stop();
        // this one threw an exception
        iter.next();
        verify(iter.next()).stop();
        verify(iter.next()).stop();

        // no additional types of operations
        verify(oper1).configure(any());
        verify(oper1).start();

        // no other types of operation
        verify(oper1, never()).shutdown();
    }

    @Test
    public void testDoShutdown() {
        actor.configure(params);
        actor.start();
        assertEquals(4, actor.getOperationNames().size());

        // arrange for second operator to throw an exception
        Iterator<Operator> iter = actor.getOperators().iterator();
        iter.next();
        doThrow(new IllegalStateException(EXPECTED_EXCEPTION)).when(iter.next()).shutdown();

        /*
         * Stop the actor.
         */
        actor.shutdown();
        assertFalse(actor.isAlive());

        iter = actor.getOperators().iterator();
        verify(iter.next()).shutdown();
        // this one threw an exception
        iter.next();
        verify(iter.next()).shutdown();
        verify(iter.next()).shutdown();

        // no additional types of operations
        verify(oper1).configure(any());
        verify(oper1).start();

        // no other types of operation
        verify(oper1, never()).stop();
    }

    @Test
    public void testAddOperator() {
        // cannot add operators if already configured
        actor.configure(params);
        assertThatIllegalStateException().isThrownBy(() -> actor.addOperator(oper1));

        /*
         * make an actor where operators two and four have names that are duplicates of
         * the others
         */
        oper2 = spy(new MyOper(OPER1));
        oper4 = spy(new MyOper(OPER3));

        actor = makeActor(oper1, oper2, oper3, oper4);

        assertEquals(2, actor.getOperationNames().size());

        assertSame(oper1, actor.getOperator(OPER1));
        assertSame(oper3, actor.getOperator(OPER3));
    }

    @Test
    public void testGetOperator() {
        assertSame(oper1, actor.getOperator(OPER1));
        assertSame(oper3, actor.getOperator(OPER3));

        assertThatIllegalArgumentException().isThrownBy(() -> actor.getOperator("unknown name"));
    }

    @Test
    public void testGetOperators() {
        // @formatter:off
        assertEquals("[add, divide, multiply, subtract]",
                        actor.getOperators().stream()
                            .map(Operator::getName)
                            .sorted()
                            .collect(Collectors.toList())
                            .toString());
        // @formatter:on
    }

    @Test
    public void testGetOperationNames() {
        // @formatter:off
        assertEquals("[add, divide, multiply, subtract]",
                        actor.getOperationNames().stream()
                            .sorted()
                            .collect(Collectors.toList())
                            .toString());
        // @formatter:on
    }

    @Test
    public void testDoConfigure() {
        actor.configure(params);
        assertTrue(actor.isConfigured());

        verify(oper1).configure(sub1);
        verify(oper2).configure(sub2);
        verify(oper3).configure(sub3);
        verify(oper4).configure(sub4);

        // no other types of operations
        verify(oper1, never()).start();
        verify(oper1, never()).stop();
        verify(oper1, never()).shutdown();
    }

    /**
     * Tests doConfigure() where operators throw parameter validation and runtime
     * exceptions.
     */
    @Test
    public void testDoConfigureExceptions() {
        makeValidException(oper1);
        makeRuntimeException(oper2);
        makeValidException(oper3);

        actor.configure(params);
        assertTrue(actor.isConfigured());
    }

    /**
     * Tests doConfigure(). Arranges for the following:
     * <ul>
     * <li>one operator is configured, but has parameters</li>
     * <li>another operator is configured, but has no parameters</li>
     * <li>another operator has no parameters and is not configured</li>
     * </ul>
     */
    @Test
    public void testDoConfigureConfigure() {
        // configure one operator
        oper1.configure(sub1);

        // configure another and remove its parameters
        oper2.configure(sub2);
        params = Map.of(ActorParams.OPERATIONS_FIELD, Map.of(OPER1, sub1, OPER3, sub3, OPER4, sub4));

        // create a new, unconfigured actor
        Operator oper5 = spy(new MyOper("UNCONFIGURED"));
        actor = makeActor(oper1, oper2, oper3, oper4, oper5);

        /*
         * Configure it.
         */
        actor.configure(params);
        assertTrue(actor.isConfigured());

        // this should have been configured again
        verify(oper1, times(2)).configure(sub1);

        // no parameters, so this should not have been configured again
        verify(oper2).configure(sub2);

        // these were only configured once
        verify(oper3).configure(sub3);
        verify(oper4).configure(sub4);

        // never configured
        verify(oper5, never()).configure(any());
        assertFalse(oper5.isConfigured());

        // start and verify that all are started except for the last
        actor.start();
        verify(oper1).start();
        verify(oper2).start();
        verify(oper3).start();
        verify(oper4).start();
        verify(oper5, never()).start();
    }

    /**
     * Arranges for an operator to throw a validation exception when
     * {@link Operator#configure(Map)} is invoked.
     *
     * @param oper operator of interest
     */
    private void makeValidException(Operator oper) {
        ParameterValidationRuntimeException ex = new ParameterValidationRuntimeException(
                        new ObjectValidationResult(actor.getName(), null, ValidationStatus.INVALID, "null"));
        doThrow(ex).when(oper).configure(any());
    }

    /**
     * Arranges for an operator to throw a runtime exception when
     * {@link Operator#configure(Map)} is invoked.
     *
     * @param oper operator of interest
     */
    private void makeRuntimeException(Operator oper) {
        IllegalStateException ex = new IllegalStateException(EXPECTED_EXCEPTION);
        doThrow(ex).when(oper).configure(any());
    }

    @Test
    public void testMakeOperatorParameters() {
        actor.configure(params);

        // each operator should have received its own parameters
        verify(oper1).configure(sub1);
        verify(oper2).configure(sub2);
        verify(oper3).configure(sub3);
        verify(oper4).configure(sub4);
    }

    /**
     * Makes an actor with the given operators.
     *
     * @param operators associated operators
     * @return a new actor
     */
    private ActorImpl makeActor(Operator... operators) {
        ActorImpl actor = new ActorImpl(ACTOR_NAME);

        for (Operator oper : operators) {
            actor.addOperator(oper);
        }

        return actor;
    }

    private static class MyOper extends OperatorPartial {

        public MyOper(String name) {
            super(ACTOR_NAME, name);
        }

        @Override
        public Operation buildOperation(ControlLoopOperationParams params) {
            return null;
        }
    }
}