From 07ad62ddd97cb095f467b2b340b77de0a910de9b Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Fri, 13 Dec 2019 14:33:20 +0100 Subject: [PATCH] use Junit5 instead of TestNG We want to be able to use @SpringBootTest tests that fully initialize the Spring application. This is much easier done with Junit than TestNG. Gradle does not support (at least not easily) to run Junit and TestNG tests. Therefore we switch to Junit with all tests. The original reason for using TestNG was that Junit didn't support data providers. But that finally changed in Junit5 with ParameterizedTest. --- .../lucares/pdb/blockstorage/BSFileTest.java | 19 +- .../pdb/blockstorage/TimeSeriesFileTest.java | 14 +- .../pdb/diskstorage/DiskStorageTest.java | 65 ++--- .../org/lucares/pdb/map/NodeEntryTest.java | 37 ++- .../pdb/map/PersistentMapDiskNodeTest.java | 9 +- .../map/PersistentMapOfListsOfLongsTest.java | 62 ----- .../lucares/pdb/map/PersistentMapTest.java | 78 +++--- build.gradle | 13 +- .../pdb/datastore/internal/DataStoreTest.java | 92 +++---- .../internal/DateIndexExtensionTest.java | 52 ++-- .../pdb/datastore/internal/ProposerTest.java | 38 +-- .../internal/QueryCompletionIndexTest.java | 24 +- .../datastore/lang/CandidateGrouperTest.java | 87 ++++--- .../lucares/pdb/api/DateTimeRangeTest.java | 32 +-- .../lucares/pdb/api/StringCompressorTest.java | 21 +- .../pdb/api/UniqueStringIntegerPairsTest.java | 27 +- pdb-ui/build.gradle | 1 + .../pdbui/CsvToEntryTransformerTest.java | 27 +- .../org/lucares/pdbui/PdbControllerTest.java | 34 ++- .../org/lucares/pdbui/TcpIngestorTest.java | 78 +++--- .../pdbui/date/FastISODateParserTest.java | 231 +++++++++--------- .../pdbui/domain/DataSeriesStatsTest.java | 29 +-- .../byteencoder/VariableByteEncoderTest.java | 106 ++++---- .../utils/cache/HotEntryCacheTest.java | 97 ++++---- .../performance/db/PdbReaderWriterTest.java | 129 ---------- .../performance/db/PerformanceDbTest.java | 95 ++++--- 26 files changed, 660 insertions(+), 837 deletions(-) delete mode 100644 block-storage/src/test/java/org/lucares/pdb/map/PersistentMapOfListsOfLongsTest.java delete mode 100644 performanceDb/src/test/java/org/lucares/performance/db/PdbReaderWriterTest.java diff --git a/block-storage/src/test/java/org/lucares/pdb/blockstorage/BSFileTest.java b/block-storage/src/test/java/org/lucares/pdb/blockstorage/BSFileTest.java index 83a678c..cc7d5c9 100644 --- a/block-storage/src/test/java/org/lucares/pdb/blockstorage/BSFileTest.java +++ b/block-storage/src/test/java/org/lucares/pdb/blockstorage/BSFileTest.java @@ -14,29 +14,29 @@ import java.util.concurrent.Future; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.lucares.collections.LongList; import org.lucares.pdb.diskstorage.DiskStorage; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class BSFileTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testBlockStorage() throws Exception { final Path file = dataDirectory.resolve("data.int.db"); final int numLongs = 1000; @@ -68,11 +68,12 @@ public class BSFileTest { final BSFile bsFile = BSFile.existingFile(blockOffset, ds, NullCustomizer.INSTANCE); final LongList actualLongs = bsFile.asLongList(); final LongList expectedLongs = LongList.rangeClosed(0, numLongs - 1); - Assert.assertEquals(actualLongs, expectedLongs); + Assertions.assertEquals(expectedLongs, actualLongs); } System.out.println("duration read: " + (System.nanoTime() - start) / 1_000_000.0 + "ms"); } + @Test public void testBlockStorageMultithreading() throws Exception { final ExecutorService pool = Executors.newCachedThreadPool(); @@ -125,7 +126,7 @@ public class BSFileTest { try (BSFile bsFile = BSFile.existingFile(rootBlockNumber, ds, NullCustomizer.INSTANCE)) { final LongList actualLongs = bsFile.asLongList(); final LongList expectedLongs = expectedValues; - Assert.assertEquals(actualLongs, expectedLongs, "for rootBlockNumber=" + rootBlockNumber); + Assertions.assertEquals(expectedLongs, actualLongs, "for rootBlockNumber=" + rootBlockNumber); } } } diff --git a/block-storage/src/test/java/org/lucares/pdb/blockstorage/TimeSeriesFileTest.java b/block-storage/src/test/java/org/lucares/pdb/blockstorage/TimeSeriesFileTest.java index 5af1aa2..6c06bdb 100644 --- a/block-storage/src/test/java/org/lucares/pdb/blockstorage/TimeSeriesFileTest.java +++ b/block-storage/src/test/java/org/lucares/pdb/blockstorage/TimeSeriesFileTest.java @@ -6,27 +6,29 @@ import java.nio.file.Path; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.lucares.collections.LongList; import org.lucares.pdb.diskstorage.DiskStorage; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; public class TimeSeriesFileTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testBlockStorageTimeValue() throws Exception { final Path file = dataDirectory.resolve("data.int.db"); final Random random = ThreadLocalRandom.current(); @@ -76,7 +78,7 @@ public class TimeSeriesFileTest { final TimeSeriesFile bsFile = TimeSeriesFile.existingFile(blockNumber, ds); final LongList actualLongs = bsFile.asTimeValueLongList(); - Assert.assertEquals(actualLongs, expectedLongs); + Assertions.assertEquals(expectedLongs, actualLongs); } System.out.println("duration read: " + (System.nanoTime() - start) / 1_000_000.0 + "ms"); } diff --git a/block-storage/src/test/java/org/lucares/pdb/diskstorage/DiskStorageTest.java b/block-storage/src/test/java/org/lucares/pdb/diskstorage/DiskStorageTest.java index 0ca00fe..583101a 100644 --- a/block-storage/src/test/java/org/lucares/pdb/diskstorage/DiskStorageTest.java +++ b/block-storage/src/test/java/org/lucares/pdb/diskstorage/DiskStorageTest.java @@ -10,24 +10,24 @@ import java.util.concurrent.Executors; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class DiskStorageTest { private static final int BLOCK_SIZE = 512; private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } @@ -38,7 +38,8 @@ public class DiskStorageTest { * * @throws Exception */ - @Test(enabled = false) + @Test + @Disabled public void testFlushingASectorOrABlock() throws Exception { final Path databaseFile = dataDirectory.resolve("db.ds"); Files.deleteIfExists(databaseFile); @@ -92,7 +93,7 @@ public class DiskStorageTest { } } - @Test(enabled = true) + @Test public void testDiskStorage() throws Exception { final Path databaseFile = dataDirectory.resolve("db.ds"); @@ -136,18 +137,20 @@ public class DiskStorageTest { } } - @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + @Test public void testAllocationSmallerThanMinimalBlockSize() throws Exception { - final Path databaseFile = dataDirectory.resolve("db.ds"); + Assertions.assertThrows(IllegalArgumentException.class, () -> { + final Path databaseFile = dataDirectory.resolve("db.ds"); - try (DiskStorage ds = new DiskStorage(databaseFile, dataDirectory)) { + try (DiskStorage ds = new DiskStorage(databaseFile, dataDirectory)) { - final int blockSize = 31; // minimal block size is 32 - ds.allocateBlock(blockSize); - } + final int blockSize = 31; // minimal block size is 32 + ds.allocateBlock(blockSize); + } + }); } - @Test(enabled = true) + @Test public void testAllocateAndFreeSingleBlockInFreeList() throws Exception { final Path databaseFile = dataDirectory.resolve("db.ds"); @@ -158,20 +161,20 @@ public class DiskStorageTest { final long block_40_71 = ds.allocateBlock(blockSize); final long block_72_103 = ds.allocateBlock(blockSize); - Assert.assertEquals(block_8_39, 8); - Assert.assertEquals(block_40_71, 40); - Assert.assertEquals(block_72_103, 72); + Assertions.assertEquals(8, block_8_39); + Assertions.assertEquals(40, block_40_71); + Assertions.assertEquals(72, block_72_103); ds.free(block_40_71, blockSize); // should reuse the block we just freed final long actual_block_40_71 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_40_71, 40); + Assertions.assertEquals(40, actual_block_40_71); } } - @Test(enabled = true) + @Test public void testAllocateAndFreeMultipleBlocksInFreeList() throws Exception { final Path databaseFile = dataDirectory.resolve("db.ds"); @@ -192,23 +195,23 @@ public class DiskStorageTest { // should reuse the first block we just freed // this removes the root node of the free list final long actual_block_40_71 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_40_71, 40); + Assertions.assertEquals(40, actual_block_40_71); // should reuse the second block we just freed final long actual_block_72_103 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_72_103, 72); + Assertions.assertEquals(72, actual_block_72_103); // should reuse the third block we just freed // this removes the last node of the free list final long actual_block_104_135 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_104_135, 104); + Assertions.assertEquals(104, actual_block_104_135); final long block_168_199 = ds.allocateBlock(blockSize); - Assert.assertEquals(block_168_199, 168); + Assertions.assertEquals(168, block_168_199); } } - @Test(enabled = true) + @Test public void testAllocateAndFreeInsertFreeNodeInTheMiddleOfTheFreeList() throws Exception { final Path databaseFile = dataDirectory.resolve("db.ds"); @@ -228,17 +231,17 @@ public class DiskStorageTest { // the first free block is re-used final long actual_block_72_103 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_72_103, block_72_103); + Assertions.assertEquals(block_72_103, actual_block_72_103); final long actual_block_104_135 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_104_135, block_104_135); + Assertions.assertEquals(block_104_135, actual_block_104_135); final long actual_block_136_167 = ds.allocateBlock(blockSize); - Assert.assertEquals(actual_block_136_167, block_136_167); + Assertions.assertEquals(block_136_167, actual_block_136_167); } } - @Test(enabled = true) + @Test public void testAllocateAndFreeMultipleBlocksWithDifferentSizes() throws Exception { final Path databaseFile = dataDirectory.resolve("db.ds"); @@ -256,7 +259,7 @@ public class DiskStorageTest { ds.free(small_block_136_167, blockSizeSmall); final long actual_small_block_136_167 = ds.allocateBlock(blockSizeSmall); - Assert.assertEquals(actual_small_block_136_167, small_block_136_167); + Assertions.assertEquals(small_block_136_167, actual_small_block_136_167); } } diff --git a/block-storage/src/test/java/org/lucares/pdb/map/NodeEntryTest.java b/block-storage/src/test/java/org/lucares/pdb/map/NodeEntryTest.java index d0d39a8..891a9aa 100644 --- a/block-storage/src/test/java/org/lucares/pdb/map/NodeEntryTest.java +++ b/block-storage/src/test/java/org/lucares/pdb/map/NodeEntryTest.java @@ -1,37 +1,34 @@ package org.lucares.pdb.map; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.lucares.pdb.map.NodeEntry.ValueType; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Assertions; -@Test public class NodeEntryTest { - @DataProvider - public Object[][] providerPrefixCompare() { - final List result = new ArrayList<>(); - - result.add(new Object[] { "ab", "abc", -1 }); - result.add(new Object[] { "abb", "abc", -1 }); - result.add(new Object[] { "abc", "abc", 0 }); - result.add(new Object[] { "abcd", "abc", 0 }); - result.add(new Object[] { "abd", "abc", 1 }); - result.add(new Object[] { "abz", "abc", 23 }); - - return result.toArray(Object[][]::new); + public static Stream providerPrefixCompare() { + return Stream.of(// + Arguments.of("ab", "abc", -1), // + Arguments.of("abb", "abc", -1), // + Arguments.of("abc", "abc", 0), // + Arguments.of("abcd", "abc", 0), // + Arguments.of("abd", "abc", 1), // + Arguments.of("abz", "abc", 23) // + ); } - @Test(dataProvider = "providerPrefixCompare") + @ParameterizedTest + @MethodSource("providerPrefixCompare") public void testPrefixCompare(final String key, final String prefix, final int expected) { final NodeEntry nodeEntry = new NodeEntry(ValueType.NODE_POINTER, key.getBytes(StandardCharsets.UTF_8), new byte[0]); final int actual = nodeEntry.compareKeyPrefix(prefix.getBytes(StandardCharsets.UTF_8)); - Assert.assertEquals(actual, expected, key + " ? " + prefix); + Assertions.assertEquals(expected, actual, key + " ? " + prefix); } } diff --git a/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapDiskNodeTest.java b/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapDiskNodeTest.java index 461018f..990e0a7 100644 --- a/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapDiskNodeTest.java +++ b/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapDiskNodeTest.java @@ -6,14 +6,13 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; +import org.junit.jupiter.api.Test; import org.lucares.pdb.diskstorage.DiskBlock; import org.lucares.pdb.map.NodeEntry.ValueType; -import org.testng.Assert; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Assertions; -@Test public class PersistentMapDiskNodeTest { - + @Test public void serializeDeserialize() throws Exception { final List entries = new ArrayList<>(); @@ -31,7 +30,7 @@ public class PersistentMapDiskNodeTest { final PersistentMapDiskNode actualNode = PersistentMapDiskNode.parse(nodeOffset, new DiskBlock(nodeOffset, byteBuffer)); - Assert.assertEquals(actualNode.getEntries(), entries); + Assertions.assertEquals(entries, actualNode.getEntries()); } private static NodeEntry newNode(final ValueType type, final String key, final String value) { diff --git a/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapOfListsOfLongsTest.java b/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapOfListsOfLongsTest.java deleted file mode 100644 index e1ac2d5..0000000 --- a/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapOfListsOfLongsTest.java +++ /dev/null @@ -1,62 +0,0 @@ -//package org.lucares.pdb.map; -// -//import java.io.IOException; -//import java.nio.file.Files; -//import java.nio.file.Path; -// -//import org.lucares.collections.LongList; -//import org.lucares.utils.file.FileUtils; -//import org.testng.Assert; -//import org.testng.annotations.AfterMethod; -//import org.testng.annotations.BeforeMethod; -//import org.testng.annotations.Test; -// -//@Test -//public class PersistentMapOfListsOfLongsTest { -// -// private Path dataDirectory; -// -// @BeforeMethod -// public void beforeMethod() throws IOException { -// dataDirectory = Files.createTempDirectory("pdb"); -// } -// -// @AfterMethod -// public void afterMethod() throws IOException { -// FileUtils.delete(dataDirectory); -// } -// -// public void test() throws IOException { -// -// final String mapFilePrefix = "test"; -// final String keyA = "a"; -// final String keyB = "b"; -// -// final int size = 10; -// final LongList a = LongList.range(0, size); -// a.shuffle(); -// final LongList b = LongList.range(0, size); -// b.shuffle(); -// -// try (PersistentMapOfListsOfLongs map = new PersistentMapOfListsOfLongs<>(dataDirectory, mapFilePrefix, -// PersistentMap.STRING_CODER)) { -// -// for (int i = 0; i < size; i++) { -// map.appendLong(keyA, a.get(i)); -// map.appendLong(keyB, b.get(i)); -// } -// } -// -// try (PersistentMapOfListsOfLongs map = new PersistentMapOfListsOfLongs<>(dataDirectory, mapFilePrefix, -// PersistentMap.STRING_CODER)) { -// -// final LongList actualA = new LongList(); -// map.getLongs(keyA).forEachOrdered(actualA::addAll); -// Assert.assertEquals(actualA, a); -// -// final LongList actualB = new LongList(); -// map.getLongs(keyB).forEachOrdered(actualB::addAll); -// Assert.assertEquals(actualB, b); -// } -// } -//} diff --git a/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapTest.java b/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapTest.java index a06d84c..e33256b 100644 --- a/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapTest.java +++ b/block-storage/src/test/java/org/lucares/pdb/map/PersistentMapTest.java @@ -15,27 +15,27 @@ import java.util.Random; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class PersistentMapTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testSingleValue() throws Exception { final Path file = dataDirectory.resolve("map.db"); final String value = "value1"; @@ -44,20 +44,20 @@ public class PersistentMapTest { try (final PersistentMap map = new PersistentMap<>(file, dataDirectory, PersistentMap.STRING_CODER, PersistentMap.STRING_CODER)) { - Assert.assertNull(map.getValue(key)); + Assertions.assertNull(map.getValue(key)); - Assert.assertNull(map.putValue(key, value)); + Assertions.assertNull(map.putValue(key, value)); - Assert.assertEquals(map.getValue(key), value); + Assertions.assertEquals(value, map.getValue(key)); } try (final PersistentMap map = new PersistentMap<>(file, dataDirectory, PersistentMap.STRING_CODER, PersistentMap.STRING_CODER)) { - Assert.assertEquals(map.getValue(key), value); + Assertions.assertEquals(value, map.getValue(key)); } } - @Test(invocationCount = 1) + @Test public void testManyValues() throws Exception { final Path file = dataDirectory.resolve("map.db"); final var insertedValues = new HashMap(); @@ -74,9 +74,9 @@ public class PersistentMapTest { final String key = nextUUID.toString() + "__" + i; final String value = "long value to waste some bytes " + i + "__" + UUID.randomUUID().toString().repeat(1); - Assert.assertNull(map.getValue(key)); + Assertions.assertNull(map.getValue(key)); - Assert.assertNull(map.putValue(key, value)); + Assertions.assertNull(map.putValue(key, value)); insertedValues.put(key, value); @@ -91,7 +91,7 @@ public class PersistentMapTest { map.print(); } - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " in the " + i + "th iteration"); } } @@ -105,21 +105,21 @@ public class PersistentMapTest { map.visitNodeEntriesPreOrder( (node, parentNode, nodeEntry, depth) -> maxDepth.set(Math.max(depth, maxDepth.get()))); - Assert.assertTrue(maxDepth.get() >= 4, + Assertions.assertTrue(maxDepth.get() >= 4, "The tree's depth. This test must have at least depth 4, " + "so that we can be sure that splitting parent nodes works recursively, but was " + maxDepth.get()); for (final var entry : insertedValues.entrySet()) { final String actualValue = map.getValue(entry.getKey()); - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " after all iterations"); } } } - @Test(invocationCount = 1) + @Test public void testManySmallValues() throws Exception { final Path file = dataDirectory.resolve("map.db"); final var insertedValues = new HashMap(); @@ -135,9 +135,9 @@ public class PersistentMapTest { final Long key = (long) (rnd.nextGaussian() * Integer.MAX_VALUE); final Long value = (long) (rnd.nextGaussian() * Integer.MAX_VALUE); - Assert.assertNull(map.getValue(key)); + Assertions.assertNull(map.getValue(key)); - Assert.assertNull(map.putValue(key, value)); + Assertions.assertNull(map.putValue(key, value)); insertedValues.put(key, value); @@ -152,7 +152,7 @@ public class PersistentMapTest { map.print(); } - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " in the " + i + "th iteration"); } } @@ -166,21 +166,21 @@ public class PersistentMapTest { map.visitNodeEntriesPreOrder( (node, parentNode, nodeEntry, depth) -> counter.addAndGet(nodeEntry.isInnerNode() ? 1 : 0)); - Assert.assertEquals(counter.get(), 4, + Assertions.assertEquals(4, counter.get(), "number of nodes should be small. Any number larger than 4 indicates, " + "that new inner nodes are created even though the existing inner " + "nodes could hold the values"); for (final var entry : insertedValues.entrySet()) { final Long actualValue = map.getValue(entry.getKey()); - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " after all iterations"); } } } - @Test(invocationCount = 1) + @Test public void testManyEmptyValues() throws Exception { final Path file = dataDirectory.resolve("map.db"); final var insertedValues = new HashMap(); @@ -196,9 +196,9 @@ public class PersistentMapTest { final Long key = (long) (rnd.nextGaussian() * Integer.MAX_VALUE); final Empty value = Empty.INSTANCE; - Assert.assertNull(map.getValue(key)); + Assertions.assertNull(map.getValue(key)); - Assert.assertNull(map.putValue(key, value)); + Assertions.assertNull(map.putValue(key, value)); insertedValues.put(key, value); @@ -213,7 +213,7 @@ public class PersistentMapTest { map.print(); } - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " in the " + i + "th iteration"); } } @@ -227,21 +227,21 @@ public class PersistentMapTest { map.visitNodeEntriesPreOrder( (node, parentNode, nodeEntry, depth) -> counter.addAndGet(nodeEntry.isInnerNode() ? 1 : 0)); - Assert.assertEquals(counter.get(), 4, + Assertions.assertEquals(4, counter.get(), "number of nodes should be small. Any number larger than 4 indicates, " + "that new inner nodes are created even though the existing inner " + "nodes could hold the values"); for (final var entry : insertedValues.entrySet()) { final Empty actualValue = map.getValue(entry.getKey()); - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " after all iterations"); } } } - @Test(invocationCount = 1) + @Test public void testEasyValues() throws Exception { final Path file = dataDirectory.resolve("map.db"); final var insertedValues = new HashMap(); @@ -259,9 +259,9 @@ public class PersistentMapTest { final String key = "" + keyNumber; final String value = "value"; - Assert.assertNull(map.getValue(key)); + Assertions.assertNull(map.getValue(key)); - Assert.assertNull(map.putValue(key, value)); + Assertions.assertNull(map.putValue(key, value)); insertedValues.put(key, value); @@ -270,7 +270,7 @@ public class PersistentMapTest { for (final var entry : insertedValues.entrySet()) { final String actualValue = map.getValue(entry.getKey()); - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " in the " + i + "th iteration"); } } @@ -286,7 +286,7 @@ public class PersistentMapTest { for (final var entry : insertedValues.entrySet()) { final String actualValue = map.getValue(entry.getKey()); - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " after all iterations"); } @@ -324,12 +324,12 @@ public class PersistentMapTest { final Visitor visitor = (key, value) -> actualBar.put(key, value); map.visitValues("bar:", visitor); - Assert.assertEquals(actualBar, expectedBar); + Assertions.assertEquals(expectedBar, actualBar); } } } - @Test(invocationCount = 1) + @Test public void testLotsOfValues() throws Exception { final Path file = dataDirectory.resolve("map.db"); final var insertedValues = new HashMap(); @@ -349,7 +349,7 @@ public class PersistentMapTest { continue; } - Assert.assertNull(map.putValue(key, value)); + Assertions.assertNull(map.putValue(key, value)); insertedValues.put(key, value); @@ -362,7 +362,7 @@ public class PersistentMapTest { map.print(); } - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " in the " + i + "th iteration"); } } @@ -381,7 +381,7 @@ public class PersistentMapTest { final long start = System.nanoTime(); for (final var entry : insertedValues.entrySet()) { final Long actualValue = map.getValue(entry.getKey()); - Assert.assertEquals(actualValue, entry.getValue(), + Assertions.assertEquals(entry.getValue(), actualValue, "value for key " + entry.getKey() + " after all iterations"); } System.out.println("nodes=" + counter.get() + ", depth=" + maxDepth.get() + ": " diff --git a/build.gradle b/build.gradle index 5df81f8..c956dbf 100644 --- a/build.gradle +++ b/build.gradle @@ -81,16 +81,19 @@ subprojects { } } - // In this example we use TestNG as our testing tool. JUnit is the default. test{ - useTestNG() - //testLogging.showStandardStreams = true + useJUnitPlatform { + includeEngines 'junit-jupiter' + excludeEngines 'junit-vintage' + } } // dependencies that all sub-projects have dependencies { - testImplementation group: 'org.testng', name: 'testng', version: '6.14.3' - } + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.5.2' // for @ParameterizedTest + testImplementation 'org.junit.platform:junit-platform-launcher:1.5.2' // needed by eclipse + } task eclipseSettings(type: Copy) { from ("${rootProject.projectDir}/eclipse/") { diff --git a/data-store/src/test/java/org/lucares/pdb/datastore/internal/DataStoreTest.java b/data-store/src/test/java/org/lucares/pdb/datastore/internal/DataStoreTest.java index 17c9180..77f707f 100644 --- a/data-store/src/test/java/org/lucares/pdb/datastore/internal/DataStoreTest.java +++ b/data-store/src/test/java/org/lucares/pdb/datastore/internal/DataStoreTest.java @@ -7,22 +7,27 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.Query; import org.lucares.pdb.api.QueryWithCaretMarker; @@ -31,27 +36,22 @@ import org.lucares.pdb.api.Tags; import org.lucares.pdb.blockstorage.BSFile; import org.lucares.pdb.datastore.Doc; import org.lucares.pdb.datastore.Proposal; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.CollectionUtils; import org.lucares.utils.DateUtils; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -@Test public class DataStoreTest { private Path dataDirectory; private DataStore dataStore; private Map tagsToBlockStorageRootBlockNumber; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); dataStore = null; @@ -59,6 +59,7 @@ public class DataStoreTest { Tags.STRING_COMPRESSOR = null; } + @Test public void testQuery() throws Exception { dataStore = new DataStore(dataDirectory); @@ -109,6 +110,7 @@ public class DataStoreTest { } + @Test public void testGetByTags() throws IOException { dataStore = new DataStore(dataDirectory); @@ -121,61 +123,58 @@ public class DataStoreTest { tagsToBlockStorageRootBlockNumber.put(flamingoJennifer, dataStore.createNewFile(partitionId, flamingoJennifer)); final Optional docsFlamingoJennifer = dataStore.getByTags(partitionId, flamingoJennifer); - Assert.assertTrue(docsFlamingoJennifer.isPresent(), "doc for docsFlamingoJennifer"); + Assertions.assertTrue(docsFlamingoJennifer.isPresent(), "doc for docsFlamingoJennifer"); } + @Test public void testBlockAlignment() throws IOException { dataStore = new DataStore(dataDirectory); final Tags eagleTim = Tags.createAndAddToDictionary("bird", "eagle", "name", "Tim"); final long eagleTimBlockOffset = dataStore.createNewFile(new ParititionId("partitionA"), eagleTim); - Assert.assertEquals(eagleTimBlockOffset % BSFile.BLOCK_SIZE, 0); + Assertions.assertEquals(0, eagleTimBlockOffset % BSFile.BLOCK_SIZE); } - @DataProvider(name = "providerProposals") - public Iterator providerProposals() { + public static Stream providerProposals() { - final List result = new ArrayList<>(); + return Stream.of( - result.add(new Object[] { "type=bird and subtype=eagle and name=|", "name", Arrays.asList("Tim") }); + Arguments.of("type=bird and subtype=eagle and name=|", "name", Arrays.asList("Tim")), - // returns Tim, because it is the only dog's name starting with 'Ti' - result.add(new Object[] { "!name=Ti| and type=dog", "name", Arrays.asList("Tim") }); + // returns Tim, because it is the only dog's name starting with 'Ti' + Arguments.of("!name=Ti| and type=dog", "name", Arrays.asList("Tim")), - // all cats - result.add(new Object[] { "type=cat and !name=|", "name", - Arrays.asList("Jane", "John", "Paul", "Sam", "Timothy") }); + // all cats + Arguments.of("type=cat and !name=|", "name", Arrays.asList("Jane", "John", "Paul", "Sam", "Timothy")), - // finds nothing, because there are not dogs names neither Jenny, nor Ti* - result.add(new Object[] { "!name=Ti| and type=dog and !name=Jenny", "name", Arrays.asList() }); + // finds nothing, because there are not dogs names neither Jenny, nor Ti* + Arguments.of("!name=Ti| and type=dog and !name=Jenny", "name", Arrays.asList()), - result.add(new Object[] { "(type=bird and age=three or type=dog and age=three) and name=|", "name", - Arrays.asList("Jenny", "Tim") }); + Arguments.of("(type=bird and age=three or type=dog and age=three) and name=|", "name", + Arrays.asList("Jenny", "Tim")), - // all but Jennifer - result.add(new Object[] { "!(type=bird) and name=|", "name", - Arrays.asList("Jane", "Jenny", "John", "Paul", "Sam", "Tim", "Timothy") }); + // all but Jennifer + Arguments.of("!(type=bird) and name=|", "name", + Arrays.asList("Jane", "Jenny", "John", "Paul", "Sam", "Tim", "Timothy")), - result.add(new Object[] { "type=bird and !subtype=eagle and name=|", "name", Arrays.asList("Jennifer") }); + Arguments.of("type=bird and !subtype=eagle and name=|", "name", Arrays.asList("Jennifer")), - // DeMorgan - // TODO should only match "Jenny", because Jenny is the only non-bird name - // starting with 'Jen' - result.add(new Object[] { "!(type=bird and name=Jen|)", "name", Arrays.asList("Jennifer", "Jenny") }); + // DeMorgan + // TODO should only match "Jenny", because Jenny is the only non-bird name + // starting with 'Jen' + Arguments.of("!(type=bird and name=Jen|)", "name", Arrays.asList("Jennifer", "Jenny")), - result.add(new Object[] { "!(type=dog and name=|) and !type=cat", "name", - Arrays.asList("Jennifer", "Jenny", "Tim") }); + Arguments.of("!(type=dog and name=|) and !type=cat", "name", Arrays.asList("Jennifer", "Jenny", "Tim")), - // not existing field - result.add(new Object[] { "name=| and XYZ=Tim", "name", Arrays.asList() }); + // not existing field + Arguments.of("name=| and XYZ=Tim", "name", Arrays.asList()), - // not existing value - result.add(new Object[] { "name=| and type=XYZ", "name", Arrays.asList() }); - - return result.iterator(); + // not existing value + Arguments.of("name=| and type=XYZ", "name", Arrays.asList())); } - @Test(dataProvider = "providerProposals") + @ParameterizedTest + @MethodSource("providerProposals") public void testProposals(final String queryWithCaret, final String field, final List expectedProposedValues) throws Exception { @@ -202,6 +201,7 @@ public class DataStoreTest { assertProposals(dateRange, queryWithCaret, field, expectedProposedValues); } + @Test public void testIdenticalDatesGoIntoSameFile() throws Exception { try (final DataStore dataStore = new DataStore(dataDirectory)) { @@ -213,7 +213,7 @@ public class DataStoreTest { dataStore.write(timestamp, tags, 1); dataStore.write(timestamp, tags, 2); - Assert.assertEquals(dataStore.sizeWriterCache(), 1, "size of the writer cache"); + Assertions.assertEquals(1, dataStore.sizeWriterCache(), "size of the writer cache"); } } @@ -313,12 +313,12 @@ public class DataStoreTest { final List proposedValues = CollectionUtils.map(proposals, Proposal::getProposedTag); Collections.sort(proposedValues); Collections.sort(expectedProposedValues); - Assert.assertEquals(proposedValues.toString(), expectedProposedValues.toString(), "proposed values:"); + Assertions.assertEquals(expectedProposedValues.toString(), proposedValues.toString(), "proposed values:"); } private void assertQueryFindsResults(final DateTimeRange dateRange, final String query) { final List result = dataStore.search(new Query(query, dateRange)); - Assert.assertFalse(result.isEmpty(), "The query '" + query + "' must return a result, but didn't."); + Assertions.assertFalse(result.isEmpty(), "The query '" + query + "' must return a result, but didn't."); } private void assertSearch(final DateTimeRange dateRange, final String queryString, final Tags... tags) { @@ -328,7 +328,7 @@ public class DataStoreTest { final List expectedPaths = CollectionUtils.map(tags, tagsToBlockStorageRootBlockNumber::get); - Assert.assertEquals(actual, expectedPaths, "Query: " + queryString + " Found: " + actual); + Assertions.assertEquals(expectedPaths, actual, "Query: " + queryString + " Found: " + actual); } } diff --git a/data-store/src/test/java/org/lucares/pdb/datastore/internal/DateIndexExtensionTest.java b/data-store/src/test/java/org/lucares/pdb/datastore/internal/DateIndexExtensionTest.java index 14bc0ed..568c5de 100644 --- a/data-store/src/test/java/org/lucares/pdb/datastore/internal/DateIndexExtensionTest.java +++ b/data-store/src/test/java/org/lucares/pdb/datastore/internal/DateIndexExtensionTest.java @@ -7,53 +7,56 @@ import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.List; import java.util.Set; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.lucares.pdb.api.DateTimeRange; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Assertions; -@Test public class DateIndexExtensionTest { - @DataProvider - public Object[][] provider() { + public static Stream provider() { - final List result = new ArrayList<>(); + final List result = new ArrayList<>(); { final OffsetDateTime start = OffsetDateTime.of(2018, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC); final OffsetDateTime end = OffsetDateTime.of(2018, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC); final Set expected = Set.of("201801"); - result.add(new Object[] { start, end, expected }); + result.add(Arguments.of(start, end, expected)); } { final OffsetDateTime start = OffsetDateTime.of(2017, 11, 1, 0, 0, 0, 0, ZoneOffset.UTC); final OffsetDateTime end = OffsetDateTime.of(2018, 02, 1, 0, 0, 0, 0, ZoneOffset.UTC); final Set expected = Set.of("201711", "201712", "201801", "201802"); - result.add(new Object[] { start, end, expected }); + result.add(Arguments.of(start, end, expected)); } { // check that adding one month to Jan 31 does not skip the February final OffsetDateTime start = OffsetDateTime.of(2018, 1, 31, 0, 0, 0, 0, ZoneOffset.UTC); final OffsetDateTime end = OffsetDateTime.of(2018, 3, 31, 0, 0, 0, 0, ZoneOffset.UTC); final Set expected = Set.of("201801", "201802", "201803"); - result.add(new Object[] { start, end, expected }); + result.add(Arguments.of(start, end, expected)); } - return result.toArray(new Object[0][]); + return result.stream(); } - @Test(dataProvider = "provider") + @ParameterizedTest + @MethodSource("provider") public void test(final OffsetDateTime start, final OffsetDateTime end, final Set expected) { final DateTimeRange dateRange = new DateTimeRange(start, end); final Set actual = DateIndexExtension.toDateIndexPrefix(dateRange); - Assert.assertEquals(actual, expected); + Assertions.assertEquals(expected, actual); } + @Test public void testDateToDateIndexPrefix() { final long mid_201711 = OffsetDateTime.of(2017, 11, 23, 2, 2, 2, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); @@ -62,12 +65,13 @@ public class DateIndexExtensionTest { final long max_201801 = OffsetDateTime.of(2018, 1, 31, 23, 59, 59, 999_999_999, ZoneOffset.UTC).toInstant() .toEpochMilli(); - Assert.assertEquals(DateIndexExtension.toDateIndexPrefix(mid_201712), "201712"); - Assert.assertEquals(DateIndexExtension.toDateIndexPrefix(min_201801), "201801"); - Assert.assertEquals(DateIndexExtension.toDateIndexPrefix(max_201801), "201801"); - Assert.assertEquals(DateIndexExtension.toDateIndexPrefix(mid_201711), "201711"); + Assertions.assertEquals("201712", DateIndexExtension.toDateIndexPrefix(mid_201712)); + Assertions.assertEquals("201801", DateIndexExtension.toDateIndexPrefix(min_201801)); + Assertions.assertEquals("201801", DateIndexExtension.toDateIndexPrefix(max_201801)); + Assertions.assertEquals("201711", DateIndexExtension.toDateIndexPrefix(mid_201711)); } + @Test public void testDateRanges() { final OffsetDateTime mid_201712 = OffsetDateTime.of(2017, 12, 7, 1, 1, 1, 0, ZoneOffset.UTC) .withOffsetSameInstant(ZoneOffset.ofHours(-2)); @@ -82,18 +86,19 @@ public class DateIndexExtensionTest { final List dateIndexPrefixesWithEmptyCache = DateIndexExtension .toPartitionIds(range_201712_201802); - Assert.assertEquals(dateIndexPrefixesWithEmptyCache, - Arrays.asList(new ParititionId("201712"), new ParititionId("201801"), new ParititionId("201802"))); + Assertions.assertEquals(Arrays.asList(new ParititionId("201712"), new ParititionId("201801"), new ParititionId("201802")), + dateIndexPrefixesWithEmptyCache); final List dateIndexPrefixesWithFilledCache = DateIndexExtension .toPartitionIds(range_201712_201801); - Assert.assertEquals(dateIndexPrefixesWithFilledCache, - Arrays.asList(new ParititionId("201712"), new ParititionId("201801"))); + Assertions.assertEquals(Arrays.asList(new ParititionId("201712"), new ParititionId("201801")), + dateIndexPrefixesWithFilledCache); final List dateIndexPrefixesOneMonth = DateIndexExtension.toPartitionIds(range_201712_201712); - Assert.assertEquals(dateIndexPrefixesOneMonth, Arrays.asList(new ParititionId("201712"))); + Assertions.assertEquals(Arrays.asList(new ParititionId("201712")), dateIndexPrefixesOneMonth); } + @Test public void testDateRangeToEpochMilli() { final OffsetDateTime mid_201712 = OffsetDateTime.of(2017, 12, 7, 1, 1, 1, 0, ZoneOffset.ofHours(3)); final OffsetDateTime min_201802 = OffsetDateTime.of(2018, 2, 15, 0, 0, 0, 0, ZoneOffset.ofHours(7)); @@ -104,9 +109,10 @@ public class DateIndexExtensionTest { final List dateIndexEpochMillis = DateIndexExtension .toDateIndexEpochMillis(new DateTimeRange(mid_201712, min_201802)); - Assert.assertEquals(dateIndexEpochMillis, Arrays.asList(exp_201712, exp_201801, exp_201802)); + Assertions.assertEquals(Arrays.asList(exp_201712, exp_201801, exp_201802), dateIndexEpochMillis); } + @Test public void testPerformance() { final long min = OffsetDateTime.of(2010, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); diff --git a/data-store/src/test/java/org/lucares/pdb/datastore/internal/ProposerTest.java b/data-store/src/test/java/org/lucares/pdb/datastore/internal/ProposerTest.java index 8d3f9d7..a94c8dd 100644 --- a/data-store/src/test/java/org/lucares/pdb/datastore/internal/ProposerTest.java +++ b/data-store/src/test/java/org/lucares/pdb/datastore/internal/ProposerTest.java @@ -7,40 +7,39 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.QueryWithCaretMarker; import org.lucares.pdb.api.QueryWithCaretMarker.ResultMode; import org.lucares.pdb.api.Tags; import org.lucares.pdb.datastore.Proposal; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.CollectionUtils; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -@Test public class ProposerTest { - private Path dataDirectory; - private DataStore dataStore; - private DateTimeRange dateRange; + private static Path dataDirectory; + private static DataStore dataStore; + private static DateTimeRange dateRange; - @BeforeClass - public void beforeClass() throws Exception { + @BeforeAll + public static void beforeClass() throws Exception { dataDirectory = Files.createTempDirectory("pdb"); initDatabase(); } - @AfterClass - public void afterClass() throws IOException { + @AfterAll + public static void afterClass() throws IOException { FileUtils.delete(dataDirectory); dataStore.close(); dataStore = null; Tags.STRING_COMPRESSOR = null; } - private void initDatabase() throws Exception { + private static void initDatabase() throws Exception { dataStore = new DataStore(dataDirectory); dateRange = DateTimeRange.now(); final ParititionId now = DateIndexExtension.toPartitionIds(dateRange).get(0); @@ -71,6 +70,7 @@ public class ProposerTest { dataStore.createNewFile(now, methodD); } + @Test public void testEmptyQuery() throws Exception { assertProposals("|", ResultMode.FULL_VALUES, // @@ -90,6 +90,7 @@ public class ProposerTest { ); } + @Test public void testPrefixOfKey() throws Exception { assertProposals("bi|", ResultMode.FULL_VALUES, // new Proposal("bird", "bird=* ", true, "bird=", 5) // @@ -110,6 +111,7 @@ public class ProposerTest { ); } + @Test public void testPrefixOfValue() throws Exception { assertProposals("name =Tim|", ResultMode.FULL_VALUES, // new Proposal("Tim", "name =Tim", true, "name =Tim", 9), @@ -135,7 +137,7 @@ public class ProposerTest { */ } - @Test(enabled = true) + @Test public void testInExpressions() throws Exception { assertProposals("name = (Timothy,|)", ResultMode.FULL_VALUES, // new Proposal("Jennifer", "name = (Timothy,Jennifer)", true, "name = (Timothy,Jennifer)", 24), // @@ -156,6 +158,7 @@ public class ProposerTest { */ } + @Test public void testProposalOnEmptyValuePrefix() throws Exception { assertProposals("name=|", ResultMode.FULL_VALUES, // new Proposal("Jennifer", "name=Jennifer", true, "name=Jennifer", 13), // @@ -182,6 +185,7 @@ public class ProposerTest { ); } + @Test public void testProposalOnValueSmartExpression() throws Exception { assertProposals("method=Foo.|", ResultMode.CUT_AT_DOT, // new Proposal("FooController.doImportantStuff", "method=FooController.doImportantStuff", true, @@ -215,6 +219,7 @@ public class ProposerTest { ); } + @Test public void testProposalOnEmptyKeyPrefix() throws Exception { assertProposals("name=* and |", ResultMode.FULL_VALUES, // proposal("name", "name=* and name=* ", "name=* and name=|"), // @@ -228,6 +233,7 @@ public class ProposerTest { ); } + @Test public void testProposalWithWildcards() throws Exception { assertProposals("name=*im|", ResultMode.FULL_VALUES, // @@ -245,6 +251,7 @@ public class ProposerTest { ); } + @Test public void testProposalWithAndExpression() throws Exception { assertProposals("name=*im| and bird=eagle", ResultMode.FULL_VALUES, // proposal("Tim", "name=Tim and bird=eagle", "name=Tim| and bird=eagle"), // @@ -257,6 +264,7 @@ public class ProposerTest { ); } + @Test public void testProposalWithAndNotExpression() throws Exception { assertProposals("name=Tim and ! dog=labrador and bird=|", ResultMode.FULL_VALUES, // proposal("eagle", "name=Tim and ! dog=labrador and bird=eagle", @@ -292,6 +300,6 @@ public class ProposerTest { System.out.println("\n\n--- " + query + " ---"); System.out.println("actual : " + String.join("\n", CollectionUtils.map(actual, Proposal::toString))); System.out.println("expected: " + String.join("\n", CollectionUtils.map(expectedList, Proposal::toString))); - Assert.assertEquals(actual, expectedList); + Assertions.assertEquals(expectedList, actual); } } diff --git a/data-store/src/test/java/org/lucares/pdb/datastore/internal/QueryCompletionIndexTest.java b/data-store/src/test/java/org/lucares/pdb/datastore/internal/QueryCompletionIndexTest.java index e5ae417..3077479 100644 --- a/data-store/src/test/java/org/lucares/pdb/datastore/internal/QueryCompletionIndexTest.java +++ b/data-store/src/test/java/org/lucares/pdb/datastore/internal/QueryCompletionIndexTest.java @@ -5,34 +5,36 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.List; +import java.util.Set; import java.util.SortedSet; +import java.util.TreeSet; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.StringCompressor; import org.lucares.pdb.api.Tag; import org.lucares.pdb.api.Tags; import org.lucares.pdb.api.UniqueStringIntegerPairs; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class QueryCompletionIndexTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void test() throws Exception { Tags.STRING_COMPRESSOR = new StringCompressor(new UniqueStringIntegerPairs()); @@ -54,20 +56,20 @@ public class QueryCompletionIndexTest { // tags A and B match final SortedSet firstnamesWithLastnameDoe = index.find(dateRange, new Tag("lastname", "Doe"), "firstname"); - Assert.assertEquals(firstnamesWithLastnameDoe, Arrays.asList("Jane", "John")); + Assertions.assertEquals(new TreeSet<>(Set.of("Jane", "John")), firstnamesWithLastnameDoe); // no duplicates are returned: // tags A and C match firstname=John, but both have country=Atlantis final SortedSet countryWithFirstnameJohn = index.find(dateRange, new Tag("firstname", "John"), "country"); - Assert.assertEquals(countryWithFirstnameJohn, Arrays.asList("Atlantis")); + Assertions.assertEquals(new TreeSet<>(Arrays.asList("Atlantis")), countryWithFirstnameJohn); // findAllValuesForField sorts alphabetically final SortedSet firstnames = index.findAllValuesForField(dateRange, "firstname"); - Assert.assertEquals(firstnames, Arrays.asList("Jane", "John"), "found: " + firstnames); + Assertions.assertEquals(new TreeSet<>(Arrays.asList("Jane", "John")), firstnames, "found: " + firstnames); final SortedSet countries = index.findAllValuesForField(dateRange, "country"); - Assert.assertEquals(countries, Arrays.asList("Atlantis", "ElDorado")); + Assertions.assertEquals(new TreeSet<>(Arrays.asList("Atlantis", "ElDorado")), countries); } } } diff --git a/data-store/src/test/java/org/lucares/pdb/datastore/lang/CandidateGrouperTest.java b/data-store/src/test/java/org/lucares/pdb/datastore/lang/CandidateGrouperTest.java index 61b168b..eef216f 100644 --- a/data-store/src/test/java/org/lucares/pdb/datastore/lang/CandidateGrouperTest.java +++ b/data-store/src/test/java/org/lucares/pdb/datastore/lang/CandidateGrouperTest.java @@ -1,58 +1,55 @@ package org.lucares.pdb.datastore.lang; -import java.util.ArrayList; -import java.util.List; import java.util.Set; import java.util.SortedSet; +import java.util.stream.Stream; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Assertions; -@Test public class CandidateGrouperTest { - @DataProvider - public Object[][] providerGroup() { - final List result = new ArrayList<>(); + public static Stream providerGroup() { + return Stream.of( - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // - "name = |", // - Set.of("aa.") }); - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // - "name = a|", // - Set.of("aa.") }); - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // - "name = aa|", // - Set.of("aa.") }); - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // - "name = aa.|", // - Set.of("aa.xx.", "aa.yy.") }); - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.xx.BB", "aa.xx.AA.YY"), // - "name = aa.x|", // - Set.of("aa.xx.") }); - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.xx.BB", "aa.xx.AA.YY"), // - "name = aa.xx.|", // - Set.of("aa.xx.AA.", "aa.xx.BB") }); - result.add(new Object[] { // - Set.of("aa.xx.AA.XX", "aa.xx.AA.YY"), // - "name = aa.xx.AA.|", // - Set.of("aa.xx.AA.XX", "aa.xx.AA.YY") }); - result.add(new Object[] { // - Set.of("XX.YY.ZZ", "XX.YY"), // - "name = XX.Y|", // - Set.of("XX.YY.", "XX.YY") }); - - return result.toArray(new Object[0][]); + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // + "name = |", // + Set.of("aa.")), + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // + "name = a|", // + Set.of("aa.")), + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // + "name = aa|", // + Set.of("aa.")), + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.yy.BB", "aa.xx.BB", "aa.xx.AA.YY"), // + "name = aa.|", // + Set.of("aa.xx.", "aa.yy.")), + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.xx.BB", "aa.xx.AA.YY"), // + "name = aa.x|", // + Set.of("aa.xx.")), + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.xx.BB", "aa.xx.AA.YY"), // + "name = aa.xx.|", // + Set.of("aa.xx.AA.", "aa.xx.BB")), + Arguments.of( // + Set.of("aa.xx.AA.XX", "aa.xx.AA.YY"), // + "name = aa.xx.AA.|", // + Set.of("aa.xx.AA.XX", "aa.xx.AA.YY")), + Arguments.of( // + Set.of("XX.YY.ZZ", "XX.YY"), // + "name = XX.Y|", // + Set.of("XX.YY.", "XX.YY"))); } - @Test(dataProvider = "providerGroup") + @ParameterizedTest + @MethodSource("providerGroup") public void testGroup(final Set values, final String queryWithCaretMarker, final Set expected) { final CandidateGrouper grouper = new CandidateGrouper(); @@ -60,6 +57,6 @@ public class CandidateGrouperTest { final SortedSet actual = grouper.group(values, query); - Assert.assertEquals(actual, expected); + Assertions.assertEquals(expected, actual); } } diff --git a/pdb-api/src/test/java/org/lucares/pdb/api/DateTimeRangeTest.java b/pdb-api/src/test/java/org/lucares/pdb/api/DateTimeRangeTest.java index 77dd994..5ee01ab 100644 --- a/pdb-api/src/test/java/org/lucares/pdb/api/DateTimeRangeTest.java +++ b/pdb-api/src/test/java/org/lucares/pdb/api/DateTimeRangeTest.java @@ -5,33 +5,35 @@ import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Assertions; public class DateTimeRangeTest { - @DataProvider - Object[][] providerIntersect() { - final List result = new ArrayList<>(); + static Stream providerIntersect() { + final List result = new ArrayList<>(); final OffsetDateTime a = Instant.ofEpochMilli(1000).atOffset(ZoneOffset.UTC); final OffsetDateTime b = Instant.ofEpochMilli(2000).atOffset(ZoneOffset.UTC); final OffsetDateTime c = Instant.ofEpochMilli(3000).atOffset(ZoneOffset.UTC); final OffsetDateTime d = Instant.ofEpochMilli(4000).atOffset(ZoneOffset.UTC); - result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(c, d), false }); - result.add(new Object[] { new DateTimeRange(a, c), new DateTimeRange(b, d), true }); - result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true }); - result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true }); - result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(b, d), true }); + result.add(Arguments.of(new DateTimeRange(a, b), new DateTimeRange(c, d), false)); + result.add(Arguments.of(new DateTimeRange(a, c), new DateTimeRange(b, d), true)); + result.add(Arguments.of(new DateTimeRange(a, d), new DateTimeRange(b, d), true)); + result.add(Arguments.of(new DateTimeRange(a, d), new DateTimeRange(b, d), true)); + result.add(Arguments.of(new DateTimeRange(a, b), new DateTimeRange(b, d), true)); - return result.toArray(new Object[result.size()][]); + return result.stream(); } - @Test(dataProvider = "providerIntersect") + @ParameterizedTest + @MethodSource("providerIntersect") public void testIntersect(final DateTimeRange a, final DateTimeRange b, final boolean expected) throws Exception { - Assert.assertEquals(a.intersect(b), expected, a + " intersects " + b); - Assert.assertEquals(b.intersect(a), expected, a + " intersects " + b); + Assertions.assertEquals(expected, a.intersect(b), a + " intersects " + b); + Assertions.assertEquals(expected, b.intersect(a), a + " intersects " + b); } } diff --git a/pdb-api/src/test/java/org/lucares/pdb/api/StringCompressorTest.java b/pdb-api/src/test/java/org/lucares/pdb/api/StringCompressorTest.java index 5fe95ba..03a9b69 100644 --- a/pdb-api/src/test/java/org/lucares/pdb/api/StringCompressorTest.java +++ b/pdb-api/src/test/java/org/lucares/pdb/api/StringCompressorTest.java @@ -10,26 +10,26 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class StringCompressorTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testKeyCompressorRoundtrip() throws Exception { final StringCompressor keyValueCompressor = StringCompressor.create(dataDirectory.resolve("key.csv")); @@ -37,9 +37,10 @@ public class StringCompressorTest { final Integer intFoo = keyValueCompressor.put(value); final String actual = keyValueCompressor.get(intFoo); - Assert.assertEquals(actual, value); + Assertions.assertEquals(value, actual); } + @Test public void testKeyCompressorInitialization() throws Exception { final Path database = dataDirectory.resolve("key.csv"); final String value = "foo"; @@ -56,7 +57,7 @@ public class StringCompressorTest { } - @Test(invocationCount = 1) + @Test public void testPutConcurrently() throws InterruptedException, ExecutionException { final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(); final StringCompressor stringCompressor = new StringCompressor(usip); @@ -75,6 +76,6 @@ public class StringCompressorTest { pool.shutdown(); pool.awaitTermination(1, TimeUnit.MILLISECONDS); - Assert.assertEquals((int) usip.getHighestInteger(), 3 * numEntries - 1); + Assertions.assertEquals(3 * numEntries - 1, (int) usip.getHighestInteger()); } } diff --git a/pdb-api/src/test/java/org/lucares/pdb/api/UniqueStringIntegerPairsTest.java b/pdb-api/src/test/java/org/lucares/pdb/api/UniqueStringIntegerPairsTest.java index 9e0f6e9..cd60616 100644 --- a/pdb-api/src/test/java/org/lucares/pdb/api/UniqueStringIntegerPairsTest.java +++ b/pdb-api/src/test/java/org/lucares/pdb/api/UniqueStringIntegerPairsTest.java @@ -4,27 +4,27 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class UniqueStringIntegerPairsTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testPutGet() throws Exception { final Path database = dataDirectory.resolve("key.csv"); final String first = "key1"; @@ -34,18 +34,19 @@ public class UniqueStringIntegerPairsTest { final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database); usip.put(first, second); - Assert.assertEquals(usip.get(first), second); - Assert.assertEquals(usip.getKey(second), first); + Assertions.assertEquals(second, usip.get(first)); + Assertions.assertEquals(first, usip.getKey(second)); } { final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database); - Assert.assertEquals(usip.get(first), second); - Assert.assertEquals(usip.getKey(second), first); + Assertions.assertEquals(second, usip.get(first)); + Assertions.assertEquals(first, usip.getKey(second)); } } + @Test public void testUniqueKeyContstraint() throws Exception { final Path database = dataDirectory.resolve("key.csv"); final String first = "key1"; @@ -57,7 +58,7 @@ public class UniqueStringIntegerPairsTest { // cannot add another pair with the first key final int another = second + 1; usip.put(first, another); - Assert.fail("expected an IllegalArgumentException"); + Assertions.fail("expected an IllegalArgumentException"); } catch (final IllegalArgumentException e) { // expected } @@ -66,7 +67,7 @@ public class UniqueStringIntegerPairsTest { // cannot add another pair with the same second value final String another = first + 1; usip.put(another, second); - Assert.fail("expected an IllegalArgumentException"); + Assertions.fail("expected an IllegalArgumentException"); } catch (final IllegalArgumentException e) { // expected } diff --git a/pdb-ui/build.gradle b/pdb-ui/build.gradle index f14a1b2..79c40a1 100644 --- a/pdb-ui/build.gradle +++ b/pdb-ui/build.gradle @@ -25,6 +25,7 @@ dependencies { testImplementation(lib_spring_boot_test){ exclude module: 'spring-boot-starter-logging' + exclude module: 'junit' } } diff --git a/pdb-ui/src/test/java/org/lucares/pdbui/CsvToEntryTransformerTest.java b/pdb-ui/src/test/java/org/lucares/pdbui/CsvToEntryTransformerTest.java index 133eb9d..989160e 100644 --- a/pdb-ui/src/test/java/org/lucares/pdbui/CsvToEntryTransformerTest.java +++ b/pdb-ui/src/test/java/org/lucares/pdbui/CsvToEntryTransformerTest.java @@ -11,32 +11,32 @@ import java.time.temporal.ChronoUnit; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.lucares.collections.LongList; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.Entries; import org.lucares.pdb.api.Query; import org.lucares.performance.db.PerformanceDb; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -@Test public class CsvToEntryTransformerTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testIngest() throws IOException, InterruptedException { final OffsetDateTime dateA = OffsetDateTime.now(); final OffsetDateTime dateB = OffsetDateTime.now(); @@ -56,13 +56,13 @@ public class CsvToEntryTransformerTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final LongList result = db.get(new Query("tag=tagValue", DateTimeRange.max())).singleGroup().flatMap(); - Assert.assertEquals(4, result.size()); + Assertions.assertEquals(result.size(), 4); - Assert.assertEquals(dateA.toInstant().toEpochMilli(), result.get(0)); - Assert.assertEquals(1, result.get(1)); + Assertions.assertEquals(result.get(0), dateA.toInstant().toEpochMilli()); + Assertions.assertEquals(result.get(1), 1); - Assert.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2)); - Assert.assertEquals(2, result.get(3)); + Assertions.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli()); + Assertions.assertEquals(result.get(3), 2); } } @@ -76,6 +76,7 @@ public class CsvToEntryTransformerTest { * @throws IOException * @throws InterruptedException */ + @Test public void testIgnoreColumns() throws IOException, InterruptedException { try (final PerformanceDb db = new PerformanceDb(dataDirectory)) { @@ -93,7 +94,7 @@ public class CsvToEntryTransformerTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final List availableFields = db.getFields(DateTimeRange.max()); - Assert.assertEquals(availableFields.toString(), List.of("tag").toString(), + Assertions.assertEquals(List.of("tag").toString(), availableFields.toString(), "the ignored field is not returned"); } } diff --git a/pdb-ui/src/test/java/org/lucares/pdbui/PdbControllerTest.java b/pdb-ui/src/test/java/org/lucares/pdbui/PdbControllerTest.java index aaf562b..eedb2ae 100644 --- a/pdb-ui/src/test/java/org/lucares/pdbui/PdbControllerTest.java +++ b/pdb-ui/src/test/java/org/lucares/pdbui/PdbControllerTest.java @@ -4,15 +4,18 @@ import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.List; +import java.util.concurrent.TimeUnit; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.lucares.collections.LongList; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.GroupResult; import org.lucares.pdb.api.Query; import org.lucares.performance.db.PerformanceDb; +import org.junit.jupiter.api.Assertions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -24,15 +27,19 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @ContextConfiguration(initializers = TestOverrides.class) @SpringBootTest(classes = MySpringTestConfiguration.class, webEnvironment = WebEnvironment.RANDOM_PORT) public class PdbControllerTest { + static { + Configurator.setRootLevel(Level.INFO); + } + @Autowired private PerformanceDb performanceDb; @@ -40,7 +47,7 @@ public class PdbControllerTest { private TestRestTemplate rest; @Test - public void testUploadCsv() { + public void testUploadCsv() throws InterruptedException { final String ignoredColumn = "ignoredColumn"; final String timeColumn = "time"; @@ -53,22 +60,23 @@ public class PdbControllerTest { final CsvReaderSettings settings = CsvReaderSettings.create(timeColumn, ',', ignoredColumn); uploadCsv(settings, csv); + TimeUnit.SECONDS.sleep(1); { final GroupResult groupResult = performanceDb.get(new Query("tag=tagValue", DateTimeRange.ofDay(dateA))) .singleGroup(); final LongList result = groupResult.flatMap(); System.out.println(PdbTestUtil.timeValueLongListToString(result)); - Assert.assertEquals(result.size(), 4); + Assertions.assertEquals(4, result.size()); - Assert.assertEquals(result.get(0), dateA.toInstant().toEpochMilli()); - Assert.assertEquals(result.get(1), 1); + Assertions.assertEquals(dateA.toInstant().toEpochMilli(), result.get(0)); + Assertions.assertEquals(1, result.get(1)); - Assert.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli()); - Assert.assertEquals(result.get(3), 2); + Assertions.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2)); + Assertions.assertEquals(2, result.get(3)); } { final List fields = performanceDb.getFields(DateTimeRange.max()); - Assert.assertTrue("ignoredColumn not in fields. fields: " + fields, !fields.contains(ignoredColumn)); + Assertions.assertTrue(!fields.contains(ignoredColumn), "ignoredColumn not in fields. fields: " + fields); } } @@ -90,7 +98,7 @@ public class PdbControllerTest { final ResponseEntity response = rest.exchange("/data", HttpMethod.POST, entity, String.class); - Assert.assertEquals("response status", HttpStatus.CREATED, response.getStatusCode()); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED, "response status"); } } diff --git a/pdb-ui/src/test/java/org/lucares/pdbui/TcpIngestorTest.java b/pdb-ui/src/test/java/org/lucares/pdbui/TcpIngestorTest.java index 43e0054..8a72d2b 100644 --- a/pdb-ui/src/test/java/org/lucares/pdbui/TcpIngestorTest.java +++ b/pdb-ui/src/test/java/org/lucares/pdbui/TcpIngestorTest.java @@ -8,7 +8,6 @@ import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -17,40 +16,41 @@ import java.util.Map; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadLocalRandom; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.lucares.collections.LongList; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.Query; import org.lucares.pdb.datastore.internal.DataStore; import org.lucares.performance.db.PdbExport; import org.lucares.performance.db.PerformanceDb; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.file.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; import com.fasterxml.jackson.databind.ObjectMapper; -@Test public class TcpIngestorTest { private static final Logger LOGGER = LoggerFactory.getLogger(TcpIngestorTest.class); private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { FileUtils.delete(dataDirectory); } + @Test public void testIngestDataViaTcpStream() throws Exception { final OffsetDateTime dateA = OffsetDateTime.now(); @@ -82,16 +82,17 @@ public class TcpIngestorTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final LongList result = db.get(new Query("host=" + host, DateTimeRange.ofDay(dateA))).singleGroup() .flatMap(); - Assert.assertEquals(result.size(), 4); + Assertions.assertEquals(4, result.size()); - Assert.assertEquals(result.get(0), dateA.toInstant().toEpochMilli()); - Assert.assertEquals(result.get(1), 1); + Assertions.assertEquals(dateA.toInstant().toEpochMilli(), result.get(0)); + Assertions.assertEquals(1, result.get(1)); - Assert.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli()); - Assert.assertEquals(result.get(3), 2); + Assertions.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2)); + Assertions.assertEquals(2, result.get(3)); } } + @Test public void testIngestDataViaTcpStream_CustomFormat() throws Exception { final long dateA = Instant.now().toEpochMilli(); @@ -137,16 +138,16 @@ public class TcpIngestorTest { // 5. check that the data is correctly inserted try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final LongList result = db.get(new Query("host=" + host, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(result.size(), 6); + Assertions.assertEquals(6, result.size()); - Assert.assertEquals(result.get(0), dateA); - Assert.assertEquals(result.get(1), 1); + Assertions.assertEquals(dateA, result.get(0)); + Assertions.assertEquals(1, result.get(1)); - Assert.assertEquals(result.get(2), dateC); - Assert.assertEquals(result.get(3), 3); + Assertions.assertEquals(dateC, result.get(2)); + Assertions.assertEquals(3, result.get(3)); - Assert.assertEquals(result.get(4), dateB); - Assert.assertEquals(result.get(5), 2); + Assertions.assertEquals(dateB, result.get(4)); + Assertions.assertEquals(2, result.get(5)); } } @@ -190,27 +191,18 @@ public class TcpIngestorTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final LongList result = db.get(new Query("host=" + host, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(result.size(), 4); + Assertions.assertEquals(4, result.size()); - Assert.assertEquals(result.get(0), dateA.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli()); - Assert.assertEquals(result.get(1), -1); + Assertions.assertEquals(dateA.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(0)); + Assertions.assertEquals(-1, result.get(1)); - Assert.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli()); - Assert.assertEquals(result.get(3), 2); + Assertions.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2)); + Assertions.assertEquals(2, result.get(3)); } } - @DataProvider - public Object[][] providerSendingFormats() { - final List data = new ArrayList<>(); - - data.add(new Object[] { "csv" }); - data.add(new Object[] { "json" }); - - return data.toArray(Object[][]::new); - } - - @Test(dataProvider = "providerSendingFormats") + @ParameterizedTest + @ValueSource(strings = { "csv", "json" }) public void testRandomOrder(final String format) throws Exception { final ThreadLocalRandom rnd = ThreadLocalRandom.current(); @@ -252,10 +244,11 @@ public class TcpIngestorTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final LongList result = db.get(new Query("host=" + host, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(LongPair.fromLongList(result), LongPair.fromLongList(expected)); + Assertions.assertEquals(LongPair.fromLongList(expected), LongPair.fromLongList(result)); } } + @Test public void testCsvIngestorIgnoresColumns() throws Exception { try (TcpIngestor ingestor = new TcpIngestor(dataDirectory)) { @@ -277,11 +270,12 @@ public class TcpIngestorTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final List availableFields = db.getFields(DateTimeRange.max()); - Assert.assertEquals(availableFields.toString(), List.of("host").toString(), + Assertions.assertEquals(List.of("host").toString(), availableFields.toString(), "the ignored field is not returned"); } } + @Test public void testCsvIngestorHandlesDurationAtEnd() throws Exception { final String host = "someHost"; @@ -311,10 +305,10 @@ public class TcpIngestorTest { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final LongList result = db.get(new Query("host=" + host, DateTimeRange.max())).singleGroup().flatMap(); - Assert.assertEquals(result.size(), 4); + Assertions.assertEquals(4, result.size()); - Assert.assertEquals(result.get(1), value1); - Assert.assertEquals(result.get(3), value2); + Assertions.assertEquals(value1, result.get(1)); + Assertions.assertEquals(value2, result.get(3)); } } } diff --git a/pdb-ui/src/test/java/org/lucares/pdbui/date/FastISODateParserTest.java b/pdb-ui/src/test/java/org/lucares/pdbui/date/FastISODateParserTest.java index b2d43de..a6161d4 100644 --- a/pdb-ui/src/test/java/org/lucares/pdbui/date/FastISODateParserTest.java +++ b/pdb-ui/src/test/java/org/lucares/pdbui/date/FastISODateParserTest.java @@ -5,197 +5,186 @@ import java.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.time.Instant; import java.time.OffsetDateTime; -import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.api.Assertions; -@Test public class FastISODateParserTest { - @DataProvider(name = "providerValidDate") - public Object[][] providerValidDate() { - return new Object[][] { // - { "2018-11-18T14:42:49.123456789Z" }, // - { "2018-11-18T14:42:49.123456789+12:34" }, // - { "2018-11-18T14:42:49.12345678Z" }, // - { "2018-11-18T14:42:49.12345678+12:34" }, // - { "2018-11-18T14:42:49.1234567Z" }, // - { "2018-11-18T14:42:49.1234567+12:34" }, // - { "2018-11-18T14:42:49.123456Z" }, // - { "2018-11-18T14:42:49.123456+12:34" }, // - { "2018-11-18T14:42:49.33256Z" }, // - { "2018-11-18T14:42:49.33256+12:34" }, // - { "2018-11-18T14:42:49.3325Z" }, // - { "2018-11-18T14:42:49.3325+12:34" }, // - { "2018-11-18T14:42:49.332Z" }, // - { "2018-11-18T14:42:49.332+00:00" }, // - { "2018-11-18T14:42:49.332+12:34" }, // - { "2018-11-18T14:42:49.332-01:23" }, // - { "2018-11-18T14:55:49.44Z" }, // - { "2018-11-18T14:55:49.55-01:23" }, // - { "2018-11-18T14:55:49.4Z" }, // - { "2018-11-18T14:55:49.5-01:23" }, // - { "2018-11-18T14:55:49.Z" }, // - { "2018-11-18T14:55:49.-01:23" }, // - { "2018-11-18T14:55:49Z" }, // - { "2018-11-18T14:55:49-01:23" },// - }; + public static Stream providerValidDate() { + return Stream.of( // + Arguments.of("2018-11-18T14:42:49.123456789Z"), // + Arguments.of("2018-11-18T14:42:49.123456789+12:34"), // + Arguments.of("2018-11-18T14:42:49.12345678Z"), // + Arguments.of("2018-11-18T14:42:49.12345678+12:34"), // + Arguments.of("2018-11-18T14:42:49.1234567Z"), // + Arguments.of("2018-11-18T14:42:49.1234567+12:34"), // + Arguments.of("2018-11-18T14:42:49.123456Z"), // + Arguments.of("2018-11-18T14:42:49.123456+12:34"), // + Arguments.of("2018-11-18T14:42:49.33256Z"), // + Arguments.of("2018-11-18T14:42:49.33256+12:34"), // + Arguments.of("2018-11-18T14:42:49.3325Z"), // + Arguments.of("2018-11-18T14:42:49.3325+12:34"), // + Arguments.of("2018-11-18T14:42:49.332Z"), // + Arguments.of("2018-11-18T14:42:49.332+00:00"), // + Arguments.of("2018-11-18T14:42:49.332+12:34"), // + Arguments.of("2018-11-18T14:42:49.332-01:23"), // + Arguments.of("2018-11-18T14:55:49.44Z"), // + Arguments.of("2018-11-18T14:55:49.55-01:23"), // + Arguments.of("2018-11-18T14:55:49.4Z"), // + Arguments.of("2018-11-18T14:55:49.5-01:23"), // + Arguments.of("2018-11-18T14:55:49.Z"), // + Arguments.of("2018-11-18T14:55:49.-01:23"), // + Arguments.of("2018-11-18T14:55:49Z"), // + Arguments.of("2018-11-18T14:55:49-01:23") // + ); + } - @Test(dataProvider = "providerValidDate") + @ParameterizedTest + @MethodSource("providerValidDate") public void testParseValidDate(final String date) { final OffsetDateTime actualDate = new FastISODateParser().parse(date); final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date)); - Assert.assertEquals(actualDate, expectedDate); + Assertions.assertEquals(expectedDate, actualDate, "date: " + date); } - @Test(dataProvider = "providerValidDate") + @ParameterizedTest + @MethodSource("providerValidDate") public void testParseValidDateAsEpochMilli(final String date) { final long actualDate = new FastISODateParser().parseAsEpochMilli(date); final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date)); - Assert.assertEquals(actualDate, expectedDate.toInstant().toEpochMilli()); + Assertions.assertEquals(expectedDate.toInstant().toEpochMilli(), actualDate, "date: " + date); } - @DataProvider(name = "providerPseudoISODate") - public Object[][] providerPseudoISODate() { - return new Object[][] { // - { "2018-11-18T14:42:49,123Z", "2018-11-18T14:42:49.123Z" }, // with comman instead of dot - { "2018-11-18T14:42:49,123+12:34", "2018-11-18T14:42:49.123+12:34" }, // with comman instead of dot - { "2018-11-18T14:42:49.123", "2018-11-18T14:42:49.123Z" }, // without timezone - { "2018-11-18T14:42:49,123", "2018-11-18T14:42:49.123Z" }, // with command, without timezone - }; + public static Stream providerPseudoISODate() { + return Stream.of( // + Arguments.of("2018-11-18T14:42:49,123Z", "2018-11-18T14:42:49.123Z"), // with comma instead of dot + Arguments.of("2018-11-18T14:42:49,123+12:34", "2018-11-18T14:42:49.123+12:34"), // with comma instead of + // dot + Arguments.of("2018-11-18T14:42:49.123", "2018-11-18T14:42:49.123Z"), // without timezone + Arguments.of("2018-11-18T14:42:49,123", "2018-11-18T14:42:49.123Z") // with command, without timezone + ); } - @Test(dataProvider = "providerPseudoISODate") + @ParameterizedTest + @MethodSource("providerPseudoISODate") public void testParsePseudoISODate(final String date, final String expectedDate) { - final OffsetDateTime actualDate = new FastISODateParser().parse(date); - final OffsetDateTime expected = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(expectedDate)); - Assert.assertEquals(actualDate, expected); + Assertions.assertEquals(expected, actualDate); } - @Test(dataProvider = "providerPseudoISODate") + @ParameterizedTest + @MethodSource("providerPseudoISODate") public void testParsePseudoISODateAsEpochMilli(final String date, final String expectedDate) { final long actualDate = new FastISODateParser().parseAsEpochMilli(date); final OffsetDateTime expected = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(expectedDate)); - Assert.assertEquals(actualDate, expected.toInstant().toEpochMilli()); + Assertions.assertEquals(expected.toInstant().toEpochMilli(), actualDate); } - @DataProvider(name = "providerParseInvalidDate") - public Object[][] providerParseInvalidDate() { - return new Object[][] { // - { "a2018-11-18T14:42:49.332Z" }, // - { "a018-11-18T14:42:49.332Z" }, // - { "2a18-11-18T14:42:49.332Z" }, // - { "20a8-11-18T14:42:49.332Z" }, // - { "201a-11-18T14:42:49.332Z" }, // - { "2018-a1-18T14:42:49.332Z" }, // - { "2018-1a-18T14:42:49.332Z" }, // - { "2018-11-a8T14:42:49.332Z" }, // - { "2018-11-1aT14:42:49.332Z" }, // - { "2018-11-18Ta4:42:49.332Z" }, // - { "2018-11-18T1a:42:49.332Z" }, // - { "2018-11-18T14:a2:49.332Z" }, // - { "2018-11-18T14:4a:49.332Z" }, // - { "2018-11-18T14:42:a9.332Z" }, // - { "2018-11-18T14:42:4a.332Z" }, // - { "2018-11-18T14:42:49.a32Z" }, // - { "2018-11-18T14:42:49.3a2Z" }, // - { "2018-11-18T14:42:49.33aZ" }, // - { "2018-11-18T14:42:49.332a" }, // - { "2018-11-18T14:42:49.332a00:00" }, // - { "2018-11-18T14:42:49.332+a0:00" }, // - { "2018-11-18T14:42:49.332+0a:00" }, // - { "2018-11-18T14:42:49.332+00:a0" }, // - { "2018-11-18T14:42:49.332+00:0a" }// - }; + public static Stream providerParseInvalidDate() { + return Stream.of( // + Arguments.of("a2018-11-18T14:42:49.332Z"), // + Arguments.of("a018-11-18T14:42:49.332Z"), // + Arguments.of("2a18-11-18T14:42:49.332Z"), // + Arguments.of("20a8-11-18T14:42:49.332Z"), // + Arguments.of("201a-11-18T14:42:49.332Z"), // + Arguments.of("2018-a1-18T14:42:49.332Z"), // + Arguments.of("2018-1a-18T14:42:49.332Z"), // + Arguments.of("2018-11-a8T14:42:49.332Z"), // + Arguments.of("2018-11-1aT14:42:49.332Z"), // + Arguments.of("2018-11-18Ta4:42:49.332Z"), // + Arguments.of("2018-11-18T1a:42:49.332Z"), // + Arguments.of("2018-11-18T14:a2:49.332Z"), // + Arguments.of("2018-11-18T14:4a:49.332Z"), // + Arguments.of("2018-11-18T14:42:a9.332Z"), // + Arguments.of("2018-11-18T14:42:4a.332Z"), // + Arguments.of("2018-11-18T14:42:49.a32Z"), // + Arguments.of("2018-11-18T14:42:49.3a2Z"), // + Arguments.of("2018-11-18T14:42:49.33aZ"), // + Arguments.of("2018-11-18T14:42:49.332a"), // + Arguments.of("2018-11-18T14:42:49.332a00:00"), // + Arguments.of("2018-11-18T14:42:49.332+a0:00"), // + Arguments.of("2018-11-18T14:42:49.332+0a:00"), // + Arguments.of("2018-11-18T14:42:49.332+00:a0"), // + Arguments.of("2018-11-18T14:42:49.332+00:0a") // + ); } - @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "providerParseInvalidDate") + @ParameterizedTest + @MethodSource("providerParseInvalidDate") public void testParseInvalidDate(final String invalidDate) { - new FastISODateParser().parse(invalidDate); + try { + new FastISODateParser().parse(invalidDate); + Assertions.fail("expected illegalArgumentException for input: " + invalidDate); + } catch (final IllegalArgumentException e) { + // expected + } } - @DataProvider(name = "providerDateToTimestamp") - public Object[][] providerDateToTimestamp() { - return new Object[][] { // - { "2018-11-18T14:42:49.123Z" }, // + public static Stream providerDateToTimestamp() { + return Stream.of( // + Arguments.of("2018-11-18T14:42:49.123Z"), // // There are no leap seconds in java-time: // In reality, UTC has a leap second 2016-12-31T23:59:60Z, but java handles // this differently. This makes it a little bit easier for us, because we do not // have to handle this. - { "2016-12-31T23:59:59.999Z" }, // before leap second - { "2017-01-01T00:00:00.000Z" }, // after leap second + Arguments.of("2016-12-31T23:59:59.999Z"), // before leap second + Arguments.of("2017-01-01T00:00:00.000Z"), // after leap second // normal leap days exist - { "2016-02-28T23:59:59.999Z" }, // before leap day - { "2016-02-29T00:00:00.000Z" }, // leap day - { "2016-02-29T23:59:59.999Z" }, // leap day - { "2016-03-01T00:00:00.000Z" }, // after leap day + Arguments.of("2016-02-28T23:59:59.999Z"), // before leap day + Arguments.of("2016-02-29T00:00:00.000Z"), // leap day + Arguments.of("2016-02-29T23:59:59.999Z"), // leap day + Arguments.of("2016-03-01T00:00:00.000Z"), // after leap day // dates with non-UTC timezones - { "2018-11-18T14:42:49.123+12:34" }, // - { "2018-11-18T02:34:56.123+12:34" }, // + Arguments.of("2018-11-18T14:42:49.123+12:34"), // + Arguments.of("2018-11-18T02:34:56.123+12:34"), // // dates with non-UTC timezones and leap days - { "2016-02-29T00:59:59.999+01:00" }, // before leap day - { "2016-02-29T01:00:00.000+01:00" }, // leap day - { "2016-03-01T00:59:59.999+01:00" }, // leap day - { "2016-03-01T01:00:00.000+01:00" }, // after leap day - }; + Arguments.of("2016-02-29T00:59:59.999+01:00"), // before leap day + Arguments.of("2016-02-29T01:00:00.000+01:00"), // leap day + Arguments.of("2016-03-01T00:59:59.999+01:00"), // leap day + Arguments.of("2016-03-01T01:00:00.000+01:00") // after leap day + ); } - @Test(dataProvider = "providerDateToTimestamp") + @ParameterizedTest + @MethodSource("providerDateToTimestamp") public void testDateToTimestamp(final String date) { final long actualEpochMilli = new FastISODateParser().parseAsEpochMilli(date); final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date)); final long expectedEpochMilli = expectedDate.toInstant().toEpochMilli(); - Assert.assertEquals(actualEpochMilli, expectedEpochMilli); + Assertions.assertEquals(expectedEpochMilli, actualEpochMilli, "date: " + date); } - @Test(dataProvider = "providerDateToTimestamp") + @ParameterizedTest + @MethodSource("providerDateToTimestamp") public void testDateToTimestampWithBytes(final String date) { - final byte[] dateAsBytes = date.getBytes(StandardCharsets.UTF_8); final long actualEpochMilli = new FastISODateParser().parseAsEpochMilli(dateAsBytes, 0); final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date)); final long expectedEpochMilli = expectedDate.toInstant().toEpochMilli(); - Assert.assertEquals(actualEpochMilli, expectedEpochMilli); - } - - @Test(enabled = false) - public void test() { - - final OffsetDateTime expectedDate = OffsetDateTime - .from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-12-31T23:00:00.000Z")); - - final long epochMilli = expectedDate.toInstant().toEpochMilli(); - - for (int i = 0; i < 1000; i++) { - - final long timestamp = epochMilli + i * 10000; - final OffsetDateTime date = Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.UTC); - - System.out.println(timestamp + " " + date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); - } + Assertions.assertEquals(expectedEpochMilli, actualEpochMilli, "date: " + date); } public static void main(final String[] args) throws IOException, InterruptedException { diff --git a/pdb-ui/src/test/java/org/lucares/pdbui/domain/DataSeriesStatsTest.java b/pdb-ui/src/test/java/org/lucares/pdbui/domain/DataSeriesStatsTest.java index d8a7bf9..aa525ad 100644 --- a/pdb-ui/src/test/java/org/lucares/pdbui/domain/DataSeriesStatsTest.java +++ b/pdb-ui/src/test/java/org/lucares/pdbui/domain/DataSeriesStatsTest.java @@ -4,30 +4,30 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.stream.Stream; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -@Test public class DataSeriesStatsTest { - @DataProvider - public Object[][] providerAverage() { - final List result = new ArrayList<>(); + public static Stream providerAverage() { + final List result = new ArrayList<>(); { final List stats = Arrays.asList(// new DataSeriesStats(10, 0, 0, 5.0)// ); final double expected = 5.0; - result.add(new Object[] { stats, expected }); + result.add(Arguments.of(stats, expected)); } { final List stats = Arrays.asList(// new DataSeriesStats(0, 0, 0, 5.0)// ); final double expected = 0.0; // no values - result.add(new Object[] { stats, expected }); + result.add(Arguments.of(stats, expected)); } { @@ -36,7 +36,7 @@ public class DataSeriesStatsTest { new DataSeriesStats(40, 0, 0, 1.0)// ); final double expected = 1.8; // 90 / 50 - result.add(new Object[] { stats, expected }); + result.add(Arguments.of(stats, expected)); } { final List stats = Arrays.asList(// @@ -45,16 +45,17 @@ public class DataSeriesStatsTest { new DataSeriesStats(20, 0, 0, 2.0)// ); final double expected = 3.0; // (35+40) / 25 - result.add(new Object[] { stats, expected }); + result.add(Arguments.of(stats, expected)); } - return result.toArray(new Object[0][]); + return result.stream(); } - @Test(dataProvider = "providerAverage") + @ParameterizedTest + @MethodSource("providerAverage") public void testAverage(final Collection stats, final double expected) { final double actual = DataSeriesStats.average(stats); - Assert.assertEquals(actual, expected, 0.01); + Assertions.assertEquals(actual, expected, 0.01); } } diff --git a/pdb-utils/src/test/java/org/lucares/utils/byteencoder/VariableByteEncoderTest.java b/pdb-utils/src/test/java/org/lucares/utils/byteencoder/VariableByteEncoderTest.java index 034847e..7bc8e38 100644 --- a/pdb-utils/src/test/java/org/lucares/utils/byteencoder/VariableByteEncoderTest.java +++ b/pdb-utils/src/test/java/org/lucares/utils/byteencoder/VariableByteEncoderTest.java @@ -1,39 +1,37 @@ package org.lucares.utils.byteencoder; -import static org.testng.Assert.assertEquals; - import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.lucares.collections.LongList; -import org.lucares.utils.byteencoder.VariableByteEncoder; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -@Test public class VariableByteEncoderTest { - @DataProvider - public Object[][] providerEncodeDecode() { - return new Object[][] { // + public static Stream providerEncodeDecode() { + return Stream.of( // // encoded into 1 byte - { 10, -5, 5 }, // - { 10, 0, 5 }, // - { 10, -63, 63 }, // + Arguments.of(10, -5, 5), // + Arguments.of(10, 0, 5), // + Arguments.of(10, -63, 63), // // encoded into 2 bytes - { 10, 130, 131 }, // + Arguments.of(10, 130, 131), // // encoded into 3 bytes - { 10, -8191, 8191 }, // + Arguments.of(10, -8191, 8191), // // encoded into n bytes - { 1, Long.MAX_VALUE / 2 - 4, Long.MAX_VALUE / 2 }, // - { 1, Long.MIN_VALUE / 2, Long.MAX_VALUE / 2 }, // - { 11, Long.MIN_VALUE / 2 + 1, Long.MIN_VALUE / 2 + 3 }, // - { 12, Long.MAX_VALUE / 2 - 3, Long.MAX_VALUE / 2 },// - }; + Arguments.of(1, Long.MAX_VALUE / 2 - 4, Long.MAX_VALUE / 2), // + Arguments.of(1, Long.MIN_VALUE / 2, Long.MAX_VALUE / 2), // + Arguments.of(11, Long.MIN_VALUE / 2 + 1, Long.MIN_VALUE / 2 + 3), // + Arguments.of(12, Long.MAX_VALUE / 2 - 3, Long.MAX_VALUE / 2)// + ); } - @Test(dataProvider = "providerEncodeDecode") + @ParameterizedTest + @MethodSource("providerEncodeDecode") public void testEncodeDecode(final long numValues, final long minValue, final long maxValue) { final LongList originalValues = new LongList(); @@ -48,63 +46,63 @@ public class VariableByteEncoderTest { final LongList actualValues = VariableByteEncoder.decode(buffer); - assertEquals(actualValues.toString(), originalValues.toString()); + Assertions.assertEquals(originalValues.toString(), actualValues.toString()); } - @DataProvider - public Object[][] providerEncodeDecodeOfTwoValues() { - return new Object[][] { // - { 12345, 67890, false, 1 }, // first value needs three bytes, it does not fit - { 12345, 67890, false, 2 }, // first value needs three bytes, it does not fit - { 12345, 67890, false, 3 }, // first value needs three bytes, second value does not fit - { 12345, 67890, false, 4 }, // first value needs three bytes, second value does not fit - { 12345, 67890, false, 5 }, // first value needs three bytes, second value does not fit - { 12345, 67890, true, 6 }, // both values need three bytes - { 12345, 67890, true, 10 }, // - }; + public static Stream providerEncodeDecodeOfTwoValues() { + return Stream.of( // + Arguments.of(12345, 67890, false, 1), // first value needs three bytes, it does not fit + Arguments.of(12345, 67890, false, 2), // first value needs three bytes, it does not fit + Arguments.of(12345, 67890, false, 3), // first value needs three bytes, second value does not fit + Arguments.of(12345, 67890, false, 4), // first value needs three bytes, second value does not fit + Arguments.of(12345, 67890, false, 5), // first value needs three bytes, second value does not fit + Arguments.of(12345, 67890, true, 6), // both values need three bytes + Arguments.of(12345, 67890, true, 10) // + ); } - @Test(dataProvider = "providerEncodeDecodeOfTwoValues") + @ParameterizedTest + @MethodSource("providerEncodeDecodeOfTwoValues") public void testEncodeDecodeOfTwoValues(final long value1, final long value2, final boolean fits, final int bufferSize) { final LongList originalValues = new LongList(); final byte[] buffer = new byte[bufferSize]; final int bytesAdded = VariableByteEncoder.encodeInto(value1, value2, buffer, 0); - Assert.assertEquals(bytesAdded > 0, fits); + Assertions.assertEquals(fits, bytesAdded > 0); if (fits) { originalValues.addAll(value1, value2); } else { - Assert.assertEquals(buffer, new byte[bufferSize], + Assertions.assertArrayEquals(new byte[bufferSize], buffer, "checks that buffer is resetted after it discovers the values do not fit"); } final LongList decodedValues = VariableByteEncoder.decode(buffer); - Assert.assertEquals(decodedValues, originalValues); + Assertions.assertEquals(originalValues, decodedValues); } - @DataProvider - public Object[][] providerNededBytes() { - return new Object[][] { // - { 0, 1 }, // - { -10, 1 }, // - { 10, 1 }, // - { -63, 1 }, // - { 63, 1 }, // - { -64, 2 }, // - { 64, 2 }, // - { -8191, 2 }, // - { 8191, 2 }, // - { -8192, 3 }, // - { 8192, 3 }, // - }; + public static Stream providerNededBytes() { + return Stream.of( // + Arguments.of(0, 1), // + Arguments.of(-10, 1), // + Arguments.of(10, 1), // + Arguments.of(-63, 1), // + Arguments.of(63, 1), // + Arguments.of(-64, 2), // + Arguments.of(64, 2), // + Arguments.of(-8191, 2), // + Arguments.of(8191, 2), // + Arguments.of(-8192, 3), // + Arguments.of(8192, 3) // + ); } - @Test(dataProvider = "providerNededBytes") + @ParameterizedTest + @MethodSource("providerNededBytes") public void testNeededBytes(final long value, final int expectedNeededBytes) { final int neededBytes = VariableByteEncoder.neededBytes(value); final byte[] encoded = VariableByteEncoder.encode(value); - Assert.assertEquals(encoded.length, neededBytes); + Assertions.assertEquals(neededBytes, encoded.length); } } diff --git a/pdb-utils/src/test/java/org/lucares/utils/cache/HotEntryCacheTest.java b/pdb-utils/src/test/java/org/lucares/utils/cache/HotEntryCacheTest.java index f5d21c7..c0190ea 100644 --- a/pdb-utils/src/test/java/org/lucares/utils/cache/HotEntryCacheTest.java +++ b/pdb-utils/src/test/java/org/lucares/utils/cache/HotEntryCacheTest.java @@ -18,28 +18,27 @@ import java.util.concurrent.TimeoutException; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; -@Test public class HotEntryCacheTest { static { - Configurator.setRootLevel(Level.TRACE); + Configurator.setRootLevel(Level.INFO); } private static final Logger LOGGER = LoggerFactory.getLogger(HotEntryCacheTest.class); private int cacheId = 0; - @Test(invocationCount = 1) + @Test public void testRemovalListenerCalledOnExpire() throws InterruptedException { - LOGGER.info(""); - LOGGER.info(""); - LOGGER.info("start: testRemovalListenerCalledOnExpire"); + LOGGER.debug(""); + LOGGER.debug(""); + LOGGER.debug("start: testRemovalListenerCalledOnExpire"); final long originalMinSleepPeriod = HotEntryCache.getMinSleepPeriod(); final long originalMaxSleepPeriod = HotEntryCache.getMaxSleepPeriod(); @@ -53,36 +52,38 @@ public class HotEntryCacheTest { HotEntryCache.setMinSleepPeriod(1); HotEntryCache.setMaxSleepPeriod(2); cache.addListener((k, v) -> { - Assert.assertEquals(k, key); - Assert.assertEquals(v, value); + Assertions.assertEquals(key, k); + Assertions.assertEquals(value, v); latch.countDown(); }); cache.put(key, value); final boolean listenerCalled = latch.await(100, TimeUnit.MILLISECONDS); - Assert.assertTrue(listenerCalled, "removal listener called"); + Assertions.assertTrue(listenerCalled, "removal listener called"); } finally { HotEntryCache.setMinSleepPeriod(originalMinSleepPeriod); HotEntryCache.setMaxSleepPeriod(originalMaxSleepPeriod); } } + @Test public void testPutAndGet() throws InterruptedException, ExecutionException, TimeoutException { final HotEntryCache cache = new HotEntryCache<>(Duration.ofSeconds(10), 10); final String replacedNull = cache.put("key", "value1"); - Assert.assertEquals(replacedNull, null); + Assertions.assertEquals(null, replacedNull); final String cachedValue1 = cache.get("key"); - Assert.assertEquals(cachedValue1, "value1"); + Assertions.assertEquals("value1", cachedValue1); final String replacedValue1 = cache.put("key", "value2"); - Assert.assertEquals(replacedValue1, "value1"); + Assertions.assertEquals("value1", replacedValue1); final String cachedValue2 = cache.get("key"); - Assert.assertEquals(cachedValue2, "value2"); + Assertions.assertEquals("value2", cachedValue2); } + @Test public void testPutTouches() throws InterruptedException, ExecutionException, TimeoutException { final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock(); final Duration timeToLive = Duration.ofSeconds(10); @@ -101,16 +102,17 @@ public class HotEntryCacheTest { // second put. final String cachedValue2 = cache.get("key"); - Assert.assertEquals(cachedValue2, "value2"); + Assertions.assertEquals("value2", cachedValue2); clock.plus(timeToLive.plusSeconds(1)); // time elapsed since the last put: timeToLive +1s cache.triggerEvictionAndWait(); final String cachedValue1_evicted = cache.get("key"); - Assert.assertEquals(cachedValue1_evicted, null); + Assertions.assertEquals(null, cachedValue1_evicted); } + @Test public void testGetTouches() throws Exception { final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock(); final Duration timeToLive = Duration.ofSeconds(10); @@ -127,9 +129,10 @@ public class HotEntryCacheTest { cache.triggerEvictionAndWait(); // if get didn't touch, then this will evict the entry final String cachedValue1 = cache.get("key"); - Assert.assertEquals(cachedValue1, "value1"); + Assertions.assertEquals("value1", cachedValue1); } + @Test public void testEvictionByBackgroundThread() throws InterruptedException, ExecutionException, TimeoutException { final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock(); final Duration timeToLive = Duration.ofSeconds(10); @@ -150,13 +153,14 @@ public class HotEntryCacheTest { cache.triggerEvictionAndWait(); final String evictedValue1 = evictionEventFuture.get(5, TimeUnit.MINUTES); // enough time for debugging - Assert.assertEquals(evictedValue1, "value1"); + Assertions.assertEquals("value1", evictedValue1); } + @Test public void testRemove() throws InterruptedException, ExecutionException, TimeoutException { - LOGGER.info(""); - LOGGER.info(""); - LOGGER.info("start: testRemove"); + LOGGER.debug(""); + LOGGER.debug(""); + LOGGER.debug("start: testRemove"); final HotEntryCache cache = new HotEntryCache<>(Duration.ofSeconds(10), 10); @@ -166,13 +170,14 @@ public class HotEntryCacheTest { cache.put("key", "value1"); final String removedValue = cache.remove("key"); - Assert.assertEquals(removedValue, "value1"); + Assertions.assertEquals("value1", removedValue); - Assert.assertEquals(removedValues, Arrays.asList("value1")); + Assertions.assertEquals(Arrays.asList("value1"), removedValues); - Assert.assertEquals(cache.get("key"), null); + Assertions.assertEquals(null, cache.get("key")); } + @Test public void testClear() throws InterruptedException, ExecutionException, TimeoutException { final HotEntryCache cache = new HotEntryCache<>(Duration.ofSeconds(10), 10); @@ -184,12 +189,13 @@ public class HotEntryCacheTest { cache.clear(); - Assert.assertEquals(cache.get("key1"), null); - Assert.assertEquals(cache.get("key2"), null); + Assertions.assertEquals(null, cache.get("key1")); + Assertions.assertEquals(null, cache.get("key2")); - Assert.assertEquals(removedValues, Arrays.asList("value1", "value2")); + Assertions.assertEquals(Arrays.asList("value1", "value2"), removedValues); } + @Test public void testForEachTouches() throws InterruptedException, ExecutionException, TimeoutException { final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock(); final Duration timeToLive = Duration.ofSeconds(10); @@ -216,13 +222,13 @@ public class HotEntryCacheTest { cache.triggerEvictionAndWait(); // if the touch didn't happen, then the value is now evicted - Assert.assertEquals(evictionEventFuture.isDone(), false); + Assertions.assertEquals(false, evictionEventFuture.isDone()); // seek again, so that the entry will get evicted clock.plus(timeToLive.minusMillis(1)); cache.triggerEvictionAndWait(); - Assert.assertEquals(cache.get("key"), null); + Assertions.assertEquals(null, cache.get("key")); } /** @@ -235,6 +241,7 @@ public class HotEntryCacheTest { * * @throws Exception */ + @Test public void testPutIfAbsentIsAtomic() throws Exception { final HotEntryCache cache = new HotEntryCache<>(Duration.ofSeconds(10), 10); @@ -264,12 +271,13 @@ public class HotEntryCacheTest { pool.awaitTermination(1, TimeUnit.MINUTES); final String actual = cache.get(key); - Assert.assertEquals(actual, valueA); + Assertions.assertEquals(valueA, actual); } finally { pool.shutdownNow(); } } + @Test public void testPutIfAbsentReturnsExistingValue() throws Exception { final HotEntryCache cache = new HotEntryCache<>(Duration.ofSeconds(10), 10); @@ -280,23 +288,25 @@ public class HotEntryCacheTest { cache.put(key, valueA); final String returnedByPutIfAbsent = cache.putIfAbsent(key, k -> valueB); - Assert.assertEquals(returnedByPutIfAbsent, valueA); + Assertions.assertEquals(valueA, returnedByPutIfAbsent); final String actualInCache = cache.get(key); - Assert.assertEquals(actualInCache, valueA); + Assertions.assertEquals(valueA, actualInCache); } + @Test public void testPutIfAbsentDoesNotAddNull() throws Exception { final HotEntryCache cache = new HotEntryCache<>(Duration.ofSeconds(10), 10); final String key = "key"; final String returnedByPutIfAbsent = cache.putIfAbsent(key, k -> null); - Assert.assertNull(returnedByPutIfAbsent, null); + Assertions.assertNull(returnedByPutIfAbsent); final String actualInCache = cache.get(key); - Assert.assertEquals(actualInCache, null); + Assertions.assertEquals(null, actualInCache); } + @Test public void testMaxSizeIsRespected() { final int maxSize = 10; final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock(); @@ -312,8 +322,8 @@ public class HotEntryCacheTest { clock.plus(2L, ChronoUnit.MILLIS); cache.updateTime(); } - Assert.assertEquals(cache.size(), maxSize, "cache is full"); - Assert.assertEquals(removedKeys, List.of(), "removed keys at point A"); + Assertions.assertEquals(maxSize, cache.size(), "cache is full"); + Assertions.assertEquals(Set.of(), removedKeys, "removed keys at point A"); // add an item to a full cache -> the oldest 20% of the entries will be evicted // before the new entry is added @@ -322,10 +332,11 @@ public class HotEntryCacheTest { cache.updateTime(); count++; - Assert.assertEquals(cache.size(), maxSize - 1, "cache was full, 20% (2 items) were removed and one added"); - Assert.assertEquals(removedKeys, Set.of("key0", "key1"), "removed keys at point B"); + Assertions.assertEquals(maxSize - 1, cache.size(), "cache was full, 20% (2 items) were removed and one added"); + Assertions.assertEquals(Set.of("key0", "key1"), removedKeys, "removed keys at point B"); } + @Test public void testEvictionDueToSizeLimitDoesNotRemoveMoreThan20Percent() { final int maxSize = 10; final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock(); @@ -339,16 +350,16 @@ public class HotEntryCacheTest { // all entries get the same eviction time due to the fixed clock cache.put("key" + count, "value" + count); } - Assert.assertEquals(cache.size(), maxSize, "cache is full"); - Assert.assertEquals(removedKeys, List.of(), "removed keys at point A"); + Assertions.assertEquals(maxSize, cache.size(), "cache is full"); + Assertions.assertEquals(Set.of(), removedKeys, "removed keys at point A"); // add an item to a full cache -> the oldest 20% of the entries will be evicted // before the new entry is added cache.put("key" + count, "value" + count); count++; - Assert.assertEquals(cache.size(), maxSize - 1, "cache was full, 20% (2 items) were removed and one added"); - Assert.assertEquals(removedKeys.size(), (int) (maxSize * 0.2), "number of removed keys at point B"); + Assertions.assertEquals(maxSize - 1, cache.size(), "cache was full, 20% (2 items) were removed and one added"); + Assertions.assertEquals((int) (maxSize * 0.2), removedKeys.size(), "number of removed keys at point B"); } private void sleep(final TimeUnit timeUnit, final long timeout) { diff --git a/performanceDb/src/test/java/org/lucares/performance/db/PdbReaderWriterTest.java b/performanceDb/src/test/java/org/lucares/performance/db/PdbReaderWriterTest.java deleted file mode 100644 index cf6d858..0000000 --- a/performanceDb/src/test/java/org/lucares/performance/db/PdbReaderWriterTest.java +++ /dev/null @@ -1,129 +0,0 @@ -//package org.lucares.performance.db; -// -//import java.io.File; -//import java.io.IOException; -//import java.nio.file.Files; -//import java.nio.file.Path; -//import java.nio.file.StandardOpenOption; -//import java.time.Instant; -//import java.time.OffsetDateTime; -//import java.time.ZoneId; -//import java.util.ArrayList; -//import java.util.Arrays; -//import java.util.Iterator; -//import java.util.List; -// -//import org.lucares.pdb.api.Entry; -//import org.lucares.pdb.api.Tags; -//import org.testng.Assert; -//import org.testng.annotations.AfterMethod; -//import org.testng.annotations.BeforeMethod; -//import org.testng.annotations.DataProvider; -//import org.testng.annotations.Test; -// -//@Test -//public class PdbReaderWriterTest { -// -// private Path dataDirectory; -// -// private static final Tags TAGS = Tags.create(); -// -// @BeforeMethod -// public void beforeMethod() throws IOException { -// dataDirectory = Files.createTempDirectory("pdb"); -// } -// -// @AfterMethod -// public void afterMethod() throws IOException { -// org.lucares.utils.file.FileUtils.delete(dataDirectory); -// } -// -// @DataProvider(name = "providerWriteRead") -// public Iterator providerWriteRead() { -// -// final OffsetDateTime two_sixteen = DateUtils.getDate(2016, 1, 1, 1, 1, 1); -// -// final List values = Arrays.asList(0L, 1L, 63L, 64L, 127L, 128L, 202L, 255L, 256L, 8191L, 8192L, 1048575L, -// 1048576L, 134217728L, 17179869183L, 17179869184L, 2199023255551L, 2199023255552L, 281474976710655L, -// 281474976710656L, 36028797018963967L, 36028797018963968L, 4611686018427387901L, 4611686018427387904L); -// -// final List result = new ArrayList<>(); -// -// // single values -// for (final Long value : values) { -// result.add(new Object[] { Arrays.asList(new Entry(two_sixteen, value, TAGS)) }); -// } -// -// // multivalues -// final List entries = new ArrayList<>(); -// for (int i = 0; i < 100; i++) { -// -// final long epochMilli = 123456 * i; -// -// final OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.of("UTC")); -// -// entries.add(new Entry(date, i, TAGS)); -// } -// result.add(new Object[] { entries }); -// -// return result.iterator(); -// } -// -// @Test(dataProvider = "providerWriteRead") -// public void testWriteRead(final List entries) throws Exception { -// -// final File file = Files.createTempFile(dataDirectory, "pdb", ".db").toFile(); -// final Path relativePath = dataDirectory.relativize(file.toPath()); -// final PdbFile pdbFile = new PdbFile(relativePath, TAGS); -// -// try (PdbWriter writer = new PdbWriter(dataDirectory, pdbFile)) { -// for (final Entry entry : entries) { -// writer.write(entry); -// } -// } -// -// try (final PdbReader reader = new PdbReader(dataDirectory, pdbFile)) { -// -// for (final Entry entry : entries) { -// -// final Entry actual = reader.readEntry().orElseThrow(() -> new AssertionError()); -// -// Assert.assertEquals(actual, entry); -// } -// reader.readEntry().ifPresent(e -> { -// throw new AssertionError(); -// }); -// } -// } -// -// @Test(expectedExceptions = FileCorruptException.class) -// public void testReadExceptionOnCorruptEntries() throws Exception { -// -// final Entry entryA = new Entry(1, 1, TAGS); -// -// final File file = Files.createTempFile(dataDirectory, "pdb", ".db").toFile(); -// final Path relativePath = dataDirectory.relativize(file.toPath()); -// final PdbFile pdbFile = new PdbFile(relativePath, TAGS); -// -// try (PdbWriter writer = new PdbWriter(dataDirectory, pdbFile)) { -// writer.write(entryA); -// } -// -// // make the file corrupt -// // two date consecutive increments will never happen in valid data -// final byte[] corruptEntries = new byte[] { // -// ByteType.DATE_INCREMENT.getBytePrefixAsByte(), // -// ByteType.DATE_INCREMENT.getBytePrefixAsByte() // -// }; -// -// Files.write(file.toPath(), corruptEntries, StandardOpenOption.APPEND); -// -// try (final PdbReader reader = new PdbReader(dataDirectory, pdbFile)) { -// -// final Entry actualA = reader.readEntry().orElseThrow(() -> new AssertionError()); -// Assert.assertEquals(actualA, entryA); -// -// reader.readEntry(); // should throw FileCorruptException -// } -// } -//} diff --git a/performanceDb/src/test/java/org/lucares/performance/db/PerformanceDbTest.java b/performanceDb/src/test/java/org/lucares/performance/db/PerformanceDbTest.java index d800b77..f9b8a75 100644 --- a/performanceDb/src/test/java/org/lucares/performance/db/PerformanceDbTest.java +++ b/performanceDb/src/test/java/org/lucares/performance/db/PerformanceDbTest.java @@ -12,6 +12,11 @@ import java.util.List; import java.util.concurrent.ThreadLocalRandom; import org.apache.commons.collections4.CollectionUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.lucares.collections.LongList; import org.lucares.pdb.api.DateTimeRange; import org.lucares.pdb.api.Entry; @@ -19,28 +24,24 @@ import org.lucares.pdb.api.GroupResult; import org.lucares.pdb.api.Query; import org.lucares.pdb.api.Result; import org.lucares.pdb.api.Tags; +import org.junit.jupiter.api.Assertions; import org.lucares.utils.DateUtils; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; -@Test public class PerformanceDbTest { private Path dataDirectory; - @BeforeMethod + @BeforeEach public void beforeMethod() throws IOException { dataDirectory = Files.createTempDirectory("pdb"); } - @AfterMethod + @AfterEach public void afterMethod() throws IOException { org.lucares.utils.file.FileUtils.delete(dataDirectory); } + @Test public void testInsertRead() throws Exception { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { @@ -53,13 +54,14 @@ public class PerformanceDbTest { final Result result = db.get(Query.createQuery(tags, DateTimeRange.ofDay(nowInUtc))); final LongList stream = result.singleGroup().flatMap(); - Assert.assertEquals(stream.size(), 2); + Assertions.assertEquals(2, stream.size()); - Assert.assertEquals(stream.get(0), date); - Assert.assertEquals(stream.get(1), value); + Assertions.assertEquals(date, stream.get(0)); + Assertions.assertEquals(value, stream.get(1)); } } + @Test public void testInsertIntoMultipleFilesRead() throws Exception { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { @@ -76,12 +78,12 @@ public class PerformanceDbTest { final LongList stream = db.get(Query.createQuery(tags, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(stream.size(), 4); + Assertions.assertEquals(4, stream.size()); - Assert.assertEquals(stream.get(0), dayOne); - Assert.assertEquals(stream.get(1), valueOne); - Assert.assertEquals(stream.get(2), dayTwo); - Assert.assertEquals(stream.get(3), valueTwo); + Assertions.assertEquals(dayOne, stream.get(0)); + Assertions.assertEquals(valueOne, stream.get(1)); + Assertions.assertEquals(dayTwo, stream.get(2)); + Assertions.assertEquals(valueTwo, stream.get(3)); } } @@ -102,16 +104,8 @@ public class PerformanceDbTest { return result; } - @DataProvider - public Object[][] providerAppendToExistingFile() throws Exception { - return new Object[][] { // - { 2 }, // - { 100 }, // - { 500 }, // - }; - } - - @Test(dataProvider = "providerAppendToExistingFile") + @ParameterizedTest + @ValueSource(longs = { 2, 100, 500 }) public void testAppendToExistingFile(final long numberOfEntries) throws Exception { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { @@ -130,30 +124,22 @@ public class PerformanceDbTest { db.putEntries(entries); final LongList actualEntries = db.get(Query.createQuery(tags, timeRange)).singleGroup().flatMap(); - Assert.assertEquals(actualEntries.size(), entries.size() * 2); + Assertions.assertEquals(entries.size() * 2, actualEntries.size()); for (int i = 0; i < entries.size(); i++) { final Entry entry = entries.get(i); final long epochMilli = entry.getEpochMilli(); final long value = entry.getValue(); - Assert.assertEquals(actualEntries.get(i * 2), epochMilli); - Assert.assertEquals(actualEntries.get(i * 2 + 1), value); + Assertions.assertEquals(epochMilli, actualEntries.get(i * 2)); + Assertions.assertEquals(value, actualEntries.get(i * 2 + 1)); } } } - @DataProvider - public Object[][] providerAppendToExistingFileWithRestart() throws Exception { - return new Object[][] { // - { 2 }, // - { 100 }, // - { 500 }, // - }; - } - - @Test(dataProvider = "providerAppendToExistingFileWithRestart") + @ParameterizedTest + @ValueSource(longs = { 2, 100, 500 }) public void testAppendToExistingFileWithRestart(final long numberOfEntries) throws Exception { final Tags tags; final List expected = new ArrayList<>(); @@ -182,12 +168,13 @@ public class PerformanceDbTest { expected.addAll(entries); final LongList actualEntries = db.get(Query.createQuery(tags, timeRange)).singleGroup().flatMap(); - Assert.assertEquals(actualEntries.size(), expected.size() * 2); + Assertions.assertEquals(expected.size() * 2, actualEntries.size()); - Assert.assertEquals(actualEntries, toExpectedValues(expected)); + Assertions.assertEquals(toExpectedValues(expected), actualEntries); } } + @Test public void testInsertIntoMultipleFilesWithDifferentTags() throws Exception { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { @@ -215,13 +202,13 @@ public class PerformanceDbTest { db.putEntries(entriesThree); final LongList actualEntriesOne = db.get(Query.createQuery(tagsOne, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(actualEntriesOne, toExpectedValues(entriesOne)); + Assertions.assertEquals(toExpectedValues(entriesOne), actualEntriesOne); final LongList actualEntriesTwo = db.get(Query.createQuery(tagsTwo, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(actualEntriesTwo, toExpectedValues(entriesTwo)); + Assertions.assertEquals(toExpectedValues(entriesTwo), actualEntriesTwo); final LongList actualEntriesThree = db.get(Query.createQuery(tagsThree, dateRange)).singleGroup().flatMap(); - Assert.assertEquals(actualEntriesThree, toExpectedValues(entriesThree)); + Assertions.assertEquals(toExpectedValues(entriesThree), actualEntriesThree); final LongList actualEntriesAll = db.get(Query.createQuery(tagsCommon, dateRange)).singleGroup().flatMap(); final List expectedAll = CollectionUtils.collate(entriesOne, @@ -232,10 +219,11 @@ public class PerformanceDbTest { actualEntriesAll.sort(); expectedValues.sort(); - Assert.assertEquals(actualEntriesAll, expectedValues); + Assertions.assertEquals(expectedValues, actualEntriesAll); } } + @Test public void testGroupBySingleField() throws Exception { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final OffsetDateTime from = DateUtils.getDate(2016, 1, 1, 00, 00, 00); @@ -260,19 +248,20 @@ public class PerformanceDbTest { final Tags groupedBy = groupResult.getGroupedBy(); if (groupedBy.equals(Tags.createAndAddToDictionary(key, "one"))) { - Assert.assertEquals(groupResult.flatMap(), entriesOne); + Assertions.assertEquals(entriesOne, groupResult.flatMap()); } else if (groupedBy.equals(Tags.createAndAddToDictionary(key, "two"))) { - Assert.assertEquals(groupResult.flatMap(), entriesTwo); + Assertions.assertEquals(entriesTwo, groupResult.flatMap()); } else if (groupedBy.isEmpty()) { - Assert.assertEquals(groupResult.flatMap(), entriesThree); + Assertions.assertEquals(entriesThree, groupResult.flatMap()); } else { - Assert.fail("unexpected group: " + groupResult.getGroupedBy()); + Assertions.fail("unexpected group: " + groupResult.getGroupedBy()); } } } } + @Test public void testGroupByMultipleFields() throws Exception { try (PerformanceDb db = new PerformanceDb(dataDirectory)) { final OffsetDateTime from = DateUtils.getDate(2016, 1, 1, 00, 00, 00); @@ -302,7 +291,7 @@ public class PerformanceDbTest { final Tags groupedBy = groupResult.getGroupedBy(); if (groupedBy.equals(Tags.createAndAddToDictionary(key1, "one", key2, "aaa"))) { - Assert.assertEquals(groupResult.flatMap(), entriesOne); + Assertions.assertEquals(entriesOne, groupResult.flatMap()); } else if (groupedBy.equals(Tags.createAndAddToDictionary(key1, "two", key2, "bbb"))) { // there is no defined order of the entries. // eventually we might return them in ascending order, but @@ -312,11 +301,11 @@ public class PerformanceDbTest { entriesTwo.sort(); actualEntries.sort(); - Assert.assertEquals(actualEntries, entriesTwo); + Assertions.assertEquals(entriesTwo, actualEntries); } else if (groupedBy.equals(Tags.createAndAddToDictionary(key1, "three"))) { - Assert.assertEquals(groupResult.flatMap(), entriesThree); + Assertions.assertEquals(entriesThree, groupResult.flatMap()); } else { - Assert.fail("unexpected group: " + groupedBy); + Assertions.fail("unexpected group: " + groupedBy); } } }