use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class PolicyResourceTest method shouldHandleInvalidQueryErrors.
@Test
public void shouldHandleInvalidQueryErrors() throws Exception {
// Given
QueryRequest request = mock(QueryRequest.class);
QueryResourceHandler handler = mock(QueryResourceHandler.class);
given(mockStore.query(request)).willThrow(new EntitlementException(EntitlementException.INVALID_SEARCH_FILTER));
// When
Promise<QueryResponse, ResourceException> promise = policyResource.queryCollection(mockServerContext, request, handler);
// Then
assertQueryPromiseFailedWithCodes(promise, ResourceException.BAD_REQUEST, EntitlementException.INVALID_SEARCH_FILTER);
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class PolicyResourceWithCopyMoveSupport method copyOrMovePoliciesByApplication.
private ActionResponse copyOrMovePoliciesByApplication(Context context, ActionRequest request, PolicyAction copyOrMoveAction) throws ResourceException {
JsonValue payload = request.getContent();
JsonValue from = payload.get("from");
JsonValue to = payload.get("to");
if (from.isNull()) {
throw new BadRequestException("from definition is missing");
}
if (!from.isDefined("application")) {
throw new BadRequestException("from application definition is missing");
}
String sourceApplication = from.get("application").asString();
if (to.isNull()) {
throw new BadRequestException("to definition is missing");
}
String sourceRealm = RealmContext.getRealm(context);
String destinationRealm = to.get("realm").defaultTo(sourceRealm).asString();
String destinationApplication = to.get("application").defaultTo(sourceApplication).asString();
JsonValue resourceTypeMapping = payload.get("resourceTypeMapping").defaultTo(Collections.emptyMap());
String namePostfix = to.get("namePostfix").defaultTo("").asString();
QueryRequest queryRequest = Requests.newQueryRequest("policies");
queryRequest.setQueryFilter(QueryFilter.equalTo(new JsonPointer("applicationName"), sourceApplication));
final List<JsonValue> policies = new ArrayList<>();
router.handleQuery(context, queryRequest, new QueryResourceHandler() {
@Override
public boolean handleResource(ResourceResponse resourceResponse) {
policies.add(resourceResponse.getContent());
return true;
}
}).getOrThrowUninterruptibly();
JsonValue actionResponseContent = json(array());
for (JsonValue policy : policies) {
ActionResponse response = copyOrMoveGivenPolicy(context, policy, destinationRealm, destinationApplication, namePostfix, resourceTypeMapping, copyOrMoveAction);
actionResponseContent.add(response.getJsonContent().asMap());
}
return Responses.newActionResponse(actionResponseContent);
}
use of org.forgerock.json.resource.QueryResourceHandler in project OpenAM by OpenRock.
the class DecisionCombinersResourceTest method testSuccessfulJsonificationAndQuery.
@Test
public void testSuccessfulJsonificationAndQuery() throws JsonMappingException {
//given
SubjectContext mockSubjectContext = mock(SubjectContext.class);
RealmContext realmContext = new RealmContext(mockSubjectContext);
Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
Subject mockSubject = new Subject();
given(mockSubjectContext.getCallerSubject()).willReturn(mockSubject);
QueryRequest mockRequest = mock(QueryRequest.class);
QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
JsonSchema mockSchema = mock(JsonSchema.class);
given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
//when
testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
//then
verify(mockHandler, times(1)).handleResource(any(ResourceResponse.class));
}
use of org.forgerock.json.resource.QueryResourceHandler 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.QueryResourceHandler in project OpenAM by OpenRock.
the class SubjectAttributesResourceV1Test method shouldPerformQueryForSubjectAttributes.
@Test
public void shouldPerformQueryForSubjectAttributes() throws Exception {
//given
SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
RealmContext realmContext = new RealmContext(mockSSOTokenContext);
Context mockServerContext = ClientContext.newInternalClientContext(realmContext);
Subject mockSubject = new Subject();
given(mockSSOTokenContext.getCallerSubject()).willReturn(mockSubject);
QueryRequest mockRequest = mock(QueryRequest.class);
QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
Set<String> attributes = new HashSet<>();
attributes.add("attr");
attributes.add("attr2");
given(mockSAM.getAvailableSubjectAttributeNames()).willReturn(attributes);
//when
Promise<QueryResponse, ResourceException> promise = subjectAttributesResource.queryCollection(mockServerContext, mockRequest, mockHandler);
//then
promise.getOrThrowUninterruptibly();
ArgumentCaptor<ResourceResponse> captor = ArgumentCaptor.forClass(ResourceResponse.class);
verify(mockHandler, times(2)).handleResource(captor.capture());
}
Aggregations