ImportDMNResolverUtil { public static <T> Either<String, T> resolveImportDMN(Import _import, Collection<T> all, Function<T, QName> idExtractor) { final String iNamespace = _import.getNamespace(); final String iName = _import.getName(); final String iModelName = _import.getAdditionalAttributes().get(TImport.MODELNAME_QNAME); List<T> allInNS = all.stream() .filter(m -> idExtractor.apply(m).getNamespaceURI().equals(iNamespace)) .collect(Collectors.toList()); if (allInNS.size() == 1) { T located = allInNS.get(0); if (iModelName == null || idExtractor.apply(located).getLocalPart().equals(iModelName)) { return Either.ofRight(located); } else { 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", iNamespace, iName, iModelName, idExtractor.apply(located))); } } else { List<T> usingNSandName = allInNS.stream() .filter(m -> idExtractor.apply(m).getLocalPart().equals(iModelName)) .collect(Collectors.toList()); if (usingNSandName.size() == 1) { return Either.ofRight(usingNSandName.get(0)); } else if (usingNSandName.size() == 0) { return Either.ofLeft(String.format("Could not locate required dependency while importing DMN for namespace: %s, name: %s, modelName: %s.", iNamespace, iName, iModelName)); } else { return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, could not locate required dependency within: %s.", iNamespace, iName, iModelName, allInNS.stream().map(idExtractor).collect(Collectors.toList()))); } } } private  ImportDMNResolverUtil(); static Either<String, T> resolveImportDMN(Import _import, Collection<T> all, Function<T, QName> idExtractor); static ImportType whichImportType(Import _import);  }
@Test public void testNSonly() { final Import i = makeImport("ns1", null, null); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
@Test public void testNSandModelName() { final Import i = makeImport("ns1", null, "m1"); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
@Test public void testNSandModelNameWithAlias() { final Import i = makeImport("ns1", "aliased", "m1"); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
@Test public void testNSnoModelNameWithAlias() { final Import i = makeImport("ns1", "mymodel", null); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
@Test public void testNSandUnexistentModelName() { final Import i = makeImport("ns1", null, "boh"); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isLeft()); }
@Test public void testNSnoModelNameDefaultWithAlias2() { final Import i = makeImport("ns1", "boh", null); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
@Test public void testLocateInNS() { final Import i = makeImport("nsA", null, "m1"); final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("nsA", "m1"), result.getOrElse(null)); }
@Test public void testLocateInNSnoModelNameWithAlias() { final Import i = makeImport("nsA", "m1", null); final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isLeft()); }
@Test public void testLocateInNSAliased() { final Import i = makeImport("nsA", "aliased", "m1"); final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("nsA", "m1"), result.getOrElse(null)); }
@Test public void testLocateInNSunexistent() { final Import i = makeImport("nsA", null, "boh"); final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isLeft()); }
@Test public void testLocateInNSnoModelNameWithAlias2() { final Import i = makeImport("nsA", "boh", null); final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isLeft()); }
@Test public void testLocateInNSAliasedBadScenario() { final Import i = makeImport("nsA", "aliased", "mA"); final List<QName> available = Arrays.asList(new QName("nsA", "mA"), new QName("nsA", "mA"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isLeft()); }