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 | |
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')
30 files changed, 784 insertions, 194 deletions
diff --git a/main/pom.xml b/main/pom.xml index 01c41545..2e221cef 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -45,11 +45,6 @@ </dependency> <dependency> <groupId>org.onap.policy.common</groupId> - <artifactId>pdp-common</artifactId> - <version>${policy.common.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.common</groupId> <artifactId>utils</artifactId> <version>${policy.common.version}</version> </dependency> @@ -65,7 +60,17 @@ </dependency> <dependency> <groupId>org.onap.policy.models</groupId> - <artifactId>models-pap</artifactId> + <artifactId>policy-models-pap</artifactId> + <version>${policy.models.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>models-pdp</artifactId> + <version>${policy.models.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-provider</artifactId> <version>${policy.models.version}</version> </dependency> <dependency> diff --git a/main/src/main/java/org/onap/policy/pap/main/PapConstants.java b/main/src/main/java/org/onap/policy/pap/main/PapConstants.java new file mode 100644 index 00000000..64401b81 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/PapConstants.java @@ -0,0 +1,41 @@ +/* + * ============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; + +/** + * Names of various items contained in the Registry. + */ +public class PapConstants { + + // Registry keys + public static final String REG_PAP_ACTIVATOR = "object:activator/pap"; + public static final String REG_STATISTICS_MANAGER = "object:manager/statistics"; + public static final String REG_PDP_MODIFY_LOCK = "lock:pdp"; + public static final String REG_PDP_MODIFY_MAP = "object:pdp/modify/map"; + public static final String REG_PAP_DAO_FACTORY = "object:pap/dao/factory"; + + // topic names + public static final String TOPIC_POLICY_PDP_PAP = "POLICY-PDP-PAP"; + + private PapConstants() { + super(); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/MultiPdpStatusListener.java b/main/src/main/java/org/onap/policy/pap/main/comm/MultiPdpStatusListener.java index 652756a4..f3cac7e3 100644 --- a/main/src/main/java/org/onap/policy/pap/main/comm/MultiPdpStatusListener.java +++ b/main/src/main/java/org/onap/policy/pap/main/comm/MultiPdpStatusListener.java @@ -29,7 +29,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.listeners.TypedMessageListener; -import org.onap.policy.pdp.common.models.PdpStatus; +import org.onap.policy.models.pdp.concepts.PdpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java index dce21f98..21dcac0a 100644 --- a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java @@ -36,6 +36,7 @@ import org.onap.policy.common.parameters.annotations.NotNull; @Getter public class PapParameterGroup extends ParameterGroupImpl { private RestServerParameters restServerParameters; + private PdpParameters pdpParameters; /** * Create the pap parameter group. diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PdpParameters.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpParameters.java new file mode 100644 index 00000000..84fe353b --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpParameters.java @@ -0,0 +1,45 @@ +/* + * ============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 lombok.Getter; +import org.onap.policy.common.parameters.ParameterGroupImpl; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Parameters for communicating with PDPs. + */ +@NotNull +@NotBlank +@Getter +public class PdpParameters extends ParameterGroupImpl { + private PdpUpdateParameters updateParameters; + private PdpStateChangeParameters stateChangeParameters; + + + /** + * Constructs the object. + */ + public PdpParameters() { + super(PdpParameters.class.getSimpleName()); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PdpRequestParameters.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpRequestParameters.java new file mode 100644 index 00000000..2df20fb3 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpRequestParameters.java @@ -0,0 +1,60 @@ +/* + * ============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 lombok.Getter; +import org.onap.policy.common.parameters.ParameterGroupImpl; +import org.onap.policy.common.parameters.annotations.Min; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Parameters for communicating with PDPs. + */ +@NotNull +@NotBlank +@Getter +public class PdpRequestParameters extends ParameterGroupImpl { + + // NOTE: these fields must not be "private" or the validator will skip them + + /** + * Maximum number of times to re-send a request to a PDP. + */ + @Min(value = 0) + protected int maxRetryCount; + + /** + * Maximum time to wait, in milliseconds, for a PDP response. + */ + @Min(value = 0) + protected long maxWaitMs; + + + /** + * Constructs the object. + * + * @param name name of this set of parameters + */ + public PdpRequestParameters(String name) { + super(name); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PdpStateChangeParameters.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpStateChangeParameters.java new file mode 100644 index 00000000..9a464864 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpStateChangeParameters.java @@ -0,0 +1,41 @@ +/* + * ============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 lombok.Getter; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Parameters for PDP STATE-CHANGE requests. + */ +@NotNull +@NotBlank +@Getter +public class PdpStateChangeParameters extends PdpRequestParameters { + + /** + * Constructs the object. + */ + public PdpStateChangeParameters() { + super(PdpStateChangeParameters.class.getSimpleName()); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PdpUpdateParameters.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpUpdateParameters.java new file mode 100644 index 00000000..c8e90bdd --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PdpUpdateParameters.java @@ -0,0 +1,41 @@ +/* + * ============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 lombok.Getter; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Parameters for PDP UPDATE requests. + */ +@NotNull +@NotBlank +@Getter +public class PdpUpdateParameters extends PdpRequestParameters { + + /** + * Constructs the object. + */ + public PdpUpdateParameters() { + super(PdpUpdateParameters.class.getSimpleName()); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java index 58dccc86..b585b9d2 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java @@ -22,6 +22,8 @@ package org.onap.policy.pap.main.rest; import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.startstop.PapActivator; /** @@ -46,7 +48,7 @@ public class HealthCheckProvider { report.setName(NAME); report.setUrl(URL); - boolean alive = PapActivator.getCurrent().isAlive(); + boolean alive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive(); report.setHealthy(alive); report.setCode(alive ? 200 : 500); diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java index 32893668..daa1a9c4 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapStatisticsManager.java @@ -22,7 +22,6 @@ package org.onap.policy.pap.main.rest; import java.util.concurrent.atomic.AtomicLong; -import lombok.Getter; /** * Class to hold statistical data for pap component. @@ -31,9 +30,6 @@ import lombok.Getter; */ public class PapStatisticsManager { - @Getter - private static final PapStatisticsManager instance = new PapStatisticsManager(); - private final AtomicLong totalPdpCount = new AtomicLong(0); private final AtomicLong totalPdpGroupCount = new AtomicLong(0); private final AtomicLong totalPolicyDeployCount = new AtomicLong(0); @@ -46,7 +42,7 @@ public class PapStatisticsManager { /** * Constructs the object. */ - protected PapStatisticsManager() { + public PapStatisticsManager() { super(); } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java index f3eb1dea..72e1fe58 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java @@ -28,9 +28,7 @@ import io.swagger.annotations.Authorization; import io.swagger.annotations.Extension; import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.ResponseHeader; - import java.util.UUID; - import javax.ws.rs.HeaderParam; import javax.ws.rs.PUT; import javax.ws.rs.Path; @@ -38,10 +36,9 @@ import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; - import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse; -import org.onap.policy.pdp.common.enums.PdpState; +import org.onap.policy.models.pdp.enums.PdpState; /** * Class to provide REST end points for PAP component to change state of a PDP group. diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java index 51441e46..49f55e2d 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java @@ -21,10 +21,9 @@ package org.onap.policy.pap.main.rest; import javax.ws.rs.core.Response; - import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse; -import org.onap.policy.pdp.common.enums.PdpState; +import org.onap.policy.models.pdp.enums.PdpState; /** * Provider for PAP component to change state of PDP group. diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java index 026ccf49..8e6410a8 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/StatisticsProvider.java @@ -21,6 +21,8 @@ package org.onap.policy.pap.main.rest; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.startstop.PapActivator; /** @@ -37,9 +39,9 @@ public class StatisticsProvider { */ public StatisticsReport fetchCurrentStatistics() { final StatisticsReport report = new StatisticsReport(); - report.setCode(PapActivator.getCurrent().isAlive() ? 200 : 500); + report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500); - PapStatisticsManager mgr = PapStatisticsManager.getInstance(); + PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class); report.setTotalPdpCount(mgr.getTotalPdpCount()); report.setTotalPdpGroupCount(mgr.getTotalPdpGroupCount()); report.setTotalPolicyDownloadCount(mgr.getTotalPolicyDownloadCount()); diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java index 223e322e..d58be4ec 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java @@ -24,6 +24,8 @@ package org.onap.policy.pap.main.startstop; import java.io.FileInputStream; import java.util.Arrays; import java.util.Properties; +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.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; @@ -89,12 +91,14 @@ public class Main { // Now, create the activator for the policy pap service activator = new PapActivator(parameterGroup, props); + Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator); // Start the activator try { - activator.initialize(); - } catch (final PolicyPapException e) { + activator.start(); + } catch (final RuntimeException e) { LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e); + Registry.unregister(PapConstants.REG_PAP_ACTIVATOR); return; } @@ -123,7 +127,7 @@ public class Main { // clear the pap activator if (activator != null) { - activator.terminate(); + activator.stop(); } } @@ -140,8 +144,8 @@ public class Main { public void run() { try { // Shutdown the policy pap service and wait for everything to stop - activator.terminate(); - } catch (final PolicyPapException e) { + activator.stop(); + } catch (final RuntimeException e) { LOGGER.warn("error occured during shut down of the policy pap service", e); } } diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java index 3334d81a..913e661d 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java @@ -21,18 +21,22 @@ package org.onap.policy.pap.main.startstop; +import java.util.Arrays; import java.util.Properties; -import lombok.Getter; -import lombok.Setter; import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; +import org.onap.policy.common.endpoints.event.comm.TopicSource; +import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher; +import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher; import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.common.utils.services.ServiceManager; -import org.onap.policy.common.utils.services.ServiceManagerException; -import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.common.utils.services.ServiceManagerContainer; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.enums.PdpMessageType; +import org.onap.policy.pap.main.PapConstants; +import org.onap.policy.pap.main.PolicyPapRuntimeException; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.rest.PapRestServer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.onap.policy.pap.main.rest.PapStatisticsManager; /** * This class wraps a distributor so that it can be activated as a complete service @@ -40,28 +44,28 @@ import org.slf4j.LoggerFactory; * * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ -public class PapActivator { - - private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class); +public class PapActivator extends ServiceManagerContainer { + private static final String[] MSG_TYPE_NAMES = {"messageName"}; + private static final String[] REQ_ID_NAMES = {"response", "responseTo"}; private final PapParameterGroup papParameterGroup; /** - * The current activator. + * The PAP REST API server. */ - @Getter - private static volatile PapActivator current = null; + private PapRestServer restServer; /** - * Used to stop the services. + * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, + * and then dispatches them to {@link #reqIdDispatcher}. */ - private final ServiceManager manager; - - @Getter - @Setter(lombok.AccessLevel.PRIVATE) - private volatile boolean alive = false; + private final MessageTypeDispatcher msgDispatcher; - private PapRestServer restServer; + /** + * Listens for {@link PdpStatus} messages and then routes them to the listener + * associated with the ID of the originating request. + */ + private final RequestIdDispatcher<PdpStatus> reqIdDispatcher; /** * Instantiate the activator for policy pap as a complete service. @@ -70,66 +74,55 @@ public class PapActivator { * @param topicProperties properties used to configure the topics */ public PapActivator(final PapParameterGroup papParameterGroup, Properties topicProperties) { + super("Policy PAP"); + TopicEndpoint.manager.addTopicSinks(topicProperties); TopicEndpoint.manager.addTopicSources(topicProperties); - this.papParameterGroup = papParameterGroup; + try { + this.papParameterGroup = papParameterGroup; + papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName()); + + this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES); + this.reqIdDispatcher = new RequestIdDispatcher<>(PdpStatus.class, REQ_ID_NAMES); + + } catch (RuntimeException e) { + throw new PolicyPapRuntimeException(e); + } + + this.msgDispatcher.register(PdpMessageType.PDP_STATUS.name(), this.reqIdDispatcher); + + final Object pdpUpdateLock = new Object(); // @formatter:off - this.manager = new ServiceManager() - .addAction("topics", - () -> TopicEndpoint.manager.start(), - () -> TopicEndpoint.manager.shutdown()) - .addAction("register parameters", - () -> registerToParameterService(papParameterGroup), - () -> deregisterToParameterService(papParameterGroup)) - .addAction("REST server", - () -> startPapRestServer(), - () -> restServer.stop()) - .addAction("set alive", - () -> setAlive(true), - () -> setAlive(false)); - // @formatter:on + addAction("PAP parameters", + () -> ParameterService.register(papParameterGroup), + () -> ParameterService.deregister(papParameterGroup.getName())); - current = this; - } + addAction("dispatcher", + () -> registerDispatcher(), + () -> unregisterDispatcher()); - /** - * Initialize pap as a complete service. - * - * @throws PolicyPapException on errors in initializing the service - */ - public void initialize() throws PolicyPapException { - if (isAlive()) { - throw new IllegalStateException("activator already initialized"); - } + addAction("topics", + () -> TopicEndpoint.manager.start(), + () -> TopicEndpoint.manager.shutdown()); - try { - LOGGER.debug("Policy pap starting as a service . . ."); - manager.start(); - LOGGER.debug("Policy pap started as a service"); - } catch (final ServiceManagerException exp) { - LOGGER.error("Policy pap service startup failed"); - throw new PolicyPapException(exp.getMessage(), exp); - } - } + addAction("PAP statistics", + () -> Registry.register(PapConstants.REG_STATISTICS_MANAGER, new PapStatisticsManager()), + () -> Registry.unregister(PapConstants.REG_STATISTICS_MANAGER)); - /** - * Terminate policy pap. - * - * @throws PolicyPapException on errors in terminating the service - */ - public void terminate() throws PolicyPapException { - if (!isAlive()) { - throw new IllegalStateException("activator is not running"); - } + addAction("PDP modification lock", + () -> Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, pdpUpdateLock), + () -> Registry.unregister(PapConstants.REG_PDP_MODIFY_LOCK)); - try { - manager.stop(); - } catch (final ServiceManagerException exp) { - LOGGER.error("Policy pap service termination failed"); - throw new PolicyPapException(exp.getMessage(), exp); - } + addAction("REST server", + () -> restServer = new PapRestServer(papParameterGroup.getRestServerParameters()), + () -> { }); + + addAction("REST server thread", + () -> restServer.start(), + () -> restServer.stop()); + // @formatter:on } /** @@ -142,33 +135,22 @@ public class PapActivator { } /** - * Method to register the parameters to Common Parameter Service. - * - * @param papParameterGroup the pap parameter group + * Registers the dispatcher with the topic source(s). */ - public void registerToParameterService(final PapParameterGroup papParameterGroup) { - ParameterService.register(papParameterGroup); - } - - /** - * Method to deregister the parameters from Common Parameter Service. - * - * @param papParameterGroup the pap parameter group - */ - public void deregisterToParameterService(final PapParameterGroup papParameterGroup) { - ParameterService.deregister(papParameterGroup.getName()); + private void registerDispatcher() { + for (TopicSource source : TopicEndpoint.manager + .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) { + source.register(msgDispatcher); + } } /** - * Starts the pap rest server using configuration parameters. - * - * @throws PolicyPapException if server start fails + * Unregisters the dispatcher from the topic source(s). */ - private void startPapRestServer() throws PolicyPapException { - papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName()); - restServer = new PapRestServer(papParameterGroup.getRestServerParameters()); - if (!restServer.start()) { - throw new PolicyPapException("Failed to start pap rest server. Check log for more details..."); + private void unregisterDispatcher() { + for (TopicSource source : TopicEndpoint.manager + .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) { + source.unregister(msgDispatcher); } } } 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()); } } diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json index 34a8f802..b35acec5 100644 --- a/main/src/test/resources/parameters/MinimumParameters.json +++ b/main/src/test/resources/parameters/MinimumParameters.json @@ -5,5 +5,15 @@ "port":6969, "userName":"healthcheck", "password":"zb!XztG34" + }, + "pdpParameters": { + "updateParameters": { + "maxRetryCount": 1, + "maxWaitMs": 1 + }, + "stateChangeParameters": { + "maxRetryCount": 1, + "maxWaitMs": 1 + } } } diff --git a/main/src/test/resources/parameters/PapConfigParameters.json b/main/src/test/resources/parameters/PapConfigParameters.json index 08d4f4a5..a510964f 100644 --- a/main/src/test/resources/parameters/PapConfigParameters.json +++ b/main/src/test/resources/parameters/PapConfigParameters.json @@ -1,10 +1,20 @@ { - "name":"PapGroup", - "restServerParameters":{ - "host":"0.0.0.0", - "port":6969, - "userName":"healthcheck", - "password":"zb!XztG34", - "https":true + "name": "PapGroup", + "restServerParameters": { + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true + }, + "pdpParameters": { + "updateParameters": { + "maxRetryCount": 1, + "maxWaitMs": 1 + }, + "stateChangeParameters": { + "maxRetryCount": 1, + "maxWaitMs": 1 + } } } diff --git a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json index 80fb8232..7b53b880 100644 --- a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json +++ b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json @@ -5,5 +5,15 @@ "port":6969, "userName":"healthcheck", "password":"zb!XztG34" + }, + "pdpParameters": { + "updateParameters": { + "maxRetryCount": 1, + "maxWaitMs": 1 + }, + "stateChangeParameters": { + "maxRetryCount": 1, + "maxWaitMs": 1 + } } } |