Skip to content

Commit c670b00

Browse files
authored
Feature/add validating files on import (#84)
* Add changes * Change project from spring to micronaut * Remove lombok because of problems with micronaut * Update bruno api endpoints * Fix some shading problems * Add graalvm config * Make graalvm native image work on for micronaut * Adjust testing WIP * Adjust Package Service Test file * Update ModelService test * Adjust testing files * Fix unit test on micronaut * Update pom.xml * Change test ttl file * Update ModelServiceTest.java * Update Movement.ttl * Upgrade to newest graalvm and esmf sdk version * Update test files to migrate * Use esmf java sdk substitution * Change pom for new graal version * Remove reduced pom xml from source code * Update to bruno and run it on pom.xml * Update github workflows * Fix workflow * Update mac and ubuntu runner * Update pom.xml * Fix bruno test * Update copyright and change some configuration * Add information into namespaces * Update pom.xml * Add better method to delete safe windows file when is locked * Change logic to delete files * Delete quitly * Add validate package before importing it * Fix package generation api
1 parent ee973b3 commit c670b00

File tree

4 files changed

+226
-185
lines changed

4 files changed

+226
-185
lines changed

aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/PackageService.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package org.eclipse.esmf.ame.services;
1515

16+
import java.io.ByteArrayInputStream;
1617
import java.io.File;
1718
import java.io.FileOutputStream;
1819
import java.io.IOException;
@@ -33,6 +34,7 @@
3334
import org.eclipse.esmf.ame.exceptions.FileNotFoundException;
3435
import org.eclipse.esmf.ame.services.models.Version;
3536
import org.eclipse.esmf.ame.services.utils.ModelGroupingUtils;
37+
import org.eclipse.esmf.ame.services.utils.ModelUtils;
3638
import org.eclipse.esmf.aspectmodel.AspectModelFile;
3739
import org.eclipse.esmf.aspectmodel.edit.AspectChangeManager;
3840
import org.eclipse.esmf.aspectmodel.edit.AspectChangeManagerConfig;
@@ -46,6 +48,7 @@
4648
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
4749
import org.eclipse.esmf.aspectmodel.resolver.fs.ModelsRoot;
4850
import org.eclipse.esmf.aspectmodel.resolver.fs.StructuredModelsRoot;
51+
import org.eclipse.esmf.aspectmodel.resolver.modelfile.RawAspectModelFile;
4952
import org.eclipse.esmf.aspectmodel.resolver.modelfile.RawAspectModelFileBuilder;
5053
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
5154
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
@@ -80,7 +83,38 @@ public byte[] exportPackage( final String aspectModelUrn ) {
8083
.orElseThrow( () -> new FileNotFoundException( String.format( "No file found for %s", aspectModelUrn ) ) ).getContent();
8184
}
8285

83-
public Map<String, List<Version>> importPackage( final CompletedFileUpload zipFile, final List<String> filesToImport ) {
86+
public List<Map<String, String>> validatePackage( final CompletedFileUpload zipFile ) {
87+
try {
88+
final byte[] zipContent = IOUtils.toByteArray( zipFile.getInputStream() );
89+
final NamespacePackage namespacePackage = new NamespacePackage( zipContent, null );
90+
91+
return namespacePackage.loadContents().flatMap( file -> {
92+
final ByteArrayInputStream inputStream =
93+
ModelUtils.createInputStream( ( (RawAspectModelFile) file ).sourceRepresentation() );
94+
final AspectModel aspectModel = aspectModelLoader.load( inputStream );
95+
if ( aspectModel != null ) {
96+
return aspectModel.files().stream()
97+
.flatMap( aspectModelFile -> {
98+
try {
99+
return aspectModelLoader.load( aspectModelFile.elements().getFirst().urn() )
100+
.files().stream().map( AspectModelFile::filename )
101+
.filter( Optional::isPresent ).map( Optional::get );
102+
} catch ( final ModelResolutionException e ) {
103+
LOG.info( "Ignoring Exception" );
104+
return Stream.empty();
105+
}
106+
} );
107+
}
108+
return Stream.empty();
109+
} )
110+
.map( filename -> Map.of( "model", filename ) )
111+
.toList();
112+
} catch ( final IOException e ) {
113+
throw new ModelResolutionException( "Could not read from input", e );
114+
}
115+
}
116+
117+
public Map<String, List<Version>> importPackage( final CompletedFileUpload zipFile ) {
84118
try {
85119
final ModelsRoot modelsRoot = new StructuredModelsRoot( this.modelPath );
86120
final byte[] zipContent = IOUtils.toByteArray( zipFile.getInputStream() );
@@ -92,9 +126,7 @@ public Map<String, List<Version>> importPackage( final CompletedFileUpload zipFi
92126
final AspectChangeManager changeManager = initChangeManager();
93127
changeManager.applyChange( new ChangeGroup( fileChanges ) );
94128

95-
final Stream<AspectModelFile> selectedFiles = filterImportedFiles( changeManager, filesToImport );
96-
97-
final Stream<URI> savedUris = saveAspectModelFiles( selectedFiles );
129+
final Stream<URI> savedUris = saveAspectModelFiles( changeManager.aspectModelFiles() );
98130

99131
return new ModelGroupingUtils( this.aspectModelLoader, this.modelPath ).groupModelsByNamespaceAndVersion( savedUris );
100132
} catch ( final IOException e ) {
@@ -116,11 +148,6 @@ private AspectChangeManager initChangeManager() {
116148
return new AspectChangeManager( config, new AspectModelLoader().emptyModel() );
117149
}
118150

119-
private Stream<AspectModelFile> filterImportedFiles( final AspectChangeManager changeManager, final List<String> filesToImport ) {
120-
return changeManager.aspectModelFiles().filter(
121-
file -> filesToImport.stream().anyMatch( path -> file.sourceLocation().map( URI::toString ).orElse( "" ).contains( path ) ) );
122-
}
123-
124151
private Stream<URI> saveAspectModelFiles( final Stream<AspectModelFile> files ) {
125152
return files.peek( this::ensureParentDirectoryExists ).peek( AspectSerializer.INSTANCE::write ).map( AspectModelFile::sourceLocation )
126153
.filter( Optional::isPresent ).map( Optional::get );

aspect-model-editor-service/src/test/java/org/eclipse/esmf/ame/services/PackageServiceTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.nio.file.Path;
2727
import java.nio.file.Paths;
2828
import java.util.Arrays;
29-
import java.util.List;
3029
import java.util.Objects;
3130
import java.util.Optional;
3231
import java.util.zip.ZipEntry;
@@ -81,8 +80,7 @@ void testImportAspectModelPackage() throws IOException {
8180

8281
final CompletedFileUpload mockedZipFile = new MockFileUpload( "TestArchive.zip", testPackage, MediaType.APPLICATION_PDF_TYPE );
8382

84-
packageService.importPackage( mockedZipFile,
85-
List.of( "org.eclipse.esmf.example/1.0.0/Esmf.ttl", "org.eclipse.esmf.test/1.0.0/Movement.ttl" ) );
83+
packageService.importPackage( mockedZipFile );
8684

8785
try ( final ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream( testPackage ) ) ) {
8886
ZipEntry entry;

0 commit comments

Comments
 (0)