From 09726e9b27466d6f32476091dbca470839cce467 Mon Sep 17 00:00:00 2001 From: Amit-Singh40 <104156566+Amit-Singh40@users.noreply.github.com> Date: Wed, 7 Feb 2024 09:34:37 +0530 Subject: [PATCH] Adding test cases for validation improvement (#51) Adding test cases for the validation improvement. Signed-off-by: Amit-Singh40 --- .../util/TransactionStateInMemoryImpl.java | 4 +- .../collector/DeviceDriverManagerTest.java | 22 +++++++ .../sensor/collector/DeviceDriverTest.java | 59 +++++++++++++++++++ .../sensor/collector/MockedConnection.java | 46 +++++++++++++++ .../collector/PravegaClientConfigTests.java | 14 +++++ .../collector/file/FileProcessorTests.java | 46 ++++++++++++++- .../csvfile/CSVFileEventGeneratorTests.java | 21 +++++++ .../CsvFileSequenceProcessorTests.java | 3 - .../parquet/ParquetEventGeneratorTests.java | 20 ++++++- .../file/rawfile/RawEventGeneratorTests.java | 19 ++++++ .../simple/DataCollectorServiceTest.java | 38 ++++++++++++ ...moryQueueToPersistentQueueServiceTest.java | 59 +++++++++++++++++++ .../simple/PersistentQueueElementTest.java | 28 +++++++++ .../collector/simple/PersistentQueueTest.java | 36 +++++++++++ .../PersistentQueueToPravegaServiceTest.java | 51 ++++++++++++++++ .../stateful/DataCollectorServiceTest.java | 53 +++++++++++++++++ .../collector/stateful/PollResponseTest.java | 30 ++++++++++ .../collector/stateful/ReadingStateTest.java | 22 +++++++ .../collector/util/AutoRollbackTest.java | 22 +++++++ .../util/FileNameWithOffsetTest.java | 22 +++++++ .../util/NonTransactionalEventWriterTest.java | 22 +++++++ .../collector/util/PersistentIdTests.java | 7 +++ .../util/PravegaWriterEventTest.java | 28 +++++++++ .../util/TransactionCoordinatorTests.java | 13 ++++ .../util/TransactionStateSQLiteImplTests.java | 35 ++++++++++- .../util/TransactionalEventWriterTest.java | 22 +++++++ 26 files changed, 730 insertions(+), 12 deletions(-) create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverManagerTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/MockedConnection.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/DataCollectorServiceTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/MemoryQueueToPersistentQueueServiceTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueElementTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueToPravegaServiceTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/DataCollectorServiceTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/PollResponseTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/ReadingStateTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/AutoRollbackTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/FileNameWithOffsetTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/NonTransactionalEventWriterTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PravegaWriterEventTest.java create mode 100644 pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionalEventWriterTest.java diff --git a/pravega-sensor-collector/src/main/java/io/pravega/sensor/collector/util/TransactionStateInMemoryImpl.java b/pravega-sensor-collector/src/main/java/io/pravega/sensor/collector/util/TransactionStateInMemoryImpl.java index b2d495ea..a612cfd7 100644 --- a/pravega-sensor-collector/src/main/java/io/pravega/sensor/collector/util/TransactionStateInMemoryImpl.java +++ b/pravega-sensor-collector/src/main/java/io/pravega/sensor/collector/util/TransactionStateInMemoryImpl.java @@ -27,9 +27,9 @@ public TransactionStateInMemoryImpl(Connection connection, TransactionCoordinato super(connection, transactionCoordinator); } @VisibleForTesting - public static TransactionStateInMemoryImpl create(String fileName) { + public static TransactionStateInMemoryImpl create(String fileName, EventWriter writer) { final Connection connection = SQliteDBUtility.createDatabase(fileName); - final TransactionCoordinator transactionCoordinator = new TransactionCoordinator(connection, null); + final TransactionCoordinator transactionCoordinator = new TransactionCoordinator(connection, writer); return new TransactionStateInMemoryImpl(connection, transactionCoordinator); } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverManagerTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverManagerTest.java new file mode 100644 index 00000000..2380f7f6 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverManagerTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class DeviceDriverManagerTest { + + @Test + public void testCreateDeviceDriverManagerWithNullProperties() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DeviceDriverManager(null)); + Assert.assertTrue("properties".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverTest.java new file mode 100644 index 00000000..081b1a97 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/DeviceDriverTest.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector; + +import io.pravega.sensor.collector.accelerometer.AccelerometerDriver; +import io.pravega.sensor.collector.file.csvfile.CsvFileIngestService; +import io.pravega.sensor.collector.file.parquet.ParquetFileIngestService; +import io.pravega.sensor.collector.file.rawfile.RawFileIngestService; +import io.pravega.sensor.collector.leap.LeapDriver; +import io.pravega.sensor.collector.network.NetworkDriver; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class DeviceDriverTest { + + @Test + public void testCreateAccelerometerDriverWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new AccelerometerDriver(null)); + Assert.assertTrue("config".equals(exception.getMessage())); + } + + @Test + public void testCreateLeapDriverWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new LeapDriver(null)); + Assert.assertTrue("config".equals(exception.getMessage())); + } + + @Test + public void testCreateNetworkDriverWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new NetworkDriver(null)); + Assert.assertTrue("config".equals(exception.getMessage())); + } + + @Test + public void testCreateCsvFileIngestServiceWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new CsvFileIngestService(null)); + Assert.assertTrue("config".equals(exception.getMessage())); + } + + @Test + public void testCreateParquetFileIngestServiceWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new ParquetFileIngestService(null)); + Assert.assertTrue("config".equals(exception.getMessage())); + } + + @Test + public void testCreateRawFileIngestServiceWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawFileIngestService(null)); + Assert.assertTrue("config".equals(exception.getMessage())); + } + +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/MockedConnection.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/MockedConnection.java new file mode 100644 index 00000000..affdea30 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/MockedConnection.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector; + +import io.pravega.sensor.collector.util.TransactionCoordinator; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.mockito.Mockito.when; + +public abstract class MockedConnection { + + @Mock + protected Connection mockConnection; + + @Mock + protected Statement mockStatement; + + @Mock + protected ResultSet mockResultSet; + + @Mock + protected TransactionCoordinator transactionCoordinator; + + + protected void before() throws SQLException { + MockitoAnnotations.initMocks(this); + when(mockConnection.createStatement()).thenReturn(mockStatement); + when(mockStatement.executeQuery("select count(id) from Queue")).thenReturn(mockResultSet); + when(mockResultSet.next()).thenReturn(true); + } + + +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/PravegaClientConfigTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/PravegaClientConfigTests.java index 003b5688..c402176b 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/PravegaClientConfigTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/PravegaClientConfigTests.java @@ -9,6 +9,7 @@ */ package io.pravega.sensor.collector; +import org.junit.Assert; import org.junit.jupiter.api.Test; import java.net.URI; @@ -68,4 +69,17 @@ public void testEqualsAndHashCode() { assertEquals(configFile1.hashCode(), configFile2.hashCode()); } + @Test + public void testCreatePravegaClientConfigWithNullControllerURI() { + URI uri = null; + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PravegaClientConfig(uri, "testScope")); + Assert.assertTrue("controllerURI".equals(exception.getMessage())); + } + + @Test + public void testCreatePravegaClientConfigWithNullScopeName() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PravegaClientConfig(URI.create("tcp://localhost:9090"), null)); + Assert.assertTrue("scopeName".equals(exception.getMessage())); + } + } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/FileProcessorTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/FileProcessorTests.java index ab204289..b7d1afd7 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/FileProcessorTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/FileProcessorTests.java @@ -11,10 +11,14 @@ import com.google.common.collect.ImmutableList; import io.pravega.client.EventStreamClientFactory; +import io.pravega.client.stream.EventWriterConfig; +import io.pravega.client.stream.Serializer; +import io.pravega.client.stream.TransactionalEventStreamWriter; import io.pravega.client.stream.TxnFailedException; import io.pravega.sensor.collector.file.rawfile.RawFileProcessor; import io.pravega.sensor.collector.util.*; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -34,8 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; public class FileProcessorTests { @@ -58,13 +61,16 @@ public class FileProcessorTests { @Mock private EventStreamClientFactory clientFactory; + @Mock + TransactionalEventStreamWriter transactionalEventStreamWriter; + @BeforeEach protected void setup() { MockitoAnnotations.initMocks(this); String stateDatabaseFileName = ":memory:"; config = new FileConfig("./psc.db","/opt/pravega-sensor-collector/Files/A","parquet","key12", - "stream1","{}",10, false, + "stream1","{}",10, true, true,20.0, 5000,"RawFileIngestService", true); } @@ -97,6 +103,7 @@ public void getDirectoryListingTest() throws IOException { */ @Test public void getEmptyNextFileSet() throws Exception { + when(clientFactory.createTransactionalEventWriter(anyString(), anyString(), any(Serializer.class), any(EventWriterConfig.class))).thenReturn(transactionalEventStreamWriter); FileProcessor fileProcessor = FileProcessor.create(config, clientFactory); fileProcessor.processFiles(); } @@ -178,4 +185,37 @@ public void copyFile() throws IOException { targetPath = Paths.get("../../pravega-sensor-collector/parquet-file-sample-data/sub3.parquet"); Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); } + + @Test + public void testCreateRawFileProcessorWithNullConfig() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawFileProcessor(null, state, transactionalEventWriter, transactionCoordinator, "test")); + Assert.assertTrue("config".equals(exception.getMessage())); +; } + + @Test + public void testCreateRawFileProcessorWithNullState() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawFileProcessor(config, null, transactionalEventWriter, transactionCoordinator, "test")); + Assert.assertTrue("state".equals(exception.getMessage())); + } + + @Test + public void testCreateRawFileProcessorWithNullEventWriter() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawFileProcessor(config, state, null, transactionCoordinator, "test")); + Assert.assertTrue("writer".equals(exception.getMessage())); + } + + @Test + public void testCreateRawFileProcessorWithNullTransactionCordinator() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawFileProcessor(config, state, transactionalEventWriter, null, "test")); + Assert.assertTrue("transactionCoordinator".equals(exception.getMessage())); + } + + @Test + public void testCreateRawFileProcessorWithNullStateDatabaseFilenameInConfig() { + FileConfig newConfig = new FileConfig(null,"/opt/pravega-sensor-collector/Files/A","parquet","key12", + "stream1","{}",10, false, + true,20.0, 5000,"RawFileIngestService", true); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawFileProcessor(newConfig, state, transactionalEventWriter, transactionCoordinator, "test")); + Assert.assertTrue("config.stateDatabaseFileName".equals(exception.getMessage())); + } } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CSVFileEventGeneratorTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CSVFileEventGeneratorTests.java index 55d5d967..915b7a9a 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CSVFileEventGeneratorTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CSVFileEventGeneratorTests.java @@ -9,10 +9,14 @@ */ package io.pravega.sensor.collector.file.csvfile; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.io.CountingInputStream; import io.pravega.sensor.collector.file.EventGenerator; import io.pravega.sensor.collector.util.PravegaWriterEvent; import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -117,4 +121,21 @@ public void test7by3() throws IOException { Assertions.assertEquals(103L, (long) nextSequenceNumberAndOffset.getLeft()); Assertions.assertEquals(csvStr.length(), (long) nextSequenceNumberAndOffset.getRight()); } + + + @Test + public void testCreateCsvFileEventGeneratorWithNullRoutingKey() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = (ObjectNode) objectMapper.readTree("{}"); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new CsvFileEventGenerator(null, 1, objectNode, objectMapper)); + Assert.assertTrue("routingKey".equals(exception.getMessage())); + } + + @Test + public void testCreateCsvFileEventGeneratorWithNullObjectMapper() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = (ObjectNode) objectMapper.readTree("{}"); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new CsvFileEventGenerator("routing-key", 1, objectNode, null)); + Assert.assertTrue("objectMapper".equals(exception.getMessage())); + } } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CsvFileSequenceProcessorTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CsvFileSequenceProcessorTests.java index cf29503d..79531a27 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CsvFileSequenceProcessorTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/csvfile/CsvFileSequenceProcessorTests.java @@ -11,13 +11,10 @@ import io.pravega.sensor.collector.file.FileProcessor; import io.pravega.sensor.collector.file.FileProcessorTests; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import static org.mockito.ArgumentMatchers.any; - public class CsvFileSequenceProcessorTests extends FileProcessorTests { @BeforeEach diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/parquet/ParquetEventGeneratorTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/parquet/ParquetEventGeneratorTests.java index d4b17281..75de0390 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/parquet/ParquetEventGeneratorTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/parquet/ParquetEventGeneratorTests.java @@ -9,6 +9,9 @@ */ package io.pravega.sensor.collector.file.parquet; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.io.CountingInputStream; import io.pravega.sensor.collector.file.EventGenerator; import io.pravega.sensor.collector.util.FileNameWithOffset; @@ -23,7 +26,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -44,4 +46,20 @@ public void TestFile() throws IOException { Assert.assertEquals(parquetData.length(), (long) nextSequenceNumberAndOffset.getRight()); } + @Test + public void testCreateParquetEventGeneratorWithNullRoutingKey() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = (ObjectNode) objectMapper.readTree("{}"); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new ParquetEventGenerator(null, 1, objectNode, objectMapper)); + Assert.assertTrue("routingKey".equals(exception.getMessage())); + } + + @Test + public void testCreateParquetEventGeneratorWithNullObjectMapper() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = (ObjectNode) objectMapper.readTree("{}"); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new ParquetEventGenerator("routing-key", 1, objectNode, null)); + Assert.assertTrue("objectMapper".equals(exception.getMessage())); + } + } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/rawfile/RawEventGeneratorTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/rawfile/RawEventGeneratorTests.java index 40c7812e..baabfa00 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/rawfile/RawEventGeneratorTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/file/rawfile/RawEventGeneratorTests.java @@ -9,6 +9,9 @@ */ package io.pravega.sensor.collector.file.rawfile; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.io.CountingInputStream; import io.pravega.sensor.collector.file.EventGenerator; import io.pravega.sensor.collector.util.PravegaWriterEvent; @@ -55,4 +58,20 @@ public void testEmptyFile() throws IOException { Assert.assertEquals(rawfileStr.length(), (long) nextSequenceNumberAndOffset.getRight()); } + + @Test + public void testCreateRawEventGeneratorWithNullRoutingKey() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = (ObjectNode) objectMapper.readTree("{}"); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawEventGenerator(null, objectNode, objectMapper)); + Assert.assertTrue("routingKey".equals(exception.getMessage())); + } + + @Test + public void testCreateRawEventGeneratorWithNullObjectMapper() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = (ObjectNode) objectMapper.readTree("{}"); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new RawEventGenerator("routing-key", objectNode, null)); + Assert.assertTrue("objectMapper".equals(exception.getMessage())); + } } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/DataCollectorServiceTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/DataCollectorServiceTest.java new file mode 100644 index 00000000..aa9bed1f --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/DataCollectorServiceTest.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.simple; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.LinkedBlockingQueue; + +public class DataCollectorServiceTest { + + + @Test + public void testCreateDataCollectorServiceWithNullInstanceName() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DataCollectorService(null, new LinkedBlockingQueue<>(1), null)); + Assert.assertTrue("instanceName".equals(exception.getMessage())); + } + + @Test + public void testCreateDataCollectorServiceWithNullMemoryQueue() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DataCollectorService("instance-name", null, null)); + Assert.assertTrue("memoryQueue".equals(exception.getMessage())); + } + + @Test + public void testCreateDataCollectorServiceWithNullDriver() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DataCollectorService("instance-name", new LinkedBlockingQueue<>(1), null)); + Assert.assertTrue("driver".equals(exception.getMessage())); + } + +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/MemoryQueueToPersistentQueueServiceTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/MemoryQueueToPersistentQueueServiceTest.java new file mode 100644 index 00000000..34547ace --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/MemoryQueueToPersistentQueueServiceTest.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.simple; + +import io.pravega.sensor.collector.DeviceDriverConfig; +import io.pravega.sensor.collector.DeviceDriverManager; +import io.pravega.sensor.collector.MockedConnection; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.concurrent.LinkedBlockingQueue; + +public class MemoryQueueToPersistentQueueServiceTest extends MockedConnection { + + private PersistentQueue persistentQueue; + + private DeviceDriverConfig config; + + @BeforeEach + public void setUp() throws SQLException { + super.before(); + persistentQueue = new PersistentQueue(mockConnection, transactionCoordinator, 10); + config = new DeviceDriverConfig("ins", "DeviceDriverConfig", new HashMap<>(), new DeviceDriverManager(new HashMap<>())); + } + + @Test + public void testCreateMemoryQueueToPersistentQueueServiceWithNullInstanceName() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new MemoryQueueToPersistentQueueService<>(null, new LinkedBlockingQueue<>(1), null, null, 1)); + Assert.assertTrue("instanceName".equals(exception.getMessage())); + } + + @Test + public void testCreateMemoryQueueToPersistentQueueServiceWithNullMemoryQueue() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new MemoryQueueToPersistentQueueService<>("instance-name", null, null, null, 1)); + Assert.assertTrue("memoryQueue".equals(exception.getMessage())); + } + + @Test + public void testCreateMemoryQueueToPersistentQueueServiceWithNullPersistentQueue() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new MemoryQueueToPersistentQueueService<>("instance-name", new LinkedBlockingQueue<>(1), null, null, 1)); + Assert.assertTrue("persistentQueue".equals(exception.getMessage())); + } + + @Test + public void testCreateMemoryQueueToPersistentQueueServiceWithNull() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new MemoryQueueToPersistentQueueService<>("instance-name", new LinkedBlockingQueue<>(1), persistentQueue, null, 1)); + Assert.assertTrue("driver".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueElementTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueElementTest.java new file mode 100644 index 00000000..05b21ff0 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueElementTest.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.simple; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class PersistentQueueElementTest { + + @Test + public void testCreatePersistentQueueElementWithNullBytes() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PersistentQueueElement(1, null, "routing-key", 1000000)); + Assert.assertTrue("bytes".equals(exception.getMessage())); + } + + @Test + public void testCreatePersistentQueueElementWithNullRoutingKey() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PersistentQueueElement(1, new byte[]{}, null, 1000000)); + Assert.assertTrue("routingKey".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueTest.java new file mode 100644 index 00000000..2f709061 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueTest.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.simple; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.sql.Connection; + +public class PersistentQueueTest { + + @Mock + private Connection mockConnection; + + @Test + public void testCreatePersistentQueueWithNullConnection() { + Exception exception = Assert.assertThrows(RuntimeException.class, () -> new PersistentQueue(null, null, 1)); + Assert.assertTrue(exception.getMessage().contains("connection")); + } + + @Test + public void testCreatePersistentQueueWithNullTransactionCoordinator() { + MockitoAnnotations.initMocks(this); + Exception exception = Assert.assertThrows(RuntimeException.class, () -> new PersistentQueue(mockConnection, null, 1)); + Assert.assertTrue(exception.getMessage().contains("transactionCoordinator")); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueToPravegaServiceTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueToPravegaServiceTest.java new file mode 100644 index 00000000..aa2a72d9 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/simple/PersistentQueueToPravegaServiceTest.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.simple; + +import io.pravega.sensor.collector.MockedConnection; +import io.pravega.sensor.collector.util.EventWriter; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import java.sql.SQLException; + +public class PersistentQueueToPravegaServiceTest extends MockedConnection { + + @Mock + private EventWriter writer; + + private PersistentQueue persistentQueue; + + @BeforeEach + public void before() throws SQLException { + super.before(); + persistentQueue = new PersistentQueue(mockConnection, transactionCoordinator, 10); + } + + @Test + public void testCreatePersistentQueueToPravegaServiceWithNull() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PersistentQueueToPravegaService(null, persistentQueue, writer, 1, 1)); + Assert.assertTrue("instanceName".equals(exception.getMessage())); + } + + @Test + public void testCreatePersistentQueueToPravegaServiceWithNullPersistentQueue() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PersistentQueueToPravegaService("instance-name", null, writer, 1, 1)); + Assert.assertTrue("persistentQueue".equals(exception.getMessage())); + } + + @Test + public void testCreatePersistentQueueToPravegaServiceWithNullWriter() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PersistentQueueToPravegaService("instance-name", persistentQueue, null, 1, 1)); + Assert.assertTrue("writer".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/DataCollectorServiceTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/DataCollectorServiceTest.java new file mode 100644 index 00000000..43fae778 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/DataCollectorServiceTest.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.stateful; + +import io.pravega.sensor.collector.DeviceDriverConfig; +import io.pravega.sensor.collector.DeviceDriverManager; +import io.pravega.sensor.collector.MockedConnection; +import io.pravega.sensor.collector.simple.PersistentQueue; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.SQLException; +import java.util.HashMap; + +public class DataCollectorServiceTest extends MockedConnection { + + private PersistentQueue persistentQueue; + + private DeviceDriverConfig config; + + @BeforeEach + public void setUp() throws SQLException { + super.before(); + persistentQueue = new PersistentQueue(mockConnection, transactionCoordinator, 10); + config = new DeviceDriverConfig("ins", "DeviceDriverConfig", new HashMap<>(), new DeviceDriverManager(new HashMap<>())); + } + + @Test + public void testCreateDataCollectorServiceTestWithNullInstanceName() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DataCollectorService<>(null, persistentQueue, null)); + Assert.assertTrue("instanceName".equals(exception.getMessage())); + } + + @Test + public void testCreateDataCollectorServiceTestWithNullPersistentQueue() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DataCollectorService<>("instance", null, null)); + Assert.assertTrue("persistentQueue".equals(exception.getMessage())); + } + + @Test + public void testCreateDataCollectorServiceTestWithNullDriver() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new DataCollectorService<>("instance", persistentQueue, null)); + Assert.assertTrue("driver".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/PollResponseTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/PollResponseTest.java new file mode 100644 index 00000000..bda3c812 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/PollResponseTest.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.stateful; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class PollResponseTest { + + @Test + public void testCreatePollResponseWithNullEvents() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PollResponse(null, "state")); + Assert.assertTrue("events".equals(exception.getMessage())); + } + + @Test + public void testCreatePollResponseWithNullState() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PollResponse(new ArrayList<>(), null)); + Assert.assertTrue("state".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/ReadingStateTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/ReadingStateTest.java new file mode 100644 index 00000000..7961ec95 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/stateful/ReadingStateTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.stateful; + +import org.junit.Assert; +import org.junit.Test; + +public class ReadingStateTest { + + @Test + public void testCreateReadingStateWithNullConnection() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new ReadingState(null)); + Assert.assertTrue("connection".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/AutoRollbackTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/AutoRollbackTest.java new file mode 100644 index 00000000..7dd0ed65 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/AutoRollbackTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.util; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class AutoRollbackTest { + + @Test + public void testCreateAutoRollbackWithNullConnection() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new AutoRollback(null)); + Assert.assertTrue("connection".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/FileNameWithOffsetTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/FileNameWithOffsetTest.java new file mode 100644 index 00000000..895a273b --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/FileNameWithOffsetTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.util; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class FileNameWithOffsetTest { + + @Test + public void testCreateFileNameWithOffsetWithNullFileName() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new FileNameWithOffset(null, 0)); + Assert.assertTrue("fileName".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/NonTransactionalEventWriterTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/NonTransactionalEventWriterTest.java new file mode 100644 index 00000000..58b12958 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/NonTransactionalEventWriterTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.util; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class NonTransactionalEventWriterTest { + + @Test + public void testCreateNonTransactionalEventWriterWithNull() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new NonTransactionalEventWriter(null)); + Assert.assertTrue("writer".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PersistentIdTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PersistentIdTests.java index 58066119..1d3643be 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PersistentIdTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PersistentIdTests.java @@ -9,6 +9,7 @@ */ package io.pravega.sensor.collector.util; +import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -44,4 +45,10 @@ public void persistentIdTest() throws SQLException { new File(fileName).delete(); } } + + @Test + public void testCreatePersistentIdWithNullConnection() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PersistentId(null)); + Assert.assertTrue("connection".equals(exception.getMessage())); + } } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PravegaWriterEventTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PravegaWriterEventTest.java new file mode 100644 index 00000000..e27a8842 --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/PravegaWriterEventTest.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.util; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class PravegaWriterEventTest { + + @Test + public void testCreatePravegaWriterEventWithNullRoutingKey() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PravegaWriterEvent(null, 1, new byte[]{})); + Assert.assertTrue("routingKey".equals(exception.getMessage())); + } + + @Test + public void testCreatePravegaWriterEventWithNullBytes() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new PravegaWriterEvent("routing-key", 1, null)); + Assert.assertTrue("bytes".equals(exception.getMessage())); + } +} diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionCoordinatorTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionCoordinatorTests.java index 9d81d9f8..0c8258b6 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionCoordinatorTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionCoordinatorTests.java @@ -10,6 +10,7 @@ package io.pravega.sensor.collector.util; import io.pravega.client.stream.TxnFailedException; +import org.junit.Assert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -277,5 +278,17 @@ public void testPerformRecoveryCommitWithOtherException() throws SQLException, T } + + @Test + public void testCreateTransactionCoordinatorWithNullConnection() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new TransactionCoordinator(null, eventWriter)); + Assert.assertTrue("connection".equals(exception.getMessage())); + } + + @Test + public void testCreateTransactionCoordinatorWithNullWriter() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new TransactionCoordinator(mockConnection, null)); + Assert.assertTrue("writer".equals(exception.getMessage())); + } } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionStateSQLiteImplTests.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionStateSQLiteImplTests.java index d6f98d43..07355f94 100644 --- a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionStateSQLiteImplTests.java +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionStateSQLiteImplTests.java @@ -12,11 +12,16 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.Assert; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.sql.Connection; import java.sql.SQLException; import java.util.HashSet; import java.util.List; @@ -25,10 +30,21 @@ public class TransactionStateSQLiteImplTests { private static final Logger log = LoggerFactory.getLogger(TransactionStateSQLiteImplTests.class); + @Mock + protected Connection mockConnection; + + @Mock + private EventWriter writer; + + @BeforeEach + public void setUp() throws SQLException { + MockitoAnnotations.initMocks(this); + } + @Test public void pendingFilesTest() throws SQLException { final String stateDatabaseFileName = ":memory:"; - final TransactionStateDB state = TransactionStateInMemoryImpl.create(stateDatabaseFileName); + final TransactionStateDB state = TransactionStateInMemoryImpl.create(stateDatabaseFileName, writer); Assertions.assertNull(state.getNextPendingFileRecord()); state.addPendingFileRecords(ImmutableList.of(new FileNameWithOffset("file1.csv", 0L))); Assertions.assertEquals(new ImmutablePair<>(new FileNameWithOffset("file1.csv", 0L), 0L), state.getNextPendingFileRecord()); @@ -41,7 +57,7 @@ public void pendingFilesTest() throws SQLException { @Test public void completedFilesTest() throws SQLException { final String stateDatabaseFileName = ":memory:"; - final TransactionStateInMemoryImpl state = TransactionStateInMemoryImpl.create(stateDatabaseFileName); + final TransactionStateInMemoryImpl state = TransactionStateInMemoryImpl.create(stateDatabaseFileName, writer); Assertions.assertNull(state.getNextPendingFileRecord()); state.addPendingFileRecords(ImmutableList.of(new FileNameWithOffset("file1.csv", 0L))); Assertions.assertEquals(new ImmutablePair<>(new FileNameWithOffset("file1.csv", 0L), 0L), state.getNextPendingFileRecord()); @@ -59,7 +75,7 @@ public void completedFilesTest() throws SQLException { @Test public void processFilesTest() throws SQLException { final String stateDatabaseFileName = ":memory:"; - final TransactionStateInMemoryImpl state = TransactionStateInMemoryImpl.create(stateDatabaseFileName); + final TransactionStateInMemoryImpl state = TransactionStateInMemoryImpl.create(stateDatabaseFileName, writer); Assertions.assertNull(state.getNextPendingFileRecord()); // Find 3 new files. state.addPendingFileRecords(ImmutableList.of(new FileNameWithOffset("file2.csv", 0L))); @@ -105,4 +121,17 @@ public void processFilesTest() throws SQLException { state.deleteCompletedFileRecord("file3.csv"); Assertions.assertTrue(state.getCompletedFileRecords().isEmpty()); } + + @Test + public void testCreateTransactionStateSQLiteImplWithNullConnection() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new TransactionStateSQLiteImpl(null, null)); + Assert.assertTrue("connection".equals(exception.getMessage())); + } + + @Test + public void testCreateTransactionStateSQLiteImplWithNullTransactionCoordinator() { + MockitoAnnotations.initMocks(this); + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new TransactionStateSQLiteImpl(mockConnection, null)); + Assert.assertTrue("transactionCoordinator".equals(exception.getMessage())); + } } diff --git a/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionalEventWriterTest.java b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionalEventWriterTest.java new file mode 100644 index 00000000..ac03a8ec --- /dev/null +++ b/pravega-sensor-collector/src/test/java/io/pravega/sensor/collector/util/TransactionalEventWriterTest.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.pravega.sensor.collector.util; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +public class TransactionalEventWriterTest { + + @Test + public void testCreateTransactionalEventWriterWithNullWriter() { + Exception exception = Assert.assertThrows(NullPointerException.class, () -> new TransactionalEventWriter(null)); + Assert.assertTrue("writer".equals(exception.getMessage())); + } +}