aboutsummaryrefslogtreecommitdiffstats
path: root/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java
blob: 818f8fc154db94d90d4e3f446b2ac64ea41b3d0b (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018-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.drools.system;

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.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY_CONTROLLER_TYPE;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.controller.internal.NullDroolsController;
import org.onap.policy.drools.features.DroolsControllerFeatureApi;
import org.onap.policy.drools.features.PolicyControllerFeatureApi;
import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
import org.onap.policy.drools.system.internal.AggregatedPolicyController;

public class PolicyControllerFactoryTest {
    private static final String POLICY_CONTROLLER_BUILDER_TAG = "PolicyControllerFactoryTest";

    private static final String MY_NAME = "my-name-a";
    private static final String MY_NAME2 = "my-name-b";

    private static final String ARTIFACT1 = "artifact-a";
    private static final String GROUP1 = "group-a";
    private static final String VERSION1 = "version-a";

    private static final String ARTIFACT2 = "artifact-b";
    private static final String GROUP2 = "group-b";
    private static final String VERSION2 = "version-b";

    private static final String FEATURE1 = "feature-a";
    private static final String FEATURE2 = "feature-b";

    private PolicyController controller;
    private PolicyController controller2;
    private Properties properties;
    private DroolsController drools;
    private DroolsController drools2;
    private DroolsConfiguration config;
    private PolicyControllerFeatureApi feature1;
    private PolicyControllerFeatureApi feature2;
    private List<PolicyControllerFeatureApi> providers;
    private IndexedPolicyControllerFactory ipc;

    /**
     * Initializes the object to be tested.
     */
    @Before
    public void setUp() {
        controller = mock(PolicyController.class);
        controller2 = mock(PolicyController.class);
        properties = new Properties();
        drools = mock(DroolsController.class);
        drools2 = mock(DroolsController.class);
        config = mock(DroolsConfiguration.class);
        feature1 = mock(PolicyControllerFeatureApi.class);
        feature2 = mock(PolicyControllerFeatureApi.class);
        providers = Arrays.asList(feature1, feature2);

        when(feature1.getName()).thenReturn(FEATURE1);
        when(feature2.getName()).thenReturn(FEATURE2);

        when(drools.getArtifactId()).thenReturn(ARTIFACT1);
        when(drools.getGroupId()).thenReturn(GROUP1);
        when(drools.getVersion()).thenReturn(VERSION1);

        when(drools2.getArtifactId()).thenReturn(ARTIFACT2);
        when(drools2.getGroupId()).thenReturn(GROUP2);
        when(drools2.getVersion()).thenReturn(VERSION2);

        when(controller.getName()).thenReturn(MY_NAME);
        when(controller.getDrools()).thenReturn(drools);
        when(controller.updateDrools(any())).thenReturn(true);

        when(controller2.getName()).thenReturn(MY_NAME2);
        when(controller2.getDrools()).thenReturn(drools2);
        when(controller2.updateDrools(any())).thenReturn(true);

        ipc = new IndexedPolicyControllerFactoryImpl();
    }

    @Test
    public void testFactory() {
        // use a REAL object instead of an Impl
        ipc = new IndexedPolicyControllerFactory();
        assertNotNull(ipc.getProviders());
    }

    @Test
    public void testBuild() {
        assertEquals(controller, ipc.build(MY_NAME, properties));

        // re-build - should not create another one
        assertEquals(controller, ipc.build(MY_NAME, properties));

        // brained
        setUp();
        when(drools.isBrained()).thenReturn(true);
        ipc.build(MY_NAME, properties);
    }

    @Test
    public void testSerialize() {
        assertEquals(controller, ipc.build(MY_NAME, properties));

        new GsonTestUtils().compareGson(ipc, PolicyControllerFactoryTest.class);
    }

    @Test
    public void testPatchStringDroolsConfiguration() {
        // unknown controller
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(MY_NAME, config));

        /*
         * Build controller to be used by remaining tests.
         */
        ipc.build(MY_NAME, properties);

        // null name
        String nullName = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(nullName, config));

        // empty name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch("", config));

        // success
        ipc.patch(MY_NAME, config);
        verify(controller).updateDrools(config);

        // create a factory whose get() method returns null
        ipc = new IndexedPolicyControllerFactory() {
            @Override
            public PolicyController get(String name) {
                return null;
            }
        };
        ipc.build(MY_NAME, properties);
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(MY_NAME, config));
    }

    @Test
    public void testPatchPolicyControllerDroolsConfiguration() {
        ipc.patch(controller, config);
        verify(controller).updateDrools(config);

        // null controller
        PolicyController nullCtlr = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(nullCtlr, config));

        // null config
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(controller, null));

        // brained
        when(drools.isBrained()).thenReturn(true);
        ipc.patch(controller, config);

        // update failed
        when(controller.updateDrools(config)).thenReturn(false);
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(controller, config));
    }

    @Test
    public void testShutdownString() {
        // null name
        String nullName = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.shutdown(nullName));

        // empty name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.shutdown(""));

        // unknown controller
        ipc.shutdown(MY_NAME);
        verify(controller, never()).shutdown();

        // valid controller
        ipc.build(MY_NAME, properties);
        ipc.shutdown(MY_NAME);
        verify(controller).shutdown();
    }

    @Test
    public void testShutdownPolicyController() {
        ipc.build(MY_NAME, properties);

        ipc.shutdown(controller);

        verify(controller).shutdown();

        // should no longer be managed
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME));
    }

    @Test
    public void testShutdown() {
        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        ipc.shutdown();

        verify(controller).shutdown();
        verify(controller2).shutdown();

        // should no longer be managed
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME));
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME2));
    }

    @Test
    public void testUnmanage() {
        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        ipc.shutdown(MY_NAME);

        verify(controller).shutdown();
        verify(controller2, never()).shutdown();

        // should no longer be managed
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME));

        // should still be managed
        assertEquals(controller2, ipc.get(MY_NAME2));

        // null controller
        PolicyController nullCtlr = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.shutdown(nullCtlr));

        // unknown controller
        ipc.shutdown(controller);
        verify(controller, times(2)).shutdown();
    }

    @Test
    public void testDestroyString() {
        // null name
        String nullName = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.destroy(nullName));

        // empty name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.destroy(""));

        // unknown controller
        ipc.destroy(MY_NAME);
        verify(controller, never()).halt();

        // valid controller
        ipc.build(MY_NAME, properties);
        ipc.destroy(MY_NAME);
        verify(controller).halt();
    }

    @Test
    public void testDestroyPolicyController() {
        ipc.build(MY_NAME, properties);

        ipc.destroy(controller);

        verify(controller).halt();

        // should no longer be managed
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME));
    }

    @Test
    public void testDestroy() {
        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        ipc.destroy();

        verify(controller).halt();
        verify(controller2).halt();

        // should no longer be managed
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME));
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME2));
    }

    @Test
    public void testGetString() {
        // unknown name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(MY_NAME));

        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        assertEquals(controller, ipc.get(MY_NAME));
        assertEquals(controller2, ipc.get(MY_NAME2));

        // null name
        String nullName = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(nullName));

        // empty name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(""));
    }

    @Test
    public void testGetStringString_testToKey() {
        // unknown controller
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(GROUP1, ARTIFACT1));

        when(drools.isBrained()).thenReturn(true);
        when(drools2.isBrained()).thenReturn(true);

        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        assertEquals(controller, ipc.get(GROUP1, ARTIFACT1));
        assertEquals(controller2, ipc.get(GROUP2, ARTIFACT2));

        // null group
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(null, ARTIFACT1));

        // empty group
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get("", ARTIFACT1));

        // null artifact
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(GROUP1, null));

        // empty artifact
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(GROUP1, ""));
    }

    @Test
    public void testGetDroolsController() {
        // unknown controller
        assertThatIllegalStateException().isThrownBy(() -> ipc.get(drools));

        when(drools.isBrained()).thenReturn(true);
        when(drools2.isBrained()).thenReturn(true);

        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        assertEquals(controller, ipc.get(drools));
        assertEquals(controller2, ipc.get(drools2));

        // null controller
        DroolsController nullDrools = null;
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.get(nullDrools));
    }

    @Test
    public void testInventory() {
        ipc.build(MY_NAME, properties);
        ipc.build(MY_NAME2, properties);

        List<PolicyController> lst = ipc.inventory();
        Collections.sort(lst, (left, right) -> left.getName().compareTo(right.getName()));
        assertEquals(Arrays.asList(controller, controller2), lst);
    }

    @Test
    public void testGetFeatures() {
        assertEquals(Arrays.asList(FEATURE1, FEATURE2), ipc.getFeatures());
    }

    @Test
    public void testGetFeatureProviders() {
        assertEquals(providers, ipc.getFeatureProviders());
    }

    @Test
    public void testGetFeatureProvider() {
        // null name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.getFeatureProvider(null));

        // empty name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.getFeatureProvider(""));

        // unknown name
        assertThatIllegalArgumentException().isThrownBy(() -> ipc.getFeatureProvider("unknown-feature"));

        assertEquals(feature1, ipc.getFeatureProvider(FEATURE1));
        assertEquals(feature2, ipc.getFeatureProvider(FEATURE2));
    }

    @Test
    public void testControllerType() {
        PolicyControllerFactory factory = new IndexedPolicyControllerFactory();
        Properties props = new Properties();

        // this should build an 'AggregatedPolicyController'
        final String name1 = "ctrl1";
        PolicyController ctrl1 = factory.build(name1, props);

        // this should build a 'TestPolicyController'
        final String name2 = "ctrl2";
        props.setProperty(PROPERTY_CONTROLLER_TYPE, POLICY_CONTROLLER_BUILDER_TAG);
        PolicyController ctrl2 = factory.build(name2, props);

        // verify controller types
        assertSame(AggregatedPolicyController.class, ctrl1.getClass());
        assertSame(TestPolicyController.class, ctrl2.getClass());
        assertSame(NullDroolsController.class, ctrl2.getDrools().getClass());

        // verify controller lookups
        assertSame(ctrl1, factory.get(name1));
        assertSame(ctrl2, factory.get(name2));
    }

    /**
     * Factory with overrides.
     */
    private class IndexedPolicyControllerFactoryImpl extends IndexedPolicyControllerFactory {

        @Override
        protected PolicyController newPolicyController(String name, Properties properties) {
            if (MY_NAME.equals(name)) {
                return controller;

            } else if (MY_NAME2.equals(name)) {
                return controller2;

            } else {
                throw new IllegalArgumentException("unknown controller name: " + name);

            }
        }

        @Override
        protected List<PolicyControllerFeatureApi> getProviders() {
            return providers;
        }
    }

    /**
     * This class provides an alternate PolicyController implementation,
     * for the purpose of easy identification within a junit test.
     */
    public static class TestPolicyController extends AggregatedPolicyController {
        public TestPolicyController(String name, Properties properties) {
            super(name, properties);
        }
    }

    /**
     * An instance of this class is created by 'IndexedPolicyControllerFactory',
     * using features. It does the build operation when the value of the
     * 'controller.type' property matches the value of POLICY_CONTROLLER_BUILDER_TAG.
     */
    public static class PolicyBuilder implements PolicyControllerFeatureApi {
        @Override
        public int getSequenceNumber() {
            return 1;
        }

        @Override
        public PolicyController beforeInstance(String name, Properties properties) {
            if (POLICY_CONTROLLER_BUILDER_TAG.equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) {
                return new TestPolicyController(name, properties);
            }
            return null;
        }
    }

    /**
     * An instance of this class is created by 'IndexedDroolsControllerFactory',
     * using features. It does the build operation when the value of the
     * 'controller.type' property matches the value of POLICY_CONTROLLER_BUILDER_TAG.
     */
    public static class DroolsBuilder implements DroolsControllerFeatureApi {
        @Override
        public int getSequenceNumber() {
            return 1;
        }

        @Override
        public DroolsController beforeInstance(Properties properties,
                String groupId, String artifactId, String version,
                List<TopicCoderFilterConfiguration> decoderConfigurations,
                List<TopicCoderFilterConfiguration> encoderConfigurations) {

            if (POLICY_CONTROLLER_BUILDER_TAG.equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) {
                return new NullDroolsController();
            }
            return null;
        }
    }
}