Skip to content

Commit

Permalink
Showing 3 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -4,12 +4,14 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
@@ -295,6 +297,7 @@ private RosettaEnumValue createEnumValue(XsdEnumeration ev) {
rosettaEnumValue.setName(value);
extractDocs(ev).ifPresent(rosettaEnumValue::setDefinition);
extractDocs(ev, DOC_ANNOTATION_SOURCE_NAME)
.filter(x -> StringUtils.isNotEmpty(x))
.filter(x -> !x.equals(ev.getValue()))
.ifPresent(rosettaEnumValue::setDisplay);

@@ -305,13 +308,14 @@ private Resource createResource(String namespace, String type) {
return resourceSet.createResource(URI.createURI(namespace.substring(namespace.indexOf('.') + 1)
.replace('.', '-') + "-" + type + ".rosetta"));
}

private Optional<String> extractDocs(XsdAnnotatedElements ev) {
return Optional.ofNullable(ev)
.map(XsdAnnotatedElements::getAnnotation)
.map(XsdAnnotation::getDocumentations)
.map(xsdDocs -> xsdDocs.stream()
.filter(x -> documentationSources.contains(x.getSource()))
// default to definition if source not specified
.filter(x -> x.getSource() == null || documentationSources.contains(x.getSource()))
.map(XsdAnnotationChildren::getContent)
.map(x -> x.replace('\n', ' '))
.map(x -> x.replace('\r', ' '))
@@ -324,6 +328,7 @@ private Optional<String> extractDocs(XsdAnnotatedElements ev, String docAnnotati
.map(XsdAnnotatedElements::getAnnotation)
.map(XsdAnnotation::getDocumentations)
.map(xsdDocs -> xsdDocs.stream()
.filter(x -> x.getSource() != null)
.filter(x -> x.getSource().equals(docAnnotationSourceName))
.map(XsdAnnotationChildren::getContent)
.map(x -> x.replace('\n', ' '))
Original file line number Diff line number Diff line change
@@ -41,7 +41,10 @@
@InjectWith(RosettaInjectorProvider.class)
public class RosettaModelFactoryTest {

// contains documentation elements with source attributes
private static final String DATA_XSD_PATH = "src/test/resources/model-import/data.xsd";
// contains documentation elements without source attributes
private static final String DATA2_XSD_PATH = "src/test/resources/model-import/data2.xsd";
private static final String NAMESPACE = "test.ns";
private static final String NAMESPACE_DEFINITION = "test.ns definition";

@@ -180,6 +183,50 @@ void shouldGenerateAddAttributesToData() {
assertAttribute(attrs.get(7), "fooBarListAttr", "Bar", 1, 2, false, "FooBarListAttr definition.");
}

@Test
void shouldGenerateAddAttributesToData2() {
GenerationProperties properties = mock(GenerationProperties.class);
when(properties.getNamespace()).thenReturn(NAMESPACE);
when(properties.getNamespaceDefinition()).thenReturn(NAMESPACE_DEFINITION);

RosettaModelFactory factory = new RosettaModelFactory(resourceSet, rosettaTypeMappings);
RosettaBody rosettaBody = factory.createBody(BODY_TYPE, BODY_NAME, BODY_DEFINITION);
RosettaCorpus rosettaCorpus = factory.createCorpus(rosettaBody, CORPUS_TYPE, CORPUS_NAME, CORPUS_DISPLAY_NAME, CORPUS_DEFINITION);
RosettaSegment rosettaSegment = factory.createSegment(SEGMENT);
RosettaModel rosettaModel = factory.createRosettaModel("type", properties, List.of());

// Load xsd elements, create data elements, and add to rosetta model elements
List<XsdNamedElements> xsdElements = getXsdElements(DATA2_XSD_PATH, XsdNamedElements.class);
List<Data> dataTypes = xsdElements.stream()
.map(element -> factory.createData(element))
.collect(Collectors.toList());
rosettaModel.getElements().addAll(dataTypes);

// test
XsdComplexType xsdComplexTypeFoo = (XsdComplexType) xsdElements.get(3);
factory.addAttributesToData(xsdComplexTypeFoo, rosettaBody, rosettaCorpus, rosettaSegment);

// assert
Data foo = dataTypes.get(3);

assertEquals("Foo", foo.getName());
assertEquals("Foo definition.", foo.getDefinition());

List<Attribute> attrs = foo.getAttributes();
assertEquals(8, attrs.size());

assertAttribute(attrs.get(0), "fooBooleanAttr", "boolean", 1, 1, false, "FooBooleanAttr definition.");
assertAttribute(attrs.get(1), "fooStrAttr", "string", 1, 1, false, "FooStrAttr definition.");
assertAttribute(attrs.get(2), "fooDecimalAttr", "number", 0, 1, false, "FooDecimalAttr definition.");
assertAttribute(attrs.get(3), "fooStringWithRestrictionAttr", "string", 1, 1, false, "FooStringWithRestrictionAttr definition.",
"Specifies a character string with a maximum length of 500 characters.", "Max500Text");
assertAttribute(attrs.get(4), "fooDecimalWithRestrictionAttr", "number", 0, 1, false, "FooDecimalWithRestrictionAttr definition.",
"Number (max 999) of objects represented as an integer.", "Max3Number");
assertAttribute(attrs.get(5), "fooBarAttr", "Bar", 1, 1, false, "FooBarAttr definition.");
assertAttribute(attrs.get(6), "fooStrListAttr", "string", 0, 0, true, "FooStrListAttr definition.");
assertAttribute(attrs.get(7), "fooBarListAttr", "Bar", 1, 2, false, "FooBarListAttr definition.");
}

private void assertAttribute(Attribute attr, String name, String type, int inf, int sup, boolean unbounded, String definition) {
assertEquals(name, attr.getName());
assertEquals(type, attr.getTypeCall().getType().getName());
80 changes: 80 additions & 0 deletions rosetta-tools/src/test/resources/model-import/data2.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="Max500Text">
<xs:annotation>
<xs:documentation>Specifies a character string with a maximum length of 500 characters.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="500"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Max3Number">
<xs:annotation>
<xs:documentation>Number (max 999) of objects represented as an integer.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="0"/>
<xs:totalDigits value="3"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Bar">
<xs:annotation>
<xs:documentation>Bar definition.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="BarStrAttr" type="xs:string">
<xs:annotation>
<xs:documentation>Bar string attribute definition.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Foo">
<xs:annotation>
<xs:documentation>Foo definition.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="FooBooleanAttr" type="xs:boolean">
<xs:annotation>
<xs:documentation>FooBooleanAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooStrAttr" type="xs:string">
<xs:annotation>
<xs:documentation>FooStrAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooDecimalAttr" type="xs:decimal" minOccurs="0">
<xs:annotation>
<xs:documentation>FooDecimalAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooStringWithRestrictionAttr" type="Max500Text">
<xs:annotation>
<xs:documentation>FooStringWithRestrictionAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooDecimalWithRestrictionAttr" type="Max3Number" minOccurs="0">
<xs:annotation>
<xs:documentation>FooDecimalWithRestrictionAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooBarAttr" type="Bar">
<xs:annotation>
<xs:documentation>FooBarAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooStrListAttr" type="xs:string" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>FooStrListAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FooBarListAttr" type="Bar" minOccurs="1" maxOccurs="2">
<xs:annotation>
<xs:documentation>FooBarListAttr definition.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

0 comments on commit e8f8d98

Please sign in to comment.