From ef4198b40ee5108cf8c549c09a0a04b09e3f959a Mon Sep 17 00:00:00 2001 From: jhh Date: Wed, 10 Feb 2021 16:20:35 -0600 Subject: support transaction reports in engine Issue-ID: POLICY-3033 Signed-off-by: jhh Change-Id: Id1f02c86be491d5145e74725f76953e294e2975c Signed-off-by: jhh --- .../drools/stats/PolicyStatsManagerTest.java | 44 +++++++++++ .../onap/policy/drools/stats/PolicyStatsTest.java | 86 ++++++++++++++++++++++ .../drools/system/PolicyEngineManagerTest.java | 40 +++++++--- .../policy/drools/system/PolicyEngineTest.java | 18 +++-- 4 files changed, 171 insertions(+), 17 deletions(-) create mode 100644 policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsManagerTest.java create mode 100644 policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsTest.java (limited to 'policy-management/src/test/java') diff --git a/policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsManagerTest.java new file mode 100644 index 00000000..9ab23979 --- /dev/null +++ b/policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsManagerTest.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 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.stats; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.drools.metrics.TransMetric; + +public class PolicyStatsManagerTest { + + @Test + public void testStat() { + PolicyStatsManager stats = new PolicyStatsManager(); + assertEquals(0, stats.getGroupStat().getPolicyExecutedCount()); + + TransMetric trans = new TransMetric(); + stats.stat("foo", trans); + stats.stat("blah", trans); + stats.stat("blah", trans); + assertEquals(3, stats.getGroupStat().getPolicyExecutedCount()); + + assertEquals(1, stats.getSubgroupStats().get("foo").getPolicyExecutedFailCount()); + assertEquals(2, stats.getSubgroupStats().get("blah").getPolicyExecutedFailCount()); + } +} \ No newline at end of file diff --git a/policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsTest.java b/policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsTest.java new file mode 100644 index 00000000..29518e93 --- /dev/null +++ b/policy-management/src/test/java/org/onap/policy/drools/stats/PolicyStatsTest.java @@ -0,0 +1,86 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 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.stats; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.drools.metrics.TransMetric; + +public class PolicyStatsTest { + + @Test + public void testStat() { + TransMetric trans1 = createTrans(); + trans1.setSuccess(true); + + PolicyStats stats = new PolicyStats(); + stats.stat(trans1); + + assertEquals(1, stats.getPolicyExecutedCount()); + assertEquals(trans1.getStartTime().toEpochMilli(), stats.getLastStart()); + assertEquals((double) trans1.getElapsedTime(), stats.getAverageExecutionTime(), 0.0d); + assertEquals(trans1.getEndTime().toEpochMilli(), stats.getLastExecutionTime()); + assertEquals(0, stats.getPolicyExecutedFailCount()); + assertEquals(1, stats.getPolicyExecutedSuccessCount()); + assertThat(stats.getBirthTime()).isGreaterThanOrEqualTo(trans1.getStartTime().toEpochMilli()); + + TransMetric trans2 = createTrans(); + trans2.setSuccess(false); + trans2.setEndTime(trans2.getStartTime().plusMillis(5)); + trans2.setElapsedTime(null); + stats.stat(trans2); + + assertEquals(2, stats.getPolicyExecutedCount()); + assertEquals(trans2.getStartTime().toEpochMilli(), stats.getLastStart()); + assertEquals((5 + 1) / 2d, stats.getAverageExecutionTime(), 0.0d); + assertEquals(trans2.getEndTime().toEpochMilli(), stats.getLastExecutionTime()); + assertEquals(1, stats.getPolicyExecutedFailCount()); + assertEquals(1, stats.getPolicyExecutedSuccessCount()); + assertThat(stats.getBirthTime()).isLessThanOrEqualTo(trans2.getStartTime().toEpochMilli()); + + TransMetric trans3 = createTrans(); + trans3.setSuccess(false); + trans3.setEndTime(trans3.getStartTime().plusMillis(9)); + trans3.setElapsedTime(null); + stats.stat(trans3); + + assertEquals(3, stats.getPolicyExecutedCount()); + assertEquals(trans3.getStartTime().toEpochMilli(), stats.getLastStart()); + assertEquals((5 + 1 + 9) / 3d, stats.getAverageExecutionTime(), 0.0d); + assertEquals(trans3.getEndTime().toEpochMilli(), stats.getLastExecutionTime()); + assertEquals(2, stats.getPolicyExecutedFailCount()); + assertEquals(1, stats.getPolicyExecutedSuccessCount()); + assertThat(stats.getBirthTime()).isLessThanOrEqualTo(trans2.getStartTime().toEpochMilli()); + } + + private TransMetric createTrans() { + TransMetric trans = new TransMetric(); + trans.setStartTime(null); + trans.setEndTime(trans.getStartTime().plusMillis(1)); + + trans.setElapsedTime(null); + assertEquals(1L, trans.getElapsedTime().longValue()); + return trans; + } + +} \ No newline at end of file diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java index f4876a95..f9b101c3 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2021 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. @@ -64,12 +64,15 @@ import org.onap.policy.drools.core.lock.LockCallback; import org.onap.policy.drools.core.lock.PolicyResourceLockManager; import org.onap.policy.drools.features.PolicyControllerFeatureApi; import org.onap.policy.drools.features.PolicyEngineFeatureApi; +import org.onap.policy.drools.metrics.Metric; +import org.onap.policy.drools.metrics.TransMetric; import org.onap.policy.drools.persistence.SystemPersistence; import org.onap.policy.drools.properties.DroolsPropertyConstants; import org.onap.policy.drools.protocol.coders.EventProtocolCoder; import org.onap.policy.drools.protocol.configuration.ControllerConfiguration; import org.onap.policy.drools.protocol.configuration.DroolsConfiguration; import org.onap.policy.drools.protocol.configuration.PdpdConfiguration; +import org.onap.policy.drools.stats.PolicyStatsManager; import org.onap.policy.drools.system.internal.SimpleLockManager; import org.onap.policy.drools.system.internal.SimpleLockProperties; @@ -115,7 +118,6 @@ public class PolicyEngineManagerTest { private HttpClientFactory clientFactory; private HttpClient client1; private HttpClient client2; - private List clients; private TopicEndpoint endpoint; private PolicyController controller; private PolicyController controller2; @@ -143,6 +145,7 @@ public class PolicyEngineManagerTest { private PolicyEngineManager mgr; private ScheduledExecutorService exsvc; private PolicyResourceLockManager lockmgr; + private PolicyStatsManager statsManager; /** * Initializes the object to be tested. @@ -172,7 +175,6 @@ public class PolicyEngineManagerTest { serverFactory = mock(HttpServletServerFactory.class); client1 = mock(HttpClient.class); client2 = mock(HttpClient.class); - clients = Arrays.asList(client1, client2); clientFactory = mock(HttpClientFactory.class); endpoint = mock(TopicEndpoint.class); controller = mock(PolicyController.class); @@ -200,6 +202,8 @@ public class PolicyEngineManagerTest { pdpConfig = new PdpdConfiguration(); exsvc = mock(ScheduledExecutorService.class); lockmgr = mock(PolicyResourceLockManager.class); + statsManager = new PolicyStatsManager(); + statsManager.getGroupStat().setBirthTime(0L); when(lockmgr.start()).thenReturn(true); when(lockmgr.stop()).thenReturn(true); @@ -235,7 +239,7 @@ public class PolicyEngineManagerTest { when(client2.start()).thenReturn(true); when(client2.stop()).thenReturn(true); - when(clientFactory.inventory()).thenReturn(clients); + when(clientFactory.inventory()).thenReturn(List.of(client1, client2)); when(source1.getTopic()).thenReturn("source1-topic"); when(source1.start()).thenReturn(true); @@ -286,7 +290,7 @@ public class PolicyEngineManagerTest { when(endpoint.lock()).thenReturn(true); when(endpoint.unlock()).thenReturn(true); when(endpoint.getTopicSink(CommInfrastructure.NOOP, MY_TOPIC)).thenReturn(sink1); - when(endpoint.getTopicSinks(MY_TOPIC)).thenReturn(Arrays.asList(sink1)); + when(endpoint.getTopicSinks(MY_TOPIC)).thenReturn(Collections.singletonList(sink1)); when(coder.encode(any(), any())).thenReturn(MESSAGE); @@ -295,6 +299,7 @@ public class PolicyEngineManagerTest { when(engine.createPolicyController(CONTROLLER3, properties)).thenReturn(controller3); when(engine.createPolicyController(CONTROLLER4, properties)).thenReturn(controller4); + when(engine.getStats()).thenReturn(statsManager); config3.setName(CONTROLLER3); config3.setOperation(ControllerConfiguration.CONFIG_CONTROLLER_OPERATION_CREATE); @@ -574,7 +579,7 @@ public class PolicyEngineManagerTest { // source list of size 1 setUp(); - when(endpoint.addTopicSources(any(Properties.class))).thenReturn(Arrays.asList(source1)); + when(endpoint.addTopicSources(any(Properties.class))).thenReturn(Collections.singletonList(source1)); mgr.configure(properties); assertTrue(mgr.configure(pdpConfig)); @@ -671,7 +676,7 @@ public class PolicyEngineManagerTest { // force exception in the first controller with invalid operation setUp(); config3.setOperation("unknown-operation"); - assertEquals(Arrays.asList(controller4), mgr.updatePolicyControllers(pdpConfig.getControllers())); + assertEquals(Collections.singletonList(controller4), mgr.updatePolicyControllers(pdpConfig.getControllers())); // controller3 should NOT have been done verify(controllerFactory, never()).patch(controller3, drools3); @@ -1354,6 +1359,18 @@ public class PolicyEngineManagerTest { assertThatIllegalArgumentException().isThrownBy(() -> mgr.getFeatureProvider("unknown-feature")); } + @Test + public void testTransaction() { + mgr.metric("foo", "bar", new Metric()); + assertEquals(0, mgr.getStats().getGroupStat().getPolicyExecutedCount()); + assertEquals(0, mgr.getStats().getSubgroupStats().size()); + + mgr.transaction("foo", "bar", new TransMetric()); + assertEquals(1, mgr.getStats().getGroupStat().getPolicyExecutedCount()); + assertEquals(1, mgr.getStats().getSubgroupStats().size()); + assertEquals(1, mgr.getStats().getSubgroupStats().get("foo[bar]").getPolicyExecutedFailCount()); + } + @Test public void testOnTopicEvent() { mgr.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, pdpConfigJson); @@ -1913,7 +1930,7 @@ public class PolicyEngineManagerTest { // remaining methods should not have been invoked assertThatThrownBy(() -> verifyBefore.accept(prov2)).isInstanceOf(AssertionError.class); - assertThatThrownBy(() -> verifyMiddle.run()).isInstanceOf(AssertionError.class); + assertThatThrownBy(verifyMiddle::run).isInstanceOf(AssertionError.class); assertThatThrownBy(() -> verifyAfter.accept(prov1)).isInstanceOf(AssertionError.class); assertThatThrownBy(() -> verifyAfter.accept(prov2)).isInstanceOf(AssertionError.class); @@ -1995,6 +2012,11 @@ public class PolicyEngineManagerTest { return exsvc; } + @Override + public PolicyStatsManager getStats() { + return statsManager; + } + /** * Shutdown thread with overrides. */ @@ -2027,7 +2049,7 @@ public class PolicyEngineManagerTest { } @FunctionalInterface - private static interface RunnableWithEx { + private interface RunnableWithEx { void run() throws Exception; } } diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java index 2864d5de..30d38ce1 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -100,7 +100,7 @@ public class PolicyEngineTest { /** * logger. */ - private static Logger logger = LoggerFactory.getLogger(PolicyEngineTest.class); + private static final Logger logger = LoggerFactory.getLogger(PolicyEngineTest.class); private static GsonTestUtils gson; @@ -148,7 +148,7 @@ public class PolicyEngineTest { } @AfterClass - public static void tearDown() throws IOException { + public static void tearDown() { logger.info("enter"); cleanUpWorkingDir(); } @@ -170,6 +170,7 @@ public class PolicyEngineTest { logger.info("engine {} has configuration {}", PolicyEngineConstants.getManager(), engineProps); + PolicyEngineConstants.getManager().getStats().getGroupStat().setBirthTime(0L); gson.compareGson(PolicyEngineConstants.getManager(), new File(PolicyEngineTest.class.getSimpleName() + "Config.json")); } @@ -234,7 +235,7 @@ public class PolicyEngineTest { } @Test - public void test400ControllerAdd() throws Exception { + public void test400ControllerAdd() { logger.info("enter"); final Properties controllerProperties = new Properties(); @@ -243,6 +244,7 @@ public class PolicyEngineTest { assertEquals(1, PolicyControllerConstants.getFactory().inventory().size()); + PolicyEngineConstants.getManager().getStats().getGroupStat().setBirthTime(0L); gson.compareGson(PolicyEngineConstants.getManager(), new File(PolicyEngineTest.class.getSimpleName() + "Add.json")); } @@ -263,7 +265,7 @@ public class PolicyEngineTest { } @Test - public void test500Deactivate() throws Exception { + public void test500Deactivate() { logger.info("enter"); PolicyEngineConstants.getManager().deactivate(); @@ -276,7 +278,7 @@ public class PolicyEngineTest { } @Test - public void test501Activate() throws Exception { + public void test501Activate() { logger.info("enter"); PolicyEngineConstants.getManager().activate(); @@ -289,7 +291,7 @@ public class PolicyEngineTest { } @Test - public void test900ControllerRemove() throws Exception { + public void test900ControllerRemove() { logger.info("enter"); PolicyEngineConstants.getManager().removePolicyController(TEST_CONTROLLER_NAME); @@ -297,7 +299,7 @@ public class PolicyEngineTest { } @Test - public void test901Stop() throws InterruptedException { + public void test901Stop() { logger.info("enter"); /* Shutdown managed resources */ -- cgit 1.2.3-korg