Skip to content

Commit da24cd3

Browse files
authored
Merge pull request #678 from bci-oss/623-meta-class-does-not-include-exampleValue
Meta Class doesn't include exampleValue
2 parents a7cf8f2 + 79992a9 commit da24cd3

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/metamodel/StaticMetaModelJavaArtifactGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Locale;
2222
import java.util.Map;
2323
import java.util.Properties;
24+
2425
import javax.xml.datatype.DatatypeConfigurationException;
2526
import javax.xml.datatype.DatatypeConstants;
2627
import javax.xml.datatype.DatatypeFactory;
@@ -90,6 +91,7 @@
9091
import org.eclipse.esmf.metamodel.impl.DefaultComplexType;
9192
import org.eclipse.esmf.metamodel.impl.DefaultEntity;
9293
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
94+
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;
9395
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
9496
import org.eclipse.esmf.samm.KnownVersion;
9597
import org.eclipse.esmf.staticmetamodel.PropertyContainer;
@@ -170,6 +172,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
170172
.put( "DefaultRangeConstraint", DefaultRangeConstraint.class )
171173
.put( "DefaultRegularExpressionConstraint", DefaultRegularExpressionConstraint.class )
172174
.put( "DefaultScalar", DefaultScalar.class )
175+
.put( "DefaultScalarValue", DefaultScalarValue.class )
173176
.put( "DefaultSet", DefaultSet.class )
174177
.put( "DefaultSingleEntity", DefaultSingleEntity.class )
175178
.put( "DefaultSortedSet", DefaultSortedSet.class )

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/metamodel/StaticMetaModelVisitor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ private <T> String getOptionalStaticDeclarationValue( final Type type, final Opt
544544
return "Optional.of(" + valueInitializer.apply( xsdType, valueExpression ) + ")";
545545
}
546546

547+
public String exampleValue( final Property property, final StaticCodeGenerationContext context ) {
548+
return property.getExampleValue()
549+
.map( exampleValue -> "Optional.of(" + this.visitScalarValue( exampleValue, context ) + ")" )
550+
.orElse( "Optional.empty()" );
551+
}
552+
547553
/*
548554
* This method is a crutch because velocity is not able to call the parameterized actual method
549555
*/
@@ -574,6 +580,7 @@ public String getMetaModelBaseAttributes( final ModelElement element, final Stat
574580
} );
575581
element.getSee().stream().sorted()
576582
.forEach( see -> builder.append( ".withSee(" ).append( AspectModelJavaUtil.createLiteral( see ) ).append( ")" ) );
583+
577584
builder.append( ".build()" );
578585
return builder.toString();
579586
}

core/esmf-aspect-model-java-generator/src/main/resources/java-static-class-property-lib.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
## $arg1, $arg2, ... ) {
132132
$modelVisitor.metaModelBaseAttributes( $property, $context ),
133133
#propertyCharacteristic( $property.getCharacteristic().get(), $propertyType ),
134-
Optional.empty(),
134+
$modelVisitor.exampleValue( $property, $context ),
135135
$property.isOptional(),
136136
$property.isNotInPayload(),
137137
Optional.of("$property.getPayloadName()"),

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/StaticMetaModelJavaGeneratorTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import java.time.LocalDate;
2525
import java.util.HashMap;
2626
import java.util.List;
27+
import java.util.Map;
2728
import java.util.Optional;
29+
import java.util.Set;
2830
import java.util.regex.Pattern;
31+
2932
import javax.xml.datatype.DatatypeFactory;
3033
import javax.xml.datatype.XMLGregorianCalendar;
3134

@@ -498,6 +501,36 @@ void testCharacteristicInstantiationForQuantifiableWithoutUnit() throws IOExcept
498501
ImmutableMap.<String, String> builder().put( "TEST_PROPERTY", expectedTestPropertyCharacteristicConstructorCall ).build(), 1 );
499502
}
500503

504+
@Test
505+
void testCharacteristicInstantiationForPropertyWithExampleValue() throws IOException {
506+
final TestAspect aspect = TestAspect.ASPECT_WITH_COLLECTION;
507+
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );
508+
509+
result.assertNumberOfFiles( 2 );
510+
511+
Map<String, Set<String>> expectedBaseAttributes = new HashMap<>();
512+
expectedBaseAttributes.put( "TEST_PROPERTY", Set.of(
513+
"withUrn(AspectModelUrn.fromUrn(NAMESPACE + \"testProperty\"))",
514+
"withPreferredName(Locale.forLanguageTag(\"en\"), \"Test Property\")",
515+
"withDescription(Locale.forLanguageTag(\"en\"), \"This is a test property.\")",
516+
"withSee(\"http://example.com/\")",
517+
"withSee(\"http://example.com/me\")"
518+
) );
519+
520+
result.assertMetaModelBaseAttributesForProperties( "MetaAspectWithCollection", expectedBaseAttributes );
521+
522+
final String expectedExampleValue = "Optional.of(new DefaultScalarValue(\"Example Value\", new DefaultScalar(\"http://www.w3"
523+
+ ".org/2001/XMLSchema#string\")))";
524+
525+
result.assertConstructorArgumentForProperties(
526+
"MetaAspectWithCollection",
527+
ImmutableMap.<String, String> builder()
528+
.put( "TEST_PROPERTY", expectedExampleValue )
529+
.build(),
530+
2
531+
);
532+
}
533+
501534
@Test
502535
void testCharacteristicInstantiationForQuantifiableWithUnit() throws IOException {
503536
final TestAspect aspect = TestAspect.ASPECT_WITH_QUANTIFIABLE_WITH_UNIT;

0 commit comments

Comments
 (0)