diff options
author | 2019-02-25 11:36:23 -0500 | |
---|---|---|
committer | 2019-02-25 12:47:32 -0500 | |
commit | 6329d5933f064f9418b300ef87235681fabd717d (patch) | |
tree | 1f8a914b40a385110dc4fd1998685156d375dee2 /main/src/test/java/org | |
parent | 92eabbc64c1ea6f6bfe6df979ccc487e99e806be (diff) |
Replace static methods with single getInstance
Some of the PAP classes use a number of static methods. These have
been modified to use regular, non-static methods, a single static
method, getInstance.
Also modified PapStatisticsManager so its methods are thread safe.
Changed "instance" to "current" for the activator, as it may be changed.
Fix new checkstyle issues.
Updated copyrights.
Renamed test class to be consistent.
Added test for getCurrent/setCurrent and isAlive.
Change-Id: Id6df55fa4c116852032ad61f80f899fcd292f864
Issue-ID: POLICY-1444
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java/org')
3 files changed, 140 insertions, 39 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java index a8451992..77ac8f1e 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapRestServer.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,6 +77,8 @@ public class TestPapRestServer { */ @After public void teardown() { + PapStatisticsManager.getInstance().resetAllStatistics(); + try { if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) { if (main != null) { @@ -103,21 +106,16 @@ public class TestPapRestServer { } @Test - public void testHealthCheckFailure() throws InterruptedException, IOException { + public void testHealthCheckFailure() throws Exception { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PAP_GROUP_NAME); restServer = new PapRestServer(restServerParams); - try { - restServer.start(); - final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); - final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); - validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report); - assertTrue(restServer.isAlive()); - assertTrue(restServer.toString().startsWith("PapRestServer [servers=")); - } catch (final Exception exp) { - LOGGER.error("testHealthCheckFailure failed", exp); - fail("Test should not throw an exception"); - } + restServer.start(); + final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); + final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report); + assertTrue(restServer.isAlive()); + assertTrue(restServer.toString().startsWith("PapRestServer [servers=")); } @Test @@ -144,7 +142,6 @@ public class TestPapRestServer { invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); report = invocationBuilder.get(StatisticsReport.class); validateStatisticsReport(report, 1, 200); - PapStatisticsManager.resetAllStatistics(); } catch (final Exception exp) { LOGGER.error("testPapStatistics_200 failed", exp); fail("Test should not throw an exception"); @@ -161,7 +158,6 @@ public class TestPapRestServer { final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); validateStatisticsReport(report, 0, 500); - PapStatisticsManager.resetAllStatistics(); } catch (final Exception exp) { LOGGER.error("testPapStatistics_500 failed", exp); fail("Test should not throw an exception"); @@ -182,15 +178,9 @@ public class TestPapRestServer { } @Test - public void testPapStatisticsConstructorIsPrivate() { - try { - final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.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")); - } + public void testPapStatisticsConstructorIsProtected() throws Exception { + final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor(); + assertTrue(Modifier.isProtected(constructor.getModifiers())); } private Main startPapService(final boolean http) { @@ -265,14 +255,16 @@ public class TestPapRestServer { } private void updateDistributionStatistics() { - PapStatisticsManager.updateTotalPdpCount(); - PapStatisticsManager.updateTotalPdpGroupCount(); - PapStatisticsManager.updateTotalPolicyDeployCount(); - PapStatisticsManager.updatePolicyDeploySuccessCount(); - PapStatisticsManager.updatePolicyDeployFailureCount(); - PapStatisticsManager.updateTotalPolicyDownloadCount(); - PapStatisticsManager.updatePolicyDownloadSuccessCount(); - PapStatisticsManager.updatePolicyDownloadFailureCount(); + PapStatisticsManager mgr = PapStatisticsManager.getInstance(); + + mgr.updateTotalPdpCount(); + mgr.updateTotalPdpGroupCount(); + mgr.updateTotalPolicyDeployCount(); + mgr.updatePolicyDeploySuccessCount(); + mgr.updatePolicyDeployFailureCount(); + mgr.updateTotalPolicyDownloadCount(); + mgr.updatePolicyDownloadSuccessCount(); + mgr.updatePolicyDownloadFailureCount(); } private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) { diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPapStatisticsManager.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapStatisticsManager.java new file mode 100644 index 00000000..d4e4f81a --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPapStatisticsManager.java @@ -0,0 +1,89 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +import org.junit.Test; + +public class TestPapStatisticsManager { + + @Test + public void test() { + PapStatisticsManager mgr = PapStatisticsManager.getInstance(); + assertNotNull(mgr); + + // should return the same manager + assertSame(mgr, PapStatisticsManager.getInstance()); + + // work with a new object so we don't have to worry about initial counts + mgr = new PapStatisticsManager(); + + // try each update + + assertEquals(0, mgr.getTotalPdpCount()); + assertEquals(1, mgr.updateTotalPdpCount()); + assertEquals(1, mgr.getTotalPdpCount()); + + assertEquals(0, mgr.getTotalPdpGroupCount()); + assertEquals(1, mgr.updateTotalPdpGroupCount()); + assertEquals(1, mgr.getTotalPdpGroupCount()); + + assertEquals(0, mgr.getTotalPolicyDeployCount()); + assertEquals(1, mgr.updateTotalPolicyDeployCount()); + assertEquals(1, mgr.getTotalPolicyDeployCount()); + + assertEquals(0, mgr.getPolicyDeploySuccessCount()); + assertEquals(1, mgr.updatePolicyDeploySuccessCount()); + assertEquals(1, mgr.getPolicyDeploySuccessCount()); + + assertEquals(0, mgr.getPolicyDeployFailureCount()); + assertEquals(1, mgr.updatePolicyDeployFailureCount()); + assertEquals(1, mgr.getPolicyDeployFailureCount()); + + assertEquals(0, mgr.getTotalPolicyDownloadCount()); + assertEquals(1, mgr.updateTotalPolicyDownloadCount()); + assertEquals(1, mgr.getTotalPolicyDownloadCount()); + + assertEquals(0, mgr.getPolicyDownloadSuccessCount()); + assertEquals(1, mgr.updatePolicyDownloadSuccessCount()); + assertEquals(1, mgr.getPolicyDownloadSuccessCount()); + + assertEquals(0, mgr.getPolicyDownloadFailureCount()); + assertEquals(1, mgr.updatePolicyDownloadFailureCount()); + assertEquals(1, mgr.getPolicyDownloadFailureCount()); + + // now check reset + mgr.resetAllStatistics(); + + assertEquals(0, mgr.getPolicyDeployFailureCount()); + assertEquals(0, mgr.getTotalPdpCount()); + assertEquals(0, mgr.getTotalPdpGroupCount()); + assertEquals(0, mgr.getTotalPolicyDeployCount()); + assertEquals(0, mgr.getPolicyDeploySuccessCount()); + assertEquals(0, mgr.getPolicyDeployFailureCount()); + assertEquals(0, mgr.getTotalPolicyDownloadCount()); + assertEquals(0, mgr.getPolicyDownloadSuccessCount()); + assertEquals(0, mgr.getPolicyDownloadFailureCount()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java index 3781be47..8e8e6c09 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,10 +22,14 @@ package org.onap.policy.pap.main.startstop; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.parameters.CommonTestData; @@ -45,6 +50,18 @@ public class TestPapActivator { private PapActivator activator; /** + * Initializes an activator. + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; + final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); + final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); + activator = new PapActivator(parGroup); + } + + /** * Method for cleanup after each test. */ @After @@ -60,12 +77,10 @@ public class TestPapActivator { @Test public void testPapActivator() throws PolicyPapException { - final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; - final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); - final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); - activator = new PapActivator(parGroup); try { + assertFalse(activator.isAlive()); activator.initialize(); + assertTrue(activator.isAlive()); assertTrue(activator.getParameterGroup().isValid()); assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName()); } catch (final Exception exp) { @@ -76,12 +91,17 @@ public class TestPapActivator { @Test(expected = PolicyPapException.class) public void testPapActivatorError() throws PolicyPapException { - final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" }; - final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); - final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); - activator = new PapActivator(parGroup); activator.initialize(); assertTrue(activator.getParameterGroup().isValid()); activator.initialize(); } + + @Test + public void testGetCurrent_testSetCurrent() { + assertNotNull(PapActivator.getCurrent()); + + PapActivator.setCurrent(activator); + + assertSame(activator, PapActivator.getCurrent()); + } } |