/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 Nordix Foundation.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021,2023 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.
package org.onap.policy.models.dao;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import java.time.Instant;
import java.util.ArrayList;
-import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
-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.PfModelException;
import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.PfReferenceTimestampKey;
import org.onap.policy.models.base.PfTimestampKey;
import org.onap.policy.models.dao.impl.DefaultPfDao;
private static final String VERSION003 = "0.0.3";
private static final String VERSION002 = "0.0.2";
private static final String VERSION001 = "0.0.1";
- private static final Date TIMESTAMP0 = new Date();
- private static final Date TIMESTAMP1 = new Date();
- private static final Date TIMESTAMP2 = new Date();
+ 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 PfDao pfDao;
+ /**
+ * Closes the DAO.
+ */
+ @After
+ public void tearDown() {
+ if (pfDao != null) {
+ pfDao.close();
+ pfDao = null;
+ }
+ }
+
@Test
public void testEntityTestSanity() throws PfModelException {
final DaoParameters daoParameters = new DaoParameters();
Properties jdbcProperties = new Properties();
// @formatter:off
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "sa");
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "");
+ jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
+ jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:EntityTest");
+ jdbcProperties.setProperty("javax.persistence.jdbc.user", "sa");
+ jdbcProperties.setProperty("javax.persistence.jdbc.password", "");
// @formatter:on
daoParameters.setJdbcProperties(jdbcProperties);
assertThatThrownBy(() -> pfDao.create(new PfConceptKey()))
.hasMessage("Policy Framework DAO has not been initialized");
-
- pfDao.close();
}
@Test
Properties jdbcProperties = new Properties();
jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
- jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
+ jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:EntityTest");
jdbcProperties.setProperty("javax.persistence.jdbc.user", "sa");
jdbcProperties.setProperty("javax.persistence.jdbc.password", "");
testAllOps();
+ testReferenceTimestamp();
+
testVersionOps();
testgetFilteredOps();
- pfDao.close();
+ testgetFilteredOps3();
}
@Test
daoParameters.setPluginClass(DefaultPfDao.class.getName());
daoParameters.setPersistenceUnit("DaoTest");
+ Properties jdbcProperties = new Properties();
+ jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
+ jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:EntityTest");
+ jdbcProperties.setProperty("javax.persistence.jdbc.user", "sa");
+ jdbcProperties.setProperty("javax.persistence.jdbc.password", "");
+
+ daoParameters.setJdbcProperties(jdbcProperties);
+
pfDao = new PfDaoFactory().createPfDao(daoParameters);
pfDao.init(daoParameters);
pfDao.get(null, nullTimeKey);
pfDao.getAll(null);
pfDao.getAll(null, nullKey);
+ pfDao.getAll(null, null, null);
pfDao.getConcept(null, nullKey);
pfDao.getConcept(PfConceptKey.class, nullKey);
pfDao.getConcept(null, nullRefKey);
pfDao.getConcept(PfReferenceKey.class, nullRefKey);
- pfDao.size(null);
- pfDao.close();
+ assertThatCode(() -> pfDao.size(null)).doesNotThrowAnyException();
}
private void testAllOps() {
pfDao.create(keyInfo0);
final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
- assertTrue(keyInfo0.equals(keyInfoBack0));
+ assertEquals(keyInfo0, keyInfoBack0);
final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
assertNull(keyInfoBackNull);
final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
- assertTrue(keyInfoBack0.equals(keyInfoBack1));
+ assertEquals(keyInfoBack0, keyInfoBack1);
final DummyConceptEntity keyInfoBack2 =
pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", VERSION001));
Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
keyInfoSetIn.add(keyInfo0);
- assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
+ assertEquals(keyInfoSetIn, keyInfoSetOut);
pfDao.delete(keyInfo1);
keyInfoSetIn.remove(keyInfo1);
keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
- assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
+ assertEquals(keyInfoSetIn, keyInfoSetOut);
pfDao.deleteCollection(keyInfoSetIn);
keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
keyInfoSetIn.add(keyInfo0);
pfDao.createCollection(keyInfoSetIn);
keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
- assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
+ assertEquals(keyInfoSetIn, keyInfoSetOut);
pfDao.delete(DummyConceptEntity.class, aKey0);
keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
keyInfoSetIn.add(keyInfo0);
pfDao.createCollection(keyInfoSetIn);
keyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyConceptEntity.class));
- assertTrue(keyInfoSetIn.equals(keyInfoSetOut));
+ assertEquals(keyInfoSetIn, keyInfoSetOut);
pfDao.deleteAll(DummyConceptEntity.class);
assertEquals(0, pfDao.size(DummyConceptEntity.class));
assertEquals(0, pfDao.size(DummyTimestampEntity.class));
}
+ private void testReferenceTimestamp() {
+ final PfConceptKey owner0Key = new PfConceptKey("Owner0", VERSION001);
+ final PfConceptKey owner1Key = new PfConceptKey("Owner1", VERSION001);
+ final PfConceptKey owner2Key = new PfConceptKey("Owner2", VERSION001);
+ final PfReferenceTimestampKey arKey0 = new PfReferenceTimestampKey(owner0Key, "AT-KEY0", TIMESTAMP0);
+ final PfReferenceTimestampKey arKey1 = new PfReferenceTimestampKey(owner1Key, "AT-KEY1", TIMESTAMP1);;
+ final PfReferenceTimestampKey arKey2 = new PfReferenceTimestampKey(owner2Key, "AT-KEY2", TIMESTAMP2);
+ final DummyReferenceTimestampEntity rkeyInfo0 = new DummyReferenceTimestampEntity(arKey0);
+ final DummyReferenceTimestampEntity rkeyInfo1 = new DummyReferenceTimestampEntity(arKey1);
+ final DummyReferenceTimestampEntity rkeyInfo2 = new DummyReferenceTimestampEntity(arKey2);
+
+ pfDao.create(rkeyInfo0);
+
+ final DummyReferenceTimestampEntity rkeyInfoBack0 = pfDao.get(DummyReferenceTimestampEntity.class, arKey0);
+ assertEquals(rkeyInfo0, rkeyInfoBack0);
+
+
+ final DummyReferenceTimestampEntity rkeyInfoBackNull =
+ pfDao.get(DummyReferenceTimestampEntity.class, PfReferenceTimestampKey.getNullKey());
+ assertNull(rkeyInfoBackNull);
+
+ final Set<DummyReferenceTimestampEntity> rkeyInfoSetIn = new TreeSet<>();
+ rkeyInfoSetIn.add(rkeyInfo1);
+ rkeyInfoSetIn.add(rkeyInfo2);
+
+ pfDao.createCollection(rkeyInfoSetIn);
+
+ Set<DummyReferenceTimestampEntity> rkeyInfoSetOut =
+ new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class));
+
+ rkeyInfoSetIn.add(rkeyInfo0);
+ assertEquals(rkeyInfoSetIn, rkeyInfoSetOut);
+
+ pfDao.delete(rkeyInfo1);
+ rkeyInfoSetIn.remove(rkeyInfo1);
+ rkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class));
+ assertEquals(rkeyInfoSetIn, rkeyInfoSetOut);
+
+ pfDao.deleteCollection(rkeyInfoSetIn);
+ rkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class));
+ assertEquals(0, rkeyInfoSetOut.size());
+
+ rkeyInfoSetIn.add(rkeyInfo2);
+ pfDao.createCollection(rkeyInfoSetIn);
+ rkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class));
+ assertEquals(rkeyInfoSetIn, rkeyInfoSetOut);
+
+ pfDao.deleteAll(DummyReferenceTimestampEntity.class);
+ assertEquals(0, pfDao.size(DummyReferenceTimestampEntity.class));
+ }
+
private void testVersionOps() {
final PfConceptKey aKey0 = new PfConceptKey("AAA0", VERSION001);
final PfConceptKey aKey1 = new PfConceptKey("AAA0", VERSION002);
pfDao.create(keyInfo4);
pfDao.create(keyInfo5);
- assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, null).size());
- assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null).size());
- assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null).size());
- assertEquals(1, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", VERSION003).size());
- assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, VERSION003).size());
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, null, null)).hasSize(6);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null)).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null)).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, "BBB0", VERSION003)).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, null, VERSION003)).hasSize(6);
final PfTimestampKey atKey0 = new PfTimestampKey("AT-KEY0", VERSION001, TIMESTAMP0);
final PfTimestampKey atKey1 = new PfTimestampKey("AT-KEY1", VERSION001, TIMESTAMP1);
pfDao.create(tkeyInfo2);
- assertEquals(1, pfDao
- .getFiltered(DummyTimestampEntity.class, "AT-KEY0", VERSION001, null, null, null, "DESC", 0).size());
- assertEquals(1,
- pfDao.getFiltered(DummyTimestampEntity.class, "AT-KEY0", null, null, null, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyTimestampEntity.class, null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(1, pfDao
- .getFiltered(DummyTimestampEntity.class, "AT-KEY0", VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(3, pfDao
- .getFiltered(DummyTimestampEntity.class, null, VERSION001, null, TIMESTAMP2, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyTimestampEntity.class, null, VERSION001, TIMESTAMP0, null, null, "DESC", 0).size());
- assertEquals(2,
- pfDao.getFiltered(DummyTimestampEntity.class, null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 2)
- .size());
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().name("AT-KEY0").version(VERSION001).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().name("AT-KEY0").build()))
+ .hasSize(1);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().version(VERSION001)
+ .startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().name("AT-KEY0")
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().version(VERSION001)
+ .startTime(TIMESTAMP0).endTime(TIMESTAMP2).sortOrder("DESC").recordNum(2).build())).hasSize(2);
Map<String, Object> filterMap = new HashMap<>();
filterMap.put("doubleValue", 200.1);
- assertEquals(1,
- pfDao.getFiltered(DummyTimestampEntity.class, null, null, null, null, filterMap, "DESC", 0).size());
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1);
+ }
+ private void testgetFilteredOps3() {
+ Map<String, Object> filterMap = new HashMap<>();
+ filterMap.put("localName", "AT-KEY0");
+
+ final PfConceptKey owner0Key = new PfConceptKey("Owner0", VERSION001);
+ final PfConceptKey owner1Key = new PfConceptKey("Owner1", VERSION001);
+ final PfConceptKey owner2Key = new PfConceptKey("Owner2", VERSION001);
+ final PfReferenceTimestampKey arKey0 = new PfReferenceTimestampKey(owner0Key, "AT-KEY0", TIMESTAMP0);
+ final PfReferenceTimestampKey arKey1 = new PfReferenceTimestampKey(owner1Key, "AT-KEY1", TIMESTAMP1);;
+ final PfReferenceTimestampKey arKey2 = new PfReferenceTimestampKey(owner2Key, "AT-KEY2", TIMESTAMP2);
+ final DummyReferenceTimestampEntity rkeyInfo0 = new DummyReferenceTimestampEntity(arKey0);
+ final DummyReferenceTimestampEntity rkeyInfo1 = new DummyReferenceTimestampEntity(arKey1);
+ final DummyReferenceTimestampEntity rkeyInfo2 = new DummyReferenceTimestampEntity(arKey2);
+
+ pfDao.create(rkeyInfo0);
+ pfDao.create(rkeyInfo1);
+ pfDao.create(rkeyInfo2);
+
+
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().name("Owner0").version(VERSION001).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().name("Owner0").build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class, PfFilterParameters.builder()
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class, PfFilterParameters.builder().name("Owner0")
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2)
+ .sortOrder("DESC").recordNum(2).build())).hasSize(2);
+
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1);
}
}