Skip to content

Commit

Permalink
Merge pull request #678 from bci-oss/623-meta-class-does-not-include-…
Browse files Browse the repository at this point in the history
…exampleValue

Meta Class doesn't include exampleValue
  • Loading branch information
Yauhenikapl authored Nov 19, 2024
2 parents a7cf8f2 + 79992a9 commit da24cd3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
Expand Down Expand Up @@ -90,6 +91,7 @@
import org.eclipse.esmf.metamodel.impl.DefaultComplexType;
import org.eclipse.esmf.metamodel.impl.DefaultEntity;
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.staticmetamodel.PropertyContainer;
Expand Down Expand Up @@ -170,6 +172,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
.put( "DefaultRangeConstraint", DefaultRangeConstraint.class )
.put( "DefaultRegularExpressionConstraint", DefaultRegularExpressionConstraint.class )
.put( "DefaultScalar", DefaultScalar.class )
.put( "DefaultScalarValue", DefaultScalarValue.class )
.put( "DefaultSet", DefaultSet.class )
.put( "DefaultSingleEntity", DefaultSingleEntity.class )
.put( "DefaultSortedSet", DefaultSortedSet.class )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,12 @@ private <T> String getOptionalStaticDeclarationValue( final Type type, final Opt
return "Optional.of(" + valueInitializer.apply( xsdType, valueExpression ) + ")";
}

public String exampleValue( final Property property, final StaticCodeGenerationContext context ) {
return property.getExampleValue()
.map( exampleValue -> "Optional.of(" + this.visitScalarValue( exampleValue, context ) + ")" )
.orElse( "Optional.empty()" );
}

/*
* This method is a crutch because velocity is not able to call the parameterized actual method
*/
Expand Down Expand Up @@ -574,6 +580,7 @@ public String getMetaModelBaseAttributes( final ModelElement element, final Stat
} );
element.getSee().stream().sorted()
.forEach( see -> builder.append( ".withSee(" ).append( AspectModelJavaUtil.createLiteral( see ) ).append( ")" ) );

builder.append( ".build()" );
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
## $arg1, $arg2, ... ) {
$modelVisitor.metaModelBaseAttributes( $property, $context ),
#propertyCharacteristic( $property.getCharacteristic().get(), $propertyType ),
Optional.empty(),
$modelVisitor.exampleValue( $property, $context ),
$property.isOptional(),
$property.isNotInPayload(),
Optional.of("$property.getPayloadName()"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

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

@Test
void testCharacteristicInstantiationForPropertyWithExampleValue() throws IOException {
final TestAspect aspect = TestAspect.ASPECT_WITH_COLLECTION;
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );

result.assertNumberOfFiles( 2 );

Map<String, Set<String>> expectedBaseAttributes = new HashMap<>();
expectedBaseAttributes.put( "TEST_PROPERTY", Set.of(
"withUrn(AspectModelUrn.fromUrn(NAMESPACE + \"testProperty\"))",
"withPreferredName(Locale.forLanguageTag(\"en\"), \"Test Property\")",
"withDescription(Locale.forLanguageTag(\"en\"), \"This is a test property.\")",
"withSee(\"http://example.com/\")",
"withSee(\"http://example.com/me\")"
) );

result.assertMetaModelBaseAttributesForProperties( "MetaAspectWithCollection", expectedBaseAttributes );

final String expectedExampleValue = "Optional.of(new DefaultScalarValue(\"Example Value\", new DefaultScalar(\"http://www.w3"
+ ".org/2001/XMLSchema#string\")))";

result.assertConstructorArgumentForProperties(
"MetaAspectWithCollection",
ImmutableMap.<String, String> builder()
.put( "TEST_PROPERTY", expectedExampleValue )
.build(),
2
);
}

@Test
void testCharacteristicInstantiationForQuantifiableWithUnit() throws IOException {
final TestAspect aspect = TestAspect.ASPECT_WITH_QUANTIFIABLE_WITH_UNIT;
Expand Down

0 comments on commit da24cd3

Please sign in to comment.