use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class SubjectTypesResourceTest method testSuccessfulJsonificationAndQuery.
@Test
public void testSuccessfulJsonificationAndQuery() throws Exception {
//given
SSOTokenContext mockSubjectContext = mock(SSOTokenContext.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);
JsonSchema mockSchema = mock(JsonSchema.class);
QueryResourceHandler mockHandler = mock(QueryResourceHandler.class);
given(mockRequest.getPageSize()).willReturn(2);
given(mockHandler.handleResource(any(ResourceResponse.class))).willReturn(true);
given(mockMapper.generateJsonSchema((Class<?>) any(Class.class))).willReturn(mockSchema);
//when
Promise<QueryResponse, ResourceException> promise = testResource.queryCollection(mockServerContext, mockRequest, mockHandler);
//then
assertThat(promise).succeeded();
verify(mockHandler, times(2)).handleResource(any(ResourceResponse.class));
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class ClientResourceTest method shouldCreateIdentity.
@Test
public void shouldCreateIdentity() throws SSOException, IdRepoException, ResourceException {
// Given
Map<String, ArrayList<String>> client = new HashMap<>();
//must contain userpassword, realm, client_id, "com.forgerock.openam.oauth2provider.clientType"
client.put("client_id", new ArrayList(Arrays.asList("client")));
client.put("userpassword", new ArrayList(Arrays.asList("password")));
client.put("realm", new ArrayList(Arrays.asList("/")));
client.put("com.forgerock.openam.oauth2provider.clientType", new ArrayList(Arrays.asList("Public")));
when(mockSubSchema.getAttributeSchema(eq("client"))).thenReturn(mock(AttributeSchema.class));
when(mockSubSchema.getAttributeSchema(eq("userpassword"))).thenReturn(mock(AttributeSchema.class));
when(mockSubSchema.getAttributeSchema(eq("realm"))).thenReturn(mock(AttributeSchema.class));
when(mockSubSchema.getAttributeSchema(eq("com.forgerock.openam.oauth2provider.clientType"))).thenReturn(mock(AttributeSchema.class));
CreateRequest request = mock(CreateRequest.class);
JsonValue val = mock(JsonValue.class);
when(request.getContent()).thenReturn(val);
when(val.getObject()).thenReturn(client);
Map<String, String> responseVal = new HashMap<String, String>();
responseVal.put("success", "true");
JsonValue response = new JsonValue(responseVal);
ResourceResponse expectedResource = newResourceResponse("results", "1", response);
// When
Promise<ResourceResponse, ResourceException> createInstancePromise = resource.createInstance(null, request);
// Then
assertThat(createInstancePromise).succeeded().withObject().isEqualToIgnoringGivenFields(expectedResource, ResourceResponse.FIELD_REVISION, ResourceResponse.FIELD_CONTENT);
ResourceResponse resourceResponse = createInstancePromise.getOrThrowUninterruptibly();
assertEquals(resourceResponse.getContent().toString(), response.toString());
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class ApplicationV1FilterTest method createFailsWhenNoResourcesDefined.
/**
* Verifies that creation fails when no resources are defined.
*/
@Test(expectedExceptions = BadRequestException.class)
public void createFailsWhenNoResourcesDefined() throws ResourceException {
// Given
// Build application JSON representation.
JsonValue jsonValue = json(object(TestData.DATA_SET_1.getActions().asJson()));
CreateRequest createRequest = mock(CreateRequest.class);
given(createRequest.getContent()).willReturn(jsonValue);
// When
Promise<ResourceResponse, ResourceException> result = filter.filterCreate(context, createRequest, requestHandler);
// Then
result.getOrThrowUninterruptibly();
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldNotDeleteInstanceWhenSubjectIsNull.
@Test(expectedExceptions = BadRequestException.class)
public void shouldNotDeleteInstanceWhenSubjectIsNull() throws EntitlementException, ResourceException {
//Given
SSOTokenContext subjectContext = mock(SSOTokenContext.class);
Context context = ClientContext.newInternalClientContext(subjectContext);
String resourceId = "RESOURCE_ID";
DeleteRequest request = mock(DeleteRequest.class);
Subject subject = null;
given(subjectContext.getCallerSubject()).willReturn(subject);
//When
Promise<ResourceResponse, ResourceException> result = applicationsResource.deleteInstance(context, resourceId, request);
//Then
verify(applicationManagerWrapper, never()).deleteApplication(subject, "REALM", resourceId);
result.getOrThrowUninterruptibly();
}
use of org.forgerock.json.resource.ResourceException in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldReturnThreeResultsOnQuery.
@Test
public void shouldReturnThreeResultsOnQuery() throws EntitlementException, IllegalAccessException, InstantiationException, ResourceException {
// Override the creation of the application wrapper so to return a mocked version.
applicationsResource = new ApplicationsResource(debug, applicationManagerWrapper, applicationTypeManagerWrapper, queryAttributes, resourceErrorHandler) {
@Override
protected ApplicationWrapper createApplicationWrapper(Application application, ApplicationTypeManagerWrapper type) {
ApplicationWrapper wrapper = mock(ApplicationWrapper.class);
String appName = application.getName();
given(wrapper.getName()).willReturn(appName);
try {
JsonValue jsonValue = json(object(field("name", appName)));
given(wrapper.toJsonValue()).willReturn(jsonValue);
} catch (EntitlementException e) {
fail();
}
return wrapper;
}
};
// Given
SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class);
Subject subject = new Subject();
given(mockSubjectContext.getCallerSubject()).willReturn(subject);
RealmContext realmContext = new RealmContext(mockSubjectContext);
realmContext.setSubRealm("abc", "abc");
Context serverContext = ClientContext.newInternalClientContext(realmContext);
// Set the page size to be three starting from the second item.
QueryRequest request = mock(QueryRequest.class);
given(request.getAdditionalParameter(QueryResponsePresentation.REMAINING)).willReturn("true");
given(request.getPageSize()).willReturn(3);
given(request.getPagedResultsOffset()).willReturn(1);
given(request.getSortKeys()).willReturn(Arrays.asList(SortKey.ascendingOrder("name")));
QueryResourceHandler handler = mock(QueryResourceHandler.class);
given(handler.handleResource(any(ResourceResponse.class))).willReturn(true);
Set<String> appNames = asSet("app1", "app2", "app3", "app4", "app5", "iPlanetAMWebAgentService");
given(applicationManagerWrapper.search(eq(subject), eq("/abc"), any(Set.class))).willReturn(appNames);
for (String appName : appNames) {
Application app = mock(Application.class);
given(app.getName()).willReturn(appName);
given(applicationManagerWrapper.getApplication(eq(subject), eq("/abc"), eq(appName))).willReturn(app);
}
// When
Promise<QueryResponse, ResourceException> result = applicationsResource.queryCollection(serverContext, request, handler);
// Then
verify(applicationManagerWrapper).search(eq(subject), eq("/abc"), any(Set.class));
verify(applicationManagerWrapper, times(appNames.size())).getApplication(eq(subject), eq("/abc"), anyString());
ArgumentCaptor<ResourceResponse> resourceCapture = ArgumentCaptor.forClass(ResourceResponse.class);
verify(handler, times(3)).handleResource(resourceCapture.capture());
List<String> selectedApps = transformList(resourceCapture.getAllValues(), new ResourceToIdMapper());
assertThat(selectedApps).containsOnly("app2", "app3", "app4");
QueryResponse response = result.getOrThrowUninterruptibly();
assertThat(response.getRemainingPagedResults()).isEqualTo(2);
}
Aggregations