Skip to content

Commit 8d010fe

Browse files
committed
Update tests, update AspectModelLoader
1 parent 2e74ea1 commit 8d010fe

File tree

10 files changed

+136
-33
lines changed

10 files changed

+136
-33
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoader.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ private AspectModel loadNamespacePackageFromStream( final InputStream inputStrea
227227

228228
while ( (entry = zis.getNextEntry()) != null ) {
229229
boolean isRelevantEntry =
230-
(hasAspectModelsFolder && entry.getName().startsWith( ASPECT_MODELS_FOLDER ) && entry.getName().endsWith( ".ttl" )) || (
231-
!hasAspectModelsFolder && entry.getName().endsWith( ".ttl" ));
230+
(hasAspectModelsFolder && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) && entry.getName()
231+
.endsWith( ".ttl" ))
232+
|| (!hasAspectModelsFolder && entry.getName().endsWith( ".ttl" ));
232233

233234
if ( isRelevantEntry ) {
234235
AspectModelFile aspectModelFile = migrate( AspectModelFileLoader.load( zis ) );
@@ -244,14 +245,14 @@ private AspectModel loadNamespacePackageFromStream( final InputStream inputStrea
244245

245246
LoaderContext loaderContext = new LoaderContext();
246247
resolve( aspectModelFiles, loaderContext );
247-
return buildAspectModel( aspectModelFiles );
248+
return buildAspectModel( loaderContext.loadedFiles() );
248249
}
249250

250251
private boolean containsFolderInNamespacePackage( final InputStream inputStream ) {
251252
try ( ZipInputStream zis = new ZipInputStream( inputStream ) ) {
252253
ZipEntry entry;
253254
while ( (entry = zis.getNextEntry()) != null ) {
254-
if ( entry.isDirectory() && entry.getName().equals( ASPECT_MODELS_FOLDER ) ) {
255+
if ( entry.isDirectory() && entry.getName().contains( String.format( "%s/", ASPECT_MODELS_FOLDER ) ) ) {
255256
return true;
256257
}
257258
}
@@ -265,8 +266,12 @@ private AspectModelFile migrate( final AspectModelFile file ) {
265266
return MetaModelVersionMigrator.INSTANCE.apply( file );
266267
}
267268

268-
private record LoaderContext( Set<String> resolvedUrns, Set<AspectModelFile> loadedFiles, Deque<String> unresolvedUrns,
269-
Deque<AspectModelFile> unresolvedFiles ) {
269+
private record LoaderContext(
270+
Set<String> resolvedUrns,
271+
Set<AspectModelFile> loadedFiles,
272+
Deque<String> unresolvedUrns,
273+
Deque<AspectModelFile> unresolvedFiles
274+
) {
270275
private LoaderContext() {
271276
this( new HashSet<>(), new HashSet<>(), new ArrayDeque<>(), new ArrayDeque<>() );
272277
}
@@ -275,12 +280,12 @@ private LoaderContext() {
275280
private Set<String> getAllUrnsInModel( final Model model ) {
276281
return Streams.stream( model.listStatements().mapWith( statement -> {
277282
final Stream<String> subjectUri = statement.getSubject().isURIResource()
278-
? Stream.of( statement.getSubject().getURI() ) :
279-
Stream.empty();
283+
? Stream.of( statement.getSubject().getURI() )
284+
: Stream.empty();
280285
final Stream<String> propertyUri = Stream.of( statement.getPredicate().getURI() );
281286
final Stream<String> objectUri = statement.getObject().isURIResource()
282-
? Stream.of( statement.getObject().asResource().getURI() ) :
283-
Stream.empty();
287+
? Stream.of( statement.getObject().asResource().getURI() )
288+
: Stream.empty();
284289

285290
return Stream.of( subjectUri, propertyUri, objectUri ).flatMap( Function.identity() )
286291
.flatMap( urn -> AspectModelUrn.from( urn ).toJavaOptional().stream() ).map( AspectModelUrn::toString );
@@ -342,14 +347,21 @@ private Optional<AspectModelFile> applyResolutionStrategy( final String urn ) {
342347
}
343348

344349
private void urnsFromModelNeedResolution( final AspectModelFile modelFile, final LoaderContext context ) {
345-
Streams.stream( modelFile.sourceModel().listStatements( null, RDF.type, (RDFNode) null ) ).map( Statement::getSubject )
346-
.filter( Resource::isURIResource ).map( Resource::getURI ).filter( uri -> uri.startsWith( "urn:samm:" ) )
350+
Streams.stream( modelFile.sourceModel().listStatements( null, RDF.type, (RDFNode) null ) )
351+
.map( Statement::getSubject )
352+
.filter( Resource::isURIResource )
353+
.map( Resource::getURI )
354+
.filter( uri -> uri.startsWith( "urn:samm:" ) )
347355
.forEach( urn -> context.resolvedUrns().add( urn ) );
348356

349-
getAllUrnsInModel( modelFile.sourceModel() ).stream().filter( urn -> !context.resolvedUrns().contains( urn ) )
350-
.filter( urn -> !urn.startsWith( XSD.NS ) ).filter( urn -> !urn.startsWith( RDF.uri ) )
351-
.filter( urn -> !urn.startsWith( SammNs.SAMM.getNamespace() ) ).filter( urn -> !urn.startsWith( SammNs.SAMMC.getNamespace() ) )
352-
.filter( urn -> !urn.startsWith( SammNs.SAMME.getNamespace() ) ).filter( urn -> !urn.startsWith( SammNs.UNIT.getNamespace() ) )
357+
getAllUrnsInModel( modelFile.sourceModel() ).stream()
358+
.filter( urn -> !context.resolvedUrns().contains( urn ) )
359+
.filter( urn -> !urn.startsWith( XSD.NS ) )
360+
.filter( urn -> !urn.startsWith( RDF.uri ) )
361+
.filter( urn -> !urn.startsWith( SammNs.SAMM.getNamespace() ) )
362+
.filter( urn -> !urn.startsWith( SammNs.SAMMC.getNamespace() ) )
363+
.filter( urn -> !urn.startsWith( SammNs.SAMME.getNamespace() ) )
364+
.filter( urn -> !urn.startsWith( SammNs.UNIT.getNamespace() ) )
353365
.forEach( urn -> context.unresolvedUrns().add( urn ) );
354366
}
355367

@@ -360,7 +372,7 @@ private void markModelFileAsLoaded( final AspectModelFile modelFile, final Loade
360372
}
361373
}
362374

363-
private void resolve( final List<AspectModelFile> inputFiles, final LoaderContext context ) {
375+
private void resolve( List<AspectModelFile> inputFiles, final LoaderContext context ) {
364376
for ( final AspectModelFile aspectModelFile : inputFiles ) {
365377
context.unresolvedFiles().push( aspectModelFile );
366378
}

core/esmf-aspect-meta-model-java/src/test/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoaderTest.java

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
import java.util.function.Function;
2828
import java.util.stream.Collectors;
2929

30+
import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
31+
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
3032
import org.eclipse.esmf.metamodel.AbstractEntity;
3133
import org.eclipse.esmf.metamodel.AspectModel;
3234
import org.eclipse.esmf.metamodel.ComplexType;
35+
import org.eclipse.esmf.samm.KnownVersion;
3336
import org.eclipse.esmf.test.TestAspect;
3437
import org.eclipse.esmf.test.TestResources;
3538

@@ -51,9 +54,8 @@ void testOfAbstractEntityCyclomaticCreation() {
5154
}
5255

5356
@Test
54-
void testLoadAspectModelFromZipArchiveFile() throws URISyntaxException {
55-
final ClassLoader classLoader = getClass().getClassLoader();
56-
final Path archivePath = Paths.get( classLoader.getResource( "namespaces.zip" ).toURI() );
57+
void testLoadAspectModelFromZipArchiveFile() {
58+
final Path archivePath = getPackage( "namespaces.zip" );
5759
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new File( archivePath.toString() ) );
5860

5961
assertThat( aspectModel.namespaces() ).hasSize( 2 );
@@ -70,9 +72,8 @@ void testLoadAspectModelFromZipArchiveFile() throws URISyntaxException {
7072
}
7173

7274
@Test
73-
void testLoadAspectModelFromZipArchiveInputStream() throws URISyntaxException, FileNotFoundException {
74-
final ClassLoader classLoader = getClass().getClassLoader();
75-
final Path archivePath = Paths.get( classLoader.getResource( "namespaces.zip" ).toURI() );
75+
void testLoadAspectModelFromZipArchiveInputStream() throws FileNotFoundException {
76+
final Path archivePath = getPackage( "namespaces.zip" );
7677
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new FileInputStream( archivePath.toString() ) );
7778

7879
assertThat( aspectModel.namespaces() ).hasSize( 2 );
@@ -92,9 +93,8 @@ void testLoadAspectModelFromZipArchiveInputStream() throws URISyntaxException, F
9293
* Test migration to the latest version of Aspect Model in Archive
9394
*/
9495
@Test
95-
void testLoadAspectModelFromZipArchive2_0_0() throws URISyntaxException, FileNotFoundException {
96-
final ClassLoader classLoader = getClass().getClassLoader();
97-
final Path archivePath = Paths.get( classLoader.getResource( "namespaces_with_old_version.zip" ).toURI() );
96+
void testLoadAspectModelFromZipArchive2_0_0() throws FileNotFoundException {
97+
final Path archivePath = getPackage( "namespaces_with_old_version.zip" );
9898
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new FileInputStream( archivePath.toString() ) );
9999

100100
assertThat( aspectModel.namespaces() ).hasSize( 2 );
@@ -109,4 +109,77 @@ void testLoadAspectModelFromZipArchive2_0_0() throws URISyntaxException, FileNot
109109
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
110110
} );
111111
}
112+
113+
@Test
114+
void testLoadAspectModelFromZipArchiveAspectModelsRoot() throws FileNotFoundException {
115+
final Path archivePath = getPackage( "namespaces-aspect-models-root.zip" );
116+
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new FileInputStream( archivePath.toString() ) );
117+
118+
assertThat( aspectModel.namespaces() ).hasSize( 2 );
119+
assertThat( aspectModel.namespaces().get( 0 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.1.0" );
120+
assertThat( aspectModel.namespaces().get( 1 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.0.0" );
121+
122+
final List<String> aspectsNames = List.of( "Movement2", "Movement3", "Movement", "SimpleAspect" );
123+
124+
assertThat( aspectModel.files() ).hasSize( 4 );
125+
assertThat( aspectModel.files() )
126+
.anySatisfy( aspectModelFile -> {
127+
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
128+
} );
129+
}
130+
131+
@Test
132+
void testLoadAspectModelFromZipArchiveAspectModelsSubfolder() throws FileNotFoundException {
133+
final Path archivePath = getPackage( "namespaces-aspect-models-subfolder.zip" );
134+
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new FileInputStream( archivePath.toString() ) );
135+
136+
assertThat( aspectModel.namespaces() ).hasSize( 2 );
137+
assertThat( aspectModel.namespaces().get( 0 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.1.0" );
138+
assertThat( aspectModel.namespaces().get( 1 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.0.0" );
139+
140+
final List<String> aspectsNames = List.of( "Movement2", "Movement3", "Movement", "SimpleAspect" );
141+
142+
assertThat( aspectModel.files() ).hasSize( 4 );
143+
assertThat( aspectModel.files() )
144+
.anySatisfy( aspectModelFile -> {
145+
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
146+
} );
147+
}
148+
149+
@Test
150+
void testLoadAspectModelFromZipArchiveWithSharedProperty() throws FileNotFoundException, URISyntaxException {
151+
final Path archivePath = getPackage( "namespace-with-shared-property.zip" );
152+
153+
final File aspectModelsRootDirectory = new File(
154+
AspectModelLoaderTest.class.getClassLoader()
155+
.getResource( KnownVersion.getLatest().toString().toLowerCase() )
156+
.toURI().getPath() );
157+
158+
final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
159+
160+
final AspectModel aspectModel = new AspectModelLoader( urnStrategy ).loadNamespacePackage(
161+
new FileInputStream( archivePath.toString() ) );
162+
163+
assertThat( aspectModel.namespaces() ).hasSize( 1 );
164+
assertThat( aspectModel.namespaces().get( 0 ).getName() ).contains( "urn:samm:org.eclipse.esmf.test:1.0.0" );
165+
166+
final List<String> aspectsNames = List.of( "Movement" );
167+
168+
assertThat( aspectModel.files() ).hasSize( 2 );
169+
assertThat( aspectModel.files() )
170+
.anySatisfy( aspectModelFile -> {
171+
if ( !aspectModelFile.aspects().isEmpty() ) {
172+
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
173+
}
174+
} );
175+
}
176+
177+
/**
178+
* Returns the File object for a test model file
179+
*/
180+
private Path getPackage( final String packageName ) {
181+
return Paths.get( String.format(
182+
"%s/../../core/esmf-test-aspect-models/src/main/resources/packages/%s",
183+
System.getProperty( "user.dir" ), packageName ) );
184+
}
112185
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
13+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
14+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
15+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
16+
17+
:propertyWithExampleValue a samm:Property ;
18+
samm:characteristic samm-c:Text ;
19+
samm:exampleValue "test" .

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/zip/AspectModelNamespacePackageCreator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
import org.apache.commons.lang3.function.TriConsumer;
1717

18-
public class AspectModelNamespacePackageCreator implements TriConsumer<AspectModel, OutputStream, Boolean> {
18+
public class AspectModelNamespacePackageCreator implements TriConsumer<AspectModel, OutputStream, String> {
1919

20-
private static final String AASX_ARCHIVE_FORMAT_PATH = "aasx/aspect-models/";
2120
private static final String BASE_ARCHIVE_FORMAT_PATH = "aspect-models/";
2221

2322
public static final AspectModelNamespacePackageCreator INSTANCE = new AspectModelNamespacePackageCreator();
@@ -26,13 +25,13 @@ private AspectModelNamespacePackageCreator() {
2625
}
2726

2827
@Override
29-
public void accept( final AspectModel aspectModel, final OutputStream outputStream, final Boolean isAasxArchiveFormat ) {
28+
public void accept( final AspectModel aspectModel, final OutputStream outputStream, final String rootPath ) {
3029
try ( FileOutputStream fos = (FileOutputStream) outputStream;
3130
BufferedOutputStream bos = new BufferedOutputStream( fos );
3231
ZipOutputStream zos = new ZipOutputStream( bos ) ) {
3332

3433
for ( final AspectModelFile aspectModelFile : aspectModel.files() ) {
35-
addFileToArchive( aspectModelFile, zos, isAasxArchiveFormat );
34+
addFileToArchive( aspectModelFile, zos, rootPath );
3635
}
3736
} catch ( IOException e ) {
3837
try {
@@ -43,11 +42,11 @@ public void accept( final AspectModel aspectModel, final OutputStream outputStre
4342
}
4443
}
4544

46-
private static void addFileToArchive( final AspectModelFile file, final ZipOutputStream zos, final boolean isAasxZipFormat )
45+
private static void addFileToArchive( final AspectModelFile file, final ZipOutputStream zos, final String rootPath )
4746
throws IOException {
4847
final String aspectString = AspectSerializer.INSTANCE.apply( file.aspect() );
4948
final String fileName = String.format( "%s/%s/%s/%s.ttl",
50-
isAasxZipFormat ? AASX_ARCHIVE_FORMAT_PATH : BASE_ARCHIVE_FORMAT_PATH,
49+
!rootPath.isBlank() ? String.format( "%s/%s", rootPath, BASE_ARCHIVE_FORMAT_PATH ) : BASE_ARCHIVE_FORMAT_PATH,
5150
file.aspect().urn().getNamespace(),
5251
file.aspect().urn().getVersion(),
5352
file.aspect().getName() );

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/zip/AspectModelZipGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void testAspectModelArchiveGeneration() throws IOException {
4545
final String outputFileName = String.format( "%s/%s", outputDirectory.toString(), "/test_zip.zip" );
4646

4747
AspectModelNamespacePackageCreator.INSTANCE.accept( aspectModel, new FileOutputStream( Paths.get( outputFileName ).toFile() ),
48-
false );
48+
"" );
4949

5050
assertThat( new File( outputFileName ) ).exists();
5151

2.21 KB
Binary file not shown.
7.36 KB
Binary file not shown.
10.1 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)