Search in sources :

Example 1 with Connection

use of org.forgerock.json.resource.Connection in project OpenAM by OpenRock.

the class ElevatedConnectionFactoryWrapperTest method requestGetsElevatedToAdminSession.

@Test
public void requestGetsElevatedToAdminSession() throws Exception {
    // Given
    SSOToken ssoToken = mock(SSOToken.class);
    given(ssoTokenPrivilegedAction.run()).willReturn(ssoToken);
    SSOPrincipal principal = new SSOPrincipal("test");
    given(ssoToken.getPrincipal()).willReturn(principal);
    SSOTokenID tokenID = mock(SSOTokenID.class);
    given(ssoToken.getTokenID()).willReturn(tokenID);
    given(internalConnectionFactory.getConnection()).willReturn(connection);
    // When
    RootContext context = new RootContext();
    ReadRequest readRequest = Requests.newReadRequest("/test", "abc");
    try (Connection connection = connectionFactory.getConnection()) {
        connection.read(context, readRequest);
    }
    // Then
    verify(connection).read(contextCaptor.capture(), eq(readRequest));
    Context capturedContext = contextCaptor.getValue();
    assertThat(capturedContext.containsContext(SecurityContext.class)).isTrue();
    SecurityContext securityContext = capturedContext.asContext(SecurityContext.class);
    assertThat(securityContext.getAuthenticationId()).isEqualTo("test");
    assertThat(securityContext.getAuthorization()).containsOnlyKeys("authLevel", "tokenId");
}
Also used : SSOTokenID(com.iplanet.sso.SSOTokenID) RootContext(org.forgerock.services.context.RootContext) SecurityContext(org.forgerock.services.context.SecurityContext) Context(org.forgerock.services.context.Context) RootContext(org.forgerock.services.context.RootContext) SSOToken(com.iplanet.sso.SSOToken) SSOPrincipal(com.iplanet.sso.providers.dpro.SSOPrincipal) Connection(org.forgerock.json.resource.Connection) SecurityContext(org.forgerock.services.context.SecurityContext) ReadRequest(org.forgerock.json.resource.ReadRequest) Test(org.testng.annotations.Test)

Example 2 with Connection

use of org.forgerock.json.resource.Connection in project OpenAM by OpenRock.

the class TrustedDevicesResourceTest method shouldDeleteTrustedDevice.

@Test
public void shouldDeleteTrustedDevice() throws ResourceException {
    //Given
    DeleteRequest request = Requests.newDeleteRequest("UUID_1");
    Connection connection = newInternalConnection(newCollection(resource));
    List<JsonValue> devices = new ArrayList<JsonValue>();
    devices.add(json(object(field("uuid", "UUID_1"), field("name", "NAME_1"))));
    devices.add(json(object(field("uuid", "UUID_2"), field("name", "NAME_2"))));
    given(dao.getDeviceProfiles(anyString(), anyString())).willReturn(devices);
    //When
    connection.delete(ctx(), request);
    //Then
    ArgumentCaptor<List> devicesCaptor = ArgumentCaptor.forClass(List.class);
    verify(dao).saveDeviceProfiles(anyString(), anyString(), devicesCaptor.capture());
    assertThat(devicesCaptor.getValue()).hasSize(1);
}
Also used : Resources.newInternalConnection(org.forgerock.json.resource.Resources.newInternalConnection) Connection(org.forgerock.json.resource.Connection) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(org.forgerock.json.resource.DeleteRequest) Test(org.testng.annotations.Test)

Example 3 with Connection

use of org.forgerock.json.resource.Connection in project OpenAM by OpenRock.

the class TrustedDevicesResourceTest method shouldQueryTrustedDevices.

