Skip to content

Commit 6a1a27e

Browse files
committed
Update logic for validate 2 and more Aspects in one file
1 parent aa29771 commit 6a1a27e

File tree

5 files changed

+162
-9
lines changed

5 files changed

+162
-9
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.eclipse.esmf.aspectmodel.resolver.ModelSource;
4646
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
4747
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategySupport;
48+
import org.eclipse.esmf.aspectmodel.resolver.exceptions.InvalidCountOfAspectsException;
4849
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
4950
import org.eclipse.esmf.aspectmodel.resolver.fs.FlatModelsRoot;
5051
import org.eclipse.esmf.aspectmodel.resolver.modelfile.DefaultAspectModelFile;
@@ -480,6 +481,14 @@ public AspectModel loadAspectModelFiles( final Collection<AspectModelFile> input
480481
.filter( modelElement -> modelElement.is( Aspect.class ) )
481482
.findFirst()
482483
.ifPresent( aspect -> mergedModel.setNsPrefix( "", aspect.urn().getUrnPrefix() ) );
484+
for ( AspectModelFile file : files ) {
485+
if ( file.aspects().size() > 1 ) {
486+
throw new InvalidCountOfAspectsException(
487+
"Invalid number of aspects for " + file
488+
+ ". Total aspects: " + file.aspects().size()
489+
);
490+
}
491+
}
483492
return new DefaultAspectModel( files, mergedModel, elements );
484493
}
485494

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.eclipse.esmf.aspectmodel.resolver.exceptions;
2+
3+
public class InvalidCountOfAspectsException extends RuntimeException {
4+
public InvalidCountOfAspectsException(String message) {
5+
super(message);
6+
}
7+
}

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

+37-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222
import java.net.URISyntaxException;
2323

2424
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
25+
import org.eclipse.esmf.aspectmodel.resolver.exceptions.InvalidCountOfAspectsException;
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;
3131
import org.eclipse.esmf.test.TestModel;
3232

33-
import org.apache.jena.rdf.model.Model;
3433
import org.apache.jena.rdf.model.Resource;
3534
import org.apache.jena.vocabulary.RDF;
3635
import org.junit.jupiter.api.Test;
@@ -118,13 +117,6 @@ void testResolveReferencedModelFromMemoryExpectSuccess() throws URISyntaxExcepti
118117

