/
EndpointInteroperabilityTest.java
460 lines (374 loc) · 17.9 KB
/
EndpointInteroperabilityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package org.infinispan.it.compatibility;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.http.HttpHeaders.ACCEPT;
import static org.infinispan.client.hotrod.test.HotRodClientTestingUtil.killServers;
import static org.infinispan.client.hotrod.test.HotRodClientTestingUtil.startHotRodServer;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_JBOSS_MARSHALLING;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_JBOSS_MARSHALLING_TYPE;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OCTET_STREAM;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN;
import static org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN_TYPE;
import static org.infinispan.server.core.test.ServerTestingUtil.findFreePort;
import static org.infinispan.test.TestingUtil.killCacheManagers;
import static org.testng.Assert.assertEquals;
import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.infinispan.client.hotrod.DataFormat;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.commons.dataconversion.StandardConversions;
import org.infinispan.commons.marshall.IdentityMarshaller;
import org.infinispan.commons.marshall.Marshaller;
import org.infinispan.commons.marshall.UTF8StringMarshaller;
import org.infinispan.commons.marshall.jboss.GenericJBossMarshaller;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.rest.RestServer;
import org.infinispan.rest.configuration.RestServerConfigurationBuilder;
import org.infinispan.server.hotrod.HotRodServer;
import org.infinispan.test.AbstractInfinispanTest;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Tests for interoperability between REST and HotRod endpoints without using compatibility mode,
* but relying on MediaType configuration, HTTP Headers and Hot Rod client DataFormat support.
*
* @since 9.2
*/
@Test(groups = {"functional", "smoke"}, testName = "it.compatibility.EndpointInteroperabilityTest")
public class EndpointInteroperabilityTest extends AbstractInfinispanTest {
/**
* Cache with no MediaType configuration, assumes K and V are application/octet-stream.
*/
private static final String BYTES_CACHE_NAME = "bytesCache";
/**
* Cache that will hold marshalled entries, configured to use application/x-jboss-marshalling for K and V.
*/
private static final String MARSHALLED_CACHE_NAME = "marshalledCache";
/**
* Cache configured for text/plain for both K and V.
*/
private static final String STRING_CACHE_NAME = "stringsCaches";
private RestServer restServer;
private HotRodServer hotRodServer;
private HttpClient restClient;
private EmbeddedCacheManager cacheManager;
private RemoteCache<byte[], byte[]> bytesRemoteCache;
private RemoteCache<Object, Object> defaultMarshalledRemoteCache;
private RemoteCache<String, String> stringRemoteCache;
@BeforeClass
protected void setup() throws Exception {
cacheManager = TestCacheManagerFactory.createServerModeCacheManager();
cacheManager.defineConfiguration(BYTES_CACHE_NAME, getBytesCacheConfiguration().build());
cacheManager.defineConfiguration(MARSHALLED_CACHE_NAME, getMarshalledCacheConfiguration().build());
cacheManager.defineConfiguration(STRING_CACHE_NAME, getStringsCacheConfiguration().build());
RestServerConfigurationBuilder builder = new RestServerConfigurationBuilder();
builder.port(findFreePort());
restServer = new RestServer();
restServer.start(builder.build(), cacheManager);
restClient = new HttpClient();
hotRodServer = startHotRodServer(cacheManager);
bytesRemoteCache = createRemoteCacheManager(IdentityMarshaller.INSTANCE).getCache(BYTES_CACHE_NAME);
defaultMarshalledRemoteCache = createRemoteCacheManager(null).getCache(MARSHALLED_CACHE_NAME);
stringRemoteCache = createRemoteCacheManager(new UTF8StringMarshaller()).getCache(STRING_CACHE_NAME);
}
private ConfigurationBuilder getBytesCacheConfiguration() {
return new ConfigurationBuilder();
}
private ConfigurationBuilder getMarshalledCacheConfiguration() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.encoding().key().mediaType(APPLICATION_JBOSS_MARSHALLING_TYPE);
builder.encoding().value().mediaType(APPLICATION_JBOSS_MARSHALLING_TYPE);
return builder;
}
private ConfigurationBuilder getStringsCacheConfiguration() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.encoding().key().mediaType(TEXT_PLAIN_TYPE);
builder.encoding().value().mediaType(TEXT_PLAIN_TYPE);
return builder;
}
private RemoteCacheManager createRemoteCacheManager(Marshaller marshaller) {
org.infinispan.client.hotrod.configuration.ConfigurationBuilder builder =
new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
builder.addServer().host("localhost").port(hotRodServer.getPort());
if (marshaller != null) {
builder.marshaller(marshaller);
}
return new RemoteCacheManager(builder.build());
}
@Test
public void testStringKeysAndByteArrayValue() throws Exception {
// The Hot Rod client writes marshalled content. The cache is explicitly configured with
// 'application/x-jboss-marshalling' for both K and V.
String key = "string-1";
byte[] value = {1, 2, 3};
byte[] marshalledValue = new GenericJBossMarshaller().objectToByteBuffer(value);
defaultMarshalledRemoteCache.put(key, value);
assertEquals(defaultMarshalledRemoteCache.get(key), value);
// Read via Rest the raw content, as it is stored
Object rawFromRest = new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key(key).accept(APPLICATION_JBOSS_MARSHALLING)
.read();
assertArrayEquals((byte[]) rawFromRest, marshalledValue);
// Write via rest raw bytes
String otherKey = "string-2";
byte[] otherValue = {0x4, 0x5, 0x6};
byte[] otherValueMarshalled = new GenericJBossMarshaller().objectToByteBuffer(otherValue);
new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key(otherKey).value(otherValue, APPLICATION_OCTET_STREAM)
.write();
// Read via Hot Rod
assertEquals(defaultMarshalledRemoteCache.get(otherKey), otherValue);
// Read via Hot Rod using a String key, and getting the raw value (as it is stored) back
DataFormat format = DataFormat.builder()
.keyType(TEXT_PLAIN)
.valueType(APPLICATION_JBOSS_MARSHALLING).valueMarshaller(IdentityMarshaller.INSTANCE)
.build();
byte[] rawValue = (byte[]) defaultMarshalledRemoteCache.withDataFormat(format).get(otherKey);
assertArrayEquals(otherValueMarshalled, rawValue);
// Read via Hot Rod using a String key, and getting the original value back
DataFormat.builder().keyType(TEXT_PLAIN).build();
byte[] result = (byte[]) defaultMarshalledRemoteCache
.withDataFormat(DataFormat.builder().keyType(TEXT_PLAIN).build()).get(otherKey);
assertArrayEquals(otherValue, result);
}
@Test
public void testIntegerKeysAndByteArrayValue() throws Exception {
String integerKeyType = "application/x-java-object; type=java.lang.Integer";
byte[] value = {12};
byte[] otherValue = "random".getBytes("UTF-8");
// Write <Integer, byte[]> via Hot Rod (the HR client is configured with the default marshaller)
defaultMarshalledRemoteCache.put(10, value);
assertEquals(defaultMarshalledRemoteCache.get(10), value);
// Read via Rest
Object bytesFromRest = new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key("10", integerKeyType).accept(APPLICATION_OCTET_STREAM)
.read();
assertArrayEquals((byte[]) bytesFromRest, value);
// Write via rest
new RestRequest().cache(MARSHALLED_CACHE_NAME).key("20", integerKeyType).value(otherValue).write();
// Read via Hot Rod
assertEquals(defaultMarshalledRemoteCache.get(20), otherValue);
}
@Test
public void testFloatKeysDoubleValues() throws Exception {
String floatContentType = "application/x-java-object; type=java.lang.Float";
String doubleContentType = "application/x-java-object; type=java.lang.Double";
GenericJBossMarshaller marshaller = new GenericJBossMarshaller();
Object key = 1.1f;
Object value = 32.4d;
byte[] valueMarshalled = marshaller.objectToByteBuffer(value);
defaultMarshalledRemoteCache.put(key, value);
assertEquals(defaultMarshalledRemoteCache.get(key), value);
// Read via Rest the raw byte[] as marshalled by the client
Object bytesFromRest = new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key(key.toString(), floatContentType)
.accept(APPLICATION_JBOSS_MARSHALLING)
.read();
assertArrayEquals((byte[]) bytesFromRest, valueMarshalled);
// Read via Rest the value as String
Object stringFromRest = new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key(key.toString(), floatContentType)
.accept(TEXT_PLAIN)
.read();
assertArrayEquals("32.4".getBytes(), (byte[]) stringFromRest);
// Write via rest
Object otherKey = 2.2f;
Object otherValue = 123.0d;
new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key(otherKey.toString(), floatContentType)
.value(otherValue.toString(), doubleContentType)
.write();
// Read via Hot Rod
assertEquals(defaultMarshalledRemoteCache.get(otherKey), otherValue);
}
@Test
public void testStringKeysAndStringValues() throws Exception {
// Write via Hot Rod (the HR client is configured with a String marshaller)
stringRemoteCache.put("key", "Hello World");
assertEquals(stringRemoteCache.get("key"), "Hello World");
// Read via Rest
Object bytesFromRest = new RestRequest().cache(STRING_CACHE_NAME).key("key").accept(TEXT_PLAIN).read();
assertEquals(asString(bytesFromRest), "Hello World");
// Write via rest
new RestRequest().cache(STRING_CACHE_NAME).key("key2").value("Testing").write();
// Read via Hot Rod
assertEquals(stringRemoteCache.get("key2"), "Testing");
// Get values as JSON from Hot Rod
Object jsonString = stringRemoteCache.withDataFormat(DataFormat.builder()
.valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build())
.get("key");
assertEquals("\"Hello World\"", jsonString);
}
@Test
public void testByteArrayKeysAndValuesWithMarshaller() throws Exception {
// Write via Hot Rod using the default marshaller
byte[] key = new byte[]{0x74, 0x18};
byte[] value = new byte[]{0x10, 0x20};
defaultMarshalledRemoteCache.put(key, value);
assertArrayEquals((byte[]) defaultMarshalledRemoteCache.get(key), value);
// Read via Rest
Object bytesFromRest = new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key("0x7418", APPLICATION_OCTET_STREAM_TYPE)
.accept(APPLICATION_OCTET_STREAM)
.read();
assertEquals(bytesFromRest, value);
// Read marshalled content directly
Object marshalledContent = new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key("0x7418", APPLICATION_OCTET_STREAM_TYPE)
.accept(APPLICATION_JBOSS_MARSHALLING)
.read();
assertArrayEquals((byte[]) marshalledContent, new GenericJBossMarshaller().objectToByteBuffer(value));
// Write via rest
byte[] newKey = new byte[]{0x23};
// Write via rest
new RestRequest().cache(MARSHALLED_CACHE_NAME)
.key("0x23", APPLICATION_OCTET_STREAM_TYPE)
.value(value)
.write();
// Read via Hot Rod
assertEquals(defaultMarshalledRemoteCache.get(newKey), value);
}
@Test
public void testByteArrayKeysAndByteArrayValues() throws Exception {
// Write via Hot Rod the byte[] content directly
byte[] key = new byte[]{0x13, 0x26};
byte[] value = new byte[]{10, 20};
bytesRemoteCache.put(key, value);
assertArrayEquals(bytesRemoteCache.get(key), value);
// Read via Rest
Object bytesFromRest = new RestRequest().cache(BYTES_CACHE_NAME)
.key("0x1326", APPLICATION_OCTET_STREAM.withParameter("encoding", "hex").toString())
.accept(APPLICATION_OCTET_STREAM)
.read();
assertEquals(bytesFromRest, value);
// Write via rest
byte[] newKey = new byte[]{0, 0, 0, 1};
// Write via rest
new RestRequest().cache(BYTES_CACHE_NAME)
.key("0x00000001", MediaType.APPLICATION_OCTET_STREAM.withParameter("encoding", "hex").toString())
.value(value)
.write();
// Read via Hot Rod
assertEquals(bytesRemoteCache.get(newKey), value);
}
@Test
public void testCustomKeysAndByteValues() throws Exception {
String customKeyType = "application/x-java-object; type=ByteArray";
CustomKey objectKey = new CustomKey("a", 1.0d, 1.0f, true);
byte[] key = new GenericJBossMarshaller().objectToByteBuffer(objectKey);
byte[] value = {12};
// Write <byte[], byte[]> via Hot Rod (the HR client is configured with the default marshaller)
bytesRemoteCache.put(key, value);
assertEquals(value, bytesRemoteCache.get(key));
String restKey = StandardConversions.bytesToHex(key);
// Read via Rest
Object bytesFromRest = new RestRequest().cache(BYTES_CACHE_NAME)
.key(restKey, customKeyType).accept(APPLICATION_OCTET_STREAM)
.read();
assertArrayEquals((byte[]) bytesFromRest, value);
}
private String getEndpoint(String cache) {
return String.format("http://localhost:%s/rest/%s", restServer.getPort(), cache);
}
private String asString(Object content) throws Exception {
if (content instanceof byte[]) {
return new String((byte[]) content, "UTF-8");
}
return content.toString();
}
private class RestRequest {
private String cacheName;
private Object key;
private Object value;
private String keyContentType;
private MediaType accept;
private String contentType;
public RestRequest cache(String cacheName) {
this.cacheName = cacheName;
return this;
}
public RestRequest key(Object key) {
this.key = key;
return this;
}
public RestRequest key(Object key, String keyContentType) {
this.key = key;
this.keyContentType = keyContentType;
return this;
}
public RestRequest value(Object value, String contentType) {
this.value = value;
this.contentType = contentType;
return this;
}
public RestRequest value(Object value, MediaType contentType) {
this.value = value;
this.contentType = contentType.toString();
return this;
}
public RestRequest value(Object value) {
this.value = value;
return this;
}
RestRequest accept(MediaType valueContentType) {
this.accept = valueContentType;
return this;
}
void write() throws Exception {
EntityEnclosingMethod post = new PostMethod(getEndpoint(this.cacheName) + "/" + this.key);
if (this.keyContentType != null) {
post.addRequestHeader("Key-Content-Type", this.keyContentType);
}
if (this.value instanceof byte[]) {
String contentType = this.contentType == null ? APPLICATION_OCTET_STREAM_TYPE : this.contentType;
post.setRequestEntity(new ByteArrayRequestEntity((byte[]) this.value, contentType));
} else {
String payload = this.value.toString();
String contentType = this.contentType == null ? TEXT_PLAIN_TYPE : this.contentType;
post.setRequestEntity(new StringRequestEntity(payload, contentType, UTF_8.toString()));
}
restClient.executeMethod(post);
assertEquals(post.getStatusCode(), HttpStatus.SC_OK);
}
Object read() throws IOException {
HttpMethod get = new GetMethod(getEndpoint(this.cacheName) + "/" + this.key);
if (this.accept != null) {
get.setRequestHeader(ACCEPT, this.accept.toString());
}
if (keyContentType != null) {
get.setRequestHeader("Key-Content-Type", this.keyContentType);
}
restClient.executeMethod(get);
assertEquals(get.getStatusCode(), HttpStatus.SC_OK);
return get.getResponseBody();
}
}
@AfterClass
protected void teardown() {
bytesRemoteCache.getRemoteCacheManager().stop();
defaultMarshalledRemoteCache.getRemoteCacheManager().stop();
stringRemoteCache.getRemoteCacheManager().stop();
if (restServer != null) {
try {
restServer.stop();
} catch (Exception ignored) {
}
}
killCacheManagers(cacheManager);
killServers(hotRodServer);
}
}