From f3d671360961af57fa8921ecfebcdf6f1fa17243 Mon Sep 17 00:00:00 2001 From: "ning.xi" Date: Thu, 26 Dec 2019 21:28:20 +0800 Subject: statistics rest api update Issue-ID: POLICY-1629 Signed-off-by: ning.xi Change-Id: I104fc448d27a282c01e7f54db300f918374b8d07 --- .../main/rest/TestStatisticsRestControllerV1.java | 7 +- .../policy/pap/main/rest/e2e/StatisticsTest.java | 88 ++++++++++++++++++++++ main/src/test/resources/META-INF/persistence.xml | 1 + 3 files changed, 95 insertions(+), 1 deletion(-) (limited to 'main/src/test') 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 1894fd7b..70deedc1 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -36,10 +36,15 @@ import org.onap.policy.pap.main.PapConstants; public class TestStatisticsRestControllerV1 extends CommonPapRestServer { private static final String STATISTICS_ENDPOINT = "statistics"; + private static final String STATISTICS_DB_ENDPOINT = "pdps/statistics"; @Test public void testSwagger() throws Exception { super.testSwagger(STATISTICS_ENDPOINT); + super.testSwagger(STATISTICS_DB_ENDPOINT); + super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}"); + super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}" + "/{type}"); + super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}" + "/{type}" + "/{pdp}"); } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/StatisticsTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/StatisticsTest.java index 13492cf9..aa2c59ea 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/StatisticsTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/StatisticsTest.java @@ -3,6 +3,7 @@ * ONAP PAP * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,15 +24,27 @@ package org.onap.policy.pap.main.rest.e2e; import static org.junit.Assert.assertEquals; import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; import org.junit.Test; import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pdp.concepts.PdpStatistics; +import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.pap.main.PapConstants; +import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; import org.onap.policy.pap.main.rest.PapStatisticsManager; import org.onap.policy.pap.main.rest.StatisticsReport; public class StatisticsTest extends End2EndBase { private static final String STATISTICS_ENDPOINT = "statistics"; + private List recordList = new ArrayList<>(); @Test @@ -56,6 +69,47 @@ public class StatisticsTest extends End2EndBase { assertEquals(report.getPolicyDeployFailureCount() + 1, report2.getPolicyDeployFailureCount()); } + @Test + public void testDb() throws Exception { + setupEnv(); + Invocation.Builder invocationBuilder = sendRequest("pdps/statistics"); + Response testResponse = invocationBuilder.get(); + verifyResponse(testResponse); + } + + @Test + public void testDbWithGroup() throws Exception { + setupEnv(); + Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup"); + Response testResponse = invocationBuilder.get(); + verifyResponse(testResponse); + + } + + @Test + public void testDbWithSubGroup() throws Exception { + setupEnv(); + Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup/apex"); + Response testResponse = invocationBuilder.get(); + verifyResponse(testResponse); + } + + @Test + public void testDbWithPdp() throws Exception { + setupEnv(); + Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup/apex/pdp1"); + Response testResponse = invocationBuilder.get(); + verifyResponse(testResponse); + } + + @Test + public void testDbWithPdpLatest() throws Exception { + setupEnv(); + Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup/apex/pdp1?recordCount=5"); + Response testResponse = invocationBuilder.get(); + verifyResponse(testResponse); + } + private void updateDistributionStatistics() { PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class); @@ -68,4 +122,38 @@ public class StatisticsTest extends End2EndBase { mgr.updatePolicyDownloadSuccessCount(); mgr.updatePolicyDownloadFailureCount(); } + + private void setupEnv() { + PolicyModelsProviderFactoryWrapper modelProviderWrapper = + Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class); + + try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) { + PdpStatistics pdpStatisticsRecord = new PdpStatistics(); + pdpStatisticsRecord.setPdpGroupName("defaultGroup"); + pdpStatisticsRecord.setPdpSubGroupName("apex"); + pdpStatisticsRecord.setPdpInstanceId("pdp1"); + pdpStatisticsRecord.setTimeStamp(new Date()); + pdpStatisticsRecord.setPolicyDeployCount(1); + pdpStatisticsRecord.setPolicyDeployFailCount(0); + pdpStatisticsRecord.setPolicyDeploySuccessCount(1); + pdpStatisticsRecord.setPolicyExecutedCount(1); + pdpStatisticsRecord.setPolicyExecutedFailCount(0); + pdpStatisticsRecord.setPolicyExecutedSuccessCount(1); + recordList.add(pdpStatisticsRecord); + databaseProvider.createPdpStatistics(recordList); + } catch (final PfModelException exp) { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, exp.getMessage()); + } + } + + private void verifyResponse(Response testResponse) { + assertEquals(Response.Status.OK.getStatusCode(), testResponse.getStatus()); + Map>> map = + testResponse.readEntity(new GenericType>>>() {}); + Map> subMap = map.get("defaultGroup"); + List resRecord = subMap.get("apex"); + assertEquals("pdp1", resRecord.get(0).getPdpInstanceId()); + assertEquals("apex", resRecord.get(0).getPdpSubGroupName()); + assertEquals("defaultGroup", resRecord.get(0).getPdpGroupName()); + } } diff --git a/main/src/test/resources/META-INF/persistence.xml b/main/src/test/resources/META-INF/persistence.xml index 40b62197..ed09e78a 100644 --- a/main/src/test/resources/META-INF/persistence.xml +++ b/main/src/test/resources/META-INF/persistence.xml @@ -32,6 +32,7 @@ org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup org.onap.policy.models.pdp.persistence.concepts.JpaPdp + org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics -- cgit 1.2.3-korg