Search in sources :

Example 26 with RestEntity

use of org.infinispan.client.rest.RestEntity in project infinispan by infinispan.

the class BaseJsonTest method writeCurrencyViaJson.

private void writeCurrencyViaJson(String key, String description, int rank) {
    Json currency = Json.object();
    currency.set(TYPE, getJsonType());
    currency.set("description", description);
    currency.set("rank", rank);
    RestEntity value = RestEntity.create(MediaType.APPLICATION_JSON, currency.toString());
    RestResponse response = join(restCacheClient.put(key, value));
    assertEquals(response.getStatus(), 204);
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) Json(org.infinispan.commons.dataconversion.internal.Json)

Example 27 with RestEntity

use of org.infinispan.client.rest.RestEntity 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));
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) HashMap(java.util.HashMap) RestResponse(org.infinispan.client.rest.RestResponse) ProtoStreamMarshaller(org.infinispan.commons.marshall.ProtoStreamMarshaller) RestCacheClient(org.infinispan.client.rest.RestCacheClient) MediaType(org.infinispan.commons.dataconversion.MediaType) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Test(org.testng.annotations.Test)

Example 28 with RestEntity

use of org.infinispan.client.rest.RestEntity 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");
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DummyInMemoryStoreConfigurationBuilder(org.infinispan.persistence.dummy.DummyInMemoryStoreConfigurationBuilder) RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) RestCacheClient(org.infinispan.client.rest.RestCacheClient) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Test(org.testng.annotations.Test)

Example 29 with RestEntity

use of org.infinispan.client.rest.RestEntity in project infinispan by infinispan.

the class CacheResourceV2Test method testCacheV2Stats.

@Test
public void testCacheV2Stats() {
    String cacheJson = "{ \"distributed-cache\" : { \"statistics\":true } }";
    RestCacheClient cacheClient = client.cache("statCache");
    RestEntity jsonEntity = RestEntity.create(APPLICATION_JSON, cacheJson);
    CompletionStage<RestResponse> response = cacheClient.createWithConfiguration(jsonEntity, VOLATILE);
    assertThat(response).isOk();
    putStringValueInCache("statCache", "key1", "data");
    putStringValueInCache("statCache", "key2", "data");
    response = cacheClient.stats();
    assertThat(response).isOk();
    Json jsonNode = Json.read(join(response).getBody());
    assertEquals(jsonNode.at("current_number_of_entries").asInteger(), 2);
    assertEquals(jsonNode.at("stores").asInteger(), 2);
    response = cacheClient.clear();
    assertThat(response).isOk();
    response = cacheClient.stats();
    assertThat(response).isOk().hasJson().hasProperty("current_number_of_entries").is(0);
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) RestCacheClient(org.infinispan.client.rest.RestCacheClient) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Json(org.infinispan.commons.dataconversion.internal.Json) Test(org.testng.annotations.Test)

Example 30 with RestEntity

use of org.infinispan.client.rest.RestEntity in project infinispan by infinispan.

the class TasksResourceTest method testTaskUpload.

@Test
public void testTaskUpload() throws Exception {
    RestTaskClient taskClient = client.tasks();
    String script = getResourceAsString("hello.js", getClass().getClassLoader());
    RestEntity scriptEntity = RestEntity.create(APPLICATION_JAVASCRIPT, script);
    CompletionStage<RestResponse> response = taskClient.uploadScript("hello", scriptEntity);
    ResponseAssertion.assertThat(response).isOk();
    response = taskClient.exec("hello", Collections.singletonMap("greetee", "Friend"));
    ResponseAssertion.assertThat(response).isOk();
    Json jsonNode = Json.read(join(response).getBody());
    assertEquals("Hello Friend", jsonNode.asString());
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString) Json(org.infinispan.commons.dataconversion.internal.Json) RestTaskClient(org.infinispan.client.rest.RestTaskClient) Test(org.testng.annotations.Test)

Aggregations

RestEntity (org.infinispan.client.rest.RestEntity)38 RestResponse (org.infinispan.client.rest.RestResponse)35 RestCacheClient (org.infinispan.client.rest.RestCacheClient)14 Test (org.testng.annotations.Test)14 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)11 Json (org.infinispan.commons.dataconversion.internal.Json)5 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)5 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)4 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)4 DummyInMemoryStoreConfigurationBuilder (org.infinispan.persistence.dummy.DummyInMemoryStoreConfigurationBuilder)3 MediaType (org.infinispan.commons.dataconversion.MediaType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashMap (java.util.HashMap)1 Cache (org.infinispan.Cache)1 RemoteCache (org.infinispan.client.hotrod.RemoteCache)1 RemoteCacheManager (org.infinispan.client.hotrod.RemoteCacheManager)1 RestClient (org.infinispan.client.rest.RestClient)1