Skip to content

Commit 937c862

Browse files
authored
Merge pull request #694 from bci-oss/655-validate-against-multiple-aspects-in-one-file
Update logic for validate 2 and more Aspects in one file
2 parents f09da9b + afa8f80 commit 937c862

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@ public AspectModel loadAspectModelFiles( final Collection<AspectModelFile> input
480480
.filter( modelElement -> modelElement.is( Aspect.class ) )
481481
.findFirst()
482482
.ifPresent( aspect -> mergedModel.setNsPrefix( "", aspect.urn().getUrnPrefix() ) );
483+
for ( AspectModelFile file : files ) {
484+
if ( file.aspects().size() > 1 ) {
485+
throw new AspectLoadingException(
486+
"Aspect model file " + file.sourceLocation().map( location -> location + " " ).orElse( "" ) + "contains " + file.aspects()
487+
.size() + " aspects, but may only contain one." );
488+
}
489+
}
483490
return new DefaultAspectModel( files, mergedModel, elements );
484491
}
485492

core/esmf-aspect-meta-model-java/src/test/java/org/eclipse/esmf/aspectmodel/resolver/AspectModelResolverTest.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@
2121
import java.io.File;
2222
import java.net.URISyntaxException;
2323

24+
import org.eclipse.esmf.aspectmodel.AspectLoadingException;
2425
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
2526
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
26-
import org.eclipse.esmf.aspectmodel.resolver.services.TurtleLoader;
2727
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
2828
import org.eclipse.esmf.metamodel.AspectModel;
2929
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
3030
import org.eclipse.esmf.samm.KnownVersion;
31+
import org.eclipse.esmf.test.InvalidTestAspect;
3132
import org.eclipse.esmf.test.TestModel;
33+
import org.eclipse.esmf.test.TestResources;
3234

33-
import org.apache.jena.rdf.model.Model;
3435
import org.apache.jena.rdf.model.Resource;
3536
import org.apache.jena.vocabulary.RDF;
3637
import org.junit.jupiter.api.Test;
3738

38-
public class AspectModelResolverTest {
39+
class AspectModelResolverTest {
3940
@Test
4041
void testLoadDataModelExpectSuccess() throws URISyntaxException {
4142
final File aspectModelsRootDirectory = new File(
@@ -118,13 +119,6 @@ void testResolveReferencedModelFromMemoryExpectSuccess() throws URISyntaxExcepti
118119

119120
final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
120121

121-
final AspectModelUrn inputUrn = AspectModelUrn
122-
.fromUrn( TestModel.TEST_NAMESPACE + "AnotherTest" );
123-
final Model model = TurtleLoader.loadTurtle(
124-
AspectModelResolverTest.class.getResourceAsStream(
125-
"/" + KnownVersion.getLatest().toString().toLowerCase()
126-
+ "/org.eclipse.esmf.test/1.0.0/Test.ttl" ) ).get();
127-
128122
final ResolutionStrategy inMemoryStrategy = new FromLoadedFileStrategy( AspectModelFileLoader.load(
129123
AspectModelResolverTest.class.getResourceAsStream(
130124
"/" + KnownVersion.getLatest().toString().toLowerCase()
@@ -193,4 +187,15 @@ void testResolutionMissingModelElementExpectFailure() throws Throwable {
193187
final AspectModel result = new AspectModelLoader( urnStrategy ).load( testUrn );
194188
} ).isInstanceOf( ModelResolutionException.class );
195189
}
190+
191+
@Test
192+
void getExceptionWhileLoadingModelWithTwoAspects() {
193+
assertThatThrownBy( () -> {
194+
TestResources.load( InvalidTestAspect.INVALID_ASPECT_WITH_TWO_ASPECTS );
195+
} )
196+
.isInstanceOf( AspectLoadingException.class )
197+
.hasMessageContaining(
198+
"Aspect model file testmodel:invalid/org.eclipse.esmf.test/1.0.0/InvalidAspectWithTwoAspects.ttl contains 2 "
199+
+ "aspects, but may only contain one." );
200+
}
196201
}

core/esmf-test-aspect-models/src/main/java/org/eclipse/esmf/test/InvalidTestAspect.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public enum InvalidTestAspect implements TestModel {
2929
INVALID_PREFERRED_NAME_DATATYPE,
3030
INVALID_CHARACTERISTIC_DATATYPE,
3131
RANGE_CONSTRAINT_WITH_WRONG_TYPE,
32-
MODEL_WITH_CYCLES;
32+
MODEL_WITH_CYCLES,
33+
34+
INVALID_ASPECT_WITH_TWO_ASPECTS;
3335

3436
@Override
3537
public String getName() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2025 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 unit: <urn:samm:org.eclipse.esmf.samm:unit:2.1.0#>.
16+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
17+
18+
:Aspect1 a samm:Aspect;
19+
samm:preferredName "Test Aspect1"@en;
20+
samm:properties ();
21+
samm:operations ();
22+
samm:events ().
23+
24+
:Aspect2 a samm:Aspect;
25+
samm:preferredName "Test Aspect2"@en;
26+
samm:properties ();
27+
samm:operations ();
28+
samm:events ().
29+

0 commit comments

Comments
 (0)