Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing prefix in AnySimpleType extension for TCK #672

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
public class DMNConstants {
public static final String DMN_FILE_EXTENSION = ".dmn";

// XSD
public static final String XSD_NS = "http://www.w3.org/2001/XMLSchema";
public static final String XSD_PREFIX = "xsd";

// XSI
public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
public static final String XSI_PREFIX = "xsi";
Expand Down
47 changes: 31 additions & 16 deletions dmn-core/src/main/java/com/gs/dmn/serialization/TCKVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class TCKVersion {
protected static final LinkedHashMap<String, String> TCK_1_OTHER_NAMESPACES = new LinkedHashMap<>();
static {
TCK_1_OTHER_NAMESPACES.put(DMNConstants.XSD_NS, DMNConstants.XSD_PREFIX);
TCK_1_OTHER_NAMESPACES.put(DMNConstants.XSI_NS, DMNConstants.XSI_PREFIX);
}

public static final TCKVersion TCK_1 = new TCKVersion("1", "tck/testCases.xsd",
"", "http://www.omg.org/spec/DMN/20160719/testcase",
TCK_1_OTHER_NAMESPACES,
Expand All @@ -29,12 +31,12 @@ public class TCKVersion {

public static final TCKVersion LATEST = TCK_1;

protected static final List<TCKVersion> VALUES = Arrays.asList(TCK_1);
protected static final List<TCKVersion> VALUES = Collections.singletonList(TCK_1);

public static TCKVersion fromVersion(String key) {
for (TCKVersion version: VALUES) {
for (TCKVersion version : VALUES) {
if (version.version.equals(key)) {
return version;
return version;
}
}
throw new IllegalArgumentException(String.format("Cannot find TCK version '%s'", key));
Expand All @@ -44,21 +46,25 @@ public static TCKVersion fromVersion(String key) {
private final String schemaLocation;
private final String prefix;
private final String namespace;
private final Map<String, String> otherNamespaces;
private final LinkedHashMap<String, String> namespaceToPrefixMap;
private final LinkedHashMap<String, String> prefixToNamespaceMap;
private final String javaPackage;
private final LinkedHashMap<String, String> namespaceMap;

TCKVersion(String version, String schemaLocation, String prefix, String namespace, Map<String, String> otherNamespaces, String javaPackage) {
this.version = version;
this.schemaLocation = schemaLocation;
this.prefix = prefix;
this.namespace = namespace;
this.otherNamespaces = otherNamespaces;
this.javaPackage = javaPackage;

this.namespaceMap = new LinkedHashMap<>();
this.namespaceToPrefixMap = new LinkedHashMap<>();
this.prefixToNamespaceMap = new LinkedHashMap<>();
addMap(namespace, prefix);
this.namespaceMap.putAll(otherNamespaces);
for (Map.Entry<String, String> entry : otherNamespaces.entrySet()) {
String otherNamespace = entry.getKey();
String otherPrefix = entry.getValue();
addMap(otherNamespace, otherPrefix);
}
}

public String getVersion() {
Expand All @@ -77,17 +83,26 @@ public String getNamespace() {
return namespace;
}

public Map<String, String> getNamespaceMap() {
return this.namespaceMap;
public Map<String, String> getNamespaceToPrefixMap() {
return this.namespaceToPrefixMap;
}

public LinkedHashMap<String, String> getPrefixToNamespaceMap() {
return prefixToNamespaceMap;
}

public String getJavaPackage() {
return javaPackage;
}

private void addMap(String namespace, String prefix) {
if (!StringUtils.isEmpty(prefix)) {
this.namespaceMap.put(namespace, prefix);
if (StringUtils.isEmpty(namespace)) {
throw new IllegalArgumentException("Namespace cannot be null or empty");
}
if (prefix == null) {
prefix = "";
}
this.namespaceToPrefixMap.put(namespace, prefix);
this.prefixToNamespaceMap.put(prefix, namespace);
}
}
6 changes: 3 additions & 3 deletions dmn-core/src/main/java/com/gs/dmn/tck/TCKSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public abstract class TCKSerializer {
public static boolean isTCKFile(File file) {
return file != null && file.isFile() &&
(
file.getName().endsWith(DEFAULT_TEST_CASE_FILE_EXTENSION) || file.getName().endsWith(TEST_CASE_FILE_EXTENSION)
file.getName().endsWith(DEFAULT_TEST_CASE_FILE_EXTENSION) || file.getName().endsWith(TEST_CASE_FILE_EXTENSION)
);
}

Expand All @@ -45,7 +45,7 @@ public TestCases read(File input) {
try {
return read(input.toURI().toURL());
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input.getPath()), e);
throw new DMNRuntimeException(String.format("Cannot read TCK from '%s'", input.getPath()), e);
}
}

Expand All @@ -66,7 +66,7 @@ public void write(TestCases testCases, File file) {
try {
this.marshaller.marshal(testCases, file);
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot write DMN to '%s'", file.getPath()), e);
throw new DMNRuntimeException(String.format("Cannot write TCK to '%s'", file.getPath()), e);
}
}
}
24 changes: 14 additions & 10 deletions dmn-core/src/main/java/com/gs/dmn/tck/ast/AnySimpleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
import java.util.Map;

import static com.gs.dmn.serialization.DMNConstants.XSI_NS;
import static com.gs.dmn.serialization.DMNConstants.XSI_PREFIX;

@JsonPropertyOrder({
"test"
})
public class AnySimpleType extends TCKBaseElement {
private static final QName NILL_QNAME = new QName(XSI_NS, "nil", XSI_PREFIX);
private static final QName TYPE_QNAME = new QName(XSI_NS, "type", XSI_PREFIX);

public static AnySimpleType from(String localPart, String value, String prefix, String text) {
AnySimpleType anySimpleType = new AnySimpleType();
anySimpleType.getOtherAttributes().put(new QName(XSI_NS, localPart, prefix), value);
Expand All @@ -44,49 +48,49 @@ public static AnySimpleType of(Object value) {
if (value == null) {
AnySimpleType result = new AnySimpleType();
result.setText("");
result.getOtherAttributes().put(new QName(XSI_NS, "nil"), "true");
result.getOtherAttributes().put(NILL_QNAME, "true");
return result;
} else if (value instanceof String) {
AnySimpleType result = new AnySimpleType();
result.setText((String) value);
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:string");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:string");
return result;
} else if (value instanceof Boolean) {
AnySimpleType result = new AnySimpleType();
result.setText("" + value);
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:boolean");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:boolean");
return result;
} else if (value instanceof Float) {
AnySimpleType result = new AnySimpleType();
result.setText("" + value);
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:float");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:float");
return result;
} else if (value instanceof Double) {
AnySimpleType result = new AnySimpleType();
result.setText("" + value);
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:double");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:double");
return result;
} else if (value instanceof BigDecimal) {
AnySimpleType result = new AnySimpleType();
result.setText("" + value);
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:decimal");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:decimal");
return result;
} else if (value instanceof XMLGregorianCalendar) {
AnySimpleType result = new AnySimpleType();
result.setText(value.toString());
QName type = ((XMLGregorianCalendar) value).getXMLSchemaType();
if (type == DatatypeConstants.DATE) {
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:date");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:date");
} else if (type == DatatypeConstants.TIME) {
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:time");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:time");
} else if (type == DatatypeConstants.DATETIME) {
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:dateTime");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:dateTime");
}
return result;
} else if (value instanceof Duration) {
AnySimpleType result = new AnySimpleType();
result.setText(value.toString());
result.getOtherAttributes().put(new QName(XSI_NS, "type"), "xsd:duration");
result.getOtherAttributes().put(TYPE_QNAME, "xsd:duration");
return result;
}
throw new IllegalArgumentException(String.format("Not supported value '%s' yet", value.getClass().getSimpleName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public TestCases unmarshal(String input, boolean validateSchema) {
}
return unmarshal(tckVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling TCK model from reader.", e);
LOGGER.error("Error unmarshalling TCK content from reader.", e);
}
return null;
}
Expand All @@ -88,7 +88,7 @@ public TestCases unmarshal(File input, boolean validateSchema) {
}
return unmarshal(tckVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling DMN model from reader.", e);
LOGGER.error("Error unmarshalling TCK content from reader.", e);
}
return null;
}
Expand All @@ -104,7 +104,7 @@ public TestCases unmarshal(URL input, boolean validateSchema) {
}
return unmarshal(tckVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling DMN model from reader.", e);
LOGGER.error("Error unmarshalling TCK content from reader.", e);
}
return null;
}
Expand All @@ -120,7 +120,7 @@ public TestCases unmarshal(InputStream input, boolean validateSchema) {
}
return unmarshal(tckVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling DMN model from reader.", e);
LOGGER.error("Error unmarshalling TCK content from reader.", e);
}
return null;
}
Expand All @@ -131,7 +131,7 @@ public TestCases unmarshal(Reader input, boolean validateSchema) {
String xml = buffer.lines().collect(Collectors.joining("\n"));
return unmarshal(xml, validateSchema);
} catch (Exception e) {
LOGGER.error("Error unmarshalling DMN model from reader.", e);
LOGGER.error("Error unmarshalling TCK content from reader.", e);
}
return null;
}
Expand Down Expand Up @@ -208,8 +208,8 @@ private boolean validateXMLSchema(Source source, String schemaPath) {
Validator validator = schema.newValidator();
validator.validate(source);
return true;
} catch (Exception e){
LOGGER.error("Invalid DMN file: " + e.getMessage());
} catch (Exception e) {
LOGGER.error("Invalid XML file: " + e.getMessage());
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.gs.dmn.serialization;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class DMNVersionTest {
@Test
void testFromVersion() {
// Used in client code
assertEquals(DMNVersion.DMN_14, DMNVersion.fromVersion("1.4"));
}

@Test
void testFromIncorrectVersion() {
// Used in client code
assertThrows(IllegalArgumentException.class,
() -> {
DMNVersion.fromVersion("xxx");
});
}

@Test
void testNamespaceToPrefixMap() {
// Used in client code
assertNotNull(DMNVersion.LATEST.getNamespaceToPrefixMap());
assertFalse(DMNVersion.LATEST.getNamespaceToPrefixMap().isEmpty());
}

@Test
void testPrefixToNamespaceMap() {
// Used in client code
assertNotNull(DMNVersion.LATEST.getPrefixToNamespaceMap());
assertFalse(DMNVersion.LATEST.getPrefixToNamespaceMap().isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.gs.dmn.serialization;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class TCKVersionTest {
@Test
void testFromVersion() {
// Used in client code
assertEquals(TCKVersion.TCK_1, TCKVersion.fromVersion("1"));
}

@Test
void testFromIncorrectVersion() {
// Used in client code
assertThrows(IllegalArgumentException.class,
() -> {
TCKVersion.fromVersion("1.4");
});
}

@Test
void testNamespaceToPrefixMap() {
// Used in client code
assertNotNull(TCKVersion.LATEST.getNamespaceToPrefixMap());
assertFalse(TCKVersion.LATEST.getNamespaceToPrefixMap().isEmpty());
}

@Test
void testPrefixToNamespaceMap() {
// Used in client code
assertNotNull(TCKVersion.LATEST.getPrefixToNamespaceMap());
assertFalse(TCKVersion.LATEST.getPrefixToNamespaceMap().isEmpty());
}
}
Loading
Loading