From 2321daf6572d236753b1959434436985efe68042 Mon Sep 17 00:00:00 2001 From: Rafael Bey Date: Wed, 12 Jun 2024 13:45:37 -0400 Subject: [PATCH] validate against the real source directory --- .../finos/legend/sdlc/entities/EntityReserializer.java | 9 ++++----- .../org/finos/legend/sdlc/entities/TestEntityMojo.java | 8 ++++---- .../legend/sdlc/entities/TestEntityReserializer.java | 7 ++++--- 3 files changed, 12 insertions(+), 12 deletions(-) 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 a53df3c17d..96262d2aa0 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,7 +14,6 @@ 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; @@ -93,7 +92,7 @@ public List reserializeDirectoryTree(Path sourceDirectory, Predicate getDefaultExtensionFilter() return getExtensionFilter(this.sourceSerializer.getDefaultFileExtension()); } - private List reserializeFile(Path sourceFile, Path targetDirectory, boolean enforceOneEntityPerFile) throws IOException + private List reserializeFile(Path sourceDirectory, Path sourceFile, Path targetDirectory, boolean enforceOneEntityPerFile) throws IOException { LOGGER.debug("Reading {}", sourceFile); List entities; @@ -116,8 +115,8 @@ private List reserializeFile(Path sourceFile, Path targetDirectory, bool if (enforceOneEntityPerFile) { Entity singleEntity = this.sourceSerializer.deserialize(inputStream); - Path expectedPath = sourceSerializer.filePathForEntity(singleEntity, Paths.get("")); - if (!sourceFile.endsWith(expectedPath)) + Path expectedPath = sourceSerializer.filePathForEntity(singleEntity, sourceDirectory); + if (!sourceFile.equals(expectedPath)) { throw new RuntimeException("Expected entity with path " + singleEntity.getPath() + " to be located on " + expectedPath); } diff --git a/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityMojo.java b/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityMojo.java index 3598b668a3..8f8f39c05a 100644 --- a/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityMojo.java +++ b/legend-sdlc-entity-maven-plugin/src/test/java/org/finos/legend/sdlc/entities/TestEntityMojo.java @@ -65,12 +65,12 @@ public void testNoSourceDirectoriesConfigured() throws Exception TestHelper.assertDirectoryEmptyOrNonExistent(outputDir); // Legend source directory exists (found as a default source directory) - Path simpleJsonModelDir = TestHelper.getPathFromResource("simple-json-model"); - TestHelper.copyDirectoryTree(simpleJsonModelDir, Files.createDirectories(srcMain.resolve("legend"))); + Path simpleJsonModelDir = TestHelper.getPathFromResource("simple-json-model/entities"); + TestHelper.copyDirectoryTree(simpleJsonModelDir, Files.createDirectories(srcMain.resolve(Paths.get("legend")))); TestHelper.assertDirectoryEmptyOrNonExistent(outputDir); this.mojoRule.executeMojo(projectDir, GOAL); - Map expectedEntities = TestHelper.loadEntities(simpleJsonModelDir); + Map expectedEntities = TestHelper.loadEntities(TestHelper.getPathFromResource("simple-json-model")); TestHelper.assertDirectoryTreeFilePaths( Iterate.collect(expectedEntities.keySet(), p -> Paths.get("entities" + outputDir.getFileSystem().getSeparator() + p.replace(EntityPaths.PACKAGE_SEPARATOR, outputDir.getFileSystem().getSeparator()) + ".json"), Sets.mutable.empty()), outputDir); @@ -278,7 +278,7 @@ public void testMultipleSourceDirectoriesWithConflict() throws Exception Path outputDir = new File(mavenProject.getBuild().getOutputDirectory()).toPath(); Path srcMain = projectDir.toPath().resolve("src").resolve("main"); - TestHelper.copyResourceDirectoryTree("simple-json-model", Files.createDirectories(srcMain.resolve("legend"))); + TestHelper.copyResourceDirectoryTree("simple-json-model/entities", Files.createDirectories(srcMain.resolve("legend"))); TestHelper.copyResourceDirectoryTree("simple-pure-model/model/domain/enums", Files.createDirectories(srcMain.resolve(Paths.get("pure", "model", "domain", "enums")))); TestHelper.assertDirectoryEmptyOrNonExistent(outputDir); 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 592fcfae3a..88a72447c6 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 @@ -98,13 +98,14 @@ public void testPureDomainDirectory() throws IOException public void testTargetFileAlreadyExists() throws IOException { EntityReserializer reserializer = EntityReserializer.newReserializer(new PureEntitySerializer(), EntitySerializers.getDefaultJsonSerializer()); - Path sourceDir = TestHelper.getPathFromResource("simple-pure-model/model/domain/enums"); + Path sourceDir = TestHelper.getPathFromResource("simple-pure-model"); + Path toFilterPath = TestHelper.getPathFromResource("simple-pure-model/model/domain/enums"); Path targetDir = this.tempFolder.getRoot().toPath().resolve("target"); Assert.assertTrue(Files.isDirectory(sourceDir)); Assert.assertTrue(Files.notExists(targetDir)); - List paths = reserializer.reserializeDirectoryTree(sourceDir, targetDir); + List paths = reserializer.reserializeDirectoryTree(sourceDir, x -> x.startsWith(toFilterPath), targetDir); Assert.assertEquals(Collections.singletonList("model::domain::enums::AddressType"), paths); IOException e = Assert.assertThrows(IOException.class, () -> reserializer.reserializeDirectoryTree(sourceDir, targetDir)); @@ -126,7 +127,7 @@ public void testSourceFileDoesNotMatchEntityPath() throws IOException // 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"), + "Error deserializing entity from " + sourceDir.resolve(Paths.get("Firm.pure")) + ": Expected entity with path model::domain::classes::Firma to be located on " + sourceDir.resolve(Paths.get("model", "domain", "classes", "Firma.pure")), e.getMessage()); // if we don't enforce, it will allow