2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.controller;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertSame;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.junit.jupiter.api.Assertions.fail;
29 import static org.onap.policy.drools.properties.DroolsPropertyConstants.PROPERTY_CONTROLLER_TYPE;
31 import java.util.List;
32 import java.util.Properties;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.drools.controller.internal.NullDroolsController;
35 import org.onap.policy.drools.features.DroolsControllerFeatureApi;
36 import org.onap.policy.drools.properties.DroolsPropertyConstants;
37 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
39 public class DroolsControllerFactoryTest {
41 private static final String DROOLS_CONTROLLER_BUILDER_TAG = "DroolsControllerFactoryTest";
42 public static final String TEST_GROUP_ID = "testGroupId";
43 public static final String TEST_ARTIFACT_ID = "testArtifactId";
44 public static final String TEST_VERSION = "testVersion";
47 void testBuildNullController() {
48 Properties droolsProps = new Properties();
49 DroolsController droolsController = DroolsControllerConstants.getFactory().build(droolsProps, null, null);
51 if (!isNullController(droolsController)) {
52 fail("drools controller is not a null controller");
57 void testGetNullController() {
58 DroolsController controller =
59 DroolsControllerConstants.getFactory().get(DroolsControllerConstants.NO_GROUP_ID,
60 DroolsControllerConstants.NO_ARTIFACT_ID, DroolsControllerConstants.NO_VERSION);
62 assertNotNull(controller);
63 assertEquals(DroolsControllerConstants.NO_GROUP_ID, controller.getGroupId());
64 assertEquals(DroolsControllerConstants.NO_ARTIFACT_ID, controller.getArtifactId());
65 assertEquals(DroolsControllerConstants.NO_VERSION, controller.getVersion());
69 void testInventory() {
70 List<DroolsController> controllers = DroolsControllerConstants.getFactory().inventory();
71 assertNotNull(controllers);
73 for (DroolsController controller : controllers) {
74 if (!isNullController(controller) && !isActualController(controller)) {
75 fail("drools controller is not a null controller");
82 DroolsControllerFactory droolsFactory = new IndexedDroolsControllerFactory();
83 droolsFactory.shutdown();
84 assertTrue(droolsFactory.inventory().isEmpty());
89 DroolsControllerFactory droolsFactory = new IndexedDroolsControllerFactory();
90 droolsFactory.destroy();
91 assertTrue(droolsFactory.inventory().isEmpty());
94 private boolean isNullController(DroolsController droolsController) {
95 if (droolsController == null) {
99 if (!DroolsControllerConstants.NO_GROUP_ID.equals(droolsController.getGroupId())) {
103 if (!DroolsControllerConstants.NO_ARTIFACT_ID.equals(droolsController.getArtifactId())) {
107 return DroolsControllerConstants.NO_VERSION.equals(droolsController.getVersion());
111 void testControllerType() {
112 DroolsControllerFactory droolsFactory = new IndexedDroolsControllerFactory();
113 Properties props = new Properties();
115 // this should build a 'NullDroolsController'
116 DroolsController ctrl1 = droolsFactory.build(props, null, null);
117 assertSame(NullDroolsController.class, ctrl1.getClass());
119 props.setProperty(PROPERTY_CONTROLLER_TYPE, DROOLS_CONTROLLER_BUILDER_TAG);
120 props.setProperty(DroolsPropertyConstants.RULES_GROUPID, TEST_GROUP_ID);
121 props.setProperty(DroolsPropertyConstants.RULES_ARTIFACTID, TEST_ARTIFACT_ID);
122 props.setProperty(DroolsPropertyConstants.RULES_VERSION, TEST_VERSION);
124 // this should build a 'TestDroolsController'
125 DroolsController ctrl2 = droolsFactory.build(props, null, null);
126 assertSame(TestDroolsController.class, ctrl2.getClass());
128 // verify that we can find the controller in the factory table
129 assertSame(ctrl2, droolsFactory.get(ctrl2.getGroupId(), ctrl2.getArtifactId(), null));
132 private boolean isActualController(DroolsController droolsController) {
133 if (droolsController == null) {
137 if (!"org.onap.policy.drools.test".equals(droolsController.getGroupId())) {
141 if (!"protocolcoder".equals(droolsController.getArtifactId())) {
145 return droolsController.getVersion() != null && droolsController.getVersion().substring(0, 1).matches("[0-9]");
149 * This class provides an alternate DroolsController implementation,
150 * for the purpose of easy identification within a Junit test.
152 public static class TestDroolsController extends NullDroolsController {
154 public String getGroupId() {
155 return TEST_GROUP_ID;
159 public String getArtifactId() {
160 return TEST_ARTIFACT_ID;
165 * An instance of this class is created by 'IndexedDroolsControllerFactory',
166 * using features. It does the build operation when the value of the
167 * 'controller.type' property matches the value of DROOLS_CONTROLLER_BUILDER_TAG.
169 public static class DroolsBuilder implements DroolsControllerFeatureApi {
171 public int getSequenceNumber() {
176 public DroolsController beforeInstance(Properties properties,
177 String groupId, String artifactId, String version,
178 List<TopicCoderFilterConfiguration> decoderConfigurations,
179 List<TopicCoderFilterConfiguration> encoderConfigurations) {
181 if (DROOLS_CONTROLLER_BUILDER_TAG.equals(properties.getProperty(PROPERTY_CONTROLLER_TYPE))) {
182 return new TestDroolsController();