Skip to content

Commit

Permalink
Merge pull request #658 from bci-oss/bugfix/652-aspect-model-loader-t…
Browse files Browse the repository at this point in the history
…hrows-npe

Fix AspectModelLoader throws NullPointerException
  • Loading branch information
Yauhenikapl authored Oct 9, 2024
2 parents 32bceac + fcc1a07 commit 5de6c1b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ protected Type getType( final Resource characteristicResource ) {
private Statement getDataType( final Resource resource ) {
return Optional.ofNullable( resource.getPropertyResourceValue( SammNs.SAMMC.baseCharacteristic() ) )
.map( this::getDataType )
.orElseGet( () -> resource.getProperty( SammNs.SAMM.dataType() ) );
.orElseGet( () -> {
final Statement dataType = resource.getProperty( SammNs.SAMM.dataType() );
if ( dataType == null ) {
throw new AspectLoadingException(
String.format( "No datatype is defined on the Characteristic instance '%s: '.", resource.getLocalName() ) );
}
return dataType;
} );
}

protected Optional<Characteristic> getElementCharacteristic( final Resource collection ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.esmf.aspectmodel.loader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -27,19 +28,29 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.esmf.aspectmodel.AspectLoadingException;
import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
import org.eclipse.esmf.metamodel.AbstractEntity;
import org.eclipse.esmf.metamodel.AspectModel;
import org.eclipse.esmf.metamodel.ComplexType;
import org.eclipse.esmf.metamodel.HasDescription;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.test.InvalidTestAspect;
import org.eclipse.esmf.test.TestAspect;
import org.eclipse.esmf.test.TestResources;

import org.junit.jupiter.api.Test;

class AspectModelLoaderTest {

@Test
void loadAspectModelWithoutCharacteristicDatatype() {
assertThatThrownBy( () -> TestResources.load( InvalidTestAspect.INVALID_CHARACTERISTIC_DATATYPE ) )
.isInstanceOf( AspectLoadingException.class )
.hasMessage( "No datatype is defined on the Characteristic instance 'Characteristic1: '." );
}

@Test
void testOfAbstractEntityCyclomaticCreation() {
final Map<String, ComplexType> entities =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public static AspectModel load( final TestAspect model ) {
"valid/" + metaModelVersion.toString().toLowerCase() );
return new AspectModelLoader( testModelsResolutionStrategy ).load( inputStream, Optional.of( URI.create( "testmodel:" + path ) ) );
}

public static AspectModel load( final InvalidTestAspect model ) {
final KnownVersion metaModelVersion = KnownVersion.getLatest();
final String path = String.format( "invalid/%s/%s/%s.ttl", model.getUrn().getNamespaceMainPart(), model.getUrn().getVersion(),
model.getName() );
final InputStream inputStream = TestResources.class.getClassLoader().getResourceAsStream( path );
final ResolutionStrategy testModelsResolutionStrategy = new ClasspathStrategy(
"invalid/" + metaModelVersion.toString().toLowerCase() );
return new AspectModelLoader( testModelsResolutionStrategy ).load( inputStream, Optional.of( URI.create( "testmodel:" + path ) ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum InvalidTestAspect implements TestModel {
MISSING_ASPECT_DECLARATION,
INVALID_EXAMPLE_VALUE_DATATYPE,
INVALID_PREFERRED_NAME_DATATYPE,
INVALID_CHARACTERISTIC_DATATYPE,
RANGE_CONSTRAINT_WITH_WRONG_TYPE,
MODEL_WITH_CYCLES;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2024 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 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 : <urn:samm:com.example.test:1.0.0#> .

:AspectWithoutCharacteristicDatatype a samm:Aspect ;
samm:properties ( :property1 ) ;
samm:operations () ;
samm:events ( ) .

:property1 a samm:Property ;
samm:characteristic :Characteristic1 .

:Characteristic1 a samm:Characteristic .

0 comments on commit 5de6c1b

Please sign in to comment.