Skip to content

Commit

Permalink
fix the path in DataSet #8
Browse files Browse the repository at this point in the history
  • Loading branch information
antkorwin committed Dec 14, 2018
1 parent 1670ee3 commit 95a5f17
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.antkorwin.springtestmongo.internal;

import com.antkorwin.commonutils.exceptions.InternalException;
import com.antkorwin.commonutils.validation.Guard;
import org.apache.commons.io.IOUtils;

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

import com.antkorwin.commonutils.exceptions.InternalException;
import com.antkorwin.commonutils.validation.Guard;
import org.apache.commons.io.IOUtils;

import static com.antkorwin.springtestmongo.errorinfo.MongoDbErrorInfo.FILE_NOT_FOUND;
import static com.antkorwin.springtestmongo.errorinfo.MongoDbErrorInfo.READ_DATASETS_FILE_ERROR;

Expand All @@ -29,16 +29,22 @@ public String read() {
try (InputStream inputStream = getResourceStream()) {
Guard.check(inputStream != null, InternalException.class, FILE_NOT_FOUND);
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);

} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
throw new InternalException(READ_DATASETS_FILE_ERROR, e);
}
}

private InputStream getResourceStream() {
return ImportFile.class.getClass()
.getResourceAsStream(this.fileName);
}
String dataFileName = (!fileName.startsWith("/"))
? "/" + fileName
: fileName;

InputStream inputStream = getClass().getResourceAsStream(dataFileName);
if (inputStream == null) {
inputStream = getClass().getResourceAsStream("/dataset" + dataFileName);
}
return inputStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ void read() {
// Asserts
assertThat(text).isEqualTo("secret-123");
}

@Test
void readWithoutSlashInPath() {
// Arrange
Text fileText = new ImportFile("dataset/internal/test_file.txt");
// Act
String text = fileText.read();
// Asserts
assertThat(text).isEqualTo("secret-123");
}

@Test
void readWithoutDataSetInPath() {
// Arrange
Text fileText = new ImportFile("internal/test_file.txt");
// Act
String text = fileText.read();
// Asserts
assertThat(text).isEqualTo("secret-123");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void exportTest() throws IOException {
}

private String getExpectedResult() throws IOException {
try (InputStream inputStream = MongoDbTestIT.class.getResourceAsStream("/dataset/internal/json_expected.json")) {
try (InputStream inputStream = getClass().getResourceAsStream("/dataset/internal/json_expected.json")) {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ protected void after() {
}

private String getExpectedJson() throws IOException {
try (InputStream inputStream = MongoDbRuleExportDataSetIT.class.getClass()
.getResourceAsStream(INPUT_DATA_SET_FILE)) {
try (InputStream inputStream = getClass().getResourceAsStream(INPUT_DATA_SET_FILE)) {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static class ExportTestExtension implements Extension, AfterEachCallback, Before

@Override
public void afterEach(ExtensionContext context) throws Exception {
try (FileInputStream inputStream = FileUtils.openInputStream(new File(OUTPUT_FILE_NAME))){
try (FileInputStream inputStream = FileUtils.openInputStream(new File(OUTPUT_FILE_NAME))) {
assertThat(inputStream).isNotNull();

String stringDataSet = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
Expand All @@ -76,8 +76,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
}

private String getExpectedJson() throws IOException {
try (InputStream inputStream = ExportTestExtension.class.getClass()
.getResourceAsStream(INPUT_DATA_SET_FILE)){
try (InputStream inputStream = getClass().getResourceAsStream(INPUT_DATA_SET_FILE)) {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
Expand Down

0 comments on commit 95a5f17

Please sign in to comment.