From a03bfe0ac168a61a8c34329b2bfb73e360b812e1 Mon Sep 17 00:00:00 2001 From: ramverma Date: Thu, 13 Sep 2018 23:05:39 +0100 Subject: Adding statistics endpoint to distribution * Adding statistics endpoint in DistributionRestController * Adding DistributionStatisticsManager to hold all stats. * Adding StatisticsReport class to represent distribution stats report. * Updating stats at each step in SdcReceptionHandler. * Adding test cases for all code changes. Change-Id: I0b0f97aecc64b18314367fdff6d3ca7bee06efc7 Issue-ID: POLICY-1035 Signed-off-by: ramverma --- .../main/rest/TestDistributionStatistics.java | 129 +++++++++++++++++++++ .../main/rest/TestStatisticsReport.java | 47 ++++++++ 2 files changed, 176 insertions(+) create mode 100644 main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java create mode 100644 main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java (limited to 'main/src/test/java') diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java new file mode 100644 index 00000000..92ae14a8 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.rest; + +import static org.junit.Assert.assertEquals; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.Test; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.CommonTestData; +import org.onap.policy.distribution.main.parameters.RestServerParameters; +import org.onap.policy.distribution.main.startstop.Main; +import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager; + +/** + * Class to perform unit test of {@link DistributionRestController}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestDistributionStatistics { + + private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionStatistics.class); + + + @Test + public void testDistributionStatistics_200() throws PolicyDistributionException, InterruptedException { + final Main main = startDistributionService(); + StatisticsReport report = getDistributionStatistics(); + + validateReport(report, 0, 200); + updateDistributionStatistics(); + report = getDistributionStatistics(); + validateReport(report, 1, 200); + stopDistributionService(main); + DistributionStatisticsManager.resetAllStatistics(); + } + + @Test + public void testDistributionStatistics_500() throws InterruptedException { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); + + final DistributionRestServer restServer = new DistributionRestServer(restServerParams); + restServer.start(); + final StatisticsReport report = getDistributionStatistics(); + + validateReport(report, 0, 500); + restServer.shutdown(); + DistributionStatisticsManager.resetAllStatistics(); + } + + + private Main startDistributionService() { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters.json" }; + return new Main(distributionConfigParameters); + } + + private void stopDistributionService(final Main main) throws PolicyDistributionException { + main.shutdown(); + } + + private StatisticsReport getDistributionStatistics() throws InterruptedException { + StatisticsReport response = null; + final ClientConfig clientConfig = new ClientConfig(); + + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + clientConfig.register(feature); + + final Client client = ClientBuilder.newClient(clientConfig); + final WebTarget webTarget = client.target("http://localhost:6969/statistics"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + while (response == null) { + try { + response = invocationBuilder.get(StatisticsReport.class); + } catch (final Exception exp) { + LOGGER.info("the server is not started yet. We will retry again"); + } + } + return response; + } + + private void updateDistributionStatistics() { + DistributionStatisticsManager.updateTotalDistributionCount(); + DistributionStatisticsManager.updateDistributionSuccessCount(); + DistributionStatisticsManager.updateDistributionFailureCount(); + DistributionStatisticsManager.updateTotalDownloadCount(); + DistributionStatisticsManager.updateDownloadSuccessCount(); + DistributionStatisticsManager.updateDownloadFailureCount(); + } + + private void validateReport(final StatisticsReport report, final int count, final int code) { + assertEquals(code, report.getCode()); + assertEquals(count, report.getTotalDistributionCount()); + assertEquals(count, report.getDistributionSuccessCount()); + assertEquals(count, report.getDistributionFailureCount()); + assertEquals(count, report.getTotalDownloadCount()); + assertEquals(count, report.getDownloadSuccessCount()); + assertEquals(count, report.getDownloadFailureCount()); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java new file mode 100644 index 00000000..64d2e5c5 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.rest; + +import com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; + +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit testing of {@link StatisticsReport}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestStatisticsReport { + + @Test + public void testStatisticsReport() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterMustExistRule()) + .with(new SetterTester()).with(new GetterTester()).build(); + validator.validate(StatisticsReport.class.getPackage().getName(), + new FilterClassName(StatisticsReport.class.getName())); + } +} -- cgit 1.2.3-korg