Search in sources :

Example 1 with BatchMessage

use of org.apache.cassandra.transport.messages.BatchMessage in project cassandra by apache.

the class MessagePayloadTest method testMessagePayload.

@Test
public void testMessagePayload() throws Throwable {
    QueryHandler queryHandler = (QueryHandler) cqlQueryHandlerField.get(null);
    cqlQueryHandlerField.set(null, new TestQueryHandler());
    try {
        requireNetwork();
        Assert.assertSame(TestQueryHandler.class, ClientState.getCQLQueryHandler().getClass());
        SimpleClient client = new SimpleClient(nativeAddr.getHostAddress(), nativePort);
        try {
            client.connect(false);
            Map<String, ByteBuffer> reqMap;
            Map<String, ByteBuffer> respMap;
            QueryMessage queryMessage = new QueryMessage("CREATE TABLE " + KEYSPACE + ".atable (pk int PRIMARY KEY, v text)", QueryOptions.DEFAULT);
            PrepareMessage prepareMessage = new PrepareMessage("SELECT * FROM " + KEYSPACE + ".atable");
            reqMap = Collections.singletonMap("foo", bytes(42));
            responsePayload = respMap = Collections.singletonMap("bar", bytes(42));
            queryMessage.setCustomPayload(reqMap);
            Message.Response queryResponse = client.execute(queryMessage);
            payloadEquals(reqMap, requestPayload);
            payloadEquals(respMap, queryResponse.getCustomPayload());
            reqMap = Collections.singletonMap("foo", bytes(43));
            responsePayload = respMap = Collections.singletonMap("bar", bytes(43));
            prepareMessage.setCustomPayload(reqMap);
            ResultMessage.Prepared prepareResponse = (ResultMessage.Prepared) client.execute(prepareMessage);
            payloadEquals(reqMap, requestPayload);
            payloadEquals(respMap, prepareResponse.getCustomPayload());
            ExecuteMessage executeMessage = new ExecuteMessage(prepareResponse.statementId, QueryOptions.DEFAULT);
            reqMap = Collections.singletonMap("foo", bytes(44));
            responsePayload = respMap = Collections.singletonMap("bar", bytes(44));
            executeMessage.setCustomPayload(reqMap);
            Message.Response executeResponse = client.execute(executeMessage);
            payloadEquals(reqMap, requestPayload);
            payloadEquals(respMap, executeResponse.getCustomPayload());
            BatchMessage batchMessage = new BatchMessage(BatchStatement.Type.UNLOGGED, Collections.<Object>singletonList("INSERT INTO " + KEYSPACE + ".atable (pk,v) VALUES (1, 'foo')"), Collections.singletonList(Collections.<ByteBuffer>emptyList()), QueryOptions.DEFAULT);
            reqMap = Collections.singletonMap("foo", bytes(45));
            responsePayload = respMap = Collections.singletonMap("bar", bytes(45));
            batchMessage.setCustomPayload(reqMap);
            Message.Response batchResponse = client.execute(batchMessage);
            payloadEquals(reqMap, requestPayload);
            payloadEquals(respMap, batchResponse.getCustomPayload());
        } finally {
            client.close();
        }
    } finally {
        cqlQueryHandlerField.set(null, queryHandler);
    }
}
Also used : QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) QueryHandler(org.apache.cassandra.cql3.QueryHandler) BatchMessage(org.apache.cassandra.transport.messages.BatchMessage) PrepareMessage(org.apache.cassandra.transport.messages.PrepareMessage) ExecuteMessage(org.apache.cassandra.transport.messages.ExecuteMessage) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ByteBuffer(java.nio.ByteBuffer) PrepareMessage(org.apache.cassandra.transport.messages.PrepareMessage) ExecuteMessage(org.apache.cassandra.transport.messages.ExecuteMessage) BatchMessage(org.apache.cassandra.transport.messages.BatchMessage) Test(org.junit.Test)

Example 2 with BatchMessage

use of org.apache.cassandra.transport.messages.BatchMessage in project cassandra by apache.

the class MessagePayloadTest method testMessagePayloadVersion3.

