Fix config files to remove outdated configuration for hibernate
[policy/models.git] / models-dao / src / test / java / org / onap / policy / models / dao / EntityTest.java
index 70505aa..4f281bd 100644 (file)
@@ -1,7 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
- *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ *  Copyright (C) 2019-2021, 2023-2024 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.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 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;
 
 /**
@@ -54,18 +61,33 @@ public class EntityTest {
     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 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("jakarta.persistence.jdbc.driver",   "org.h2.Driver");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.url",      "jdbc:h2:mem:EntityTest");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.user",     "sa");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.password", "");
         // @formatter:on
 
         daoParameters.setJdbcProperties(jdbcProperties);
@@ -85,8 +107,6 @@ public class EntityTest {
 
         assertThatThrownBy(() -> pfDao.create(new PfConceptKey()))
                         .hasMessage("Policy Framework DAO has not been initialized");
-
-        pfDao.close();
     }
 
     @Test
@@ -97,10 +117,10 @@ public class EntityTest {
         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:testdb");
-        jdbcProperties.setProperty("javax.persistence.jdbc.user", "sa");
-        jdbcProperties.setProperty("javax.persistence.jdbc.password", "");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.driver", "org.h2.Driver");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.url", "jdbc:h2:mem:EntityTest");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.user", "sa");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.password", "");
 
         daoParameters.setJdbcProperties(jdbcProperties);
 
@@ -109,11 +129,13 @@ public class EntityTest {
 
         testAllOps();
 
+        testReferenceTimestamp();
+
         testVersionOps();
 
         testgetFilteredOps();
 
-        pfDao.close();
+        testgetFilteredOps3();
     }
 
     @Test
@@ -122,11 +144,20 @@ public class EntityTest {
         daoParameters.setPluginClass(DefaultPfDao.class.getName());
         daoParameters.setPersistenceUnit("DaoTest");
 
+        Properties jdbcProperties = new Properties();
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.driver", "org.h2.Driver");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.url", "jdbc:h2:mem:EntityTest");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.user", "sa");
+        jdbcProperties.setProperty("jakarta.persistence.jdbc.password", "");
+
+        daoParameters.setJdbcProperties(jdbcProperties);
+
         pfDao = new PfDaoFactory().createPfDao(daoParameters);
         pfDao.init(daoParameters);
 
         final PfConceptKey nullKey = null;
         final PfReferenceKey nullRefKey = null;
+        final PfTimestampKey nullTimeKey = null;
         final List<PfConceptKey> nullKeyList = null;
         final List<PfConceptKey> emptyKeyList = new ArrayList<>();
         final List<PfReferenceKey> nullRKeyList = null;
@@ -141,6 +172,7 @@ public class EntityTest {
         pfDao.deleteCollection(emptyKeyList);
         pfDao.delete(PfConceptKey.class, nullKey);
         pfDao.delete(PfReferenceKey.class, nullRefKey);
+        pfDao.delete(PfTimestampKey.class, nullTimeKey);
         pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
         pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
         pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
@@ -148,15 +180,16 @@ public class EntityTest {
 
         pfDao.get(null, nullKey);
         pfDao.get(null, nullRefKey);
+        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() {
@@ -173,13 +206,13 @@ public class EntityTest {
         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));
@@ -194,12 +227,12 @@ public class EntityTest {
         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));
@@ -210,7 +243,7 @@ public class EntityTest {
         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));
@@ -232,7 +265,7 @@ public class EntityTest {
         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));
@@ -297,6 +330,108 @@ public class EntityTest {
         assertEquals(2, deletedRCount);
 
         pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
+
+        final PfTimestampKey atKey0 = new PfTimestampKey("AT-KEY0", VERSION001, TIMESTAMP0);
+        final PfTimestampKey atKey1 = new PfTimestampKey("AT-KEY1", VERSION001, TIMESTAMP1);
+        final PfTimestampKey atKey2 = new PfTimestampKey("AT-KEY2", VERSION001, TIMESTAMP2);
+        final DummyTimestampEntity tkeyInfo0 = new DummyTimestampEntity(atKey0, 200.0);
+        final DummyTimestampEntity tkeyInfo1 = new DummyTimestampEntity(atKey1, 200.1);
+        final DummyTimestampEntity tkeyInfo2 = new DummyTimestampEntity(atKey2, 200.2);
+
+        pfDao.create(tkeyInfo0);
+
+        final DummyTimestampEntity tkeyInfoBack0 = pfDao.get(DummyTimestampEntity.class, atKey0);
+        assertEquals(tkeyInfo0, tkeyInfoBack0);
+
+        final DummyTimestampEntity tkeyInfoBackNull =
+                pfDao.get(DummyTimestampEntity.class, PfTimestampKey.getNullKey());
+        assertNull(tkeyInfoBackNull);
+
+
+
+        final Set<DummyTimestampEntity> tkeyInfoSetIn = new TreeSet<>();
+        tkeyInfoSetIn.add(tkeyInfo1);
+        tkeyInfoSetIn.add(tkeyInfo2);
+
+        pfDao.createCollection(tkeyInfoSetIn);
+
+        Set<DummyTimestampEntity> tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class));
+
+        tkeyInfoSetIn.add(tkeyInfo0);
+        assertEquals(tkeyInfoSetIn, tkeyInfoSetOut);
+
+        pfDao.delete(tkeyInfo1);
+        tkeyInfoSetIn.remove(tkeyInfo1);
+        tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class));
+        assertEquals(tkeyInfoSetIn, tkeyInfoSetOut);
+
+        pfDao.deleteCollection(tkeyInfoSetIn);
+        tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class));
+        assertEquals(0, tkeyInfoSetOut.size());
+
+        tkeyInfoSetIn.add(tkeyInfo2);
+        pfDao.createCollection(tkeyInfoSetIn);
+        tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class));
+        assertEquals(keyInfoSetIn, keyInfoSetOut);
+
+        pfDao.delete(DummyTimestampEntity.class, atKey2);
+        tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class));
+        assertEquals(3, keyInfoSetOut.size());
+        assertEquals(1, pfDao.size(DummyTimestampEntity.class));
+
+        pfDao.deleteAll(DummyTimestampEntity.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() {
@@ -358,10 +493,81 @@ public class EntityTest {
         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);
+        final PfTimestampKey atKey2 = new PfTimestampKey("AT-KEY2", VERSION001, TIMESTAMP2);
+        final DummyTimestampEntity tkeyInfo0 = new DummyTimestampEntity(atKey0, 200.0);
+        final DummyTimestampEntity tkeyInfo1 = new DummyTimestampEntity(atKey1, 200.1);
+        final DummyTimestampEntity tkeyInfo2 = new DummyTimestampEntity(atKey2, 200.2);
+
+        pfDao.create(tkeyInfo0);
+        pfDao.create(tkeyInfo1);
+        pfDao.create(tkeyInfo2);
+
+
+        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);
+        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);
     }
 }