@Test
public void shouldQueryTrustedDevices() throws ResourceException {
    //Given
    QueryRequest request = Requests.newQueryRequest("");
    Connection connection = newInternalConnection(newCollection(resource));
    QueryResourceHandler handler = mock(QueryResourceHandler.class);
    List<JsonValue> devices = new ArrayList<>();
    devices.add(json(object(field("name", "NAME_1"), field("lastSelectedDate", new Date().getTime()))));
    devices.add(json(object(field("name", "NAME_2"), field("lastSelectedDate", new Date().getTime() + 1000))));
    given(dao.getDeviceProfiles(anyString(), anyString())).willReturn(devices);
    //When
    connection.query(ctx(), request, handler);
    //Then
    verify(handler, times(2)).handleResource(Matchers.<ResourceResponse>anyObject());
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) Resources.newInternalConnection(org.forgerock.json.resource.Resources.newInternalConnection) Connection(org.forgerock.json.resource.Connection) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 4 with Connection

use of org.forgerock.json.resource.Connection in project OpenAM by OpenRock.

the class AuditEventPublisherImpl method publishForRealm.

private void publishForRealm(String realm, String topic, AuditEvent auditEvent) throws ResourceException {
    AMAuditService auditService = auditServiceProvider.getAuditService(realm);
    Connection connection = newInternalConnection(auditService);
    CreateRequest request = newCreateRequest(topic, auditEvent.getValue());
    try {
        connection.create(new RootContext(), request);
    } catch (ServiceUnavailableException e) {
        debug.message("Audit Service for realm {} is unavailable. Trying the default Audit Service.", realm, e);
        publishToDefault(topic, auditEvent);
    }
}
Also used : RootContext(org.forgerock.services.context.RootContext) CreateRequest(org.forgerock.json.resource.CreateRequest) Requests.newCreateRequest(org.forgerock.json.resource.Requests.newCreateRequest) Connection(org.forgerock.json.resource.Connection) Resources.newInternalConnection(org.forgerock.json.resource.Resources.newInternalConnection) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException)

Example 5 with Connection

use of org.forgerock.json.resource.Connection in project OpenAM by OpenRock.

the class TrustedDevicesResourceTest method shouldNotDeleteTrustedDeviceWhenNotFound.

@Test(expectedExceptions = NotFoundException.class)
public void shouldNotDeleteTrustedDeviceWhenNotFound() throws ResourceException {
    //Given
    DeleteRequest request = Requests.newDeleteRequest("UUID_3");
    Connection connection = newInternalConnection(newCollection(resource));
    List<JsonValue> devices = new ArrayList<JsonValue>();
    devices.add(json(object(field("uuid", "UUID_1"), field("name", "NAME_1"))));
    devices.add(json(object(field("uuid", "UUID_2"), field("name", "NAME_2"))));
    given(dao.getDeviceProfiles(anyString(), anyString())).willReturn(devices);
    //When
    connection.delete(ctx(), request);
//Then
//Expected NotFoundException
}
Also used : Resources.newInternalConnection(org.forgerock.json.resource.Resources.newInternalConnection) Connection(org.forgerock.json.resource.Connection) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) DeleteRequest(org.forgerock.json.resource.DeleteRequest) Test(org.testng.annotations.Test)

Aggregations

Connection (org.forgerock.json.resource.Connection)7 Resources.newInternalConnection (org.forgerock.json.resource.Resources.newInternalConnection)5 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)3 JsonValue (org.forgerock.json.JsonValue)3 RootContext (org.forgerock.services.context.RootContext)3 SSOToken (com.iplanet.sso.SSOToken)2 CreateRequest (org.forgerock.json.resource.CreateRequest)2 DeleteRequest (org.forgerock.json.resource.DeleteRequest)2 Requests.newCreateRequest (org.forgerock.json.resource.Requests.newCreateRequest)2 SSOTokenID (com.iplanet.sso.SSOTokenID)1 SSOPrincipal (com.iplanet.sso.providers.dpro.SSOPrincipal)1 Date (java.util.Date)1 List (java.util.List)1 QueryRequest (org.forgerock.json.resource.QueryRequest)1 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)1 ReadRequest (org.forgerock.json.resource.ReadRequest)1 ServiceUnavailableException (org.forgerock.json.resource.ServiceUnavailableException)1 Context (org.forgerock.services.context.Context)1 SecurityContext (org.forgerock.services.context.SecurityContext)1