diff options
author | Jim Hahn <jrh3@att.com> | 2020-09-30 10:47:12 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-09-30 10:59:45 -0400 |
commit | 61dafe1cb86e4f82b2062d56bbc343df19c31c5f (patch) | |
tree | 2caa753a7298175f95711a870f56b6befdee1481 /main | |
parent | 71555aaa4387098616a404bf193e7bb6936e3f76 (diff) |
Fix new sonars in policy/pap
Addressed the following sonar issues:
- too many assertions in test method
- extract common test method
- replace this null literal by an "Optional" object
Issue-ID: POLICY-2679
Change-Id: Ie80e5c5fdb94230a2773ea2931758cf54204ae86
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main')
3 files changed, 67 insertions, 64 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java index 674a07fd..d7ee15bd 100644 --- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java +++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java @@ -158,18 +158,15 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator { private void handlePdpHeartbeat(final PdpStatus message, final PolicyModelsProvider databaseProvider) throws PfModelException { - Optional<PdpSubGroup> pdpSubgroup = null; - Optional<Pdp> pdpInstance = null; - PdpGroup pdpGroup = null; final PdpGroupFilter filter = PdpGroupFilter.builder().name(message.getPdpGroup()).groupState(PdpState.ACTIVE).build(); final List<PdpGroup> pdpGroups = databaseProvider.getFilteredPdpGroups(filter); if (!pdpGroups.isEmpty()) { - pdpGroup = pdpGroups.get(0); - pdpSubgroup = findPdpSubGroup(message, pdpGroup); + PdpGroup pdpGroup = pdpGroups.get(0); + Optional<PdpSubGroup> pdpSubgroup = findPdpSubGroup(message, pdpGroup); if (pdpSubgroup.isPresent()) { - pdpInstance = findPdpInstance(message, pdpSubgroup.get()); + Optional<Pdp> pdpInstance = findPdpInstance(message, pdpSubgroup.get()); if (pdpInstance.isPresent()) { processPdpDetails(message, pdpSubgroup.get(), pdpInstance.get(), pdpGroup, databaseProvider); } else { 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 b600711f..5412ac94 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -22,59 +22,74 @@ package org.onap.policy.pap.main.rest; import static org.junit.Assert.assertEquals; +import java.util.function.ToLongFunction; import org.junit.Test; public class TestPapStatisticsManager { @Test - public void test() { - PapStatisticsManager mgr = new PapStatisticsManager(); + public void testTotalPdpCount() { + verifyCount(PapStatisticsManager::getTotalPdpCount, + PapStatisticsManager::updateTotalPdpCount); + } - // try each update + @Test + public void testTotalPdpGroupCount() { + verifyCount(PapStatisticsManager::getTotalPdpGroupCount, + PapStatisticsManager::updateTotalPdpGroupCount); + } - assertEquals(0, mgr.getTotalPdpCount()); - assertEquals(1, mgr.updateTotalPdpCount()); - assertEquals(1, mgr.getTotalPdpCount()); + @Test + public void testTotalPolicyDeployCount() { + verifyCount(PapStatisticsManager::getTotalPolicyDeployCount, + PapStatisticsManager::updateTotalPolicyDeployCount); + } - assertEquals(0, mgr.getTotalPdpGroupCount()); - assertEquals(1, mgr.updateTotalPdpGroupCount()); - assertEquals(1, mgr.getTotalPdpGroupCount()); + @Test + public void testPolicyDeploySuccessCount() { + verifyCount(PapStatisticsManager::getPolicyDeploySuccessCount, + PapStatisticsManager::updatePolicyDeploySuccessCount); + } - assertEquals(0, mgr.getTotalPolicyDeployCount()); - assertEquals(1, mgr.updateTotalPolicyDeployCount()); - assertEquals(1, mgr.getTotalPolicyDeployCount()); + @Test + public void testPolicyDeployFailureCount() { + verifyCount(PapStatisticsManager::getPolicyDeployFailureCount, + PapStatisticsManager::updatePolicyDeployFailureCount); + } - assertEquals(0, mgr.getPolicyDeploySuccessCount()); - assertEquals(1, mgr.updatePolicyDeploySuccessCount()); - assertEquals(1, mgr.getPolicyDeploySuccessCount()); + @Test + public void testTotalPolicyDownloadCount() { + verifyCount(PapStatisticsManager::getTotalPolicyDownloadCount, + PapStatisticsManager::updateTotalPolicyDownloadCount); + } - assertEquals(0, mgr.getPolicyDeployFailureCount()); - assertEquals(1, mgr.updatePolicyDeployFailureCount()); - assertEquals(1, mgr.getPolicyDeployFailureCount()); + @Test + public void testPolicyDownloadSuccessCount() { + verifyCount(PapStatisticsManager::getPolicyDownloadSuccessCount, + PapStatisticsManager::updatePolicyDownloadSuccessCount); + } - assertEquals(0, mgr.getTotalPolicyDownloadCount()); - assertEquals(1, mgr.updateTotalPolicyDownloadCount()); - assertEquals(1, mgr.getTotalPolicyDownloadCount()); + @Test + public void testPolicyDownloadFailureCount() { + verifyCount(PapStatisticsManager::getPolicyDownloadFailureCount, + PapStatisticsManager::updatePolicyDownloadFailureCount); + } + + private void verifyCount(ToLongFunction<PapStatisticsManager> getCount, + ToLongFunction<PapStatisticsManager> updateCount) { + + PapStatisticsManager mgr = new PapStatisticsManager(); - assertEquals(0, mgr.getPolicyDownloadSuccessCount()); - assertEquals(1, mgr.updatePolicyDownloadSuccessCount()); - assertEquals(1, mgr.getPolicyDownloadSuccessCount()); + assertEquals(0, getCount.applyAsLong(mgr)); + assertEquals(1, updateCount.applyAsLong(mgr)); + assertEquals(1, getCount.applyAsLong(mgr)); - assertEquals(0, mgr.getPolicyDownloadFailureCount()); - assertEquals(1, mgr.updatePolicyDownloadFailureCount()); - assertEquals(1, mgr.getPolicyDownloadFailureCount()); + assertEquals(2, updateCount.applyAsLong(mgr)); + assertEquals(2, getCount.applyAsLong(mgr)); // now check reset mgr.resetAllStatistics(); - assertEquals(0, mgr.getPolicyDeployFailureCount()); - assertEquals(0, mgr.getTotalPdpCount()); - assertEquals(0, mgr.getTotalPdpGroupCount()); - assertEquals(0, mgr.getTotalPolicyDeployCount()); - assertEquals(0, mgr.getPolicyDeploySuccessCount()); - assertEquals(0, mgr.getPolicyDeployFailureCount()); - assertEquals(0, mgr.getTotalPolicyDownloadCount()); - assertEquals(0, mgr.getPolicyDownloadSuccessCount()); - assertEquals(0, mgr.getPolicyDownloadFailureCount()); + assertEquals(0, getCount.applyAsLong(mgr)); } } 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 aa2c59ea..75aafba6 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -71,43 +71,28 @@ public class StatisticsTest extends End2EndBase { @Test public void testDb() throws Exception { - setupEnv(); - Invocation.Builder invocationBuilder = sendRequest("pdps/statistics"); - Response testResponse = invocationBuilder.get(); - verifyResponse(testResponse); + verifyResponse("pdps/statistics"); } @Test public void testDbWithGroup() throws Exception { - setupEnv(); - Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup"); - Response testResponse = invocationBuilder.get(); - verifyResponse(testResponse); + verifyResponse("pdps/statistics/defaultGroup"); } @Test public void testDbWithSubGroup() throws Exception { - setupEnv(); - Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup/apex"); - Response testResponse = invocationBuilder.get(); - verifyResponse(testResponse); + verifyResponse("pdps/statistics/defaultGroup/apex"); } @Test public void testDbWithPdp() throws Exception { - setupEnv(); - Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup/apex/pdp1"); - Response testResponse = invocationBuilder.get(); - verifyResponse(testResponse); + verifyResponse("pdps/statistics/defaultGroup/apex/pdp1"); } @Test public void testDbWithPdpLatest() throws Exception { - setupEnv(); - Invocation.Builder invocationBuilder = sendRequest("pdps/statistics/defaultGroup/apex/pdp1?recordCount=5"); - Response testResponse = invocationBuilder.get(); - verifyResponse(testResponse); + verifyResponse("pdps/statistics/defaultGroup/apex/pdp1?recordCount=5"); } private void updateDistributionStatistics() { @@ -146,6 +131,12 @@ public class StatisticsTest extends End2EndBase { } } + private void verifyResponse(String endpoint) throws Exception { + setupEnv(); + Invocation.Builder invocationBuilder = sendRequest(endpoint); + verifyResponse(invocationBuilder.get()); + } + private void verifyResponse(Response testResponse) { assertEquals(Response.Status.OK.getStatusCode(), testResponse.getStatus()); Map<String, Map<String, List<PdpStatistics>>> map = |