Skip to content

Commit 561a6a9

Browse files
committed
[#756] Enhance the typeRef validator to check all the typeRefs
1 parent d6d59fc commit 561a6a9

File tree

3 files changed

+116
-8
lines changed

3 files changed

+116
-8
lines changed

dmn-core/src/main/java/com/gs/dmn/validation/TypeRefValidator.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public List<Pair<TNamedElement, String>> makeErrorReport(DMNModelRepository dmnM
5555
TypeRefValidatorVisitor visitor = new TypeRefValidatorVisitor(this.logger, this.errorHandler);
5656
for (TDefinitions definitions: dmnModelRepository.getAllDefinitions()) {
5757
TypeRefValidationContext context = new TypeRefValidationContext(definitions, dmnModelRepository, errorReport);
58-
definitions.accept(visitor, context);
58+
try {
59+
definitions.accept(visitor, context);
60+
} catch (Exception e) {
61+
String errorMessage = String.format("Error during validation of typeRefs for model '%s': %s", definitions.getName(), e.getMessage());
62+
logger.error(errorMessage);
63+
}
5964
}
6065
return errorReport;
6166
}
@@ -132,14 +137,23 @@ protected QName visitTypeRef(QName typeRef, TypeRefValidationContext context) {
132137
if (isPrimitiveType(typeRef1) || StringUtils.isEmpty(typeRef1)) {
133138
return typeRef;
134139
}
135-
136-
// Lookup for itemDefinitions starting from the current model
137140
TDefinitions model = context.getModel();
138-
QualifiedName qualifiedName = QualifiedName.toQualifiedName(model, typeRef1);
139-
TItemDefinition itemDefinition = context.getRepository().lookupItemDefinition(model, qualifiedName);
140-
if (itemDefinition == null) {
141+
try {
142+
// Lookup for itemDefinitions starting from the current model
143+
QualifiedName qualifiedName = QualifiedName.toQualifiedName(model, typeRef1);
144+
TItemDefinition itemDefinition = context.getRepository().lookupItemDefinition(model, qualifiedName);
145+
if (itemDefinition == null) {
146+
// Record error
147+
TNamedElement element = context.getElement();
148+
String errorMessage = ErrorFactory.makeDMNErrorMessage(model, element, String.format("Cannot find definition of typeRef '%s'", typeRef1));
149+
context.getErrorReport().add(new Pair<>(element, errorMessage));
150+
151+
this.logger.debug(errorMessage);
152+
}
153+
} catch (Exception e) {
154+
// Record error
141155
TNamedElement element = context.getElement();
142-
String errorMessage = ErrorFactory.makeDMNErrorMessage(model, element, String.format("Cannot find definition of typeRef '%s'", typeRef1));
156+
String errorMessage = ErrorFactory.makeDMNErrorMessage(model, element, String.format("Error during lookup of typeRef '%s': %s", typeRef1, e.getMessage()));
143157
context.getErrorReport().add(new Pair<>(element, errorMessage));
144158

145159
this.logger.debug(errorMessage);

dmn-core/src/test/java/com/gs/dmn/validation/TypeRefValidatorTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.junit.jupiter.api.Test;
1616

17+
import java.util.Arrays;
1718
import java.util.Collections;
1819
import java.util.List;
1920

@@ -29,13 +30,22 @@ public void testValidateWhenCorrect() {
2930
}
3031

3132
@Test
32-
public void validate() {
33+
public void testValidateWhenMissingV11() {
3334
List<String> expectedErrors = Collections.singletonList(
3435
"(model='test-dmn-with-missing-type-ref', label='Applicant', name='applicant', id='id-d2376567fde3c9400ee327ecec21e36d'): Cannot find definition of typeRef 'applicant'"
3536
);
3637
validate(validator, resource("dmn/input/1.1/test-dmn-with-missing-type-ref.dmn"), expectedErrors);
3738
}
3839

40+
@Test
41+
public void testValidateWhenMissingV15() {
42+
List<String> expectedErrors = Arrays.asList(
43+
"(model='test-dmn-with-missing-type-ref', label='Applicant', name='applicant', id='id-d2376567fde3c9400ee327ecec21e36d'): Cannot find definition of typeRef 'applicant'",
44+
"(model='test-dmn-with-missing-type-ref', label='Applicant1', name='applicant1', id='id-d2376567fde3c9400ee327ecec21e36d-2'): Error during lookup of typeRef 'prefix.importedType': Cannot find DM for namespace 'missing-namespace'"
45+
);
46+
validate(validator, resource("dmn/input/1.5/test-dmn-with-missing-type-ref.dmn"), expectedErrors);
47+
}
48+
3949
@Test
4050
public void testValidateDefinitionsWhenNull() {
4151
List<String> actualErrors = validator.validate(null);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitions xmlns="https://www.omg.org/spec/DMN/20230324/MODEL/" id="definitions" name="test-dmn-with-missing-type-ref"
3+
namespace="http://camunda.org/schema/1.0/dmn">
4+
<import name="prefix" namespace="missing-namespace" importType="https://www.omg.org/spec/DMN/20230324/MODEL/" />
5+
<decision id="cip-assessments" name="CIP Assessments">
6+
<decisionTable id="decisionTable">
7+
<input id="input1" label="Gender">
8+
<inputExpression id="inputExpression1" typeRef="string">
9+
<text>gender</text>
10+
</inputExpression>
11+
</input>
12+
<input id="InputClause_1szr045" label="Marital Status">
13+
<inputExpression id="LiteralExpression_0xv8t8j" typeRef="string">
14+
<text>maritalStatus</text>
15+
</inputExpression>
16+
</input>
17+
<input id="InputClause_1ykgpxy" label="Organization Detail - Legal Name">
18+
<inputExpression id="LiteralExpression_19vzu6u" typeRef="string">
19+
<text>organizationDetail.legalName</text>
20+
</inputExpression>
21+
</input>
22+
<input id="InputClause_1io1f6n" label="OD - Date of Formation">
23+
<inputExpression id="LiteralExpression_1dd8ao2" typeRef="date">
24+
<text>organizationDetail.dateOfFormationOrEstablishment</text>
25+
</inputExpression>
26+
</input>
27+
<input id="InputClause_1eayjts" label="Registration - registeredWith">
28+
<inputExpression id="LiteralExpression_1m52w10" typeRef="string">
29+
<text>registration.registeredWith</text>
30+
</inputExpression>
31+
</input>
32+
<output id="output1" label="" name="" typeRef="string"/>
33+
<rule id="row-168857152-1">
34+
<inputEntry id="UnaryTests_0793v4j">
35+
<text><![CDATA[= "Female"]]></text>
36+
</inputEntry>
37+
<inputEntry id="UnaryTests_11h9jt8">
38+
<text></text>
39+
</inputEntry>
40+
<inputEntry id="UnaryTests_1oudzn9">
41+
<text></text>
42+
</inputEntry>
43+
<inputEntry id="UnaryTests_0lujdme">
44+
<text></text>
45+
</inputEntry>
46+
<inputEntry id="UnaryTests_1dt0ujl">
47+
<text></text>
48+
</inputEntry>
49+
<outputEntry id="LiteralExpression_0eot1u3">
50+
<text>O1</text>
51+
</outputEntry>
52+
</rule>
53+
<rule id="row-168857152-2">
54+
<inputEntry id="UnaryTests_0z8g5ay">
55+
<text></text>
56+
</inputEntry>
57+
<inputEntry id="UnaryTests_0hgmxbd">
58+
<text><![CDATA[= "Married"]]></text>
59+
</inputEntry>
60+
<inputEntry id="UnaryTests_0kj7afx">
61+
<text></text>
62+
</inputEntry>
63+
<inputEntry id="UnaryTests_10duijr">
64+
<text></text>
65+
</inputEntry>
66+
<inputEntry id="UnaryTests_0wmifyx">
67+
<text></text>
68+
</inputEntry>
69+
<outputEntry id="LiteralExpression_1jqtn61">
70+
<text>O2</text>
71+
</outputEntry>
72+
</rule>
73+
</decisionTable>
74+
</decision>
75+
<inputData name="applicant" id="id-d2376567fde3c9400ee327ecec21e36d" label="Applicant">
76+
<variable typeRef="applicant" name="applicant" id="id-d2376567fde3c9400ee327ecec21e36d_variable"/>
77+
</inputData>
78+
<inputData name="string" id="id-d2376567fde3c9400ee327ecec21e36d-1" label="String">
79+
<variable typeRef="string" name="string" id="id-d2376567fde3c9400ee327ecec21e36d-1_variable"/>
80+
</inputData>
81+
<inputData name="applicant1" id="id-d2376567fde3c9400ee327ecec21e36d-2" label="Applicant1">
82+
<variable typeRef="prefix.importedType" name="applicant1" id="id-d2376567fde3c9400ee327ecec21e36d-2_variable"/>
83+
</inputData>
84+
</definitions>

0 commit comments

Comments
 (0)