Skip to content

v0.2.51..v0.2.52 changeset DbUtilsTest.java

Garret Voltz edited this page Jan 15, 2020 · 1 revision
diff --git a/hoot-services/src/test/java/hoot/services/utils/DbUtilsTest.java b/hoot-services/src/test/java/hoot/services/utils/DbUtilsTest.java
index 51629af..da4138f 100644
--- a/hoot-services/src/test/java/hoot/services/utils/DbUtilsTest.java
+++ b/hoot-services/src/test/java/hoot/services/utils/DbUtilsTest.java
@@ -22,20 +22,24 @@
  * This will properly maintain the copyright information. DigitalGlobe
  * copyrights will be updated automatically.
  *
- * @copyright Copyright (C) 2016, 2017, 2018 DigitalGlobe (http://www.digitalglobe.com/)
+ * @copyright Copyright (C) 2016, 2017, 2018, 2019, 2020 DigitalGlobe (http://www.digitalglobe.com/)
  */
 package hoot.services.utils;
 
-import static hoot.services.utils.DbUtils.createQuery;
 import static hoot.services.utils.MapUtils.insertMap;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.text.DateFormat;
+import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.commons.io.IOUtils;
 import org.json.simple.JSONObject;
@@ -52,11 +56,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.support.AnnotationConfigContextLoader;
 import org.springframework.transaction.annotation.Transactional;
 
-import com.querydsl.core.types.dsl.Expressions;
-
 import hoot.services.ApplicationContextUtils;
 import hoot.services.UnitTest;
 import hoot.services.jerseyframework.HootServicesSpringTestConfig;
+import hoot.services.models.db.Maps;
+import hoot.services.models.osm.MapLayer;
 
 
 @RunWith(SpringJUnit4ClassRunner.class)
@@ -78,32 +82,26 @@ public class DbUtilsTest {
     @Category(UnitTest.class)
     @Transactional
     public void testDeleteTables() throws Exception {
+        MapUtils.cleanupTestUsers();
+
         long userId = MapUtils.insertUser();
         long mapId = insertMap(userId);
 
-        //burn a sequence to see if this makes them persist
-        //after table delete
-        Long burner = DbUtils.createQuery()
-                .select(Expressions.numberTemplate(Long.class, "nextval('changesets_" + mapId + "_id_seq')"))
-                .from()
-                .fetchOne();
-
-        DbUtils.createQuery().getConnection().commit();
-
+        assertTrue(DbUtils.userExists(userId));
         assertTrue(DbUtils.mapExists(String.valueOf(mapId)));
-        assertTrue(checkForDependents(mapId));
 
-        MapUtils.deleteOSMRecord(mapId);
-        DbUtils.createQuery().getConnection().commit();
+        DbUtils.deleteMapRelatedTablesByMapId(mapId);
+        DbUtils.deleteMap(mapId);
 
         assertFalse(DbUtils.mapExists(String.valueOf(mapId)));
-        assertFalse(checkForDependents(mapId));
 
-    }
+        MapUtils.deleteUser(userId);
+        assertFalse(DbUtils.userExists(userId));
+   }
 
 
     public boolean checkForDependents(long mapId) throws SQLException {
-        return DbUtils.getMapTableSeqCount(mapId) > 0;
+        return DbUtils.getMapTableSeqCount(mapId) == 10;
     }
 
     @Test
@@ -198,6 +196,10 @@ public class DbUtilsTest {
         assertEquals(v2, checkTags.get(k2));
         assertEquals(v4, checkTags.get(k4));
         assertEquals(v6, checkTags.get(k6));
+
+        DbUtils.deleteMapRelatedTablesByMapId(mapId);
+        DbUtils.deleteMap(mapId);
+        MapUtils.deleteUser(userId);
     }
 
     @Test
@@ -255,4 +257,90 @@ public class DbUtilsTest {
         JSONObject outJson = (JSONObject) parser.parse(output.replaceAll("\\\\\"", "\""));
         assertEquals(exJson, outJson);
     }
+
+
+    @Test
+    @Category(UnitTest.class)
+    @Transactional
+    public void testDeleteEmptyFolders() throws Exception {
+        //Create a user, some folders and a map in one of the folders
+        long userId = MapUtils.insertUser();
+        long folderId1 = DbUtils.createFolder("empty1", 0L, userId, false);
+        long folderId2 = DbUtils.createFolder("empty2", folderId1, userId, false);
+        long folderId3 = DbUtils.createFolder("full3", 0L, userId, false);
+        long mapId = insertMap(userId);
+        DbUtils.updateFolderMapping(mapId, folderId3);
+
+        //Confirm the map and folders were all created
+        assertTrue(DbUtils.userExists(userId));
+        assertTrue(DbUtils.mapExists(String.valueOf(mapId)));
+        Set<Long> fids = DbUtils.getFolderIdsForUser(Long.valueOf(userId));
+        assertTrue(fids.contains(folderId1));
+        assertTrue(fids.contains(folderId2));
+        assertTrue(fids.contains(folderId3));
+
+        //Delete the empty folders
+        DbUtils.deleteEmptyFolders();
+
+        //Confirm emppty folders were deleted
+        fids = DbUtils.getFolderIdsForUser(Long.valueOf(userId));
+        assertFalse(fids.contains(folderId1));
+        assertFalse(fids.contains(folderId2));
+        assertTrue(fids.contains(folderId3));
+
+        //Delete the map and resulting empty folders and user
+        DbUtils.deleteMapRelatedTablesByMapId(mapId);
+        DbUtils.deleteMap(mapId);
+        DbUtils.deleteEmptyFolders();
+        MapUtils.deleteUser(userId);
+
+        //Confirm map and all folders (now empty) are gone
+        fids = DbUtils.getFolderIdsForUser(Long.valueOf(userId));
+        assertFalse(DbUtils.userExists(userId));
+        assertFalse(DbUtils.mapExists(String.valueOf(mapId)));
+        assertFalse(fids.contains(folderId1));
+        assertFalse(fids.contains(folderId2));
+        assertFalse(fids.contains(folderId3));
+
+    }
+
+    @Test
+    @Category(UnitTest.class)
+    @Transactional
+    public void testGetOldMaps() throws Exception {
+        //Create a user, some folders and a map in one of the folders
+        long userId = MapUtils.insertUser();
+        long mapIdOld = insertMap(userId);
+        long mapIdNew = insertMap(userId);
+
+        Map<String, String> tagsOld = new HashMap<>();
+        tagsOld.put("lastAccessed", "2019-10-24T17:03:51.125Z");
+
+        Map<String, String> tagsNew = new HashMap<>();
+        DateFormat dateFormat = MapLayer.format;
+        Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis());
+        tagsNew.put("lastAccessed", dateFormat.format(now));
+
+
+        DbUtils.updateMapsTableTags(tagsOld, mapIdOld);
+        DbUtils.updateMapsTableTags(tagsNew, mapIdNew);
+
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH, -1);
+        Timestamp ts = new Timestamp(cal.getTime().getTime());
+        List<Maps> oldMaps = DbUtils.getStaleMaps(ts);
+
+        List<Long> oldMapIds = oldMaps.stream().map(m -> m.getId()).collect(Collectors.toList());
+        assertTrue(oldMapIds.contains(mapIdOld));
+        assertFalse(oldMapIds.contains(mapIdNew));
+
+        Map<String, Long> staleMapSummary = DbUtils.getStaleMapsSummary(ts);
+        assertTrue(staleMapSummary.get(userId + "::MapUtils::insertUser()") == 1);
+
+        DbUtils.deleteMapRelatedTablesByMapId(mapIdOld);
+        DbUtils.deleteMap(mapIdOld);
+        DbUtils.deleteMapRelatedTablesByMapId(mapIdNew);
+        DbUtils.deleteMap(mapIdNew);
+        MapUtils.deleteUser(userId);
+    }
 }
Clone this wiki locally