Search in sources :

Example 36 with RestCacheClient

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");
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestCacheClient(org.infinispan.client.rest.RestCacheClient) Test(org.testng.annotations.Test)

Example 37 with RestCacheClient

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));
}
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 38 with RestCacheClient

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");
}
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 39 with RestCacheClient

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));
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestCacheClient(org.infinispan.client.rest.RestCacheClient) Util.getResourceAsString(org.infinispan.commons.util.Util.getResourceAsString)

Example 40 with RestCacheClient

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();
}
Also used : 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)

Aggregations

RestCacheClient (org.infinispan.client.rest.RestCacheClient)66 RestResponse (org.infinispan.client.rest.RestResponse)41 Test (org.testng.annotations.Test)34 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)16 RestEntity (org.infinispan.client.rest.RestEntity)14 Json (org.infinispan.commons.dataconversion.internal.Json)13 RestClient (org.infinispan.client.rest.RestClient)11 Test (org.junit.Test)8 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)7 AbstractMultipleSitesTest (org.infinispan.xsite.AbstractMultipleSitesTest)7 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)6 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)5 TestClass (org.infinispan.rest.TestClass)5 TestUser (org.infinispan.server.test.api.TestUser)5 DummyInMemoryStoreConfigurationBuilder (org.infinispan.persistence.dummy.DummyInMemoryStoreConfigurationBuilder)3 HashMap (java.util.HashMap)2 RestCacheManagerClient (org.infinispan.client.rest.RestCacheManagerClient)2 RestMetricsClient (org.infinispan.client.rest.RestMetricsClient)2 MediaType (org.infinispan.commons.dataconversion.MediaType)2 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)2