Skip to content

Commit dbdfc71

Browse files
authored
[7.67.x-blue] Improve Import Resolver error messages to be more user friendly (#6014) (#6020)
* [incubator-kie-issues#1150] Improve Import Resolver error messages to be more user friendly (#6014) * Improved Error Messages + Logs * dependabot.yml fixed * dependabot.yml fixed * Change Request * Minor change * Tests fixed (cherry picked from commit ddb72c6) * JAVA 11 API
1 parent 689b0b7 commit dbdfc71

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed

kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/compiler/ImportDMNResolverUtil.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,88 @@
2424
import javax.xml.namespace.QName;
2525

2626
import org.kie.dmn.feel.util.Either;
27+
import org.kie.dmn.model.api.Definitions;
2728
import org.kie.dmn.model.api.Import;
2829
import org.kie.dmn.model.api.NamespaceConsts;
2930
import org.kie.dmn.model.v1_1.TImport;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3033

3134
public class ImportDMNResolverUtil {
3235

36+
private static final Logger LOGGER = LoggerFactory.getLogger(ImportDMNResolverUtil.class);
37+
3338
private ImportDMNResolverUtil() {
3439
// No constructor for util class.
3540
}
3641

37-
public static <T> Either<String, T> resolveImportDMN(Import _import, Collection<T> all, Function<T, QName> idExtractor) {
38-
final String iNamespace = _import.getNamespace();
39-
final String iName = _import.getName();
40-
final String iModelName = _import.getAdditionalAttributes().get(TImport.MODELNAME_QNAME);
41-
List<T> allInNS = all.stream()
42-
.filter(m -> idExtractor.apply(m).getNamespaceURI().equals(iNamespace))
43-
.collect(Collectors.toList());
44-
if (allInNS.size() == 1) {
45-
T located = allInNS.get(0);
42+
public static <T> Either<String, T> resolveImportDMN(Import importElement, Collection<T> dmns, Function<T, QName> idExtractor) {
43+
final String importerDMNNamespace = ((Definitions) importElement.getParent()).getNamespace();
44+
final String importerDMNName = ((Definitions) importElement.getParent()).getName();
45+
final String importNamespace = importElement.getNamespace();
46+
final String importName = importElement.getName();
47+
final String importLocationURI = importElement.getLocationURI(); // This is optional
48+
final String importModelName = importElement.getAdditionalAttributes().get(TImport.MODELNAME_QNAME);
49+
50+
LOGGER.debug("Resolving an Import in DMN Model with name={} and namespace={}. " +
51+
"Importing a DMN model with namespace={} name={} locationURI={}, modelName={}",
52+
importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName);
53+
54+
List<T> matchingDMNList = dmns.stream()
55+
.filter(m -> idExtractor.apply(m).getNamespaceURI().equals(importNamespace))
56+
.collect(Collectors.toList());
57+
if (matchingDMNList.size() == 1) {
58+
T located = matchingDMNList.get(0);
4659
// Check if the located DMN Model in the NS, correspond for the import `drools:modelName`.
47-
if (iModelName == null || idExtractor.apply(located).getLocalPart().equals(iModelName)) {
60+
if (importModelName == null || idExtractor.apply(located).getLocalPart().equals(importModelName)) {
61+
LOGGER.debug("DMN Model with name={} and namespace={} successfully imported a DMN " +
62+
"with namespace={} name={} locationURI={}, modelName={}",
63+
importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName);
4864
return Either.ofRight(located);
4965
} else {
50-
return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, located within namespace only %s but does not match for the actual name",
51-
iNamespace, iName, iModelName,
52-
idExtractor.apply(located)));
66+
LOGGER.error("DMN Model with name={} and namespace={} can't import a DMN with namespace={}, name={}, modelName={}, " +
67+
"located within namespace only {} but does not match for the actual modelName",
68+
importerDMNNamespace, importerDMNName, importNamespace, importName, importModelName, idExtractor.apply(located));
69+
return Either.ofLeft(String.format(
70+
"DMN Model with name=%s and namespace=%s can't import a DMN with namespace=%s, name=%s, modelName=%s, " +
71+
"located within namespace only %s but does not match for the actual modelName",
72+
importerDMNNamespace, importerDMNName, importNamespace, importName, importModelName, idExtractor.apply(located)));
5373
}
5474
} else {
55-
List<T> usingNSandName = allInNS.stream()
56-
.filter(m -> idExtractor.apply(m).getLocalPart().equals(iModelName))
57-
.collect(Collectors.toList());
75+
List<T> usingNSandName = matchingDMNList.stream()
76+
.filter(dmn -> idExtractor.apply(dmn).getLocalPart().equals(importModelName))
77+
.collect(Collectors.toList());
5878
if (usingNSandName.size() == 1) {
79+
LOGGER.debug("DMN Model with name={} and namespace={} successfully imported a DMN " +
80+
"with namespace={} name={} locationURI={}, modelName={}",
81+
importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName);
5982
return Either.ofRight(usingNSandName.get(0));
60-
} else if (usingNSandName.size() == 0) {
61-
return Either.ofLeft(String.format("Could not locate required dependency while importing DMN for namespace: %s, name: %s, modelName: %s.",
62-
iNamespace, iName, iModelName));
83+
} else if (usingNSandName.isEmpty()) {
84+
LOGGER.error("DMN Model with name={} and namespace={} failed to import a DMN with namespace={} name={} locationURI={}, modelName={}.",
85+
importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName);
86+
return Either.ofLeft(String.format(
87+
"DMN Model with name=%s and namespace=%s failed to import a DMN with namespace=%s name=%s locationURI=%s, modelName=%s. ",
88+
importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName));
6389
} else {
64-
return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, could not locate required dependency within: %s.",
65-
iNamespace, iName, iModelName,
66-
allInNS.stream().map(idExtractor).collect(Collectors.toList())));
90+
LOGGER.error("DMN Model with name={} and namespace={} detected a collision ({} elements) trying to import a DMN with namespace={} name={} locationURI={}, modelName={}",
91+
importerDMNNamespace, importerDMNName, usingNSandName.size(), importNamespace, importName, importLocationURI, importModelName);
92+
return Either.ofLeft(String.format(
93+
"DMN Model with name=%s and namespace=%s detected a collision trying to import a DMN with %s namespace, " +
94+
"%s name and modelName %s. There are %s DMN files with the same namespace in your project. " +
95+
"Please change the DMN namespaces and make them unique to fix this issue.",
96+
importerDMNNamespace, importerDMNName, importNamespace, importName, importModelName, usingNSandName.size()));
6797
}
6898
}
6999
}
70100

71-
public static enum ImportType {
101+
public enum ImportType {
72102
UNKNOWN,
73103
DMN,
74104
PMML;
75105
}
76106

77-
public static ImportType whichImportType(Import _import) {
78-
switch (_import.getImportType()) {
107+
public static ImportType whichImportType(Import importElement) {
108+
switch (importElement.getImportType()) {
79109
case org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase.URI_DMN:
80110
case "http://www.omg.org/spec/DMN1-2Alpha/20160929/MODEL":
81111
case org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase.URI_DMN:

kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/compiler/ImportDMNResolverUtilTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
import org.junit.Test;
1212
import org.kie.dmn.feel.util.Either;
13+
import org.kie.dmn.model.api.Definitions;
1314
import org.kie.dmn.model.api.Import;
1415
import org.kie.dmn.model.v1_1.TImport;
1516

1617
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.mockito.Mockito.mock;
1719

1820
public class ImportDMNResolverUtilTest {
1921

@@ -156,6 +158,10 @@ private Import makeImport(final String namespace, final String name, final Strin
156158
addAttributes.put(TImport.MODELNAME_QNAME, modelName);
157159
}
158160
i.setAdditionalAttributes(addAttributes);
161+
final Definitions definitions = mock(Definitions.class);
162+
definitions.setNamespace("ParentDMNNamespace");
163+
definitions.setName("ParentDMN");
164+
i.setParent(definitions);
159165
return i;
160166
}
161167

0 commit comments

Comments
 (0)