Search in sources :

Example 1 with TabularRecordStore

use of au.gov.asd.tac.constellation.graph.processing.TabularRecordStore in project constellation by constellation-app.

the class SaveResultsFileWriterNGTest method testWriteRecordStoreInvalidDataAccessResultsDir.

/**
 * Test of writeRecordStore method for case when
 * DataAccessPreferenceKeys.getDataAccessResultsDir() returns an invalid
 * directory. Because directory (and hence generated file path) is invalid,
 * and exception will be thrown and
 * ConstellationLoggerHelper.exportPropertyBuilder called with appropriate
 * ERROR status.
 */
@Test
public void testWriteRecordStoreInvalidDataAccessResultsDir() {
    Plugin plugin = new ExampleClass();
    TabularRecordStore tabularRecordStore = new TabularRecordStore();
    mockedDataAccessPreferenceKeys.when(() -> DataAccessPreferenceUtilities.getDataAccessResultsDir()).thenReturn(new File("/BADDIR/"));
    mockedConstellationLoggerHelper.when(() -> ConstellationLoggerHelper.exportPropertyBuilder(any(), any(), any(), any())).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        File passedFile = (File) args[2];
        constellationLoggerHelperStatus = (String) args[3];
        // Ensure the file was not created
        fileCreated = passedFile.exists();
        if (fileCreated) {
            passedFile.delete();
        }
        // Return properties (which are ignored)
        Properties properties = new Properties();
        return properties;
    });
    try {
        SaveResultsFileWriter.writeRecordStore(plugin, tabularRecordStore);
        // Execution should not make it this far, an invalid file should result in an exception.
        Assert.fail("PluginException is expected.");
    } catch (Exception ex) {
        mockedRecordStoreUtilities.verify(times(0), () -> RecordStoreUtilities.toCsv(Mockito.any(), Mockito.any()));
        Assert.assertFalse(fileCreated, "Record store file was not created.");
        Assert.assertEquals(constellationLoggerHelperStatus, ConstellationLoggerHelper.FAILURE, "ConstellationLoggerHelper passed status = FAILURE.");
    }
}
Also used : TabularRecordStore(au.gov.asd.tac.constellation.graph.processing.TabularRecordStore) Properties(java.util.Properties) File(java.io.File) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 2 with TabularRecordStore

use of au.gov.asd.tac.constellation.graph.processing.TabularRecordStore in project constellation by constellation-app.

the class SaveResultsFileWriterNGTest method testWriteRecordStore.

/**
 * Test of writeRecordStore method for case when
 * DataAccessPreferenceKeys.getDataAccessResultsDir() returns a valid
 * directory. Confirm file is created with expected contents and that no
 * exception is thrown. confirm
 * ConstellationLoggerHelper.exportPropertyBuilder called with appropriate
 * SUCCESS status. This test executes a code path through
 * SaveResultsFileWriter.writeRecordStore that does not throw an exception.
 *
 * @throws java.lang.Exception
 */
@Test
public void testWriteRecordStore() throws Exception {
    Plugin plugin = new ExampleClass();
    TabularRecordStore tabularRecordStore = new TabularRecordStore();
    String key = "TEST1KEY";
    String value = "TEST1VALUE";
    tabularRecordStore.add();
    tabularRecordStore.set(key, value);
    // This test actually creates the output file in user.home directory, checks its contents, and then removes it.
    mockedDataAccessPreferenceKeys.when(DataAccessPreferenceUtilities::getDataAccessResultsDir).thenReturn(new File(System.getProperty("user.home")));
    mockedRecordStoreUtilities.when(() -> RecordStoreUtilities.toCsv(Mockito.any(), Mockito.any())).thenCallRealMethod();
    mockedConstellationLoggerHelper.when(() -> ConstellationLoggerHelper.exportPropertyBuilder(any(), any(), any(), any())).thenAnswer((var invocation) -> {
        Object[] args = invocation.getArguments();
        File passedFile = (File) args[2];
        constellationLoggerHelperStatus = (String) args[3];
        // Ensure the file was created
        fileCreated = passedFile.exists();
        if (fileCreated) {
            // Read file and confirm it contains 3 rows
            int rows = 0;
            Scanner reader = new Scanner(passedFile);
            while (reader.hasNextLine()) {
                rows++;
                String data = reader.nextLine();
                if (rows == 1) {
                    dataValid = dataValid && (data.equals(key));
                }
                if (rows == 2) {
                    dataValid = dataValid && (data.equals(value));
                }
                if (rows >= 3) {
                    dataValid = false;
                }
            }
            if (rows == 0) {
                dataValid = false;
            }
            passedFile.delete();
        }
        // Return properties (which are ignored)
        Properties properties = new Properties();
        return properties;
    });
    SaveResultsFileWriter.writeRecordStore(plugin, tabularRecordStore);
    mockedRecordStoreUtilities.verify(times(1), () -> RecordStoreUtilities.toCsv(Mockito.any(), Mockito.any()));
    Assert.assertTrue(fileCreated, "Record store file was created.");
    Assert.assertEquals(constellationLoggerHelperStatus, ConstellationLoggerHelper.SUCCESS, "ConstellationLoggerHelper passed status = SUCCESS.");
    Assert.assertTrue(dataValid, "Record contents matches expected.");
}
Also used : TabularRecordStore(au.gov.asd.tac.constellation.graph.processing.TabularRecordStore) Scanner(java.util.Scanner) Properties(java.util.Properties) File(java.io.File) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 3 with TabularRecordStore

use of au.gov.asd.tac.constellation.graph.processing.TabularRecordStore in project constellation by constellation-app.

the class SaveResultsFileWriterNGTest method testWriteRecordStoreNoDataAccessResultsDir.

/**
 * Test of writeRecordStore method for case when
 * DataAccessPreferenceKeys.getDataAccessResultsDir() returns null. The
 * expectation is that no attempted writes are made. This test executes a
 * code path through SaveResultsFileWriter.writeRecordStore that does not
 * throw an exception.
 *
 * @throws java.lang.Exception
 */
@Test
public void testWriteRecordStoreNoDataAccessResultsDir() throws Exception {
    Plugin plugin = new ExampleClass();
    TabularRecordStore tabularRecordStore = new TabularRecordStore();
    mockedDataAccessPreferenceKeys.when(() -> DataAccessPreferenceUtilities.getDataAccessResultsDir()).thenReturn(null);
    SaveResultsFileWriter.writeRecordStore(plugin, tabularRecordStore);
    Assert.assertTrue(true, "Testing writing record to non existant directory - code will do nothing.");
}
Also used : TabularRecordStore(au.gov.asd.tac.constellation.graph.processing.TabularRecordStore) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Aggregations

TabularRecordStore (au.gov.asd.tac.constellation.graph.processing.TabularRecordStore)3 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)3 Test (org.testng.annotations.Test)3 File (java.io.File)2 Properties (java.util.Properties)2 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)1 Scanner (java.util.Scanner)1