Skip to content

Commit

Permalink
ISPN-14100 REST keys operation cache value should match all
Browse files Browse the repository at this point in the history
  • Loading branch information
jabolina authored and tristantarrant committed Sep 27, 2022
1 parent 52a8897 commit 7debb3b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Expand Up @@ -43,7 +43,9 @@ public int available() {
}

private byte[] escape(byte[] content) {
return ("\"" + new String(content, UTF_8) + "\"").getBytes(UTF_8);
String stringified = new String(content, UTF_8);
String escaped = stringified.replaceAll("\"", "\\\\\"");
return ("\"" + escaped + "\"").getBytes(UTF_8);
}

@Override
Expand Down
Expand Up @@ -12,6 +12,7 @@
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OCTET_STREAM;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_XML;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_YAML;
import static org.infinispan.commons.dataconversion.MediaType.MATCH_ALL;
import static org.infinispan.commons.dataconversion.MediaType.TEXT_EVENT_STREAM;
import static org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN;
import static org.infinispan.rest.framework.Method.DELETE;
Expand Down Expand Up @@ -361,7 +362,7 @@ private CompletionStage<RestResponse> streamKeys(RestRequest request) {
int batch = batchParam == null || batchParam.isEmpty() ? STREAM_BATCH_SIZE : Integer.parseInt(batchParam);
int limit = limitParam == null || limitParam.isEmpty() ? -1 : Integer.parseInt(limitParam);

Cache<?, ?> cache = invocationHelper.getRestCacheManager().getCache(cacheName, TEXT_PLAIN, TEXT_PLAIN, request);
Cache<?, ?> cache = invocationHelper.getRestCacheManager().getCache(cacheName, TEXT_PLAIN, MATCH_ALL, request);
if (cache == null)
return notFoundResponseFuture();

Expand Down
Expand Up @@ -668,6 +668,53 @@ public void testGetAllKeys() {
assertEquals(5, keysLimited.size());
}

@Test
public void testGetAllKeysWithDifferentType() {
String cacheJson = "{ \"distributed-cache\": { \"mode\": \"SYNC\","
+ " \"encoding\": {"
+ " \"key\": {\"media-type\": \"application/json\"},"
+ " \"value\": {\"media-type\": \"application/xml\"}}}}";
String value = "<?xml version=\"1.0\"?>\n"
+ "<log category=\"CLUSTER\">\n"
+ " <content level=\"INFO\" message=\"hello\" detail=\"testing\"/>\n"
+ " <meta instant=\"42\" context=\"testing\" scope=\"\" who=\"\"/>\n"
+ "</log>\n";
String cacheName = "xmlCaching";
RestCacheClient cacheClient = client.cache(cacheName);

RestEntity jsonEntity = RestEntity.create(APPLICATION_JSON, cacheJson);
CompletionStage<RestResponse> r = cacheClient.createWithConfiguration(jsonEntity, VOLATILE);
assertThat(r).isOk();

RestResponse response = join(client.cache(cacheName).keys());
Collection<?> emptyKeys = Json.read(response.getBody()).asJsonList();
assertEquals(0, emptyKeys.size());

// Test key with escape.
putInCache(cacheName, "{\"text\": \"I'm right \\\\\"here\\\\\".\"}", APPLICATION_JSON_TYPE, value, APPLICATION_XML);
response = join(client.cache(cacheName).keys());
Collection<?> singleSet = Json.read(response.getBody()).asJsonList();
assertEquals(1, singleSet.size());
join(client.cache(cacheName).clear());

int entries = 10;
for (int i = 0; i < entries; i++) {
putInCache(cacheName, String.format("{\"v\": %d}", i), APPLICATION_JSON_TYPE, value, APPLICATION_XML);
}
response = join(client.cache(cacheName).keys());
List<Json> keys = Json.read(response.getBody()).asJsonList();
assertEquals(entries, keys.size());

response = join(client.cache(cacheName).keys(5));
List<?> keysLimited = Json.read(response.getBody()).asJsonList();
assertEquals(5, keysLimited.size());
}

private void putInCache(String cacheName, String key, String keyType, String value, MediaType valueType) {
CompletionStage<RestResponse> r = client.cache(cacheName).put(key, keyType, RestEntity.create(valueType, value));
ResponseAssertion.assertThat(r).isOk();
}

@Test
public void testStreamEntries() {
RestResponse response = join(client.cache("default").entries());
Expand Down

0 comments on commit 7debb3b

Please sign in to comment.