use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ApplicationsResourceTest method mockQueryRequest.
private QueryRequest mockQueryRequest(QueryFilter<JsonPointer> filter) {
QueryRequest request = mock(QueryRequest.class);
given(request.getQueryFilter()).willReturn(filter);
return request;
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ConditionTypesResourceTest method testSuccessfulJsonificationAndQuery.
@Test
public void testSuccessfulJsonificationAndQuery() throws JsonMappingException {
//given
SubjectContext mockSubjectContext = mock(SubjectContext.class);
RealmContext realmContext = new RealmContext(mockSubjectContext);
Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
Subject mockSubject = new Subject();
given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
QueryRequest mockRequest = mock(QueryRequest.class);
JsonSchema mockSchema = mock(JsonSchema.class);
given(mockRequest.getPageSize()).willReturn(2);
given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
//when
testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
//then
verify(mockHandler, times(2)).handleResource(any(ResourceResponse.class));
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method shouldHandleQueryPageLargerThanResults.
@Test
public void shouldHandleQueryPageLargerThanResults() throws Exception {
//given
QueryRequest queryRequest = makeMockQueryRequest();
QueryResponseHandler queryHandler = makeQueryResponseHandler();
final int lastPageSize = 5;
final int pageOffset = 10;
final int resultSize = 11;
Map<String, Map<String, Set<String>>> resourceTypes = new HashMap<>();
for (int i = 0; i < resultSize; i++) {
resourceTypes.put(UUID.randomUUID().toString(), rawData);
}
when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenReturn(resourceTypes);
when(resourceTypeService.getResourceType(any(Subject.class), anyString(), anyString())).thenReturn(EntitlementUtils.resourceTypeFromMap(UUID.randomUUID().toString(), rawData));
when(queryRequest.getPagedResultsOffset()).thenReturn(pageOffset);
when(queryRequest.getPageSize()).thenReturn(lastPageSize);
Answer<Boolean> onlyFirstPage = new Answer<Boolean>() {
int count = pageOffset;
@Override
public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
return count++ < resultSize;
}
};
when(queryHandler.handleResource(any(ResourceResponse.class))).thenAnswer(onlyFirstPage);
//when
Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
//then
QueryResponse queryResponse = promise.getOrThrowUninterruptibly();
verify(queryHandler, times(resultSize - pageOffset)).handleResource(any(ResourceResponse.class));
assertThat(queryResponse.getRemainingPagedResults()).isEqualTo(0);
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method makeMockQueryRequest.
/**
* Generates a mock request which will simulate the required behaviour of requesting
* the now deprecated behaviour of returning the remaining results.
*
* @return Non null.
*/
private static QueryRequest makeMockQueryRequest() {
QueryRequest mockRequest = mock(QueryRequest.class);
given(mockRequest.getAdditionalParameter(QueryResponsePresentation.REMAINING)).willReturn("true");
return mockRequest;
}
use of org.forgerock.json.resource.QueryRequest in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method queryShouldPageResults.
@Test
public void queryShouldPageResults() throws Exception {
//given
QueryRequest queryRequest = makeMockQueryRequest();
QueryResponseHandler queryHandler = makeQueryResponseHandler();
final int firstPageSize = 2;
final int pageOffset = 0;
final int resultSize = 10;
Map<String, Map<String, Set<String>>> resourceTypes = new HashMap<>();
for (int i = 0; i < resultSize; i++) {
resourceTypes.put(UUID.randomUUID().toString(), rawData);
}
when(resourceTypeService.getResourceTypesData(any(Subject.class), anyString())).thenReturn(resourceTypes);
when(resourceTypeService.getResourceType(any(Subject.class), anyString(), anyString())).thenReturn(EntitlementUtils.resourceTypeFromMap(UUID.randomUUID().toString(), rawData));
when(queryRequest.getPagedResultsOffset()).thenReturn(pageOffset);
when(queryRequest.getPageSize()).thenReturn(firstPageSize);
Answer<Boolean> onlyFirstPage = new Answer<Boolean>() {
int count = 0;
@Override
public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
return count++ < firstPageSize;
}
};
when(queryHandler.handleResource(any(ResourceResponse.class))).thenAnswer(onlyFirstPage);
//when
Promise<QueryResponse, ResourceException> promise = resourceTypesResource.queryCollection(mockServerContext, queryRequest, queryHandler);
//then
QueryResponse queryResponse = promise.getOrThrowUninterruptibly();
verify(queryHandler, times(firstPageSize)).handleResource(any(ResourceResponse.class));
assertThat(queryResponse.getRemainingPagedResults()).isEqualTo(resultSize - firstPageSize);
}
Aggregations