Skip to content

Commit

Permalink
validate against the real source directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Jun 12, 2024
1 parent fafe9bf commit 2321daf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,7 +92,7 @@ public List<String> reserializeDirectoryTree(Path sourceDirectory, Predicate<? s
}
else if ((filter == null) || filter.test(entry))
{
entityPaths.addAll(reserializeFile(entry, targetEntitiesDirectory, enforceOneEntityPerFile));
entityPaths.addAll(reserializeFile(sourceDirectory, entry, targetEntitiesDirectory, enforceOneEntityPerFile));
}
}
}
Expand All @@ -107,7 +106,7 @@ public Predicate<Path> getDefaultExtensionFilter()
return getExtensionFilter(this.sourceSerializer.getDefaultFileExtension());
}

private List<String> reserializeFile(Path sourceFile, Path targetDirectory, boolean enforceOneEntityPerFile) throws IOException
private List<String> reserializeFile(Path sourceDirectory, Path sourceFile, Path targetDirectory, boolean enforceOneEntityPerFile) throws IOException
{
LOGGER.debug("Reading {}", sourceFile);
List<Entity> entities;
Expand All @@ -116,8 +115,8 @@ private List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Entity> expectedEntities = TestHelper.loadEntities(simpleJsonModelDir);
Map<String, Entity> 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);
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> paths = reserializer.reserializeDirectoryTree(sourceDir, targetDir);
List<String> 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));
Expand All @@ -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
Expand Down

0 comments on commit 2321daf

Please sign in to comment.