Skip to content

Commit 0f3ae37

Browse files
committed
[java-driver#755] Handling empty resultMetadataId in Execute message.
1 parent 6a8678f commit 0f3ae37

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

driver-core/src/main/java/com/datastax/driver/core/Requests.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public String toString() {
224224

225225
static class Execute extends Message.Request {
226226

227+
public static final byte[] EMPTY_BYTES = new byte[0];
228+
227229
static final Message.Coder<Execute> coder =
228230
new Message.Coder<Execute>() {
229231
@Override
@@ -233,17 +235,23 @@ public void encode(
233235
ProtocolVersion version,
234236
ProtocolFeatureStore featureStore) {
235237
CBUtil.writeShortBytes(msg.statementId.bytes, dest);
236-
if (ProtocolFeatures.PREPARED_METADATA_CHANGES.isSupportedBy(version, featureStore))
237-
CBUtil.writeShortBytes(msg.resultMetadataId.bytes, dest);
238+
if (ProtocolFeatures.PREPARED_METADATA_CHANGES.isSupportedBy(version, featureStore)) {
239+
byte[] bytes =
240+
msg.resultMetadataId != null ? msg.resultMetadataId.bytes : EMPTY_BYTES;
241+
CBUtil.writeShortBytes(bytes, dest);
242+
}
238243
msg.options.encode(dest, version);
239244
}
240245

241246
@Override
242247
public int encodedSize(
243248
Execute msg, ProtocolVersion version, ProtocolFeatureStore featureStore) {
244249
int size = CBUtil.sizeOfShortBytes(msg.statementId.bytes);
245-
if (ProtocolFeatures.PREPARED_METADATA_CHANGES.isSupportedBy(version, featureStore))
246-
size += CBUtil.sizeOfShortBytes(msg.resultMetadataId.bytes);
250+
if (ProtocolFeatures.PREPARED_METADATA_CHANGES.isSupportedBy(version, featureStore)) {
251+
byte[] bytes =
252+
msg.resultMetadataId != null ? msg.resultMetadataId.bytes : EMPTY_BYTES;
253+
size += CBUtil.sizeOfShortBytes(bytes);
254+
}
247255
size += msg.options.encodedSize(version);
248256
return size;
249257
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.datastax.driver.core;
2+
3+
import static org.testng.Assert.assertEquals;
4+
5+
import com.datastax.driver.core.utils.Bytes;
6+
import io.netty.buffer.ByteBuf;
7+
import io.netty.buffer.ByteBufAllocator;
8+
import org.testng.annotations.DataProvider;
9+
import org.testng.annotations.Test;
10+
11+
public class RequestsTest {
12+
13+
public static class ExecuteTest {
14+
15+
@Test(groups = "unit", dataProvider = "shouldProperlyEncodeMessages")
16+
void should_properly_encode_messages(
17+
MD5Digest resultMetadataId,
18+
ProtocolVersion protocolVersion,
19+
ProtocolFeatureStore protocolFeatureStore,
20+
int expectedSize) {
21+
// given
22+
ByteBuf destination = ByteBufAllocator.DEFAULT.buffer();
23+
Requests.Execute request =
24+
new Requests.Execute(
25+
MD5Digest.wrap(Bytes.fromHexString("0xcafebabe").array()),
26+
resultMetadataId,
27+
Requests.QueryProtocolOptions.DEFAULT,
28+
false);
29+
30+
// when
31+
int size = Requests.Execute.coder.encodedSize(request, protocolVersion, protocolFeatureStore);
32+
Requests.Execute.coder.encode(request, destination, protocolVersion, protocolFeatureStore);
33+
34+
// then
35+
assertEquals(size, expectedSize);
36+
assertEquals(destination.writerIndex(), expectedSize);
37+
}
38+
39+
@DataProvider
40+
Object[][] shouldProperlyEncodeMessages() {
41+
return new Object[][] {
42+
{
43+
MD5Digest.wrap(Bytes.fromHexString("0xdeadbeef").array()),
44+
ProtocolVersion.V4,
45+
new ProtocolFeatureStore(null, null, null, false),
46+
9
47+
},
48+
{
49+
MD5Digest.wrap(Bytes.fromHexString("0xdeadbeef").array()),
50+
ProtocolVersion.V4,
51+
new ProtocolFeatureStore(null, null, null, true),
52+
15
53+
},
54+
{null, ProtocolVersion.V4, new ProtocolFeatureStore(null, null, null, true), 11},
55+
{
56+
MD5Digest.wrap(Bytes.fromHexString("0xdeadbeef").array()),
57+
ProtocolVersion.V5,
58+
new ProtocolFeatureStore(null, null, null, false),
59+
18
60+
},
61+
{null, ProtocolVersion.V5, new ProtocolFeatureStore(null, null, null, false), 14},
62+
};
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)