Skip to content

Commit

Permalink
fix: update the query pick code & test
Browse files Browse the repository at this point in the history
  • Loading branch information
berezovskyi committed Oct 1, 2024
1 parent 1928470 commit 8317ffc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,65 @@ private synchronized void initializeRdf() {
List<Resource> responseInfos = iter.toList();

infoResource = null;
if (responseInfos.size() == 1) {
infoResource = responseInfos.get(0);
} else if (responseInfos.size() > 1) {
List<Resource> 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<Resource> responseInfos) {
List<Resource> 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<Resource> responseInfos) {
List<Resource> 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<Resource> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, () -> {
//
// });
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8317ffc

Please sign in to comment.