Search in sources :

Example 71 with RealmContext

use of org.forgerock.openam.rest.RealmContext 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));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) RealmContext(org.forgerock.openam.rest.RealmContext) QueryRequest(org.forgerock.json.resource.QueryRequest) ResourceResponse(org.forgerock.json.resource.ResourceResponse) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 72 with RealmContext

use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.

the class ApplicationsResourceTest method shouldThrowBadRequestIfSubjectNotFoundOnRead.

@Test(expectedExceptions = BadRequestException.class)
public void shouldThrowBadRequestIfSubjectNotFoundOnRead() throws ResourceException {
    // Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("REALM", "REALM");
    Context context = ClientContext.newInternalClientContext(realmContext);
    given(subjectContext.getCallerSubject()).willReturn(null);
    // When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.readInstance(context, null, null);
    // Then
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 73 with RealmContext

use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.

the class ApplicationsResourceTest method updateInstanceShouldReturnConflictExceptionWhenApplicationNameAlreadyExists.

@Test(expectedExceptions = ConflictException.class)
public void updateInstanceShouldReturnConflictExceptionWhenApplicationNameAlreadyExists() throws EntitlementException, ResourceException {
    //Given
    SSOTokenContext subjectContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("REALM", "REALM");
    Context context = ClientContext.newInternalClientContext(realmContext);
    String resourceId = "iPlanetAMWebAgentService";
    UpdateRequest request = mock(UpdateRequest.class);
    Subject subject = new Subject();
    JsonValue content = mock(JsonValue.class);
    Application application = mock(Application.class);
    Application newApplication = mock(Application.class);
    given(subjectContext.getCallerSubject()).willReturn(subject);
    given(request.getContent()).willReturn(content);
    given(applicationManagerWrapper.getApplication(subject, "/REALM", resourceId)).willReturn(application);
    given(applicationManagerWrapper.getApplication(subject, "/REALM", "APP_NAME")).willReturn(application);
    given(applicationWrapper.getName()).willReturn("APP_NAME");
    given(applicationWrapper.getApplication()).willReturn(newApplication);
    given(newApplication.getLastModifiedDate()).willReturn(1000L);
    doThrow(EntitlementException.class).when(applicationWrapper).toJsonValue();
    //When
    Promise<ResourceResponse, ResourceException> result = applicationsResource.updateInstance(context, resourceId, request);
    //Then
    result.getOrThrowUninterruptibly();
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) UpdateRequest(org.forgerock.json.resource.UpdateRequest) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Matchers.anyString(org.mockito.Matchers.anyString) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 74 with RealmContext

use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.

the class ApplicationsResourceTest method shouldThrowInternalErrorIfResourceWillNotSave.

@Test(expectedExceptions = InternalServerErrorException.class)
public void shouldThrowInternalErrorIfResourceWillNotSave() throws EntitlementException, ResourceException {
    //given
    SSOTokenContext mockSSOTokenContext = mock(SSOTokenContext.class);
    RealmContext realmContext = new RealmContext(mockSSOTokenContext);
    realmContext.setSubRealm("/", "/");
    CreateRequest mockCreateRequest = mock(CreateRequest.class);
    Subject subject = new Subject();
    Application mockApplication = mock(Application.class);
    given(mockSSOTokenContext.getCallerSubject()).willReturn(subject);
    given(applicationWrapper.getApplication()).willReturn(mockApplication);
    given(mockApplication.getName()).willReturn("newApplication");
    doThrow(new EntitlementException(1)).when(applicationManagerWrapper).saveApplication(any(Subject.class), anyString(), any(Application.class));
    //when
    Promise<ResourceResponse, ResourceException> result = applicationsResource.createInstance(realmContext, mockCreateRequest);
    //then
    result.getOrThrowUninterruptibly();
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CreateRequest(org.forgerock.json.resource.CreateRequest) ResourceException(org.forgerock.json.resource.ResourceException) Application(com.sun.identity.entitlement.Application) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 75 with RealmContext

use of org.forgerock.openam.rest.RealmContext in project OpenAM by OpenRock.

the class ResourceTypesResourceTest method setUp.

@BeforeMethod
public void setUp() throws ResourceException {
    callerSubject = new Subject();
    // to mock the HTTP method, we need the following contexts
    Context httpContext = new HttpContext(json(object(field(HttpContext.ATTR_HEADERS, Collections.singletonMap("method", Arrays.asList("PUT"))), field(HttpContext.ATTR_PARAMETERS, Collections.emptyMap()))), null);
    Context securityContext = new SecurityContext(httpContext, null, null);
    Context subjectContext = new SSOTokenContext(mock(Debug.class), null, securityContext) {

        @Override
        public Subject getCallerSubject() {
            return callerSubject;
        }
    };
    RealmContext realmContext = new RealmContext(subjectContext);
    realmContext.setSubRealm("/", "/");
    mockServerContext = ClientContext.newInternalClientContext(realmContext);
    resourceTypeService = mock(MockResourceTypeService.class);
    Debug debug = mock(Debug.class);
    resourceTypesResource = new ResourceTypesResource(debug, new EntitlementsExceptionMappingHandler(EntitlementRestGuiceModule.getEntitlementsErrorHandlers()), resourceTypeService);
    rawData.put("name", Collections.singleton("myResourceType"));
    rawData.put("description", Collections.singleton("myResourceType"));
    rawData.put("realm", Collections.singleton("/"));
    rawData.put("actions", Collections.singleton("CREATE"));
    rawData.put("patterns", Collections.singleton("http://example.com:80/*"));
    rawData.put("creationDate", Collections.singleton(String.valueOf(new Date().getTime())));
    rawData.put("lastModifiedDate", Collections.singleton(String.valueOf(new Date().getTime())));
}
Also used : SecurityContext(org.forgerock.services.context.SecurityContext) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) HttpContext(org.forgerock.json.resource.http.HttpContext) SecurityContext(org.forgerock.services.context.SecurityContext) Subject(javax.security.auth.Subject) Debug(com.sun.identity.shared.debug.Debug) Date(java.util.Date) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

RealmContext (org.forgerock.openam.rest.RealmContext)94 ResourceException (org.forgerock.json.resource.ResourceException)63 ResourceResponse (org.forgerock.json.resource.ResourceResponse)58 Context (org.forgerock.services.context.Context)53 Test (org.testng.annotations.Test)53 Subject (javax.security.auth.Subject)42 ClientContext (org.forgerock.services.context.ClientContext)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)40 JsonValue (org.forgerock.json.JsonValue)35 Matchers.anyString (org.mockito.Matchers.anyString)27 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)20 BadRequestException (org.forgerock.json.resource.BadRequestException)19 SSOException (com.iplanet.sso.SSOException)17 Application (com.sun.identity.entitlement.Application)16 ForbiddenException (org.forgerock.json.resource.ForbiddenException)16 NotFoundException (org.forgerock.json.resource.NotFoundException)15 PermanentException (org.forgerock.json.resource.PermanentException)15 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)15 ReadRequest (org.forgerock.json.resource.ReadRequest)15 SSOToken (com.iplanet.sso.SSOToken)14