diff options
author | Jim Hahn <jrh3@att.com> | 2019-03-24 08:13:58 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-03-25 20:56:13 -0400 |
commit | 602572eb8abaf0ea6528ee45bd1006df16798ea9 (patch) | |
tree | 9ff9d3642b7e024bab139a978cf50ff298b9c0d4 /main/src/test/java/org/onap | |
parent | 7b5fd6ca1ca335369aeed22ad29f691a16c333e9 (diff) |
Changes to PAP infrastructure to support PDP
Modified PapActivator to implement the Startable interface and used
that to manage it instead of the original method names.
Replaced pdp-common enums with models-pdp enums.
Added the use of the Registry.
Added parameters to be used for PDP interaction.
Added DAO interface classes.
Modified HTTP client, in junit tests, to use GSON instead of Jackson.
Removed DAO code, as the interfaces are not merged into policy/common
yet.
Added pdpParameters to default json config file.
Changed tabs to spaces in json files.
Removed "internal" classes that were moved to models-pdp.
Change-Id: I969a7747e926680ad403557b44a0a52d289fb6d9
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java/org/onap')
12 files changed, 413 insertions, 69 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/PapConstantsTest.java b/main/src/test/java/org/onap/policy/pap/main/PapConstantsTest.java new file mode 100644 index 00000000..9509d03b --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/PapConstantsTest.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main; + +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +public class PapConstantsTest { + + @Test + public void test() throws Exception { + // verify that constructor does not throw an exception + Whitebox.invokeConstructor(PapConstants.class); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java index d0dba808..9cef0c95 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java @@ -31,9 +31,9 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.Test; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.models.pdp.concepts.PdpResponseDetails; +import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.pap.main.comm.MultiPdpStatusListener; -import org.onap.policy.pdp.common.models.PdpResponseDetails; -import org.onap.policy.pdp.common.models.PdpStatus; public class MultiPdpStatusListenerTest { private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java index 8054194b..9d0a1abb 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java @@ -21,6 +21,7 @@ package org.onap.policy.pap.main.parameters; +import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import org.onap.policy.common.parameters.ParameterGroup; @@ -72,7 +73,7 @@ public class CommonTestData { map.put("name", name); map.put("restServerParameters", getRestServerParametersMap(false)); - map.put("pdpGroupDeploymentParameters", getPdpGroupDeploymentParametersMap()); + map.put("pdpParameters", getPdpParametersMap()); return map; } @@ -99,6 +100,47 @@ public class CommonTestData { } /** + * Returns a property map for a PdpParameters map for test cases. + * @return a property map suitable for constructing an object + */ + public Map<String,Object> getPdpParametersMap() { + Map<String,Object> map = new TreeMap<>(); + + map.put("updateParameters", getPdpUpdateParametersMap()); + map.put("stateChangeParameters", getPdpStateChangeParametersMap()); + + return map; + } + + /** + * Returns a property map for a PdpUpdateParameters map for test cases. + * @return a property map suitable for constructing an object + */ + public Map<String,Object> getPdpUpdateParametersMap() { + return getPdpRequestParametersMap(); + } + + /** + * Returns a property map for a PdpStateChangeParameters map for test cases. + * @return a property map suitable for constructing an object + */ + public Map<String,Object> getPdpStateChangeParametersMap() { + return getPdpRequestParametersMap(); + } + + /** + * Returns a property map for a PdpParameters map for test cases. + * @return a property map suitable for constructing an object + */ + public Map<String,Object> getPdpRequestParametersMap() { + Map<String, Object> map = new HashMap<>(); + map.put("maxRetryCount", "1"); + map.put("maxWaitMs", "2"); + + return map; + } + + /** * Returns a property map for a PdpGroupDeploymentParameters map for test cases. * * @return a property map suitable for constructing an object diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpParameters.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpParameters.java new file mode 100644 index 00000000..05bcc103 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpParameters.java @@ -0,0 +1,95 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.parameters; + +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.util.Map; +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class TestPdpParameters { + private static CommonTestData testData = new CommonTestData(); + + @Test + public void testGetters() { + PdpParameters params = testData.toObject(testData.getPdpParametersMap(), PdpParameters.class); + + PdpUpdateParameters update = params.getUpdateParameters(); + assertNotNull(update); + assertEquals(1, update.getMaxRetryCount()); + + PdpStateChangeParameters state = params.getStateChangeParameters(); + assertNotNull(state); + assertEquals(2, state.getMaxWaitMs()); + } + + @Test + public void testValidate() { + // valid + Map<String, Object> map = testData.getPdpParametersMap(); + GroupValidationResult result = testData.toObject(map, PdpParameters.class).validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + + // no update params + map = testData.getPdpParametersMap(); + map.remove("updateParameters"); + result = testData.toObject(map, PdpParameters.class).validate(); + assertFalse(result.isValid()); + assertTrue(result.getResult().contains("field 'updateParameters'".replace('\'', '"'))); + assertTrue(result.getResult().contains("is null")); + + // invalid update params + map = testData.getPdpParametersMap(); + @SuppressWarnings("unchecked") + Map<String, Object> updmap = (Map<String, Object>) map.get("updateParameters"); + updmap.put("maxRetryCount", "-2"); + result = testData.toObject(map, PdpParameters.class).validate(); + assertFalse(result.isValid()); + assertTrue(result.getResult().contains("parameter group 'PdpUpdateParameters'".replace('\'', '"'))); + assertTrue(result.getResult().contains( + "field 'maxRetryCount' type 'int' value '-2' INVALID, must be >= 0".replace('\'', '"'))); + + // no state-change params + map = testData.getPdpParametersMap(); + map.remove("stateChangeParameters"); + result = testData.toObject(map, PdpParameters.class).validate(); + assertFalse(result.isValid()); + + // invalid state-change params + map = testData.getPdpParametersMap(); + @SuppressWarnings("unchecked") + Map<String, Object> statemap = (Map<String, Object>) map.get("stateChangeParameters"); + statemap.put("maxRetryCount", "-3"); + result = testData.toObject(map, PdpParameters.class).validate(); + assertFalse(result.isValid()); + System.out.println(result.getResult()); + assertTrue(result.getResult().contains("parameter group 'PdpStateChangeParameters'".replace('\'', '"'))); + assertTrue(result.getResult().contains( + "field 'maxRetryCount' type 'int' value '-3' INVALID, must be >= 0".replace('\'', '"'))); + } + +} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpRequestParameters.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpRequestParameters.java new file mode 100644 index 00000000..e852442f --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpRequestParameters.java @@ -0,0 +1,76 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; + +public class TestPdpRequestParameters { + private static final Coder coder = new StandardCoder(); + + @Test + public void test() throws Exception { + PdpRequestParameters params = makeParams(10, 20); + assertEquals(10, params.getMaxRetryCount()); + assertEquals(20, params.getMaxWaitMs()); + } + + @Test + public void testValidate() throws Exception { + // valid, zeroes + PdpRequestParameters params = makeParams(0, 0); + GroupValidationResult result = params.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + + // valid + params = makeParams(100, 110); + result = params.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + + // invalid retry count + params = makeParams(-1, 120); + result = params.validate(); + assertFalse(result.isValid()); + assertTrue(result.getResult().contains( + "field 'maxRetryCount' type 'int' value '-1' INVALID, must be >= 0".replace('\'', '"'))); + + // invalid wait time + params = makeParams(130, -1); + result = params.validate(); + assertFalse(result.isValid()); + assertTrue(result.getResult() + .contains("field 'maxWaitMs' type 'long' value '-1' INVALID, must be >= 0".replace('\'', '"'))); + } + + private PdpRequestParameters makeParams(int maxRetry, long maxWait) throws Exception { + String json = "{'name':'abc', 'maxRetryCount':" + maxRetry + ", 'maxWaitMs':" + maxWait + "}"; + return coder.decode(json.replace('\'', '"'), PdpRequestParameters.class); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpStateChangeParameters.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpStateChangeParameters.java new file mode 100644 index 00000000..5744303f --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpStateChangeParameters.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.parameters; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; + +/** + * As {@link TestPdpRequestParameters} tests the "getXxx()" methods, all we need to verify + * here is that the object is valid after loading from JSON. + */ +public class TestPdpStateChangeParameters { + private static final Coder coder = new StandardCoder(); + + @Test + public void testValidate() throws Exception { + // valid, zeroes + PdpStateChangeParameters params = makeParams(10, 20); + GroupValidationResult result = params.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + } + + private PdpStateChangeParameters makeParams(int maxRetry, long maxWait) throws Exception { + String json = "{'maxRetryCount':" + maxRetry + ", 'maxWaitMs':" + maxWait + "}"; + return coder.decode(json.replace('\'', '"'), PdpStateChangeParameters.class); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpUpdateParameters.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpUpdateParameters.java new file mode 100644 index 00000000..6866c401 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpUpdateParameters.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.parameters; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; + +/** + * As {@link TestPdpRequestParameters} tests the "getXxx()" methods, all we need to verify + * here is that the object is valid after loading from JSON. + */ +public class TestPdpUpdateParameters { + private static final Coder coder = new StandardCoder(); + + @Test + public void testValidate() throws Exception { + // valid, zeroes + PdpUpdateParameters params = makeParams(10, 20); + GroupValidationResult result = params.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + } + + private PdpUpdateParameters makeParams(int maxRetry, long maxWait) throws Exception { + String json = "{'maxRetryCount':" + maxRetry + ", 'maxWaitMs':" + maxWait + "}"; + return coder.decode(json.replace('\'', '"'), PdpUpdateParameters.class); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java b/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java index 25ab1c37..32eb3b2c 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java @@ -26,7 +26,6 @@ import static org.junit.Assert.assertTrue; import java.io.File; import java.security.SecureRandom; -import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.function.Function; @@ -37,15 +36,20 @@ import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.onap.policy.common.gson.GsonMessageBodyHandler; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.parameters.CommonTestData; import org.onap.policy.pap.main.startstop.Main; import org.onap.policy.pap.main.startstop.PapActivator; import org.powermock.reflect.Whitebox; @@ -119,7 +123,7 @@ public class CommonPapRestServer { startMain(); } - activatorWasAlive = PapActivator.getCurrent().isAlive(); + activatorWasAlive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive(); } /** @@ -127,7 +131,7 @@ public class CommonPapRestServer { */ @After public void tearDown() { - Whitebox.setInternalState(PapActivator.getCurrent(), "alive", activatorWasAlive); + markActivator(activatorWasAlive); } /** @@ -149,20 +153,11 @@ public class CommonPapRestServer { * @throws Exception if an error occurs */ private static void makeConfigFile() throws Exception { - Map<String, Object> restParams = new HashMap<>(); - restParams.put("host", "0.0.0.0"); - restParams.put("port", port); - restParams.put("userName", "healthcheck"); - restParams.put("password", "zb!XztG34"); - restParams.put("https", true); - - Map<String, Object> pdpGroupDeploy = new HashMap<>(); - pdpGroupDeploy.put("waitResponseMs", "0"); + Map<String, Object> config = new CommonTestData().getPapParameterGroupMap("PapGroup"); - Map<String, Object> config = new HashMap<>(); - config.put("name", "PapGroup"); - config.put("restServerParameters", restParams); - config.put("pdpGroupDeploymentParameters", pdpGroupDeploy); + @SuppressWarnings("unchecked") + Map<String, Object> restParams = (Map<String, Object>) config.get("restServerParameters"); + restParams.put("port", port); File file = new File("src/test/resources/parameters/TestConfigParams.json"); file.deleteOnExit(); @@ -176,6 +171,8 @@ public class CommonPapRestServer { * @throws Exception if an error occurs */ private static void startMain() throws Exception { + Registry.newRegistry(); + // make sure port is available if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) { throw new IllegalStateException("port " + port + " is still in use"); @@ -218,7 +215,13 @@ public class CommonPapRestServer { * Mark the activator as dead, but leave its REST server running. */ protected void markActivatorDead() { - Whitebox.setInternalState(PapActivator.getCurrent(), "alive", false); + markActivator(false); + } + + private void markActivator(boolean wasAlive) { + Object manager = Whitebox.getInternalState(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class), + "serviceManager"); + Whitebox.setInternalState(manager, "running", wasAlive); } /** @@ -272,6 +275,9 @@ public class CommonPapRestServer { ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); final Client client = clientBuilder.build(); + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + if (includeAuth) { final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); client.register(feature); 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 index d4e4f81a..b600711f 100644 --- 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 @@ -21,8 +21,6 @@ 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; @@ -30,14 +28,7 @@ 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(); + PapStatisticsManager mgr = new PapStatisticsManager(); // try each update diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java index 9874389e..1894fd7b 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestStatisticsRestControllerV1.java @@ -22,13 +22,11 @@ package org.onap.policy.pap.main.rest; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import java.lang.reflect.Constructor; -import java.lang.reflect.Modifier; import javax.ws.rs.client.Invocation; -import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.pap.main.PapConstants; /** * Class to perform unit test of {@link PapRestServer}. @@ -39,18 +37,6 @@ public class TestStatisticsRestControllerV1 extends CommonPapRestServer { private static final String STATISTICS_ENDPOINT = "statistics"; - /** - * Set up. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - super.setUp(); - - PapStatisticsManager.getInstance().resetAllStatistics(); - } - @Test public void testSwagger() throws Exception { super.testSwagger(STATISTICS_ENDPOINT); @@ -76,21 +62,15 @@ public class TestStatisticsRestControllerV1 extends CommonPapRestServer { markActivatorDead(); - PapStatisticsManager.getInstance().resetAllStatistics(); + Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class).resetAllStatistics(); Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT); StatisticsReport report = invocationBuilder.get(StatisticsReport.class); validateStatisticsReport(report, 0, 500); } - @Test - public void testPapStatisticsConstructorIsProtected() throws Exception { - final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor(); - assertTrue(Modifier.isProtected(constructor.getModifiers())); - } - private void updateDistributionStatistics() { - PapStatisticsManager mgr = PapStatisticsManager.getInstance(); + PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class); mgr.updateTotalPdpCount(); mgr.updateTotalPdpGroupCount(); diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java index 37da7e9b..650e8243 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestMain.java @@ -22,10 +22,14 @@ package org.onap.policy.pap.main.startstop; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.junit.After; +import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.parameters.CommonTestData; @@ -38,15 +42,23 @@ public class TestMain { private Main main; /** + * Set up. + */ + @Before + public void setUp() { + Registry.newRegistry(); + } + + /** * Shuts "main" down. * @throws Exception if an error occurs */ @After public void tearDown() throws Exception { // shut down activator - PapActivator activator = PapActivator.getCurrent(); + PapActivator activator = Registry.getOrDefault(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class, null); if (activator != null && activator.isAlive()) { - activator.terminate(); + activator.stop(); } } @@ -58,6 +70,9 @@ public class TestMain { assertTrue(main.getParameters().isValid()); assertEquals(CommonTestData.PAP_GROUP_NAME, main.getParameters().getName()); + // ensure items were added to the registry + assertNotNull(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class)); + main.shutdown(); } 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 059ea0b4..cfa2ae92 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 @@ -24,7 +24,7 @@ package org.onap.policy.pap.main.startstop; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.FileInputStream; @@ -32,10 +32,13 @@ import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.parameters.CommonTestData; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; +import org.onap.policy.pap.main.rest.PapStatisticsManager; /** @@ -54,13 +57,15 @@ public class TestPapActivator { */ @Before public void setUp() throws Exception { + Registry.newRegistry(); + final String[] papConfigParameters = {"-c", "parameters/PapConfigParameters.json", "-p", "parameters/topic.properties"}; final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters); final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments); Properties props = new Properties(); - String propFile = arguments.getFullConfigurationFilePath(); + String propFile = arguments.getFullPropertyFilePath(); try (FileInputStream stream = new FileInputStream(propFile)) { props.load(stream); } @@ -75,37 +80,36 @@ public class TestPapActivator { @After public void teardown() throws Exception { if (activator != null && activator.isAlive()) { - activator.terminate(); + activator.stop(); } } @Test public void testPapActivator() throws PolicyPapException { assertFalse(activator.isAlive()); - activator.initialize(); + activator.start(); assertTrue(activator.isAlive()); assertTrue(activator.getParameterGroup().isValid()); assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName()); + // ensure items were added to the registry + assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class)); + assertNotNull(Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class)); + // repeat - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.initialize()); + assertThatIllegalStateException().isThrownBy(() -> activator.start()); assertTrue(activator.isAlive()); assertTrue(activator.getParameterGroup().isValid()); } @Test - public void testGetCurrent_testSetCurrent() { - assertSame(activator, PapActivator.getCurrent()); - } - - @Test public void testTerminate() throws Exception { - activator.initialize(); - activator.terminate(); + activator.start(); + activator.stop(); assertFalse(activator.isAlive()); // repeat - should throw an exception - assertThatIllegalStateException().isThrownBy(() -> activator.terminate()); + assertThatIllegalStateException().isThrownBy(() -> activator.stop()); assertFalse(activator.isAlive()); } } |