From c0e7f2705a3375ee849f7211315ae7c04ddf72c6 Mon Sep 17 00:00:00 2001 From: Rafael Bey Date: Tue, 11 Jun 2024 15:56:57 -0400 Subject: [PATCH] Ensure filename matches entity path --- .../sdlc/entities/EntityReserializer.java | 9 +++++++- .../sdlc/entities/TestEntityReserializer.java | 21 +++++++++++++++++++ .../model/domain/classes/Firm.pure | 6 ++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 legend-sdlc-entity-maven-plugin/src/test/resources/simple-wrong-file-name/model/domain/classes/Firm.pure diff --git a/legend-sdlc-entity-maven-plugin/src/main/java/org/finos/legend/sdlc/entities/EntityReserializer.java b/legend-sdlc-entity-maven-plugin/src/main/java/org/finos/legend/sdlc/entities/EntityReserializer.java index bf122a4fea..a53df3c17d 100644 --- a/legend-sdlc-entity-maven-plugin/src/main/java/org/finos/legend/sdlc/entities/EntityReserializer.java +++ b/legend-sdlc-entity-maven-plugin/src/main/java/org/finos/legend/sdlc/entities/EntityReserializer.java @@ -14,6 +14,7 @@ package org.finos.legend.sdlc.entities; +import java.nio.file.Paths; import org.eclipse.collections.api.RichIterable; import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.list.MutableList; @@ -114,7 +115,13 @@ private List reserializeFile(Path sourceFile, Path targetDirectory, bool { if (enforceOneEntityPerFile) { - entities = Collections.singletonList(this.sourceSerializer.deserialize(inputStream)); + Entity singleEntity = this.sourceSerializer.deserialize(inputStream); + Path expectedPath = sourceSerializer.filePathForEntity(singleEntity, Paths.get("")); + if (!sourceFile.endsWith(expectedPath)) + { + throw new RuntimeException("Expected entity with path " + singleEntity.getPath() + " to be located on " + expectedPath); + } + entities = Collections.singletonList(singleEntity); } else { diff --git a/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityReserializer.java b/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityReserializer.java index 2a942066d3..cfcb7c391c 100644 --- a/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityReserializer.java +++ b/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityReserializer.java @@ -113,6 +113,27 @@ public void testTargetFileAlreadyExists() throws IOException e.getMessage()); } + @Test + public void testSourceFileDoesNotMatchEntityPath() throws IOException + { + EntityReserializer reserializer = EntityReserializer.newReserializer(new PureEntitySerializer(), EntitySerializers.getDefaultJsonSerializer()); + Path sourceDir = TestHelper.getPathFromResource("simple-wrong-file-name/model/domain/classes"); + Path targetDir = this.tempFolder.getRoot().toPath().resolve("target"); + + Assert.assertTrue(Files.isDirectory(sourceDir)); + Assert.assertTrue(Files.notExists(targetDir)); + + // if we enforce one entity per file, the path needs to match file location + RuntimeException e = Assert.assertThrows(RuntimeException.class, () -> reserializer.reserializeDirectoryTree(sourceDir, null, targetDir, true)); + Assert.assertEquals( + "Error deserializing entity from " + sourceDir.resolve(Paths.get("Firm.pure")) + ": Expected entity with path model::domain::classes::Firma to be located on " + Paths.get("model", "domain" , "classes", "Firma.pure"), + e.getMessage()); + + // if we don't enforce, it will allow + List paths = reserializer.reserializeDirectoryTree(sourceDir, null, targetDir, false); + Assert.assertEquals(Collections.singletonList("model::domain::classes::Firma"), paths); + } + @Test public void testMixedSourceDirectoryWithFiltering() throws IOException { diff --git a/legend-sdlc-entity-maven-plugin/src/test/resources/simple-wrong-file-name/model/domain/classes/Firm.pure b/legend-sdlc-entity-maven-plugin/src/test/resources/simple-wrong-file-name/model/domain/classes/Firm.pure new file mode 100644 index 0000000000..ca63c637f4 --- /dev/null +++ b/legend-sdlc-entity-maven-plugin/src/test/resources/simple-wrong-file-name/model/domain/classes/Firm.pure @@ -0,0 +1,6 @@ +Class model::domain::classes::Firma +{ + legalName: String[1]; + commonName: String[0..1]; + founded: StrictDate[0..1]; +}