Skip to content

Commit 0954f70

Browse files
committed
solves #191
In case entities are extended from each other, the code generation for the static meta model will fail due to a NullPointerException. This happened in the DefaultAbstractEntity:.getExtendingElements due to a map without excluding null Objects.
1 parent 4599bb9 commit 0954f70

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/impl/DefaultAbstractEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.openmanufacturing.sds.metamodel.impl;
1515

1616
import java.util.List;
17+
import java.util.Objects;
1718
import java.util.Optional;
1819
import java.util.stream.Collectors;
1920

@@ -52,7 +53,7 @@ private DefaultAbstractEntity( final MetaModelBaseAttributes metaModelBaseAttrib
5253
*/
5354
@Override
5455
public List<ComplexType> getExtendingElements() {
55-
return extendingElements.stream().map( instances::get ).collect( Collectors.toList() );
56+
return extendingElements.stream().map( instances::get ).filter( Objects::nonNull ).collect( Collectors.toList() );
5657
}
5758

5859
/**

core/sds-aspect-model-java-generator/src/test/java/io/openmanufacturing/sds/aspectmodel/java/StaticMetaModelJavaGeneratorTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import io.openmanufacturing.sds.staticmetamodel.constraint.StaticConstraintContainerProperty;
5353
import io.openmanufacturing.sds.staticmetamodel.constraint.StaticConstraintProperty;
5454
import io.openmanufacturing.sds.test.TestAspect;
55+
import io.openmanufacturing.sds.test.TestSharedAspect;
5556

5657
public class StaticMetaModelJavaGeneratorTest extends StaticMetaModelGeneratorTest {
5758

@@ -65,6 +66,17 @@ public void testCodeGeneration( final TestAspect testAspect ) {
6566
assertThatCode( () -> TestContext.generateStaticAspectCode().apply( getGenerators( testAspect, KnownVersion.getLatest() ) ) ).doesNotThrowAnyException();
6667
}
6768

69+
/**
70+
* Tests that code generation succeeds for all test models for the latest meta model version
71+
* @param testAspect the injected Aspect model
72+
*/
73+
@ParameterizedTest
74+
@EnumSource( value = TestSharedAspect.class )
75+
public void testCodeGenerationSharedAspect( final TestSharedAspect testAspect ) {
76+
assertThatCode( () -> TestContext.generateStaticAspectCode().apply( getGenerators( testAspect, KnownVersion.getLatest() ) ) ).doesNotThrowAnyException();
77+
}
78+
79+
6880
@ParameterizedTest
6981
@MethodSource( value = "allVersions" )
7082
public void testGenerateStaticMetaModelWithOptionalProperties( final KnownVersion metaModelVersion ) throws IOException {
@@ -142,6 +154,18 @@ ImmutableMap.<String, Object> builder().put( "NAMESPACE", String.class ).put( "M
142154
} ).build(), new HashMap<>() );
143155
}
144156

157+
@ParameterizedTest
158+
@MethodSource( value = "versionsStartingWith2_0_0" )
159+
public void testGenerateStaticMetaModelWithMeasurementTwo( final KnownVersion metaModelVersion ) throws IOException {
160+
final TestAspect aspect = TestAspect.ASPECT_WITH_EXTENDED_ENTITY;
161+
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect, metaModelVersion ) );
162+
result.assertNumberOfFiles( 8 );
163+
result.assertFields( "MetaAspectWithExtendedEntity",
164+
ImmutableMap.<String, Object> builder().put( "NAMESPACE", String.class ).put( "MODEL_ELEMENT_URN", String.class )
165+
.put( "CHARACTERISTIC_NAMESPACE", String.class ).put( "INSTANCE", "MetaAspectWithExtendedEntity" )
166+
.put( "TEST_PROPERTY", "StaticContainerProperty<TestEntity,java.util.LinkedHashSet<TestEntity>>").build(), new HashMap<>() );
167+
}
168+
145169
@ParameterizedTest
146170
@MethodSource( value = "allVersions" )
147171
public void testGenerateStaticMetaModelWithRecursiveAspectWithOptional( final KnownVersion metaModelVersion ) throws IOException {
@@ -211,7 +235,7 @@ ImmutableMap.<String, Object> builder().put( "NAMESPACE", String.class ).put( "M
211235
}
212236

213237
@ParameterizedTest
214-
// @MethodSource( value = "allVersions" )
238+
// @MethodSource( value = "allVersions" )
215239
@MethodSource( value = "latestVersion" )
216240
public void testGenerateStaticMetaModelWithConstraints( final KnownVersion metaModelVersion ) throws IOException {
217241
final TestAspect aspect = TestAspect.ASPECT_WITH_CONSTRAINTS;

core/sds-test-aspect-models/src/main/java/io/openmanufacturing/sds/test/TestAspect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public enum TestAspect implements TestModel {
9494
ASPECT_WITH_ERROR_COLLECTION,
9595
ASPECT_WITH_EVENT,
9696
ASPECT_WITH_EXCLUSIVE_RANGE_CONSTRAINT,
97+
ASPECT_WITH_EXTENDED_ENTITY,
9798
ASPECT_WITH_EXTENDED_ENUMS,
9899
ASPECT_WITH_EXTENDED_ENUMS_WITH_NOT_IN_PAYLOAD_PROPERTY,
99100
ASPECT_WITH_FIXED_POINT,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2021 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 : <urn:bamm:io.openmanufacturing.test:1.0.0#> .
13+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:2.0.0#> .
14+
@prefix bamm-c: <urn:bamm:io.openmanufacturing:characteristic:2.0.0#> .
15+
@prefix bamm-e: <urn:bamm:io.openmanufacturing:entity:2.0.0#> .
16+
@prefix unit: <urn:bamm:io.openmanufacturing:unit:2.0.0#> .
17+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
18+
19+
:AspectWithExtendedEntity
20+
a bamm:Aspect ;
21+
bamm:name "AspectWithExtendedEntity" ;
22+
bamm:properties ( :testProperty ) ;
23+
bamm:operations ( ) .
24+
25+
:testProperty a bamm:Property ;
26+
bamm:preferredName "Test Property"@en ;
27+
bamm:description "This is a test property."@en ;
28+
bamm:see <http://example.com/omp> ;
29+
bamm:see <http://example.com/me> ;
30+
bamm:characteristic [ a bamm-c:SortedSet ;
31+
bamm:name "AspectWithExtendedEntity" ;
32+
bamm:dataType :TestEntity ] .
33+
34+
:TestEntity
35+
a bamm:Entity ;
36+
bamm:extends :ParentTestEntity ;
37+
bamm:name "TestEntity" ;
38+
bamm:properties ( ) .
39+
40+
:ParentTestEntity
41+
a bamm:AbstractEntity ;
42+
bamm:extends :ParentOfParentEntity ;
43+
bamm:name "ParentTestEntity" ;
44+
bamm:properties ( :parentString ) .
45+
46+
:ParentOfParentEntity
47+
a bamm:AbstractEntity ;
48+
bamm:name "ParentOfParentEntity" ;
49+
bamm:properties ( :parentOfParentString ) .
50+
51+
:StringCode
52+
a bamm-c:Code ;
53+
bamm:name "StringCode" ;
54+
bamm:dataType xsd:string .
55+
56+
:parentString
57+
a bamm:Property ;
58+
bamm:name "parentString" ;
59+
bamm:characteristic :StringCode .
60+
61+
:parentOfParentString
62+
a bamm:Property ;
63+
bamm:name "parentOfParentString" ;
64+
bamm:characteristic :StringCode .
65+

0 commit comments

Comments
 (0)