Skip to content

Commit 5de6c1b

Browse files
authored
Merge pull request #658 from bci-oss/bugfix/652-aspect-model-loader-throws-npe
Fix AspectModelLoader throws NullPointerException
2 parents 32bceac + fcc1a07 commit 5de6c1b

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,14 @@ protected Type getType( final Resource characteristicResource ) {
116116
private Statement getDataType( final Resource resource ) {
117117
return Optional.ofNullable( resource.getPropertyResourceValue( SammNs.SAMMC.baseCharacteristic() ) )
118118
.map( this::getDataType )
119-
.orElseGet( () -> resource.getProperty( SammNs.SAMM.dataType() ) );
119+
.orElseGet( () -> {
120+
final Statement dataType = resource.getProperty( SammNs.SAMM.dataType() );
121+
if ( dataType == null ) {
122+
throw new AspectLoadingException(
123+
String.format( "No datatype is defined on the Characteristic instance '%s: '.", resource.getLocalName() ) );
124+
}
125+
return dataType;
126+
} );
120127
}
121128

122129
protected Optional<Characteristic> getElementCharacteristic( final Resource collection ) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.esmf.aspectmodel.loader;
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
1718

1819
import java.io.File;
1920
import java.io.FileInputStream;
@@ -27,19 +28,29 @@
2728
import java.util.function.Function;
2829
import java.util.stream.Collectors;
2930

31+
import org.eclipse.esmf.aspectmodel.AspectLoadingException;
3032
import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
3133
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
3234
import org.eclipse.esmf.metamodel.AbstractEntity;
3335
import org.eclipse.esmf.metamodel.AspectModel;
3436
import org.eclipse.esmf.metamodel.ComplexType;
3537
import org.eclipse.esmf.metamodel.HasDescription;
3638
import org.eclipse.esmf.samm.KnownVersion;
39+
import org.eclipse.esmf.test.InvalidTestAspect;
3740
import org.eclipse.esmf.test.TestAspect;
3841
import org.eclipse.esmf.test.TestResources;
3942

4043
import org.junit.jupiter.api.Test;
4144

4245
class AspectModelLoaderTest {
46+
47+
@Test
48+
void loadAspectModelWithoutCharacteristicDatatype() {
49+
assertThatThrownBy( () -> TestResources.load( InvalidTestAspect.INVALID_CHARACTERISTIC_DATATYPE ) )
50+
.isInstanceOf( AspectLoadingException.class )
51+
.hasMessage( "No datatype is defined on the Characteristic instance 'Characteristic1: '." );
52+
}
53+
4354
@Test
4455
void testOfAbstractEntityCyclomaticCreation() {
4556
final Map<String, ComplexType> entities =

core/esmf-aspect-meta-model-java/src/test/java/org/eclipse/esmf/test/TestResources.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ public static AspectModel load( final TestAspect model ) {
3333
"valid/" + metaModelVersion.toString().toLowerCase() );
3434
return new AspectModelLoader( testModelsResolutionStrategy ).load( inputStream, Optional.of( URI.create( "testmodel:" + path ) ) );
3535
}
36+
37+
public static AspectModel load( final InvalidTestAspect model ) {
38+
final KnownVersion metaModelVersion = KnownVersion.getLatest();
39+
final String path = String.format( "invalid/%s/%s/%s.ttl", model.getUrn().getNamespaceMainPart(), model.getUrn().getVersion(),
40+
model.getName() );
41+
final InputStream inputStream = TestResources.class.getClassLoader().getResourceAsStream( path );
42+
final ResolutionStrategy testModelsResolutionStrategy = new ClasspathStrategy(
43+
"invalid/" + metaModelVersion.toString().toLowerCase() );
44+
return new AspectModelLoader( testModelsResolutionStrategy ).load( inputStream, Optional.of( URI.create( "testmodel:" + path ) ) );
45+
}
3646
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public enum InvalidTestAspect implements TestModel {
2626
MISSING_ASPECT_DECLARATION,
2727
INVALID_EXAMPLE_VALUE_DATATYPE,
2828
INVALID_PREFERRED_NAME_DATATYPE,
29+
INVALID_CHARACTERISTIC_DATATYPE,
2930
RANGE_CONSTRAINT_WITH_WRONG_TYPE,
3031
MODEL_WITH_CYCLES;
3132

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2024 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 samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
13+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
14+
@prefix : <urn:samm:com.example.test:1.0.0#> .
15+
16+
:AspectWithoutCharacteristicDatatype a samm:Aspect ;
17+
samm:properties ( :property1 ) ;
18+
samm:operations () ;
19+
samm:events ( ) .
20+
21+
:property1 a samm:Property ;
22+
samm:characteristic :Characteristic1 .
23+
24+
:Characteristic1 a samm:Characteristic .

0 commit comments

Comments
 (0)