Skip to content

Commit

Permalink
Merge pull request #694 from bci-oss/655-validate-against-multiple-as…
Browse files Browse the repository at this point in the history
…pects-in-one-file

Update logic for validate 2 and more Aspects in one file
  • Loading branch information
Yauhenikapl authored Jan 14, 2025
2 parents f09da9b + afa8f80 commit 937c862
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@ public AspectModel loadAspectModelFiles( final Collection<AspectModelFile> input
.filter( modelElement -> modelElement.is( Aspect.class ) )
.findFirst()
.ifPresent( aspect -> mergedModel.setNsPrefix( "", aspect.urn().getUrnPrefix() ) );
for ( AspectModelFile file : files ) {
if ( file.aspects().size() > 1 ) {
throw new AspectLoadingException(
"Aspect model file " + file.sourceLocation().map( location -> location + " " ).orElse( "" ) + "contains " + file.aspects()
.size() + " aspects, but may only contain one." );
}
}
return new DefaultAspectModel( files, mergedModel, elements );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@
import java.io.File;
import java.net.URISyntaxException;

import org.eclipse.esmf.aspectmodel.AspectLoadingException;
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
import org.eclipse.esmf.aspectmodel.resolver.services.TurtleLoader;
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
import org.eclipse.esmf.metamodel.AspectModel;
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.test.InvalidTestAspect;
import org.eclipse.esmf.test.TestModel;
import org.eclipse.esmf.test.TestResources;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.junit.jupiter.api.Test;

public class AspectModelResolverTest {
class AspectModelResolverTest {
@Test
void testLoadDataModelExpectSuccess() throws URISyntaxException {
final File aspectModelsRootDirectory = new File(
Expand Down Expand Up @@ -118,13 +119,6 @@ void testResolveReferencedModelFromMemoryExpectSuccess() throws URISyntaxExcepti

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

final AspectModelUrn inputUrn = AspectModelUrn
.fromUrn( TestModel.TEST_NAMESPACE + "AnotherTest" );
final Model model = TurtleLoader.loadTurtle(
AspectModelResolverTest.class.getResourceAsStream(
"/" + KnownVersion.getLatest().toString().toLowerCase()
+ "/org.eclipse.esmf.test/1.0.0/Test.ttl" ) ).get();

final ResolutionStrategy inMemoryStrategy = new FromLoadedFileStrategy( AspectModelFileLoader.load(
AspectModelResolverTest.class.getResourceAsStream(
"/" + KnownVersion.getLatest().toString().toLowerCase()
Expand Down Expand Up @@ -193,4 +187,15 @@ void testResolutionMissingModelElementExpectFailure() throws Throwable {
final AspectModel result = new AspectModelLoader( urnStrategy ).load( testUrn );
} ).isInstanceOf( ModelResolutionException.class );
}

@Test
void getExceptionWhileLoadingModelWithTwoAspects() {
assertThatThrownBy( () -> {
TestResources.load( InvalidTestAspect.INVALID_ASPECT_WITH_TWO_ASPECTS );
} )
.isInstanceOf( AspectLoadingException.class )
.hasMessageContaining(
"Aspect model file testmodel:invalid/org.eclipse.esmf.test/1.0.0/InvalidAspectWithTwoAspects.ttl contains 2 "
+ "aspects, but may only contain one." );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public enum InvalidTestAspect implements TestModel {
INVALID_PREFERRED_NAME_DATATYPE,
INVALID_CHARACTERISTIC_DATATYPE,
RANGE_CONSTRAINT_WITH_WRONG_TYPE,
MODEL_WITH_CYCLES;
MODEL_WITH_CYCLES,

INVALID_ASPECT_WITH_TWO_ASPECTS;

@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#>.
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#>.
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#>.
@prefix unit: <urn:samm:org.eclipse.esmf.samm:unit:2.1.0#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.

:Aspect1 a samm:Aspect;
samm:preferredName "Test Aspect1"@en;
samm:properties ();
samm:operations ();
samm:events ().

:Aspect2 a samm:Aspect;
samm:preferredName "Test Aspect2"@en;
samm:properties ();
samm:operations ();
samm:events ().

0 comments on commit 937c862

Please sign in to comment.