Skip to content

Commit

Permalink
Ensure filename matches entity path
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Jun 11, 2024
1 parent ca0d784 commit c0e7f27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,7 +115,13 @@ private List<String> 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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> paths = reserializer.reserializeDirectoryTree(sourceDir, null, targetDir, false);
Assert.assertEquals(Collections.singletonList("model::domain::classes::Firma"), paths);
}

@Test
public void testMixedSourceDirectoryWithFiltering() throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Class model::domain::classes::Firma
{
legalName: String[1];
commonName: String[0..1];
founded: StrictDate[0..1];
}

0 comments on commit c0e7f27

Please sign in to comment.