diff options
author | a.sreekumar <ajith.sreekumar@bell.ca> | 2022-02-15 12:16:30 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@bell.ca> | 2022-02-16 11:49:27 +0000 |
commit | 676194789a8b880e2416f9d3bf2484a9fc6be1bc (patch) | |
tree | 71617241837026c53dec1f76b9c0b3ae7a584dff /models-dao/src/test/java/org/onap | |
parent | 45b653fc5a8d641452247eca5c80cf580609e9bf (diff) |
Fix issue with GeneratedValue in PfGeneratedIdKey
PfGeneratedIdKey class (which is used as a composite key
in JpaPolicyAudit and JpaPdpStatistics) uses GeneratedValue
in a wrong way and not according to the specification.
This review fixes it. PfGeneratedIdKey class is removed, and the
generatedId is directly specified in the JpaPolicyAudit and
JpaPdpStatistics classes.
Note: These classes are only used by PAP, so the related methods for db
interaction is removed as PAP directly talks to DB using spring
repository layer. Also the only end result this change brings is that the
'generatedId' alone will be used as the primary key instead of
'generatedId, name and version' together.
Corresponding changes in
DB Migrator: https://gerrit.onap.org/r/c/policy/docker/+/127139
PAP: https://gerrit.onap.org/r/c/policy/pap/+/127130
Change-Id: Ib4ea8b60ffe5c2480746569c0354bf474a6b7006
Issue-ID: POLICY-3897
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
Diffstat (limited to 'models-dao/src/test/java/org/onap')
-rw-r--r-- | models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java | 132 | ||||
-rw-r--r-- | models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java | 150 |
2 files changed, 1 insertions, 281 deletions
diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java b/models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java deleted file mode 100644 index 6ba045c54..000000000 --- a/models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java +++ /dev/null @@ -1,132 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * 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.models.dao; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import javax.persistence.Column; -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NonNull; -import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfGeneratedIdKey; -import org.onap.policy.models.base.PfKey; - - -@Entity -@Table(name = "DummyGeneratedIdEntity") -@Data -@EqualsAndHashCode(callSuper = false) -public class DummyGeneratedIdEntity extends PfConcept { - private static final long serialVersionUID = -2962570563281067896L; - - @EmbeddedId() - @NonNull - private PfGeneratedIdKey key; - - @Column(precision = 3) - @Temporal(TemporalType.TIMESTAMP) - private Date timeStamp; - - @Column - private double doubleValue; - - /** - * Default constructor. - */ - public DummyGeneratedIdEntity() { - this.key = new PfGeneratedIdKey(); - this.timeStamp = new Date(); - } - - public DummyGeneratedIdEntity(DummyGeneratedIdEntity source) { - this.key = source.key; - this.timeStamp = source.timeStamp; - } - - /** - * Constructor. - * - * @param key the key - * @param timeStamp the date value - */ - public DummyGeneratedIdEntity(final PfGeneratedIdKey key, final Date timeStamp) { - this.key = key; - this.timeStamp = timeStamp; - } - - /** - * Constructor. - * - * @param key the key - * @param timeStamp the date value - * @param doubleValue the double value * - */ - public DummyGeneratedIdEntity(final PfGeneratedIdKey key, final Date timeStamp, final double doubleValue) { - this.key = key; - this.timeStamp = timeStamp; - this.doubleValue = doubleValue; - } - - @Override - public List<PfKey> getKeys() { - final List<PfKey> keyList = new ArrayList<>(); - keyList.add(getKey()); - return keyList; - } - - @Override - public BeanValidationResult validate(@NonNull String fieldName) { - BeanValidationResult result = new BeanValidationResult(fieldName, this); - result.addResult(key.validate("key")); - return result; - } - - @Override - public void clean() { - key.clean(); - } - - @Override - public int compareTo(@NonNull final PfConcept otherObj) { - if (this == otherObj) { - return 0; - } - - if (getClass() != otherObj.getClass()) { - return this.getClass().getName().compareTo(otherObj.getClass().getName()); - } - - final DummyGeneratedIdEntity other = (DummyGeneratedIdEntity) otherObj; - if (this.timeStamp != other.timeStamp) { - return timeStamp.compareTo(other.timeStamp); - } - return Double.compare(doubleValue, other.doubleValue); - } - -} diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java index c0b0f2ad0..65d22863f 100644 --- a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java +++ b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2022 Bell Canada. 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. @@ -30,7 +31,6 @@ import static org.junit.Assert.assertNull; import java.time.Instant; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -42,7 +42,6 @@ import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfGeneratedIdKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfReferenceTimestampKey; @@ -66,9 +65,6 @@ public class EntityTest { private static final Instant TIMESTAMP0 = Instant.ofEpochSecond(1613494293); private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1613494293).plusSeconds(55); private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1613494293).plusSeconds(90); - private static final Long GENERATEDID0 = 10000L; - private static final Long GENERATEDID1 = 10001L; - private static final Long GENERATEDID2 = 10002L; private PfDao pfDao; @@ -134,16 +130,12 @@ public class EntityTest { testAllOps(); - testGeneratedId(); - testReferenceTimestamp(); testVersionOps(); testgetFilteredOps(); - testgetFilteredOps2(); - testgetFilteredOps3(); } @@ -384,111 +376,6 @@ public class EntityTest { assertEquals(0, pfDao.size(DummyTimestampEntity.class)); } - private void testGeneratedId() { - final PfGeneratedIdKey agKey0 = new PfGeneratedIdKey("AT-KEY0", VERSION001, GENERATEDID0); - final PfGeneratedIdKey agKey1 = new PfGeneratedIdKey("AT-KEY1", VERSION001, GENERATEDID1); - final PfGeneratedIdKey agKey2 = new PfGeneratedIdKey("AT-KEY2", VERSION001, GENERATEDID2); - final DummyGeneratedIdEntity gkeyInfo0 = new DummyGeneratedIdEntity(agKey0, Date.from(TIMESTAMP0)); - final DummyGeneratedIdEntity gkeyInfo1 = new DummyGeneratedIdEntity(agKey1, Date.from(TIMESTAMP1)); - final DummyGeneratedIdEntity gkeyInfo2 = new DummyGeneratedIdEntity(agKey2, Date.from(TIMESTAMP2)); - - pfDao.create(gkeyInfo0); - - final DummyGeneratedIdEntity gkeyInfoBack0 = pfDao.get(DummyGeneratedIdEntity.class, agKey0); - assertEquals(gkeyInfo0, gkeyInfoBack0); - - assertEquals(1, pfDao.getByTimestamp(DummyGeneratedIdEntity.class, agKey0, TIMESTAMP0).size()); - - final DummyGeneratedIdEntity gkeyInfoBackNull = - pfDao.get(DummyGeneratedIdEntity.class, PfGeneratedIdKey.getNullKey()); - assertNull(gkeyInfoBackNull); - - final Set<DummyGeneratedIdEntity> gkeyInfoSetIn = new TreeSet<>(); - gkeyInfoSetIn.add(gkeyInfo1); - gkeyInfoSetIn.add(gkeyInfo2); - - pfDao.createCollection(gkeyInfoSetIn); - - Set<DummyGeneratedIdEntity> gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - - gkeyInfoSetIn.add(gkeyInfo0); - assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); - - pfDao.delete(gkeyInfo1); - gkeyInfoSetIn.remove(gkeyInfo1); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); - - pfDao.deleteCollection(gkeyInfoSetIn); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(0, gkeyInfoSetOut.size()); - - gkeyInfoSetIn.add(gkeyInfo2); - pfDao.createCollection(gkeyInfoSetIn); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); - - pfDao.delete(DummyGeneratedIdEntity.class, agKey2); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(gkeyInfoSetOut.size(), pfDao.size(DummyGeneratedIdEntity.class)); - - pfDao.deleteAll(DummyGeneratedIdEntity.class); - assertEquals(0, pfDao.size(DummyGeneratedIdEntity.class)); - - final PfGeneratedIdKey agKey3 = new PfGeneratedIdKey("AT-KEY0", VERSION001); - final PfGeneratedIdKey agKey4 = new PfGeneratedIdKey("AT-KEY1", VERSION001); - final PfGeneratedIdKey agKey5 = new PfGeneratedIdKey("AT-KEY2", VERSION001); - final DummyGeneratedIdEntity gkeyInfo3 = new DummyGeneratedIdEntity(agKey3, Date.from(TIMESTAMP0)); - final DummyGeneratedIdEntity gkeyInfo4 = new DummyGeneratedIdEntity(agKey4, Date.from(TIMESTAMP1)); - final DummyGeneratedIdEntity gkeyInfo5 = new DummyGeneratedIdEntity(agKey5, Date.from(TIMESTAMP2)); - - pfDao.create(gkeyInfo3); - - final DummyGeneratedIdEntity gkeyInfoBack3 = pfDao.get(DummyGeneratedIdEntity.class, agKey3); - assertEquals(gkeyInfo3, gkeyInfoBack3); - - assertEquals(1, pfDao.getByTimestamp(DummyGeneratedIdEntity.class, agKey3, TIMESTAMP0).size()); - - assertEquals(1, gkeyInfo3.getKeys().size()); - - assertEquals(1, gkeyInfo4.compareTo(gkeyInfo3)); - - assertNull(gkeyInfo4.validate(VERSION002).getResult()); - - - gkeyInfoSetIn.clear(); - gkeyInfoSetIn.add(gkeyInfo4); - gkeyInfoSetIn.add(gkeyInfo5); - - pfDao.createCollection(gkeyInfoSetIn); - - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - - gkeyInfoSetIn.add(gkeyInfo3); - assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); - - pfDao.delete(gkeyInfo4); - gkeyInfoSetIn.remove(gkeyInfo4); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); - - pfDao.deleteCollection(gkeyInfoSetIn); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(0, gkeyInfoSetOut.size()); - - gkeyInfoSetIn.add(gkeyInfo5); - pfDao.createCollection(gkeyInfoSetIn); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); - - pfDao.delete(DummyGeneratedIdEntity.class, agKey5); - gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); - assertEquals(gkeyInfoSetOut.size(), pfDao.size(DummyGeneratedIdEntity.class)); - - pfDao.deleteAll(DummyGeneratedIdEntity.class); - assertEquals(0, pfDao.size(DummyGeneratedIdEntity.class)); - } - private void testReferenceTimestamp() { final PfConceptKey owner0Key = new PfConceptKey("Owner0", VERSION001); final PfConceptKey owner1Key = new PfConceptKey("Owner1", VERSION001); @@ -638,41 +525,6 @@ public class EntityTest { PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1); } - private void testgetFilteredOps2() { - Map<String, Object> filterMap = new HashMap<>(); - filterMap.put("doubleValue", 200.1); - - final PfGeneratedIdKey agKey0 = new PfGeneratedIdKey("AT-KEY0", VERSION001); - final PfGeneratedIdKey agKey1 = new PfGeneratedIdKey("AT-KEY1", VERSION001); - final PfGeneratedIdKey agKey2 = new PfGeneratedIdKey("AT-KEY2", VERSION001); - final DummyGeneratedIdEntity gkeyInfo0 = new DummyGeneratedIdEntity(agKey0, Date.from(TIMESTAMP0), 200.0); - final DummyGeneratedIdEntity gkeyInfo1 = new DummyGeneratedIdEntity(agKey1, Date.from(TIMESTAMP1), 200.1); - final DummyGeneratedIdEntity gkeyInfo2 = new DummyGeneratedIdEntity(agKey2, Date.from(TIMESTAMP2), 200.2); - - pfDao.create(gkeyInfo0); - pfDao.create(gkeyInfo1); - pfDao.create(gkeyInfo2); - - - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, - PfFilterParameters.builder().name("AT-KEY0").version(VERSION001).build())).hasSize(1); - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, - PfFilterParameters.builder().name("AT-KEY0").build())).hasSize(1); - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, PfFilterParameters.builder().version(VERSION001) - .startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(3); - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, PfFilterParameters.builder().name("AT-KEY0") - .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(1); - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, - PfFilterParameters.builder().version(VERSION001).endTime(TIMESTAMP2).build())).hasSize(3); - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, - PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).build())).hasSize(3); - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, PfFilterParameters.builder().version(VERSION001) - .startTime(TIMESTAMP0).endTime(TIMESTAMP2).sortOrder("DESC").recordNum(2).build())).hasSize(2); - - assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, - PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1); - } - private void testgetFilteredOps3() { Map<String, Object> filterMap = new HashMap<>(); filterMap.put("localName", "AT-KEY0"); |