<commit_before><commit_msg>ODE-217 Added tests for ImporterWatchService.java<commit_after>package us.dot.its.jpo.ode.importer;

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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.Test;
import org.slf4j.Logger;

import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Tested;
import us.dot.its.jpo.ode.coder.AbstractCoder;

public class ImporterWatchServiceTest {

    @Tested // (fullyInitialized=false)
    ImporterWatchService testImporterWatchService;
    @Injectable
    Path dir;
    @Injectable
    Path backupDir;
    @Injectable
    AbstractCoder coder;
    @Injectable
    Logger logger;
    @Injectable
    String kafkaTopic = "foo";

    @Test
    public void testRun() {
        testImporterWatchService.run();
    }

    @Test
    public void createDirectoryRecursivelyShouldThrowExceptionDirDoesNotExist() {
        try {
            testImporterWatchService.createDirectoryRecursively(dir);
            fail("Expected IOException directory does not exist");
        } catch (Exception e) {
            assertEquals(IOException.class, e.getClass());
            assertEquals("Failed to verify directory creation - directory does not exist.", e.getMessage());
        }
    }

    @Test
    public void createDirectoryRecursivelyShouldThrowExceptionUnableToCreateDirectory(@Mocked final Files unused) {

        try {
            new Expectations() {
                {
                    Files.createDirectories((Path) any);
                    result = new IOException("testException123");
                }
            };
        } catch (IOException e) {
            fail("Unexpected exception in expectations block: " + e);
        }

        try {
            testImporterWatchService.createDirectoryRecursively(dir);
            fail("Expected IOException while trying to create directory:");
        } catch (Exception e) {
            assertEquals(IOException.class, e.getClass());
            assertTrue(e.getMessage().startsWith("Exception while trying to create directory:"));
        }
    }

    @Test
    public void testCreateDirectoryRecursively() {
        new Expectations() {
            {
                dir.toFile().exists();
                result = true;
            }
        };

        try {
            testImporterWatchService.createDirectoryRecursively(dir);
        } catch (Exception e) {
            fail("Unexpected exception: " + e);
        }
    }

    @Test
    public void backupFileShouldThrowExceptionBackupDirDoesNotExist(@Mocked Path mockFile) {

        new Expectations() {
            {
                backupDir.toFile().exists();
                result = false;
            }
        };

        try {
            testImporterWatchService.backupFile(mockFile, backupDir);
            fail("Expected IOException while trying to backup file:");
        } catch (Exception e) {
            assertEquals(IOException.class, e.getClass());
            assertTrue(e.getMessage().startsWith("Backup directory does not exist:"));
        }
    }

    @Test
    public void backupFileShouldThrowExceptionUnableToMoveFile(@Mocked final Files unused, @Injectable Path mockFile) {

        try {
            new Expectations() {
                {
                    backupDir.toFile().exists();
                    result = true;

                    Files.move((Path) any, (Path) any, (CopyOption) any);
                    result = new IOException("testException123");
                }
            };
        } catch (IOException e) {
            fail("Unexpected exception in expectations block: " + e);
        }

        try {
            testImporterWatchService.backupFile(mockFile, backupDir);
            fail("Expected IOException while trying to move file:");
        } catch (Exception e) {
            assertEquals(IOException.class, e.getClass());
            assertTrue(e.getMessage().startsWith("Unable to move file to backup:"));
        }
    }

    @Test
    public void testBackupFile(@Mocked final Files unused, @Injectable Path mockFile) {

        new Expectations() {
            {
                backupDir.toFile().exists();
                result = true;
            }
        };

        try {
            testImporterWatchService.backupFile(mockFile, backupDir);
        } catch (Exception e) {
            fail("Unexpected exception: " + e);
        }
    }
    
    @Test
    public void processExistingFilesShouldCatchExceptionFailedToCreateStream(@Mocked final Files unused) {
        
        try {
            new Expectations() {{
                Files.newDirectoryStream((Path) any);
                result = null;
            }};
        } catch (IOException e) {
            fail("Unexpected exception in expectations block: " + e);
        }
        
        testImporterWatchService.processExistingFiles();
    }
    
    @Test
    public void processFileShouldCatchExceptionUnableToOpenFile(@Injectable Path mockFile) {
        
        new Expectations() {{
            mockFile.toFile();
            result = new IOException("testException123");
        }};
        testImporterWatchService.processFile(mockFile);
    }
    

}
{'fraction_non_alphanumeric': 0.09346826126954921, 'fraction_numerical': 0.002391904323827047, 'mean_word_length': 2.0677200902934536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2374643', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1363, 'n_words': 304}