Search in sources :

Example 1 with ClientConnection

use of org.apache.cayenne.remote.ClientConnection in project cayenne by apache.

the class CayenneContextIT method testBeforeHollowDeleteShouldChangeStateToCommited.

@Test
public void testBeforeHollowDeleteShouldChangeStateToCommited() {
    ObjectId gid = new ObjectId("MtTable1", "a", "b");
    final ClientMtTable1 inflated = new ClientMtTable1();
    inflated.setPersistenceState(PersistenceState.COMMITTED);
    inflated.setObjectId(gid);
    inflated.setGlobalAttribute1("abc");
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(inflated));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    CayenneContext context = new CayenneContext(channel);
    context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
    ClientMtTable1 hollow = context.localObject(inflated);
    assertEquals(PersistenceState.HOLLOW, hollow.getPersistenceState());
    // testing this...
    context.deleteObjects(hollow);
    assertSame(hollow, context.getGraphManager().getNode(gid));
    assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
    assertEquals(PersistenceState.DELETED, hollow.getPersistenceState());
}
Also used : GenericResponse(org.apache.cayenne.util.GenericResponse) BootstrapMessage(org.apache.cayenne.remote.BootstrapMessage) ClientMessage(org.apache.cayenne.remote.ClientMessage) EntityResolver(org.apache.cayenne.map.EntityResolver) ClientChannel(org.apache.cayenne.remote.ClientChannel) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConnection(org.apache.cayenne.remote.ClientConnection) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 2 with ClientConnection

use of org.apache.cayenne.remote.ClientConnection in project cayenne by apache.

the class CayenneContextIT method testBeforePropertyReadShouldInflateHollow.

@Test
public void testBeforePropertyReadShouldInflateHollow() {
    ObjectId gid = new ObjectId("MtTable1", "a", "b");
    final ClientMtTable1 inflated = new ClientMtTable1();
    inflated.setPersistenceState(PersistenceState.COMMITTED);
    inflated.setObjectId(gid);
    inflated.setGlobalAttribute1("abc");
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(inflated));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    // check that a HOLLOW object is infalted on "beforePropertyRead"
    ClientMtTable1 hollow = new ClientMtTable1();
    hollow.setPersistenceState(PersistenceState.HOLLOW);
    hollow.setObjectId(gid);
    final boolean[] selectExecuted = new boolean[1];
    CayenneContext context = new CayenneContext(channel) {

        @Override
        public List<?> performQuery(Query query) {
            selectExecuted[0] = true;
            return super.performQuery(query);
        }
    };
    context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
    context.graphManager.registerNode(hollow.getObjectId(), hollow);
    // testing this...
    context.prepareForAccess(hollow, ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, false);
    assertTrue(selectExecuted[0]);
    assertSame(hollow, context.getGraphManager().getNode(gid));
    assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
    assertEquals(PersistenceState.COMMITTED, hollow.getPersistenceState());
}
Also used : Query(org.apache.cayenne.query.Query) GenericResponse(org.apache.cayenne.util.GenericResponse) BootstrapMessage(org.apache.cayenne.remote.BootstrapMessage) ClientMessage(org.apache.cayenne.remote.ClientMessage) EntityResolver(org.apache.cayenne.map.EntityResolver) ClientChannel(org.apache.cayenne.remote.ClientChannel) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConnection(org.apache.cayenne.remote.ClientConnection) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 3 with ClientConnection

use of org.apache.cayenne.remote.ClientConnection in project cayenne by apache.

the class ClientLocalRuntimeTest method testGetConnection.

@Test
public void testGetConnection() {
    final DataContext serverContext = mock(DataContext.class);
    Module serverModule = binder -> binder.bind(ObjectContextFactory.class).toInstance(new ObjectContextFactory() {

        public ObjectContext createContext(DataChannel parent) {
            return null;
        }

        public ObjectContext createContext() {
            return serverContext;
        }
    });
    ClientRuntime runtime = ClientRuntime.builder().local(DIBootstrap.createInjector(serverModule)).build();
    ClientConnection connection = runtime.getConnection();
    assertNotNull(connection);
    assertTrue(connection instanceof LocalConnection);
    LocalConnection localConnection = (LocalConnection) connection;
    assertTrue(localConnection.getChannel() instanceof ClientServerChannel);
    ClientServerChannel clientServerChannel = (ClientServerChannel) localConnection.getChannel();
    assertSame(serverContext, clientServerChannel.getParentChannel());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) DataContext(org.apache.cayenne.access.DataContext) Collection(java.util.Collection) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) DataChannel(org.apache.cayenne.DataChannel) ClientServerChannel(org.apache.cayenne.access.ClientServerChannel) ClientConnection(org.apache.cayenne.remote.ClientConnection) LocalConnection(org.apache.cayenne.remote.service.LocalConnection) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) DataContext(org.apache.cayenne.access.DataContext) DataChannel(org.apache.cayenne.DataChannel) LocalConnection(org.apache.cayenne.remote.service.LocalConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) ClientServerChannel(org.apache.cayenne.access.ClientServerChannel) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) Test(org.junit.Test)

Example 4 with ClientConnection

use of org.apache.cayenne.remote.ClientConnection in project cayenne by apache.

the class ClientRuntimeBuilderTest method testClientConnection.

@Test
public void testClientConnection() {
    Map<String, String> properties1 = new HashMap<>();
    properties1.put(ClientConstants.ROP_SERVICE_URL_PROPERTY, "http://localhost/YuM");
    ClientModule module = new ClientModule() {

        @Override
        public void configure(Binder binder) {
            super.configure(binder);
            ServerModule.contributeProperties(binder).putAll(properties1);
        }
    };
    Injector injector = DIBootstrap.createInjector(module);
    ClientConnection connection = injector.getInstance(ClientConnection.class);
    assertNotNull(connection);
    assertTrue(connection instanceof HttpClientConnection);
    assertSame("Connection must be a singleton", connection, injector.getInstance(ClientConnection.class));
}
Also used : Binder(org.apache.cayenne.di.Binder) HashMap(java.util.HashMap) Injector(org.apache.cayenne.di.Injector) HttpClientConnection(org.apache.cayenne.rop.HttpClientConnection) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) HttpClientConnection(org.apache.cayenne.rop.HttpClientConnection) Test(org.junit.Test)

Aggregations

ClientConnection (org.apache.cayenne.remote.ClientConnection)4 Test (org.junit.Test)4 MockEventManager (org.apache.cayenne.event.MockEventManager)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 BootstrapMessage (org.apache.cayenne.remote.BootstrapMessage)2 ClientChannel (org.apache.cayenne.remote.ClientChannel)2 ClientMessage (org.apache.cayenne.remote.ClientMessage)2 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)2 GenericResponse (org.apache.cayenne.util.GenericResponse)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 DataChannel (org.apache.cayenne.DataChannel)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ClientServerChannel (org.apache.cayenne.access.ClientServerChannel)1 DataContext (org.apache.cayenne.access.DataContext)1 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)1 Binder (org.apache.cayenne.di.Binder)1 DIBootstrap (org.apache.cayenne.di.DIBootstrap)1 Injector (org.apache.cayenne.di.Injector)1