/** * Queries all the links for a given mapping, indexed by the source identifier * * <p>This method expects a {@code "links-for-linkType"} defined with a parameter of {@code * "linkType"}. * * @param mapping the mapping to look up the links for * @throws SynchronizationException if the query could not be performed. * @return the mapping from source identifier to the link object for it */ public static Map<String, Link> getLinksForMapping(ObjectMapping mapping, String linkQualifier) throws SynchronizationException { Map<String, Link> sourceIdToLink = new ConcurrentHashMap<String, Link>(); if (mapping != null) { JsonValue query = new JsonValue(new HashMap<String, Object>()); query.put( PARAM_QUERY_FILTER, QueryFilter.and( Arrays.asList( QueryFilter.equalTo("/linkType", mapping.getLinkType().getName()), QueryFilter.equalTo("/linkQualifier", linkQualifier))) .toString()); JsonValue queryResults = linkQuery( mapping.getService().getServerContext(), mapping.getService().getConnectionFactory(), query); for (JsonValue entry : queryResults) { Link link = new Link(mapping); link.fromJsonValue(entry); sourceIdToLink.put(link.sourceId, link); } } return sourceIdToLink; }
/** * Queries the links for a match on the second system (links can be bi-directional) * * <p>This method expects a {@code "links-for-targetId"} defined with a parameter of {@code * "targetId"}. * * @param targetId TODO. * @throws SynchronizationException TODO. */ void getLinkFromSecond(String id) throws SynchronizationException { clear(); if (id != null) { JsonValue query = new JsonValue(new HashMap<String, Object>()); query.put( PARAM_QUERY_FILTER, QueryFilter.and( Arrays.asList( QueryFilter.equalTo("/linkType", mapping.getLinkType().getName()), QueryFilter.equalTo("/linkQualifier", linkQualifier), QueryFilter.equalTo("/secondId", id))) .toString()); getLink(query); } }
@Test public void nameQueryShouldBeSupported() throws Exception { // Given ServerContext context = mock(ServerContext.class); QueryRequest request = mock(QueryRequest.class); given(request.getFields()).willReturn(Arrays.asList(new JsonPointer("/fred"))); QueryResultHandler handler = mock(QueryResultHandler.class); ResourceSetDescription resourceSet = mock(ResourceSetDescription.class); QueryFilter queryFilter = QueryFilter.and( QueryFilter.equalTo("/name", "NAME"), QueryFilter.equalTo("/resourceServer", "myclient"), QueryFilter.equalTo("/policy/permissions/subject", "SUBJECT")); Promise<Collection<ResourceSetDescription>, ResourceException> resourceSetsPromise = Promises.newSuccessfulPromise((Collection<ResourceSetDescription>) asSet(resourceSet)); given(contextHelper.getRealm(context)).willReturn("REALM"); given(contextHelper.getUserId(context)).willReturn("RESOURCE_OWNER_ID"); given(request.getQueryFilter()).willReturn(queryFilter); given( resourceSetService.getResourceSets( eq(context), eq("REALM"), Matchers.<ResourceSetWithPolicyQuery>anyObject(), eq("RESOURCE_OWNER_ID"), eq(false))) .willReturn(resourceSetsPromise); // When resource.queryCollection(context, request, handler); // Then ArgumentCaptor<ResourceSetWithPolicyQuery> queryCaptor = ArgumentCaptor.forClass(ResourceSetWithPolicyQuery.class); verify(resourceSetService) .getResourceSets( eq(context), eq("REALM"), queryCaptor.capture(), eq("RESOURCE_OWNER_ID"), eq(false)); assertThat(queryCaptor.getValue().getOperator()).isEqualTo(AggregateQuery.Operator.AND); assertThat(queryCaptor.getValue().getPolicyQuery()) .isEqualTo(QueryFilter.equalTo("/permissions/subject", "SUBJECT")); assertThat(queryCaptor.getValue().getResourceSetQuery()) .isEqualTo( org.forgerock.util.query.QueryFilter.and( org.forgerock.util.query.QueryFilter.equalTo("name", "NAME"), org.forgerock.util.query.QueryFilter.equalTo("clientId", "myclient"))); verify(handler).handleResult(any(QueryResult.class)); }
@Test public void queryShouldNotBeSupported() { // Given ServerContext context = mock(ServerContext.class); QueryRequest request = mock(QueryRequest.class); QueryResultHandler handler = mock(QueryResultHandler.class); given(request.getQueryFilter()).willReturn(QueryFilter.equalTo(new JsonPointer("/fred"), 5)); // When resource.queryCollection(context, request, handler); // Then verify(handler).handleError(Matchers.<NotSupportedException>anyObject()); }