119118
final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
120119

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-
128120
final ResolutionStrategy inMemoryStrategy = new FromLoadedFileStrategy( AspectModelFileLoader.load(
129121
AspectModelResolverTest.class.getResourceAsStream(
130122
"/" + KnownVersion.getLatest().toString().toLowerCase()
@@ -193,4 +185,40 @@ void testResolutionMissingModelElementExpectFailure() throws Throwable {
193185
final AspectModel result = new AspectModelLoader( urnStrategy ).load( testUrn );
194186
} ).isInstanceOf( ModelResolutionException.class );
195187
}
188+
189+
@Test
190+
void getExceptionWhileLoadingModelWithTwoAspects() throws URISyntaxException {
191+
final File aspectModelsRootDirectory = new File(
192+
AspectModelResolverTest.class.getClassLoader().getResource( KnownVersion.getLatest().toString().toLowerCase() )
193+
.toURI().getPath() );
194+
195+
final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
196+
197+
final AspectModelUrn testUrn = AspectModelUrn.fromUrn( "urn:samm:io.catenax.shared.uuid:2.0.0#Uuid2" );
198+
199+
assertThatThrownBy( () -> {
200+
final AspectModel result = new AspectModelLoader( urnStrategy ).load( testUrn );
201+
} )
202+
.isInstanceOf( InvalidCountOfAspectsException.class )
203+
.hasMessageContaining( aspectModelsRootDirectory.getPath()
204+
+ "/io.catenax.shared.uuid/2.0.0/invalid_uudi_aspect_contains_two_aspects.ttl. Total aspects: 2" );
205+
}
206+
207+
@Test
208+
void getExceptionWhileLoadingModelWithTwoAspectsInSharedFiles() throws URISyntaxException {
209+
final File aspectModelsRootDirectory = new File(
210+
AspectModelResolverTest.class.getClassLoader().getResource( KnownVersion.getLatest().toString().toLowerCase() )
211+
.toURI().getPath() );
212+
213+
final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
214+
215+
final AspectModelUrn testUrn = AspectModelUrn.fromUrn( "urn:samm:io.catenax.shared.uuid:2.0.0#Uuid" );
216+
217+
assertThatThrownBy( () -> {
218+
final AspectModel result = new AspectModelLoader( urnStrategy ).load( testUrn );
219+
} )
220+
.isInstanceOf( InvalidCountOfAspectsException.class )
221+
.hasMessageContaining( aspectModelsRootDirectory.getPath()
222+
+ "/io.catenax.shared.uuid/2.0.0/invalid_uudi_aspect_contains_two_aspects.ttl. Total aspects: 2" );
223+
}
196224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#######################################################################
2+
# Copyright (c) 2023,2024 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This work is made available under the terms of the
8+
# Creative Commons Attribution 4.0 International (CC-BY-4.0) license,
9+
# which is available at
10+
# https://creativecommons.org/licenses/by/4.0/legalcode.
11+
#
12+
# SPDX-License-Identifier: CC-BY-4.0
13+
#######################################################################
14+
15+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#>.
16+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#>.
17+
@prefix samm-e: <urn:samm:org.eclipse.esmf.samm:entity:2.1.0#>.
18+
@prefix unit: <urn:samm:org.eclipse.esmf.samm:unit:2.1.0#>.
19+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
20+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
21+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
22+
@prefix : <urn:samm:io.catenax.shared.uuid:2.0.0#>.
23+
24+
:Uuid2 a samm:Aspect;
25+
samm:preferredName "Shared Aspect for UUIDs v4"@en;
26+
samm:description "This is a shared aspect for UUIDs with a regex."@en;
27+
samm:properties (:uuidV4Property);
28+
samm:operations ();
29+
samm:events ().
30+
:uuidV4Property a samm:Property;
31+
samm:preferredName "UUID v4 Property"@en;
32+
samm:description "Property based on a UUID v4."@en;
33+
samm:characteristic :UuidV4Trait;
34+
samm:exampleValue "urn:uuid:48878d48-6f1d-47f5-8ded-a441d0d879df".
35+
:UuidV4Trait a samm-c:Trait;
36+
samm:preferredName "Trait for UUIDs v4"@en;
37+
samm:description "Trait to ensure UUID v4 data format."@en;
38+
samm-c:baseCharacteristic :Uuidv4Characteristic;
39+
samm-c:constraint :Uuidv4RegularExpression.
40+
:Uuidv4Characteristic a samm:Characteristic;
41+
samm:preferredName "UUID v4"@en;
42+
samm:description "A version 4 UUID is a universally unique identifier that is generated using random 32 hexadecimal characters."@en;
43+
samm:dataType xsd:string;
44+
samm:see <https://tools.ietf.org/html/rfc4122>.
45+
:Uuidv4RegularExpression a samm-c:RegularExpressionConstraint;
46+
samm:preferredName "UUID v4 Regular Expression"@en;
47+
samm:description "The provided regular expression ensures that the UUID is composed of five groups of characters separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens), optionally prefixed by \"urn:uuid:\" to make it an IRI."@en;
48+
samm:see <https://datatracker.ietf.org/doc/html/rfc4122>;
49+
samm:value "(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)|(^urn:uuid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)".
50+
:Uuid3 a samm:Aspect;
51+
samm:preferredName "Shared Aspect for UUIDs v4"@en;
52+
samm:description "This is a shared aspect for UUIDs with a regex."@en;
53+
samm:properties (:uuidV4Property);
54+
samm:operations ();
55+
samm:events ().
56+
:uuidV4Property3 a samm:Property;
57+
samm:preferredName "UUID v4 Property"@en;
58+
samm:description "Property based on a UUID v4."@en;
59+
samm:characteristic :UuidV4Trait;
60+
samm:exampleValue "urn:uuid:48878d48-6f1d-47f5-8ded-a441d0d879df".
61+
:UuidV4Trait3 a samm-c:Trait;
62+
samm:preferredName "Trait for UUIDs v4"@en;
63+
samm:description "Trait to ensure UUID v4 data format."@en;
64+
samm-c:baseCharacteristic :Uuidv4Characteristic;
65+
samm-c:constraint :Uuidv4RegularExpression.
66+
:Uuidv4Characteristic3 a samm:Characteristic;
67+
samm:preferredName "UUID v4"@en;
68+
samm:description "A version 4 UUID is a universally unique identifier that is generated using random 32 hexadecimal characters."@en;
69+
samm:dataType xsd:string;
70+
samm:see <https://tools.ietf.org/html/rfc4122>.
71+
:Uuidv4RegularExpression3 a samm-c:RegularExpressionConstraint;
72+
samm:preferredName "UUID v4 Regular Expression"@en;
73+
samm:description "The provided regular expression ensures that the UUID is composed of five groups of characters separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens), optionally prefixed by \"urn:uuid:\" to make it an IRI."@en;
74+
samm:see <https://datatracker.ietf.org/doc/html/rfc4122>;
75+
samm:value "(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)|(^urn:uuid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)".
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#######################################################################
2+
# Copyright (c) 2023,2024 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This work is made available under the terms of the
8+
# Creative Commons Attribution 4.0 International (CC-BY-4.0) license,
9+
# which is available at
10+
# https://creativecommons.org/licenses/by/4.0/legalcode.
11+
#
12+
# SPDX-License-Identifier: CC-BY-4.0
13+
#######################################################################
14+
15+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#>.
16+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#>.
17+
@prefix samm-e: <urn:samm:org.eclipse.esmf.samm:entity:2.1.0#>.
18+
@prefix unit: <urn:samm:org.eclipse.esmf.samm:unit:2.1.0#>.
19+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
20+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
21+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
22+
@prefix : <urn:samm:io.catenax.shared.uuid:2.0.0#>.
23+
24+
:Uuid a samm:Aspect;
25+
samm:preferredName "Shared Aspect for UUIDs v4"@en;
26+
samm:description "This is a shared aspect for UUIDs with a regex."@en;
27+
samm:properties (:uuidV4Property);
28+
samm:operations ();
29+
samm:events ().
30+
:uuidV4Property a samm:Property;
31+
samm:preferredName "UUID v4 Property"@en;
32+
samm:description "Property based on a UUID v4."@en;
33+
samm:characteristic :UuidV4Trait;
34+
samm:exampleValue "urn:uuid:48878d48-6f1d-47f5-8ded-a441d0d879df".

0 commit comments

Comments
 (0)