Skip to content

Commit

Permalink
Adding test cases for validation improvement (#51)
Browse files Browse the repository at this point in the history
Adding test cases for the validation improvement.

Signed-off-by: Amit-Singh40 <[email protected]>
  • Loading branch information
Amit-Singh40 authored Feb 7, 2024
1 parent 4a67317 commit 09726e9
Show file tree
Hide file tree
Showing 26 changed files with 730 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}

}
Original file line number Diff line number Diff line change
@@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package io.pravega.sensor.collector;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.net.URI;
Expand Down Expand Up @@ -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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}
Loading

0 comments on commit 09726e9

Please sign in to comment.