use of org.infinispan.client.rest.RestCacheClient in project infinispan by infinispan.
the class CacheResourceV2Test method testCacheAvailability.
@Test
public void testCacheAvailability() {
RestCacheClient cacheClient = adminClient.cache("denyReadWritesCache");
RestResponse restResponse = join(cacheClient.getAvailability());
ResponseAssertion.assertThat(restResponse).isOk().containsReturnedText("AVAILABLE");
restResponse = join(cacheClient.setAvailability("DEGRADED_MODE"));
ResponseAssertion.assertThat(restResponse).isOk();
restResponse = join(cacheClient.getAvailability());
ResponseAssertion.assertThat(restResponse).isOk().containsReturnedText("DEGRADED_MODE");
// Ensure that the endpoints can be utilised with internal caches
cacheClient = adminClient.cache(GlobalConfigurationManager.CONFIG_STATE_CACHE_NAME);
restResponse = join(cacheClient.getAvailability());
ResponseAssertion.assertThat(restResponse).isOk().containsReturnedText("AVAILABLE");
// No-op in core as the cache uses the PreferAvailabilityStategy
// Call to ensure that accessing internal cache doesn't throw an exception
restResponse = join(cacheClient.setAvailability("DEGRADED_MODE"));
ResponseAssertion.assertThat(restResponse).isOk();
// The availability will always be AVAILABLE
restResponse = join(cacheClient.getAvailability());
ResponseAssertion.assertThat(restResponse).isOk().containsReturnedText("AVAILABLE");
}
use of org.infinispan.client.rest.RestCacheClient in project infinispan by infinispan.
the class CacheResourceV2Test method testCRUDWithProtobufPrimitives.
@Test
public void testCRUDWithProtobufPrimitives() throws Exception {
RestCacheClient client = this.client.cache("proto");
MediaType integerType = MediaType.APPLICATION_OBJECT.withClassType(Integer.class);
// Insert a pair of Integers
RestEntity value = RestEntity.create(integerType, "1");
CompletionStage<RestResponse> response = client.put("1", integerType.toString(), value);
assertThat(response).isOk();
// Change the value to another Integer
RestEntity anotherValue = RestEntity.create(integerType, "2");
response = client.put("1", integerType.toString(), anotherValue);
assertThat(response).isOk();
// Read the changed value as an integer
Map<String, String> headers = new HashMap<>();
headers.put(KEY_CONTENT_TYPE_HEADER.getValue(), integerType.toString());
headers.put(ACCEPT_HEADER.getValue(), integerType.toString());
response = client.get("1", headers);
assertThat(response).isOk();
assertThat(response).hasReturnedText("2");
// Read the changed value as protobuf
headers = new HashMap<>();
headers.put(KEY_CONTENT_TYPE_HEADER.getValue(), integerType.toString());
headers.put(ACCEPT_HEADER.getValue(), MediaType.APPLICATION_PROTOSTREAM_TYPE);
response = client.get("1", headers);
assertThat(response).isOk();
assertThat(response).hasReturnedBytes(new ProtoStreamMarshaller().objectToByteBuffer(2));
}
use of org.infinispan.client.rest.RestCacheClient in project infinispan by infinispan.
the class CacheResourceV2Test method testLazySearchMapping.
@Test
public void testLazySearchMapping() {
String proto = " package future;\n" + " /* @Indexed */\n" + " message Entity {\n" + " /* @Field */\n" + " optional string name=1;\n" + " }";
String value = Json.object().set("_type", "future.Entity").set("name", "Kim").toString();
RestEntity restEntity = RestEntity.create(APPLICATION_JSON, value);
// Create a cache with a declared, not yet registered protobuf entity
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.indexing().enable().storage(LOCAL_HEAP).addIndexedEntities("future.Entity");
String cacheConfig = cacheConfigToJson("index-lazy", builder.build());
RestCacheClient cacheClient = client.cache("index-lazy");
RestEntity config = RestEntity.create(APPLICATION_JSON, cacheConfig);
CompletionStage<RestResponse> response = cacheClient.createWithConfiguration(config);
assertThat(response).isOk();
// Queries should return error
RestResponse restResponse = join(cacheClient.query("From future.Entity"));
assertThat(restResponse).containsReturnedText("Unknown type name : future.Entity");
// Writes too
restResponse = join(cacheClient.put("key", restEntity));
assertThat(restResponse).containsReturnedText("Unknown type name : future.Entity");
// Register the protobuf
restResponse = join(client.schemas().put("future.proto", proto));
assertThat(restResponse).isOk();
// All operations should work
restResponse = join(cacheClient.put("key", restEntity));
assertThat(restResponse).isOk();
restResponse = join(cacheClient.query("From future.Entity"));
assertThat(restResponse).isOk();
assertThat(restResponse).containsReturnedText("Kim");
}
use of org.infinispan.client.rest.RestCacheClient in project infinispan by infinispan.
the class CacheResourceV2Test method testStreamEntriesFromCache.
private void testStreamEntriesFromCache(String cacheName, MediaType cacheMediaType, MediaType writeMediaType, Map<String, String> data) {
// Create the cache with the supplied encoding
createCache(cacheName, cacheMediaType);
// Write entries with provided data and writeMediaType
data.forEach((key, value) -> writeEntry(key, value, cacheName, writeMediaType));
// Get entries
RestCacheClient cacheClient = client.cache(cacheName);
RestResponse response = join(cacheClient.entries(true));
Map<String, String> entries = entriesAsMap(response);
// Obtain the negotiated media type of the entries
String contentTypeHeader = response.getHeader(ResponseHeader.VALUE_CONTENT_TYPE_HEADER.getValue());
// Check entries are in the required format
assertEquals(data.size(), entries.size());
entries.forEach((key, value) -> assertEquals(value, data.get(key)));
// Change an entry using the content type returned from the previous call to getEntries
String aKey = data.keySet().iterator().next();
String aValue = data.get(aKey);
String changedValue = aValue.replace("value", "value-changed");
writeEntry(aKey, changedValue, cacheName, MediaType.fromString(contentTypeHeader));
// Check changed entry
entries = entriesAsMap(join(cacheClient.entries(true)));
assertEquals(changedValue, entries.get(aKey));
}
use of org.infinispan.client.rest.RestCacheClient in project infinispan by infinispan.
the class CacheResourceV2Test method testCreateAndAlterCache.
@Test
public void testCreateAndAlterCache() {
String cacheConfig = "{\n" + " \"distributed-cache\" : {\n" + " \"mode\" : \"SYNC\",\n" + " \"statistics\" : true,\n" + " \"encoding\" : {\n" + " \"key\" : {\n" + " \"media-type\" : \"application/x-protostream\"\n" + " },\n" + " \"value\" : {\n" + " \"media-type\" : \"application/x-protostream\"\n" + " }\n" + " },\n" + " \"expiration\" : {\n" + " \"lifespan\" : \"60000\"\n" + " },\n" + " \"memory\" : {\n" + " \"max-count\" : \"1000\",\n" + " \"when-full\" : \"REMOVE\"\n" + " }\n" + " }\n" + "}\n";
String cacheConfigAlter = "{\n" + " \"distributed-cache\" : {\n" + " \"mode\" : \"SYNC\",\n" + " \"statistics\" : true,\n" + " \"encoding\" : {\n" + " \"key\" : {\n" + " \"media-type\" : \"application/x-protostream\"\n" + " },\n" + " \"value\" : {\n" + " \"media-type\" : \"application/x-protostream\"\n" + " }\n" + " },\n" + " \"expiration\" : {\n" + " \"lifespan\" : \"30000\"\n" + " },\n" + " \"memory\" : {\n" + " \"max-count\" : \"2000\",\n" + " \"when-full\" : \"REMOVE\"\n" + " }\n" + " }\n" + "}\n";
String cacheConfigConflict = "{\n" + " \"distributed-cache\" : {\n" + " \"mode\" : \"ASYNC\"\n" + " }\n" + "}\n";
RestCacheClient cacheClient = client.cache("mutable");
CompletionStage<RestResponse> response = cacheClient.createWithConfiguration(RestEntity.create(APPLICATION_JSON, cacheConfig));
assertThat(response).isOk();
response = cacheClient.updateWithConfiguration(RestEntity.create(APPLICATION_JSON, cacheConfigAlter));
assertThat(response).isOk();
response = cacheClient.configuration();
assertThat(response).isOk();
String configFromServer = join(response).getBody();
assertTrue(configFromServer.contains("\"expiration\":{\"lifespan\":\"30000\"}"));
assertTrue(configFromServer.contains("\"memory\":{\"max-count\":\"2000\""));
response = cacheClient.updateWithConfiguration(RestEntity.create(APPLICATION_JSON, cacheConfigConflict));
assertThat(response).isBadRequest();
}
Aggregations