Skip to content

Commit 9ca6c92

Browse files
committed
Fix AspectModelLoader throws NullPointerException by adding another message and exception type
1 parent 08b6df0 commit 9ca6c92

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-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 IllegalStateException(
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: 10 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;
@@ -40,6 +41,15 @@
4041
import org.junit.jupiter.api.Test;
4142

4243
class AspectModelLoaderTest {
44+
45+
@Test
46+
void loadAspectModelWithoutCharacteristicDatatype() {
47+
assertThatThrownBy( () -> new AspectModelLoader().load( AspectModelLoaderTest.class.getResourceAsStream(
48+
String.format( "/%s/invalid_characteristic_datatype.ttl", KnownVersion.SAMM_2_1_0 ) ) ) )
49+
.isInstanceOf( IllegalStateException.class )
50+
.hasMessage( "No datatype is defined on the Characteristic instance 'Characteristic1: '." );
51+
}
52+
4353
@Test
4454
void testOfAbstractEntityCyclomaticCreation() {
4555
final Map<String, ComplexType> entities =
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)