Skip to content

Commit b1ce28e

Browse files
authored
fix: return empty document for non-existent file when parsing from path (#1197)
### Motivation The document api rewrite introduced a breaking change (which affects internals as well) as the `DocumentFactory#read(Path)` (prior `Document#read(Path)`) now throws an exception in case the file does not exist rather than returing a new, empty document. ### Modification Let `DocumentFactory#read(Path)` return a new, empty document in case the file does not exist or is not a file and change the specification of the method to reflect the change. ### Result The `DocumentFactory#read(Path)` method now returns an empty document in case the file does either not exist or is not a regular file.
1 parent 3c80d3a commit b1ce28e

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

driver/src/main/java/eu/cloudnetservice/driver/document/DocumentFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public interface DocumentFactory {
6262

6363
/**
6464
* Parses a document of the factory supported document type from the file at the given path. The given data must be
65-
* the root object of a document in order to work.
65+
* the root object of a document in order to work. Note: if the file at the given does not exist or the given path is
66+
* a directory, this method returns a new, empty document from this factory (as specified by {@link #newDocument()}).
67+
* However, this method invocation will fail in case the current jvm does not have the appropriate privileges that
68+
* would allow it open the file for reading.
6669
*
6770
* @param path the path to the file to parse.
6871
* @return a parsed document from the file at the given path.

driver/src/main/java/eu/cloudnetservice/driver/document/gson/GsonDocumentFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ private GsonDocumentFactory() {
8080
*/
8181
@Override
8282
public @NonNull Document.Mutable parse(@NonNull Path path) {
83-
try (var stream = Files.newInputStream(path)) {
84-
return this.parse(stream);
85-
} catch (IOException exception) {
86-
throw new DocumentParseException("Unable to parse document from path " + path, exception);
83+
if (Files.exists(path) && Files.isRegularFile(path)) {
84+
try (var stream = Files.newInputStream(path)) {
85+
return this.parse(stream);
86+
} catch (IOException exception) {
87+
throw new DocumentParseException("Unable to parse document from path " + path, exception);
88+
}
8789
}
90+
91+
// in case that the file does not exist just return an empty document
92+
return this.newDocument();
8893
}
8994

9095
/**

driver/src/test/java/eu/cloudnetservice/driver/document/DocumentSerialisationTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.nio.file.Path;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.UUID;
2627
import java.util.stream.Stream;
2728
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.Test;
2830
import org.junit.jupiter.api.io.CleanupMode;
2931
import org.junit.jupiter.api.io.TempDir;
3032
import org.junit.jupiter.params.ParameterizedTest;
@@ -67,6 +69,16 @@ static Stream<Arguments> serialisationInputSource() {
6769
StandardSerialisationStyle.COMPACT));
6870
}
6971

72+
@Test
73+
void testFileReadReturnsNewDocumentIfMissing() {
74+
var targetFile = Path.of("random_test_file_data_" + UUID.randomUUID());
75+
Assertions.assertFalse(Files.exists(targetFile));
76+
Assertions.assertFalse(Files.isRegularFile(targetFile));
77+
78+
var deserialized = Assertions.assertDoesNotThrow(() -> DocumentFactory.json().parse(targetFile));
79+
Assertions.assertTrue(deserialized.empty());
80+
}
81+
7082
@ParameterizedTest
7183
@MethodSource("serialisationInputSource")
7284
void testFileSerialisation(Document input, SerialisationStyle style) {

0 commit comments

Comments
 (0)