From 8317ffc2ee343ef07bbd3a4169a0a1a910c5c65b Mon Sep 17 00:00:00 2001 From: Andrew Berezovskyi Date: Tue, 1 Oct 2024 23:07:14 +0200 Subject: [PATCH] fix: update the query pick code & test --- .../lyo/client/query/OslcQueryResult.java | 72 ++++++++++++++----- .../eclipse/lyo/client/OslcClientTest.java | 3 - .../lyo/client/query/OslcQueryResultTest.java | 7 +- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/client/oslc-client/src/main/java/org/eclipse/lyo/client/query/OslcQueryResult.java b/client/oslc-client/src/main/java/org/eclipse/lyo/client/query/OslcQueryResult.java index 49488d67..a51dacd5 100644 --- a/client/oslc-client/src/main/java/org/eclipse/lyo/client/query/OslcQueryResult.java +++ b/client/oslc-client/src/main/java/org/eclipse/lyo/client/query/OslcQueryResult.java @@ -129,31 +129,65 @@ private synchronized void initializeRdf() { List responseInfos = iter.toList(); infoResource = null; - if (responseInfos.size() == 1) { - infoResource = responseInfos.get(0); - } else if (responseInfos.size() > 1) { - List infos_sameURI = responseInfos.stream().filter(ri -> ri.getURI().equals(query.getQueryUrl())).toList(); - if (infos_sameURI.size() == 1) { - infoResource = infos_sameURI.get(0); - } else if (infos_sameURI.size() > 1) { - throw new IllegalStateException("Multiple ResponseInfo objects found with the same URI"); - } else { - // TODO: also check for oslc:nextPage before giving up - throw new IllegalStateException("Multiple ResponseInfo objects found; neither matches the Query URI"); - } + infoResource = tryFindOnlyResponseInfo(responseInfos); + if(infoResource == null && responseInfos.size() > 1) { + infoResource = tryFindExactResponseInfoUri(responseInfos); + } + if(infoResource == null && responseInfos.size() > 1) { + infoResource = tryFindPrefixedResponseInfoUri(responseInfos); + } + if(infoResource == null) { + // TODO: also check for oslc:nextPage before giving up + throw new IllegalStateException("Multiple ResponseInfo objects found; neither matches the Query URI"); } - - while (iter.hasNext()) { - infoResource = iter.next(); - break; - } - membersResource = rdfModel.getResource(query.getCapabilityUrl()); } } - String getNextPageUrl() { + /** + * Extracts a ResourceInfo resource if one and only one has the same prefix as the query URI. + * @param responseInfos from OSLC Query results + * @return a ResourceInfo resource if one satisfies the conditions; null otherwise + */ + private Resource tryFindPrefixedResponseInfoUri(List responseInfos) { + List filteredObjects = responseInfos.stream().filter(ri -> ri.getURI().startsWith(query.getQueryUrl())).toList(); + if (filteredObjects.size() == 1) { + return filteredObjects.get(0); + } else if (filteredObjects.size() > 1) { + throw new IllegalStateException("Multiple ResponseInfo objects found starting with the same Query URI"); + } + return null; + } + + /** + * Extracts a ResourceInfo resource if one and only one has exactly the same URI as the query URI. + * @param responseInfos from OSLC Query results + * @return a ResourceInfo resource if one satisfies the conditions; null otherwise + */ + private Resource tryFindExactResponseInfoUri(List responseInfos) { + List filteredObjects = responseInfos.stream().filter(ri -> ri.getURI().equals(query.getQueryUrl())).toList(); + if (filteredObjects.size() == 1) { + return filteredObjects.get(0); + } else if (filteredObjects.size() > 1) { + throw new IllegalStateException("Multiple ResponseInfo objects found with the same URI"); + } + return null; + } + + /** + * Extracts a ResourceInfo resource if one and only one exists in the results. + * @param responseInfos from OSLC Query results + * @return a ResourceInfo resource if one satisfies the conditions; null otherwise + */ + private Resource tryFindOnlyResponseInfo(List responseInfos) { + if (responseInfos.size() == 1) { + return responseInfos.get(0); + } + return null; + } + + String getNextPageUrl() { initializeRdf(); if ((nextPageUrl == null || nextPageUrl.isEmpty()) && infoResource != null) { Property predicate = rdfModel.getProperty(OslcConstants.OSLC_CORE_NAMESPACE, "nextPage"); diff --git a/client/oslc-client/src/test/java/org/eclipse/lyo/client/OslcClientTest.java b/client/oslc-client/src/test/java/org/eclipse/lyo/client/OslcClientTest.java index 66fe6fe2..3643f231 100644 --- a/client/oslc-client/src/test/java/org/eclipse/lyo/client/OslcClientTest.java +++ b/client/oslc-client/src/test/java/org/eclipse/lyo/client/OslcClientTest.java @@ -42,9 +42,6 @@ public void postInvalidOlscResource() throws IOException, URISyntaxException { request.getExtendedProperties().put(new QName("http://example.com/ns#", "test"), "test"); Response response = client.createResource("http://open-services.net/.well-known/resource-that-should-not-exist-whose-status-code-should-not-be-200", request, OSLCConstants.CT_RDF); assertThat(response.getStatusInfo().getFamily() != Family.SUCCESSFUL); -// assertThrows(ClientErrorException.class, () -> { -// -// }); }); } diff --git a/client/oslc-client/src/test/java/org/eclipse/lyo/client/query/OslcQueryResultTest.java b/client/oslc-client/src/test/java/org/eclipse/lyo/client/query/OslcQueryResultTest.java index a11e4fc4..4f1dfc44 100644 --- a/client/oslc-client/src/test/java/org/eclipse/lyo/client/query/OslcQueryResultTest.java +++ b/client/oslc-client/src/test/java/org/eclipse/lyo/client/query/OslcQueryResultTest.java @@ -104,14 +104,15 @@ public void testAnyMember() { @Test public void testMultiResponseInfos() { -// System.setProperty(OslcQueryResult.SELECT_ANY_MEMBER, "true"); + // seems to work with both +// System.setProperty(OslcQueryResult.SELECT_ANY_MEMBER, "false"); Response mockedResponse = mockClientResponse("/multiResponseQuery.rdf"); OslcQueryParameters params = new OslcQueryParameters(); params.setSelect("dcterms:title"); - OslcQuery query = new OslcQuery(new OslcClient(), "http://example.com/query"); + OslcQuery query = new OslcQuery(new OslcClient(), "https://nordic.clm.ibmcloud.com/ccm/oslc/contexts/_2nC4UBNvEeutmoeSPr3-Ag/workitems"); OslcQueryResult result = new OslcQueryResult(query, mockedResponse); - assertEquals(5, result.getMembersUrls().length); + assertEquals(20, result.getMembersUrls().length); } private Response mockClientResponse(String file) {