diff options
author | Jim Hahn <jrh3@att.com> | 2021-07-16 15:34:32 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-07-21 11:23:29 -0400 |
commit | 014db93590258af9d04d317a391f0f43ac71526c (patch) | |
tree | 863fb44daaa9c601ee8f580aa2fb82621f35d3eb /main/src/test/java/org/onap | |
parent | 6c5332aef8f348c362a2aa4b91f47078fc68dcfc (diff) |
Use singleton for api statistics
Reimplemented the statistics manager to use a singleton instead of
static fields.
Also changed some fields to "private".
Issue-ID: POLICY-3392
Change-Id: Ic708f7fa29cba795c47d387764267016163f679f
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java/org/onap')
5 files changed, 55 insertions, 74 deletions
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index aac37f19..ba3ad895 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -28,12 +28,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.Modifier; import java.security.SecureRandom; import java.util.Collections; import java.util.List; @@ -226,20 +223,6 @@ public class TestApiRestServer { } @Test - public void testApiStatisticsConstructorIsPrivate() { - - try { - final Constructor<ApiStatisticsManager> constructor = ApiStatisticsManager.class.getDeclaredConstructor(); - assertTrue(Modifier.isPrivate(constructor.getModifiers())); - constructor.setAccessible(true); - constructor.newInstance(); - - } catch (final Exception exp) { - assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed")); - } - } - - @Test public void testCreatePolicyTypes() throws Exception { for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { Response rawResponse = createResource(POLICYTYPES, resrcName); @@ -815,22 +798,23 @@ public class TestApiRestServer { } private void updateApiStatistics() { - - ApiStatisticsManager.updateTotalApiCallCount(); - ApiStatisticsManager.updateApiCallSuccessCount(); - ApiStatisticsManager.updateApiCallFailureCount(); - ApiStatisticsManager.updateTotalPolicyGetCount(); - ApiStatisticsManager.updateTotalPolicyPostCount(); - ApiStatisticsManager.updateTotalPolicyTypeGetCount(); - ApiStatisticsManager.updateTotalPolicyTypePostCount(); - ApiStatisticsManager.updatePolicyGetSuccessCount(); - ApiStatisticsManager.updatePolicyGetFailureCount(); - ApiStatisticsManager.updatePolicyPostSuccessCount(); - ApiStatisticsManager.updatePolicyPostFailureCount(); - ApiStatisticsManager.updatePolicyTypeGetSuccessCount(); - ApiStatisticsManager.updatePolicyTypeGetFailureCount(); - ApiStatisticsManager.updatePolicyTypePostSuccessCount(); - ApiStatisticsManager.updatePolicyTypePostFailureCount(); + var mgr = ApiStatisticsManager.getInstance(); + + mgr.updateTotalApiCallCount(); + mgr.updateApiCallSuccessCount(); + mgr.updateApiCallFailureCount(); + mgr.updateTotalPolicyGetCount(); + mgr.updateTotalPolicyPostCount(); + mgr.updateTotalPolicyTypeGetCount(); + mgr.updateTotalPolicyTypePostCount(); + mgr.updatePolicyGetSuccessCount(); + mgr.updatePolicyGetFailureCount(); + mgr.updatePolicyPostSuccessCount(); + mgr.updatePolicyPostFailureCount(); + mgr.updatePolicyTypeGetSuccessCount(); + mgr.updatePolicyTypeGetFailureCount(); + mgr.updatePolicyTypePostSuccessCount(); + mgr.updatePolicyTypePostFailureCount(); } private void validateStatisticsReport(final StatisticsReport report, final int code) { diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java index 148ec1b8..5f5c586d 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2019 IBM. * Modifications Copyright (C) 2020 Bell Canada. + * Modifications 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. @@ -25,38 +26,34 @@ package org.onap.policy.api.main.rest; import static org.junit.Assert.assertEquals; -import org.junit.Before; import org.junit.Test; public class TestApiStatisticsManager { - @Before - public void setUp() { - ApiStatisticsManager.resetAllStatistics(); - } - @Test public void testUpdateMethods() { - assertEquals(1, ApiStatisticsManager.updateTotalApiCallCount()); - assertEquals(1, ApiStatisticsManager.updateApiCallSuccessCount()); - assertEquals(1, ApiStatisticsManager.updateApiCallFailureCount()); - assertEquals(1, ApiStatisticsManager.updateTotalPolicyGetCount()); - assertEquals(1, ApiStatisticsManager.updateTotalPolicyPostCount()); - assertEquals(1, ApiStatisticsManager.updateTotalPolicyDeleteCount()); - assertEquals(1, ApiStatisticsManager.updateTotalPolicyTypeGetCount()); - assertEquals(1, ApiStatisticsManager.updateTotalPolicyTypePostCount()); - assertEquals(1, ApiStatisticsManager.updateTotalPolicyTypeDeleteCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyGetSuccessCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyGetFailureCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyPostSuccessCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyPostFailureCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyDeleteSuccessCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyDeleteFailureCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyTypeGetSuccessCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyTypeGetFailureCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyTypePostSuccessCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyTypePostFailureCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyTypeDeleteSuccessCount()); - assertEquals(1, ApiStatisticsManager.updatePolicyTypeDeleteFailureCount()); + var mgr = new ApiStatisticsManager(); + + assertEquals(1, mgr.updateTotalApiCallCount()); + assertEquals(1, mgr.updateApiCallSuccessCount()); + assertEquals(1, mgr.updateApiCallFailureCount()); + assertEquals(1, mgr.updateTotalPolicyGetCount()); + assertEquals(1, mgr.updateTotalPolicyPostCount()); + assertEquals(1, mgr.updateTotalPolicyDeleteCount()); + assertEquals(1, mgr.updateTotalPolicyTypeGetCount()); + assertEquals(1, mgr.updateTotalPolicyTypePostCount()); + assertEquals(1, mgr.updateTotalPolicyTypeDeleteCount()); + assertEquals(1, mgr.updatePolicyGetSuccessCount()); + assertEquals(1, mgr.updatePolicyGetFailureCount()); + assertEquals(1, mgr.updatePolicyPostSuccessCount()); + assertEquals(1, mgr.updatePolicyPostFailureCount()); + assertEquals(1, mgr.updatePolicyDeleteSuccessCount()); + assertEquals(1, mgr.updatePolicyDeleteFailureCount()); + assertEquals(1, mgr.updatePolicyTypeGetSuccessCount()); + assertEquals(1, mgr.updatePolicyTypeGetFailureCount()); + assertEquals(1, mgr.updatePolicyTypePostSuccessCount()); + assertEquals(1, mgr.updatePolicyTypePostFailureCount()); + assertEquals(1, mgr.updatePolicyTypeDeleteSuccessCount()); + assertEquals(1, mgr.updatePolicyTypeDeleteFailureCount()); } } diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java index 7541c50c..e47a4ce6 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy API * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. * ================================================================================ @@ -81,12 +81,12 @@ public class TestPolicyProvider { "policies/vCPE.policy.different.policy.fields.json"; private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json"; - public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = "policytypes/onap.policies.controlloop.operational.Common.yaml"; - public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = "policytypes/onap.policies.controlloop.operational.common.Drools.yaml"; private static final String POLICY_RESOURCE_OPERATIONAL = "policies/vCPE.policy.operational.input.tosca.json"; - public static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; + private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; /** * Initializes parameters. diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java index 95f93865..355f3d45 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy API * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ @@ -64,15 +64,15 @@ public class TestPolicyTypeProvider { "policytypes/onap.policies.optimization.Resource.no.version.yaml"; private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2"; - public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON = "policytypes/onap.policies.controlloop.operational.Common.yaml"; - public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS = "policytypes/onap.policies.controlloop.operational.common.Drools.yaml"; - public static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX = + private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX = "policytypes/onap.policies.controlloop.operational.common.Apex.yaml"; - public static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common"; - public static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex"; - public static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; + private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common"; + private static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex"; + private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools"; /** * Initializes parameters. diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java index b899e014..ccd0f14d 100644 --- a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java +++ b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -47,8 +47,8 @@ public class TestApiActivator { final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments); final ApiActivator activator = new ApiActivator(parGroup); activator.initialize(); - assertTrue(activator.getApiParameterGroup().isValid()); - assertEquals(CommonTestData.API_GROUP_NAME, activator.getApiParameterGroup().getName()); + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.API_GROUP_NAME, activator.getParameterGroup().getName()); activator.terminate(); } } |