@Test
public void testMessagePayloadVersion3() throws Throwable {
    QueryHandler queryHandler = (QueryHandler) cqlQueryHandlerField.get(null);
    cqlQueryHandlerField.set(null, new TestQueryHandler());
    try {
        requireNetwork();
        Assert.assertSame(TestQueryHandler.class, ClientState.getCQLQueryHandler().getClass());
        SimpleClient client = new SimpleClient(nativeAddr.getHostAddress(), nativePort, ProtocolVersion.V3);
        try {
            client.connect(false);
            Map<String, ByteBuffer> reqMap;
            QueryMessage queryMessage = new QueryMessage("CREATE TABLE " + KEYSPACE + ".atable (pk int PRIMARY KEY, v text)", QueryOptions.DEFAULT);
            PrepareMessage prepareMessage = new PrepareMessage("SELECT * FROM " + KEYSPACE + ".atable");
            reqMap = Collections.singletonMap("foo", bytes(42));
            responsePayload = Collections.singletonMap("bar", bytes(42));
            queryMessage.setCustomPayload(reqMap);
            try {
                client.execute(queryMessage);
                Assert.fail();
            } catch (RuntimeException e) {
                Assert.assertTrue(e.getCause() instanceof ProtocolException);
            }
            queryMessage.setCustomPayload(null);
            client.execute(queryMessage);
            reqMap = Collections.singletonMap("foo", bytes(43));
            responsePayload = Collections.singletonMap("bar", bytes(43));
            prepareMessage.setCustomPayload(reqMap);
            try {
                client.execute(prepareMessage);
                Assert.fail();
            } catch (RuntimeException e) {
                Assert.assertTrue(e.getCause() instanceof ProtocolException);
            }
            prepareMessage.setCustomPayload(null);
            ResultMessage.Prepared prepareResponse = (ResultMessage.Prepared) client.execute(prepareMessage);
            ExecuteMessage executeMessage = new ExecuteMessage(prepareResponse.statementId, QueryOptions.DEFAULT);
            reqMap = Collections.singletonMap("foo", bytes(44));
            responsePayload = Collections.singletonMap("bar", bytes(44));
            executeMessage.setCustomPayload(reqMap);
            try {
                client.execute(executeMessage);
                Assert.fail();
            } catch (RuntimeException e) {
                Assert.assertTrue(e.getCause() instanceof ProtocolException);
            }
            BatchMessage batchMessage = new BatchMessage(BatchStatement.Type.UNLOGGED, Collections.<Object>singletonList("INSERT INTO " + KEYSPACE + ".atable (pk,v) VALUES (1, 'foo')"), Collections.singletonList(Collections.<ByteBuffer>emptyList()), QueryOptions.DEFAULT);
            reqMap = Collections.singletonMap("foo", bytes(45));
            responsePayload = Collections.singletonMap("bar", bytes(45));
            batchMessage.setCustomPayload(reqMap);
            try {
                client.execute(batchMessage);
                Assert.fail();
            } catch (RuntimeException e) {
                Assert.assertTrue(e.getCause() instanceof ProtocolException);
            }
        } finally {
            client.close();
        }
    } finally {
        cqlQueryHandlerField.set(null, queryHandler);
    }
}
Also used : QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) QueryHandler(org.apache.cassandra.cql3.QueryHandler) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ByteBuffer(java.nio.ByteBuffer) PrepareMessage(org.apache.cassandra.transport.messages.PrepareMessage) ExecuteMessage(org.apache.cassandra.transport.messages.ExecuteMessage) BatchMessage(org.apache.cassandra.transport.messages.BatchMessage) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 QueryHandler (org.apache.cassandra.cql3.QueryHandler)2 BatchMessage (org.apache.cassandra.transport.messages.BatchMessage)2 ExecuteMessage (org.apache.cassandra.transport.messages.ExecuteMessage)2 PrepareMessage (org.apache.cassandra.transport.messages.PrepareMessage)2 QueryMessage (org.apache.cassandra.transport.messages.QueryMessage)2 ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)2 Test (org.junit.Test)2