Skip to content

Commit

Permalink
added method for reading the file and then checked if they equal to e…
Browse files Browse the repository at this point in the history
…ach other
  • Loading branch information
vsych321 committed Oct 1, 2023
1 parent d36c739 commit b45803b
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package core.basesyntax.service.impl;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import core.basesyntax.service.FileWriterService;
import core.basesyntax.storage.Storage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -40,4 +44,28 @@ void writer_NonExistingReportFile_notOk() {
assertThrows(
RuntimeException.class, () -> fileWriterService.writeToFile(report, actual));
}

@Test
void writer_WriteToFile_Ok() {
String expected = "fruit,quantity" + System.lineSeparator()
+ "banana, 152" + System.lineSeparator()
+ "apple, 90" + System.lineSeparator();
fileWriterService.writeToFile(expected, "src/test/resources/validReportFile.csv");
String actual = readFromFile("src/test/resources/validReportFile.csv");
assertEquals(expected, actual);

}

private String readFromFile(String fromFileName) {
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fromFileName))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append(System.lineSeparator());
}
return stringBuilder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit b45803b

Please sign in to comment.