From ce7b217261784bb4cd2edd44d482ce6a705c9232 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 15 Feb 2022 12:45:29 +0000 Subject: Fix issue with GeneratedValue in PfGeneratedIdKey Change-Id: If244237e4aa39e1f3184cab04ae7d5ad7e85e5fa Issue-ID: POLICY-3897 Signed-off-by: a.sreekumar --- .../main/repository/PdpStatisticsRepository.java | 19 ++++++------ .../pap/main/repository/PolicyAuditRepository.java | 19 ++++++------ .../pap/main/service/PdpStatisticsService.java | 21 ++++--------- .../pap/main/service/PolicyAuditService.java | 36 +++++++++------------- .../pap/main/service/PdpStatisticsServiceTest.java | 5 +-- 5 files changed, 39 insertions(+), 61 deletions(-) diff --git a/main/src/main/java/org/onap/policy/pap/main/repository/PdpStatisticsRepository.java b/main/src/main/java/org/onap/policy/pap/main/repository/PdpStatisticsRepository.java index c93fea60..f2dfeebd 100644 --- a/main/src/main/java/org/onap/policy/pap/main/repository/PdpStatisticsRepository.java +++ b/main/src/main/java/org/onap/policy/pap/main/repository/PdpStatisticsRepository.java @@ -22,14 +22,13 @@ package org.onap.policy.pap.main.repository; import java.util.Date; import java.util.List; -import org.onap.policy.models.base.PfGeneratedIdKey; import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface PdpStatisticsRepository extends JpaRepository { +public interface PdpStatisticsRepository extends JpaRepository { List findByTimeStampBetween(Date startTime, Date endTime, Pageable topRecordsSize); @@ -60,26 +59,26 @@ public interface PdpStatisticsRepository extends JpaRepository findByPdpGroupNameAndPdpSubGroupNameAndTimeStampLessThanEqual(String pdpGroup, String pdpSubGroup, Date endTime, Pageable topRecordsSize); - List findByPdpGroupNameAndPdpSubGroupNameAndKeyName(String pdpGroup, String pdpSubGroup, + List findByPdpGroupNameAndPdpSubGroupNameAndName(String pdpGroup, String pdpSubGroup, String pdp, Pageable topRecordsSize); - List findByPdpGroupNameAndPdpSubGroupNameAndKeyNameAndTimeStampGreaterThanEqual(String pdpGroup, + List findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampGreaterThanEqual(String pdpGroup, String pdpSubGroup, String pdp, Date startTime, Pageable topRecordsSize); - List findByPdpGroupNameAndPdpSubGroupNameAndKeyNameAndTimeStampLessThanEqual(String pdpGroup, + List findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampLessThanEqual(String pdpGroup, String pdpSubGroup, String pdp, Date endTime, Pageable topRecordsSize); - List findByPdpGroupNameAndPdpSubGroupNameAndKeyNameAndTimeStampBetween(String pdpGroup, + List findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampBetween(String pdpGroup, String pdpSubGroup, String pdp, Date startTime, Date endTime, Pageable topRecordsSize); - List findByKeyName(String pdp, Pageable topRecordsSize); + List findByName(String pdp, Pageable topRecordsSize); - List findByKeyNameAndTimeStampGreaterThanEqual(String pdp, Date startTime, + List findByNameAndTimeStampGreaterThanEqual(String pdp, Date startTime, Pageable topRecordsSize); - List findByKeyNameAndTimeStampLessThanEqual(String pdp, Date startTime, Pageable topRecordsSize); + List findByNameAndTimeStampLessThanEqual(String pdp, Date startTime, Pageable topRecordsSize); - List findByKeyNameAndTimeStampBetween(String pdp, Date startTime, Date endTime, + List findByNameAndTimeStampBetween(String pdp, Date startTime, Date endTime, Pageable topRecordsSize); } diff --git a/main/src/main/java/org/onap/policy/pap/main/repository/PolicyAuditRepository.java b/main/src/main/java/org/onap/policy/pap/main/repository/PolicyAuditRepository.java index f140fc6b..b0fcbfc0 100644 --- a/main/src/main/java/org/onap/policy/pap/main/repository/PolicyAuditRepository.java +++ b/main/src/main/java/org/onap/policy/pap/main/repository/PolicyAuditRepository.java @@ -22,14 +22,13 @@ package org.onap.policy.pap.main.repository; import java.util.Date; import java.util.List; -import org.onap.policy.models.base.PfGeneratedIdKey; import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface PolicyAuditRepository extends JpaRepository { +public interface PolicyAuditRepository extends JpaRepository { List findByTimeStampBetween(Date startTime, Date endTime, Pageable topRecordsSize); @@ -48,26 +47,26 @@ public interface PolicyAuditRepository extends JpaRepository findByPdpGroupAndTimeStampBetween(String pdpGroup, Date startTime, Date endTime, Pageable topRecordsSize); - List findByPdpGroupAndKeyNameAndKeyVersion(String pdpGroup, String policyName, String policyVersion, + List findByPdpGroupAndNameAndVersion(String pdpGroup, String policyName, String policyVersion, Pageable topRecordsSize); - List findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampGreaterThanEqual(String pdpGroup, + List findByPdpGroupAndNameAndVersionAndTimeStampGreaterThanEqual(String pdpGroup, String policyName, String policyVersion, Date startTime, Pageable topRecordsSize); - List findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampLessThanEqual(String pdpGroup, + List findByPdpGroupAndNameAndVersionAndTimeStampLessThanEqual(String pdpGroup, String policyName, String policyVersion, Date endTime, Pageable topRecordsSize); - List findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampBetween(String pdpGroup, String policyName, + List findByPdpGroupAndNameAndVersionAndTimeStampBetween(String pdpGroup, String policyName, String policyVersion, Date startTime, Date endTime, Pageable topRecordsSize); - List findByKeyNameAndKeyVersion(String policyName, String policyVersion, Pageable topRecordsSize); + List findByNameAndVersion(String policyName, String policyVersion, Pageable topRecordsSize); - List findByKeyNameAndKeyVersionAndTimeStampGreaterThanEqual(String policyName, String policyVersion, + List findByNameAndVersionAndTimeStampGreaterThanEqual(String policyName, String policyVersion, Date startTime, Pageable topRecordsSize); - List findByKeyNameAndKeyVersionAndTimeStampLessThanEqual(String policyName, String policyVersion, + List findByNameAndVersionAndTimeStampLessThanEqual(String policyName, String policyVersion, Date endTime, Pageable topRecordsSize); - List findByKeyNameAndKeyVersionAndTimeStampBetween(String policyName, String policyVersion, + List findByNameAndVersionAndTimeStampBetween(String policyName, String policyVersion, Date startTime, Date endTime, Pageable topRecordsSize); } diff --git a/main/src/main/java/org/onap/policy/pap/main/service/PdpStatisticsService.java b/main/src/main/java/org/onap/policy/pap/main/service/PdpStatisticsService.java index c9a831ee..db340041 100644 --- a/main/src/main/java/org/onap/policy/pap/main/service/PdpStatisticsService.java +++ b/main/src/main/java/org/onap/policy/pap/main/service/PdpStatisticsService.java @@ -26,15 +26,12 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; import javax.ws.rs.core.Response; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.base.PfGeneratedIdKey; -import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics; @@ -58,8 +55,6 @@ public class PdpStatisticsService { private static final int DEFAULT_RECORD_COUNT = 10; private static final int MAX_RECORD_COUNT = 100; - private AtomicLong generatedId = new AtomicLong(); - private final PdpStatisticsRepository pdpStatisticsRepository; /** @@ -98,19 +93,15 @@ public class PdpStatisticsService { if (!validationResult.isValid()) { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult()); } - //TODO: Fix this as part of POLICY-3897 - jpaPdpStatistics.getKey().setGeneratedId(generatedId.incrementAndGet()); pdpStatisticsRepository.saveAndFlush(jpaPdpStatistics); - pdpStatistics.setGeneratedId(jpaPdpStatistics.getKey().getGeneratedId()); + pdpStatistics.setGeneratedId(jpaPdpStatistics.getGeneratedId()); } // Return the created PDP statistics List pdpStatistics = new ArrayList<>(pdpStatisticsList.size()); for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) { - var jpaPdpStatistics = - pdpStatisticsRepository.getById(new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(), - PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId())); + var jpaPdpStatistics = pdpStatisticsRepository.getById(pdpStatisticsItem.getGeneratedId()); pdpStatistics.add(jpaPdpStatistics.toAuthorative()); } return pdpStatistics; @@ -133,19 +124,19 @@ public class PdpStatisticsService { Pageable recordSize = getRecordSize(recordCount); if (startTime != null && endTime != null) { return generatePdpStatistics(asPdpStatisticsList(pdpStatisticsRepository - .findByPdpGroupNameAndPdpSubGroupNameAndKeyNameAndTimeStampBetween(pdpGroup, pdpSubGroup, pdp, + .findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampBetween(pdpGroup, pdpSubGroup, pdp, convertInstantToDate(startTime), convertInstantToDate(endTime), recordSize))); } else if (startTime == null && endTime == null) { return generatePdpStatistics( - asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndKeyName(pdpGroup, + asPdpStatisticsList(pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndName(pdpGroup, pdpSubGroup, pdp, recordSize))); } else if (startTime != null) { return generatePdpStatistics(asPdpStatisticsList( - pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndKeyNameAndTimeStampGreaterThanEqual( + pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampGreaterThanEqual( pdpGroup, pdpSubGroup, pdp, convertInstantToDate(startTime), recordSize))); } else { return generatePdpStatistics(asPdpStatisticsList( - pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndKeyNameAndTimeStampLessThanEqual( + pdpStatisticsRepository.findByPdpGroupNameAndPdpSubGroupNameAndNameAndTimeStampLessThanEqual( pdpGroup, pdpSubGroup, pdp, convertInstantToDate(endTime), recordSize))); } } diff --git a/main/src/main/java/org/onap/policy/pap/main/service/PolicyAuditService.java b/main/src/main/java/org/onap/policy/pap/main/service/PolicyAuditService.java index 951a3cbd..f582135d 100644 --- a/main/src/main/java/org/onap/policy/pap/main/service/PolicyAuditService.java +++ b/main/src/main/java/org/onap/policy/pap/main/service/PolicyAuditService.java @@ -23,7 +23,6 @@ package org.onap.policy.pap.main.service; import java.time.Instant; import java.util.Date; import java.util.List; -import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; import javax.ws.rs.core.Response; import lombok.NonNull; @@ -47,8 +46,6 @@ public class PolicyAuditService { private static final Integer DEFAULT_MAX_RECORDS = 100; private static final Integer DEFAULT_MIN_RECORDS = 10; - private AtomicLong generatedId = new AtomicLong(); - private final PolicyAuditRepository policyAuditRepository; /** @@ -64,8 +61,6 @@ public class PolicyAuditService { var count = 0; for (JpaPolicyAudit jpaAudit : jpaAudits) { result.addResult(jpaAudit.validate(String.valueOf(count++))); - // TODO: Fix this as part of POLICY-3897 - jpaAudit.getKey().setGeneratedId(generatedId.incrementAndGet()); } if (!result.isValid()) { @@ -139,19 +134,17 @@ public class PolicyAuditService { @NonNull String policyVersion, int recordCount, Instant startTime, Instant endTime) { Pageable recordSize = getRecordSize(recordCount); if (startTime != null && endTime != null) { - return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampBetween( - pdpGroup, policyName, policyVersion, Date.from(startTime), Date.from(endTime), recordSize)); + return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampBetween(pdpGroup, + policyName, policyVersion, Date.from(startTime), Date.from(endTime), recordSize)); } else if (startTime == null && endTime == null) { - return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersion(pdpGroup, policyName, - policyVersion, recordSize)); - } else if (startTime != null) { return asPolicyAuditList( - policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampGreaterThanEqual(pdpGroup, - policyName, policyVersion, Date.from(startTime), recordSize)); + policyAuditRepository.findByPdpGroupAndNameAndVersion(pdpGroup, policyName, policyVersion, recordSize)); + } else if (startTime != null) { + return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampGreaterThanEqual( + pdpGroup, policyName, policyVersion, Date.from(startTime), recordSize)); } else { - return asPolicyAuditList( - policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampLessThanEqual(pdpGroup, - policyName, policyVersion, Date.from(endTime), recordSize)); + return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampLessThanEqual( + pdpGroup, policyName, policyVersion, Date.from(endTime), recordSize)); } } @@ -169,17 +162,16 @@ public class PolicyAuditService { Instant startTime, Instant endTime) { Pageable recordSize = getRecordSize(recordCount); if (startTime != null && endTime != null) { - return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampBetween(policyName, + return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampBetween(policyName, policyVersion, Date.from(startTime), Date.from(endTime), recordSize)); } else if (startTime == null && endTime == null) { - return asPolicyAuditList( - policyAuditRepository.findByKeyNameAndKeyVersion(policyName, policyVersion, recordSize)); + return asPolicyAuditList(policyAuditRepository.findByNameAndVersion(policyName, policyVersion, recordSize)); } else if (startTime != null) { - return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampGreaterThanEqual( - policyName, policyVersion, Date.from(startTime), recordSize)); + return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampGreaterThanEqual(policyName, + policyVersion, Date.from(startTime), recordSize)); } else { - return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampLessThanEqual( - policyName, policyVersion, Date.from(endTime), recordSize)); + return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampLessThanEqual(policyName, + policyVersion, Date.from(endTime), recordSize)); } } diff --git a/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java index fc604cee..7bfc9973 100644 --- a/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/service/PdpStatisticsServiceTest.java @@ -30,7 +30,6 @@ import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.models.base.Validated; import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.pap.main.repository.PdpStatisticsRepository; import org.onap.policy.pap.main.rest.CommonPapRestServer; @@ -101,11 +100,9 @@ public class PdpStatisticsServiceTest extends CommonPapRestServer { PdpStatistics pdpStatisticsErr = new PdpStatistics(); pdpStatisticsErr.setPdpInstanceId("NULL"); pdpStatisticsErr.setPdpGroupName(GROUP); - assertThatThrownBy(() -> { pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr)); - }).hasMessageContaining("pdp statistics").hasMessageContaining("key") - .hasMessageContaining(Validated.IS_A_NULL_KEY); + }).hasMessageContaining("item \"name\" value \"NULL\" INVALID, is null"); } @Test -- cgit 1.2.3-korg