Skip to content

Commit

Permalink
fix tests in jpo-ode-svcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mwodahl committed Jun 30, 2023
1 parent 00ba4d6 commit 30897f2
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.Set;

import org.apache.kafka.clients.producer.Producer;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;

import mockit.Expectations;
Expand All @@ -44,7 +44,7 @@ public class SerializableMessageProducerPoolTest {
@Injectable
OdeProperties mockOdeProperties;

@Before
@BeforeEach
public void setUp() {
new Expectations() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package us.dot.its.jpo.ode.coder.stream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -24,7 +25,7 @@
import java.util.List;

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

import mockit.Expectations;
import mockit.Injectable;
Expand Down Expand Up @@ -94,38 +95,42 @@ public void testPublishEOF(@Mocked LogFileParser mockLogFileParser) throws Excep
assertTrue(dataList.isEmpty());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testPublishThrowsIllegalArgumentException() throws Exception {
// If the filename does not follow expected filename pattern,
// IllegalArgumentException should be thrown
testLogFileToAsn1CodecPublisher.publish(new BufferedInputStream(new ByteArrayInputStream(new byte[0])),
"fileName", ImporterFileType.LEAR_LOG_FILE);
fail("Expected an IllegalArgumentException to be thrown");
assertThrows(IllegalArgumentException.class, () -> {
// If the filename does not follow expected filename pattern,
// IllegalArgumentException should be thrown
testLogFileToAsn1CodecPublisher.publish(new BufferedInputStream(new ByteArrayInputStream(new byte[0])),
"fileName", ImporterFileType.LEAR_LOG_FILE);
fail("Expected an IllegalArgumentException to be thrown");
});
}

@Test(expected = LogFileToAsn1CodecPublisherException.class)
@Test
public void testPublishThrowsLogFileToAsn1CodecPublisherException(@Mocked LogFileParser mockLogFileParser)
throws Exception {
new Expectations() {
{
LogFileParser.factory(anyString);
result = mockLogFileParser;

/*
* If the embedded parser fails to parse a log file header, it may throw an
* exception
* which is then caught by the parser and re-thrown as
* LogFileToAsn1CodecPublisherException.
* This mocked object will simulate that eventuality.
*/
mockLogFileParser.parseFile((BufferedInputStream) any, anyString);
result = new LogFileToAsn1CodecPublisherException(anyString, (Exception) any);
}
};

testLogFileToAsn1CodecPublisher.publish(new BufferedInputStream(new ByteArrayInputStream(new byte[0])),
"fileName", ImporterFileType.LEAR_LOG_FILE);
fail("Expected an LogFileToAsn1CodecPublisherException to be thrown");
assertThrows(LogFileToAsn1CodecPublisherException.class, () -> {
new Expectations() {
{
LogFileParser.factory(anyString);
result = mockLogFileParser;

/*
* If the embedded parser fails to parse a log file header, it may throw an
* exception
* which is then caught by the parser and re-thrown as
* LogFileToAsn1CodecPublisherException.
* This mocked object will simulate that eventuality.
*/
mockLogFileParser.parseFile((BufferedInputStream) any, anyString);
result = new LogFileToAsn1CodecPublisherException(anyString, (Exception) any);
}
};

testLogFileToAsn1CodecPublisher.publish(new BufferedInputStream(new ByteArrayInputStream(new byte[0])),
"fileName", ImporterFileType.LEAR_LOG_FILE);
fail("Expected an LogFileToAsn1CodecPublisherException to be thrown");
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import javax.websocket.Session;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;

import mockit.Expectations;
Expand All @@ -48,7 +48,7 @@ public class DdsDepositRequestManagerTest {
@Mocked OdeProperties mockOdeProperties;
@Mocked Logger mockLogger;

@Before
@BeforeEach
public void setup() {
new Expectations() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

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

import mockit.Capturing;
import mockit.Expectations;
Expand Down Expand Up @@ -60,7 +59,7 @@ public class ImporterDirectoryWatcherTest {
@Mocked
ScheduledExecutorService mockScheduledExecutorService;

@Before
// @BeforeEach
public void testConstructor() throws IOException {
new Expectations() {
{
Expand All @@ -74,14 +73,21 @@ public void testConstructor() throws IOException {
}

@Test
public void testRun() throws InterruptedException {
public void testRun() throws InterruptedException, IOException {
new Expectations() {
{
OdeFileUtils.createDirectoryRecursively((Path) any);
times = 3;

Executors.newScheduledThreadPool(1);
result = mockScheduledExecutorService;

mockScheduledExecutorService.scheduleWithFixedDelay((Runnable) any, anyLong, anyLong, TimeUnit.SECONDS);

mockScheduledExecutorService.awaitTermination(anyLong, TimeUnit.SECONDS);
}
};
testImporterDirectoryWatcher = new ImporterDirectoryWatcher(injectableOdeProperties, backupDir, failureDir, backupDir, injectableImporterFileType, timePeriod);

testImporterDirectoryWatcher.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
package us.dot.its.jpo.ode.importer;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertTimeout;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.time.Duration;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import mockit.Capturing;
import mockit.Expectations;
Expand All @@ -35,7 +39,7 @@
import us.dot.its.jpo.ode.OdeProperties;
import us.dot.its.jpo.ode.importer.ImporterDirectoryWatcher.ImporterFileType;

@Ignore
@Disabled
public class ImporterDirectoryWatcherTestOld {

ImporterDirectoryWatcher testImporterDirectoryWatcher;
Expand Down Expand Up @@ -63,20 +67,22 @@ public class ImporterDirectoryWatcherTestOld {
@Capturing
ImporterProcessor capturingImporterProcessor;

@Before
@BeforeEach
public void createTestObject() {
try {
new Expectations() {
{
OdeFileUtils.createDirectoryRecursively((Path) any);
times = 3;
}
};
} catch (IOException e) {
fail("Unexpected exception in expectations block: " + e);
if (testImporterDirectoryWatcher == null) {
try {
new Expectations() {
{
OdeFileUtils.createDirectoryRecursively((Path) any);
times = 3;
}
};
} catch (IOException e) {
fail("Unexpected exception in expectations block: " + e);
}
testImporterDirectoryWatcher = new ImporterDirectoryWatcher(injectableOdeProperties, mockDir, backupDir, failureDir, ImporterFileType.LEAR_LOG_FILE, timePeriod);
testImporterDirectoryWatcher.setWatching(false);
}
testImporterDirectoryWatcher = new ImporterDirectoryWatcher(injectableOdeProperties, mockDir, backupDir, failureDir, ImporterFileType.LEAR_LOG_FILE, timePeriod);
testImporterDirectoryWatcher.setWatching(false);
}

@Test
Expand All @@ -94,34 +100,37 @@ public void testConstructorOdeUtilsException() {
new ImporterDirectoryWatcher(injectableOdeProperties, mockDir, backupDir, failureDir, ImporterFileType.LEAR_LOG_FILE, timePeriod);
}

@Test(timeout = 4000)
@Test
public void runShouldCatchException() {
try {
new Expectations() {
{
mockDir.register((WatchService) any, (Kind<?>) any);
result = null;
}
};
} catch (IOException e) {
fail("Unexpected exception in expectations block: " + e);
}
testImporterDirectoryWatcher.run();
assertTimeout(Duration.ofMillis(4000), () -> {
try {
new Expectations() {
{
mockDir.register((WatchService) any, (Kind<?>) any);
result = null;
}
};
} catch (IOException e) {
fail("Unexpected exception in expectations block: " + e);
}
testImporterDirectoryWatcher.run();
});
}

@Test(timeout = 4000)
@Test
@Timeout(4)
public void shouldRunNoProblems() {
try {
new Expectations() {
{
mockDir.register((WatchService) any, (Kind<?>) any);
result = mockWatchKey;
}
};
} catch (IOException e) {
fail("Unexpected exception in expectations block: " + e);
}
testImporterDirectoryWatcher.run();
try {
new Expectations() {
{
mockDir.register((WatchService) any, (Kind<?>) any);
result = mockWatchKey;
}
};
} catch (IOException e) {
fail("Unexpected exception in expectations block: " + e);
}
testImporterDirectoryWatcher.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.nio.file.Path;
import java.util.Iterator;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import mockit.Capturing;
import mockit.Expectations;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class ImporterProcessorTest {
Path injectableFailureDir;


@Test @Ignore
@Test @Disabled
public void processExistingFilesShouldCatchExceptionFailedToCreateStream() {

try {
Expand All @@ -85,7 +85,7 @@ public void processExistingFilesShouldCatchExceptionFailedToCreateStream() {
testImporterProcessor.processDirectory(injectableDir, injectableBackupDir, injectableFailureDir);
}

@Test @Ignore
@Test @Disabled
public void processExistingFilesShouldProcessOneFile(@Mocked DirectoryStream<Path> mockDirectoryStream,
@Mocked Iterator<Path> mockIterator) {

Expand All @@ -109,7 +109,7 @@ public void processExistingFilesShouldProcessOneFile(@Mocked DirectoryStream<Pat
testImporterProcessor.processDirectory(injectableDir, injectableBackupDir, injectableFailureDir);
}

@Test @Ignore
@Test @Disabled
public void processAndBackupFileFileShouldCatchExceptionStream() {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;

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

import us.dot.its.jpo.ode.importer.parser.FileParser.FileParserException;
import us.dot.its.jpo.ode.importer.parser.FileParser.ParserStatus;
Expand Down Expand Up @@ -73,10 +74,12 @@ public void testFactory_driverAlert() {
assertEquals(recordType, parser.getRecordType());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testFactoryThrowsException() {
LogFileParser.factory("invalidFileName");
fail("Expected IllegalArgumentException");
assertThrows(IllegalArgumentException.class, () -> {
LogFileParser.factory("invalidFileName");
fail("Expected IllegalArgumentException");
});
}

@Test
Expand Down
Loading

0 comments on commit 30897f2

Please sign in to comment.