Skip to content

Commit

Permalink
Merge pull request #9 from antkorwin/feature/fix-dataset-path
Browse files Browse the repository at this point in the history
Feature/fix dataset path
  • Loading branch information
antkorwin authored Dec 14, 2018
2 parents 1670ee3 + 0b1ff65 commit 86d7e30
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.antkorwin</groupId>
<artifactId>spring-test-mongo</artifactId>
<version>0.7-SNAPSHOT</version>
<version>0.8-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-test-mongo</name>
Expand Down
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 86d7e30

Please sign in to comment.