Skip to content

Commit

Permalink
fix(App): Using file output to workaround issues in windows java
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Dickerson committed May 16, 2024
1 parent d4f8b3f commit 0f2ccf5
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ coverage/
convertedAttachments/
integrationTest.sh
/*.csv
/login.txt
/meta.json

######################
# Maven
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bullhorn/dataloader/rest/CompleteUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bullhorn.dataloader.rest;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -137,7 +138,7 @@ private synchronized void writeResultsFile() {
try {
String resultsString = resultsWrapper.get().toString(2);
File file = new File(propertyFileUtil.getResultsFilePath());
FileUtils.writeStringToFile(file, resultsString, "UTF-8");
FileUtils.writeStringToFile(file, resultsString, StandardCharsets.UTF_8);
} catch (Exception e) {
printUtil.printAndLog("Error writing results file: " + e);
}
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/bullhorn/dataloader/service/LoginService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bullhorn.dataloader.service;

import com.bullhorn.dataloader.rest.RestSession;
import com.bullhorn.dataloader.util.FileUtil;
import com.bullhorn.dataloader.util.PrintUtil;

/**
Expand All @@ -18,16 +19,22 @@ public class LoginService implements Action {

@Override
public void run(String[] args) {
try {
restSession.getRestApi();
printUtil.printAndLog("Login Successful");
} catch (Exception e) {
printUtil.printAndLog("Login Failed");
}
String output = isLoginSuccessful() ? "Login Successful" : "Login Failed";
printUtil.printAndLog(output);
FileUtil.writeStringToFileAndLogException("login.txt", output, printUtil);
}

@Override
public boolean isValidArguments(String[] args) {
return true;
}

private boolean isLoginSuccessful() {
try {
restSession.getRestApi();
} catch (Exception e) {
return false;
}
return true;
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/bullhorn/dataloader/service/MetaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ public class MetaService implements Action {
@SuppressWarnings("unchecked")
public void run(String[] args) {
EntityInfo entityInfo = FileUtil.extractEntityFromFileName(args[1]);
String entityName = Objects.requireNonNull(entityInfo).getEntityName();
RestApi restApi = restSession.getRestApi();

try {
printUtil.log("Getting meta for " + Objects.requireNonNull(entityInfo).getEntityName() + "...");
printUtil.log("Getting meta for " + entityName + "...");
MetaData<?> metaData = restApi.getMetaData(entityInfo.getEntityClass(), MetaParameter.FULL, Sets.newHashSet(StringConsts.ALL_FIELDS));
enrichMeta(metaData);
JSONObject jsonMeta = metaToJson(metaData);
printUtil.print(jsonMeta.toString());
printUtil.log("Done generating meta for " + Objects.requireNonNull(entityInfo).getEntityName());
String jsonString = metaToJson(metaData).toString();
printUtil.print(jsonString);
FileUtil.writeStringToFileAndLogException("meta.json", jsonString, printUtil);
printUtil.log("Done generating meta for " + entityName);
} catch (Exception e) {
printUtil.printAndLog("ERROR: Failed to get Meta for " + Objects.requireNonNull(entityInfo).getEntityName());
printUtil.printAndLog("ERROR: Failed to get Meta for " + entityName);
printUtil.printAndLog(e);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/bullhorn/dataloader/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bullhorn.dataloader.util;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -9,6 +10,7 @@
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import com.bullhorn.dataloader.data.Row;
Expand Down Expand Up @@ -177,4 +179,16 @@ public static File getAttachmentFile(Row row) {
}
return attachmentFile;
}

/**
* Shortcut for writing to a file and logging exceptions
*/
public static void writeStringToFileAndLogException(String filename, String output, PrintUtil printUtil) {
try {
File file = new File(filename);
FileUtils.writeStringToFile(file, output, StandardCharsets.UTF_8);
} catch (Exception e) {
printUtil.log("Failed to write \"" + output + "\" to file: " + filename);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/bullhorn/dataloader/util/MethodUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static String findBestMatch(Set<String> fieldSet, String searchField) {
}

/**
* Converts the given string value to the given type, and if it's a date, using the given dateTimeFormatter.
* Converts the given string value to the given type, and if it is a date, using the given dateTimeFormatter.
* <p>
* If the date is being used to query for existing records, then it does not need to be in the form of
* the date time format, it can stay as a string until used in the find call.
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/bullhorn/dataloader/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -207,9 +208,9 @@ public static void replaceTextInFiles(File directory, String findText, String re
for (File file : directoryListing) {
String extension = FilenameUtils.getExtension(file.getPath());
if (extension.equalsIgnoreCase(StringConsts.CSV)) {
String content = FileUtils.readFileToString(file, "UTF-8");
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
content = content.replaceAll(findText, replaceText);
FileUtils.writeStringToFile(file, content, "UTF-8");
FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8);
} else if (file.isDirectory()) {
replaceTextInFiles(file, findText, replaceText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CsvFileReaderTest {
public void setup() throws IOException {
printUtilMock = mock(PrintUtil.class);

// Normally, we would mock out these low level dependencies, but it's in fact easier and more
// Normally, we would mock out these low level dependencies, but it is in fact easier and more
// straightforward to use the real objects when testing.
String path = TestUtils.getResourceFilePath("unitTest.properties");
Map<String, String> envVars = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -78,10 +79,10 @@ public void testIntegration() throws IOException {
// Test that incorrect capitalization will be fixed instead of cause errors
runAllCommandsAgainstDirectory(TestUtils.getResourceFilePath("capitalization"));

// Test that the byte order mark is ignored when it's present in the input file as the first (hidden) character
// Test that the byte order mark is ignored when it is present in the input file as the first (hidden) character
runAllCommandsAgainstDirectory(TestUtils.getResourceFilePath("byteOrderMark"));

// Test that country names are case insensitive
// Test that country names are case-insensitive
runAllCommandsAgainstDirectory(TestUtils.getResourceFilePath("countryNames"));

// Run a test for skipping updates (with the setting turned on)
Expand Down Expand Up @@ -142,7 +143,7 @@ public void testIntegration() throws IOException {
* 7. Load - Update
* 8. Delete
* <p> <p>
* The unique IDs of all of the entities are changed from `-ext-1` to something unique, after the examples have been
* The unique IDs of entities are changed from `-ext-1` to something unique, after the examples have been
* cloned to a test folder.
* <p> <p>
* Test assertions of both command line output and results files created. These steps cover the presence of records
Expand Down Expand Up @@ -172,7 +173,7 @@ private void runAllCommandsAgainstDirectory(String directoryPath) throws IOExcep
// region LOAD - INSERT
if (!skipInserts) {
FileUtils.deleteQuietly(new File(CsvFileWriter.RESULTS_DIR)); // Cleanup from previous runs
System.setIn(IOUtils.toInputStream("yes", "UTF-8")); // Accepts command for entire directory
System.setIn(IOUtils.toInputStream("yes", StandardCharsets.UTF_8)); // Accepts command for entire directory
consoleOutputCapturer.start();
Main.main(new String[]{"load", tempDirPath});
TestUtils.checkCommandLineOutput(consoleOutputCapturer.stop(), Result.Action.INSERT);
Expand Down Expand Up @@ -200,7 +201,7 @@ private void runAllCommandsAgainstDirectory(String directoryPath) throws IOExcep
// endregion

// region LOAD ATTACHMENTS - UPDATE
// Do not cleanup from previous run here - both Candidate and CandidateUpdate need to be present for delete step
// Do not clean up after previous run here - both Candidate and CandidateUpdate need to be present for delete step
consoleOutputCapturer.start();
Main.main(new String[]{"loadAttachments", tempAttachmentsDirectory.getPath() + "/CandidateUpdate.csv"});
TestUtils.checkCommandLineOutput(consoleOutputCapturer.stop(), Result.Action.UPDATE);
Expand All @@ -220,7 +221,7 @@ private void runAllCommandsAgainstDirectory(String directoryPath) throws IOExcep
// region EXPORT
if (!skipExports) {
FileUtils.deleteQuietly(new File(CsvFileWriter.RESULTS_DIR)); // Cleanup from previous runs
System.setIn(IOUtils.toInputStream("yes", "UTF-8")); // Accepts command for entire directory
System.setIn(IOUtils.toInputStream("yes", StandardCharsets.UTF_8)); // Accepts command for entire directory
consoleOutputCapturer.start();
Main.main(new String[]{"export", tempDirPath});
TestUtils.checkCommandLineOutput(consoleOutputCapturer.stop(), Result.Action.EXPORT);
Expand All @@ -234,7 +235,7 @@ private void runAllCommandsAgainstDirectory(String directoryPath) throws IOExcep
TestUtils.replaceTextInFiles(tempDirectory, "2001-01-01", "2002-02-02");

FileUtils.deleteQuietly(new File(CsvFileWriter.RESULTS_DIR)); // Cleanup from previous runs
System.setIn(IOUtils.toInputStream("yes", "UTF-8")); // Accepts command for entire directory
System.setIn(IOUtils.toInputStream("yes", StandardCharsets.UTF_8)); // Accepts command for entire directory
consoleOutputCapturer.start();
Main.main(new String[]{"load", tempDirPath});
Result.Action expectedAction = skipDuplicates ? Result.Action.SKIP : Result.Action.UPDATE;
Expand All @@ -248,7 +249,7 @@ private void runAllCommandsAgainstDirectory(String directoryPath) throws IOExcep
// Capture results file directory state
File[] resultsFiles = resultsDir.listFiles();

System.setIn(IOUtils.toInputStream("yes", "UTF-8")); // Accepts command for entire directory
System.setIn(IOUtils.toInputStream("yes", StandardCharsets.UTF_8)); // Accepts command for entire directory
consoleOutputCapturer.start();
Main.main(new String[]{"delete", CsvFileWriter.RESULTS_DIR});
TestUtils.checkCommandLineOutput(consoleOutputCapturer.stop(), Result.Action.DELETE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void setup() throws IOException, InterruptedException {
actionTotalsMock = mock(ActionTotals.class);
completeUtilMock = mock(CompleteUtil.class);
RestSession restSessionMock = mock(RestSession.class);
InputStream inputStreamFake = IOUtils.toInputStream("yes", "UTF-8");
InputStream inputStreamFake = IOUtils.toInputStream("yes", StandardCharsets.UTF_8);
printUtilMock = mock(PrintUtil.class);
processRunnerMock = mock(ProcessRunner.class);
PropertyFileUtil propertyFileUtilMock = mock(PropertyFileUtil.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void setup() throws Exception {
actionTotalsMock = mock(ActionTotals.class);
completeUtilMock = mock(CompleteUtil.class);
restSessionMock = mock(RestSession.class);
inputStreamFake = IOUtils.toInputStream("Yes!", "UTF-8");
inputStreamFake = IOUtils.toInputStream("Yes!", StandardCharsets.UTF_8);
printUtilMock = mock(PrintUtil.class);
processRunnerMock = mock(ProcessRunner.class);
propertyFileUtilMock = mock(PropertyFileUtil.class);
Expand Down Expand Up @@ -115,7 +116,7 @@ public void testRunDirectoryFourFiles() throws Exception {

@Test
public void testRunDirectoryFourFilesContinueNo() throws Exception {
inputStreamFake = IOUtils.toInputStream("No", "UTF-8");
inputStreamFake = IOUtils.toInputStream("No", StandardCharsets.UTF_8);
exportService = new ExportService(printUtilMock, propertyFileUtilMock, completeUtilMock, restSessionMock, processRunnerMock, inputStreamFake, timerMock);

final String filePath = TestUtils.getResourceFilePath("loadFromDirectory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void setup() throws IOException, InterruptedException {
actionTotalsMock = mock(ActionTotals.class);
completeUtilMock = mock(CompleteUtil.class);
restSessionMock = mock(RestSession.class);
inputStreamFake = IOUtils.toInputStream("Yes!", "UTF-8");
inputStreamFake = IOUtils.toInputStream("Yes!", StandardCharsets.UTF_8);
printUtilMock = mock(PrintUtil.class);
processRunnerMock = mock(ProcessRunner.class);
propertyFileUtilMock = mock(PropertyFileUtil.class);
Expand Down Expand Up @@ -131,7 +132,7 @@ public void testRunDirectoryFourFiles() throws IOException, InterruptedException

@Test
public void testRunDirectoryFourFilesContinueNo() throws IOException, InterruptedException {
inputStreamFake = IOUtils.toInputStream("No", "UTF-8");
inputStreamFake = IOUtils.toInputStream("No", StandardCharsets.UTF_8);
loadService = new LoadService(printUtilMock, propertyFileUtilMock, completeUtilMock, restSessionMock, processRunnerMock, inputStreamFake, timerMock);

final String filePath = TestUtils.getResourceFilePath("loadFromDirectory");
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/bullhorn/dataloader/util/FileUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bullhorn.dataloader.util;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.File;
import java.util.Arrays;
Expand All @@ -22,10 +24,12 @@
@SuppressWarnings("InstantiationOfUtilityClass")
public class FileUtilTest {

private PrintUtil printUtilMock;
private PropertyFileUtil propertyFileUtilMock;

@Before
public void setup() {
printUtilMock = mock(PrintUtil.class);
propertyFileUtilMock = mock(PropertyFileUtil.class);
}

Expand Down Expand Up @@ -146,4 +150,10 @@ public void testIsCsvFileNonCsvFile() {
String path = TestUtils.getResourceFilePath("unitTest.properties");
Assert.assertFalse(FileUtil.isCsvFile(path));
}

@Test
public void testWriteStringToFileAndLogException() {
FileUtil.writeStringToFileAndLogException(null, "this should fail", printUtilMock);
verify(printUtilMock, times(1)).log("Failed to write \"this should fail\" to file: null");
}
}

0 comments on commit 0f2ccf5

Please sign in to comment.