use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class RequesterTest method shouldExecuteReadOperation.
@Test
public void shouldExecuteReadOperation() throws ResourceException {
// *** GIVEN ***
String location = "location";
String resourceId = "resourceId";
JsonValue expectedResult = JsonValueBuilder.toJsonValue("{ \"lemon\" : \"custard\" } ");
// Set up mocks
Promise<ResourceResponse, ResourceException> mockResponse = mock(Promise.class);
ResourceResponse mockResourceResponse = mock(ResourceResponse.class);
// Stub mocks
given(mockRealmRouterProvider.get()).willReturn(mockRouter);
given(mockRouter.handleRead(any(Context.class), any(ReadRequest.class))).willReturn(mockResponse);
given(mockResponse.getOrThrowUninterruptibly()).willReturn(mockResourceResponse);
given(mockResourceResponse.getContent()).willReturn(expectedResult);
// *** WHEN ***
JsonValue actualResult = requester.read(location, resourceId, mockServerContext);
// *** THEN ***
verify(mockRealmRouterProvider, times(1)).get();
verifyNoMoreInteractions(mockRealmRouterProvider);
verify(mockRouter, times(1)).handleRead(any(Context.class), any(ReadRequest.class));
verifyNoMoreInteractions(mockRouter);
verify(mockResponse, times(1)).getOrThrowUninterruptibly();
verifyNoMoreInteractions(mockResponse);
verify(mockResourceResponse, times(1)).getContent();
verifyNoMoreInteractions(mockResourceResponse);
assertThat(actualResult).isEqualTo(expectedResult);
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class RequesterTest method shouldExecuteDeleteRequest.
@Test
public void shouldExecuteDeleteRequest() throws ResourceException {
// given
String location = "location";
String resourceId = "resourceId";
JsonValue expectedResult = JsonValueBuilder.toJsonValue("{ \"lemon\" : \"custard\" } ");
// Set up mocks
Promise<ResourceResponse, ResourceException> mockResponse = mock(Promise.class);
ResourceResponse mockResourceResponse = mock(ResourceResponse.class);
// Stub mocks
given(mockRealmRouterProvider.get()).willReturn(mockRouter);
given(mockRouter.handleDelete(any(Context.class), any(DeleteRequest.class))).willReturn(mockResponse);
given(mockResponse.getOrThrowUninterruptibly()).willReturn(mockResourceResponse);
given(mockResourceResponse.getContent()).willReturn(expectedResult);
// when
JsonValue actualResult = requester.delete(location, resourceId, mockServerContext);
// then
verify(mockRealmRouterProvider, times(1)).get();
verifyNoMoreInteractions(mockRealmRouterProvider);
verify(mockRouter, times(1)).handleDelete(any(Context.class), any(DeleteRequest.class));
verifyNoMoreInteractions(mockRouter);
verify(mockResponse, times(1)).getOrThrowUninterruptibly();
verifyNoMoreInteractions(mockResponse);
verify(mockResourceResponse, times(1)).getContent();
verifyNoMoreInteractions(mockResourceResponse);
assertThat(actualResult).isEqualTo(expectedResult);
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class ScriptResourceTest method shouldUpdateScriptConfigurationWithoutError.
@Test
public void shouldUpdateScriptConfigurationWithoutError() throws Exception {
// given
String resourceId = "1234567890";
JsonValue requestJson = json(object(field(SCRIPT_NAME, "MyJavaScript"), field(SCRIPT_DESCRIPTION, "A test script configuration"), field(SCRIPT_TEXT, encodeScript), field(SCRIPT_LANGUAGE, "JAVASCRIPT"), field(SCRIPT_CONTEXT, "POLICY_CONDITION")));
UpdateRequest updateRequest = mock(UpdateRequest.class);
when(updateRequest.getContent()).thenReturn(requestJson);
// when
ResourceResponse response = scriptResource.updateInstance(context, resourceId, updateRequest).getOrThrowUninterruptibly();
// then
assertNotNull(response);
assertNotNull(response.getId());
JsonValue responseJson = response.getContent();
assertEquals(responseJson.get(SCRIPT_NAME).asString(), "MyJavaScript");
assertEquals(responseJson.get(SCRIPT_DESCRIPTION).asString(), "A test script configuration");
assertEquals(responseJson.get(SCRIPT_TEXT).asString(), encodeScript);
assertEquals(getLanguageFromString(responseJson.get(SCRIPT_LANGUAGE).asString()), JAVASCRIPT);
assertEquals(getContextFromString(responseJson.get(SCRIPT_CONTEXT).asString()), POLICY_CONDITION);
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class ScriptResourceTest method shouldQueryScriptConfigurationWithPaging.
@Test
public void shouldQueryScriptConfigurationWithPaging() throws ScriptException, ResourceException {
// given
scriptConfigSet.clear();
for (int i = 0; i < 9; i++) {
ScriptConfiguration sc = ScriptConfiguration.builder().generateId().setName("MyJavaScript" + i).setScript(script).setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build();
scriptConfigSet.put(sc.getId(), sc);
}
QueryResourceHandler resultHandler = mock(QueryResourceHandler.class);
given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
QueryRequest queryRequest = mock(QueryRequest.class);
when(queryRequest.getPageSize()).thenReturn(5);
// when
when(queryRequest.getPagedResultsOffset()).thenReturn(0);
scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
// then
ArgumentCaptor<ResourceResponse> resources = ArgumentCaptor.forClass(ResourceResponse.class);
verify(resultHandler, times(5)).handleResource(resources.capture());
List<ResourceResponse> responses = resources.getAllValues();
assertThat(responses).isNotNull().hasSize(5);
int count = 0;
for (ResourceResponse resource : responses) {
assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
}
// when
Mockito.reset(resultHandler);
given(resultHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
resources = ArgumentCaptor.forClass(ResourceResponse.class);
when(queryRequest.getPagedResultsOffset()).thenReturn(5);
scriptResource.queryCollection(context, queryRequest, resultHandler).getOrThrowUninterruptibly();
verify(resultHandler, times(4)).handleResource(resources.capture());
// then
responses = resources.getAllValues();
assertThat(responses).isNotNull().hasSize(4);
for (ResourceResponse resource : responses) {
assertThat(resource.getContent().get(SCRIPT_NAME).asString()).endsWith(String.valueOf(count++));
}
}
use of org.forgerock.json.resource.ResourceResponse in project OpenAM by OpenRock.
the class UmaPolicyResourceTest method shouldSuccessfullyCreatePolicy.
@Test
@SuppressWarnings("unchecked")
public void shouldSuccessfullyCreatePolicy() {
//Given
Context context = mock(Context.class);
CreateRequest request = Requests.newCreateRequest("/policies", json(object()));
UmaPolicy policy = mock(UmaPolicy.class);
Promise<UmaPolicy, ResourceException> promise = Promises.newResultPromise(policy);
given(policy.getId()).willReturn("ID");
given(policy.getRevision()).willReturn("REVISION");
given(policyService.createPolicy(context, request.getContent())).willReturn(promise);
//When
Promise<ResourceResponse, ResourceException> result = policyResource.createInstance(context, request);
//Then
assertThat(result).succeeded().withId().isEqualTo("ID");
assertThat(result).succeeded().withRevision().isEqualTo("REVISION");
assertThat(result).succeeded().withContent().isEmpty();
}
Aggregations