Search in sources :

Example 1 with RestCacheClient

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

the class Put method exec.

@Override
protected CompletionStage<RestResponse> exec(ContextAwareCommandInvocation invocation, RestClient client, org.infinispan.cli.resources.Resource resource) {
    if ((file != null) && (args.size() != 1)) {
        throw Messages.MSG.illegalCommandArguments();
    } else if ((file == null) && (args.size() != 2)) {
        throw Messages.MSG.illegalCommandArguments();
    }
    RestCacheClient cacheClient = client.cache(cache != null ? cache : CacheResource.cacheName(resource));
    MediaType putEncoding = encoding != null ? MediaType.fromString(encoding) : invocation.getContext().getEncoding();
    RestEntity value = file != null ? RestEntity.create(putEncoding, new File(file.getAbsolutePath())) : RestEntity.create(putEncoding, args.get(1));
    if (ifAbsent) {
        return cacheClient.post(args.get(0), value, ttl, maxIdle);
    } else {
        return cacheClient.put(args.get(0), value, ttl, maxIdle);
    }
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) RestCacheClient(org.infinispan.client.rest.RestCacheClient) MediaType(org.infinispan.commons.dataconversion.MediaType) File(java.io.File)

Example 2 with RestCacheClient

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

the class EmbeddedRestHotRodTest method testCustomObjectRestPutHotRodEmbeddedGet.

public void testCustomObjectRestPutHotRodEmbeddedGet() throws Exception {
    final String key = "77";
    Person p = new Person("Iker");
    // 1. Put with Rest
    RestCacheClient restClient = cacheFactory.getRestCacheClient();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(bout)) {
        oos.writeObject(p);
    }
    RestEntity value = RestEntity.create(APPLICATION_SERIALIZED_OBJECT, new ByteArrayInputStream(bout.toByteArray()));
    join(restClient.put(key, value));
    // 2. Get with Hot Rod
    RemoteCache<String, Object> remote = cacheFactory.getHotRodCache();
    assertEquals(p, remote.get(key));
    // 3. Get with Embedded
    assertEquals(p, cacheFactory.getEmbeddedCache().getAdvancedCache().get(key));
}
Also used : RestEntity(org.infinispan.client.rest.RestEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) RestCacheClient(org.infinispan.client.rest.RestCacheClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 3 with RestCacheClient

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

the class EmbeddedRestHotRodTest method testRestPutEmbeddedHotRodGet.

public void testRestPutEmbeddedHotRodGet() {
    final String key = "1";
    // 1. Put with REST
    RestCacheClient restCacheClient = cacheFactory.getRestCacheClient();
    CompletionStage<RestResponse> response = restCacheClient.put(key, RestEntity.create(TEXT_PLAIN, "<hey>ho</hey>"));
    assertEquals(204, join(response).getStatus());
    // 2. Get with Embedded
    assertEquals("<hey>ho</hey>", cacheFactory.getEmbeddedCache().get(key));
    // 3. Get with Hot Rod
    assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key));
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestCacheClient(org.infinispan.client.rest.RestCacheClient)

Example 4 with RestCacheClient

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

the class IndexedCacheNonIndexedEntityTest method shouldPreventNonIndexedEntities.

@Test
public void shouldPreventNonIndexedEntities() {
    CompletionStage<RestResponse> response = client.schemas().post("customer", SCHEMA);
    ResponseAssertion.assertThat(response).isOk();
    ConfigurationBuilder configurationBuilder = getDefaultStandaloneCacheConfig(false);
    configurationBuilder.statistics().enable();
    configurationBuilder.encoding().mediaType(APPLICATION_PROTOSTREAM_TYPE).indexing().enable().storage(LOCAL_HEAP).addIndexedEntity("NonIndexed");
    String config = cacheConfigToJson(CACHE_NAME, configurationBuilder.build());
    RestEntity configEntity = RestEntity.create(MediaType.APPLICATION_JSON, config);
    RestCacheClient cacheClient = client.cache(CACHE_NAME);
    response = cacheClient.createWithConfiguration(configEntity, VOLATILE);
    // The SearchMapping is started lazily, creating and starting the cache will not throw errors
    ResponseAssertion.assertThat(response).isOk();
    // Force initialization of the SearchMapping
    response = cacheClient.query("FROM NonIndexed");
    ResponseAssertion.assertThat(response).isBadRequest();
    String errorText = "The configured indexed-entity type 'NonIndexed' must be indexed. Please annotate it with @Indexed";
    ResponseAssertion.assertThat(response).containsReturnedText(errorText);
    // Call Indexer operations
    response = cacheClient.clearIndex();
    ResponseAssertion.assertThat(response).containsReturnedText(errorText);
    // The Indexer should not have "running" status
    Indexer indexer = Search.getIndexer(cacheManager.getCache(CACHE_NAME));
    assertFalse(indexer.isRunning());
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) Indexer(org.infinispan.query.Indexer) RestEntity(org.infinispan.client.rest.RestEntity) RestResponse(org.infinispan.client.rest.RestResponse) RestCacheClient(org.infinispan.client.rest.RestCacheClient) SingleCacheManagerTest(org.infinispan.test.SingleCacheManagerTest) Test(org.testng.annotations.Test)

Example 5 with RestCacheClient

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

the class RestMetricsResource method testTimerMetrics.

@Test
public void testTimerMetrics() throws Exception {
    RestClient client = SERVER_TEST.rest().create();
    RestMetricsClient metricsClient = client.metrics();
    // this is a histogram of write times
    String metricName = "cache_manager_default_cache_" + SERVER_TEST.getMethodName() + "_statistics_store_times";
    int NUM_PUTS = 10;
    try (RestResponse response = sync(metricsClient.metrics())) {
        assertEquals(200, response.getStatus());
        checkIsPrometheus(response.contentType());
        String body = response.getBody();
        assertThat(body).contains("base", "vendor", metricName);
        checkRule(body, "vendor_" + metricName, (stringValue) -> {
            double parsed = Double.parseDouble(stringValue);
            assertThat(parsed).isZero();
        });
    }
    // put some entries then check that the stats were updated
    RestCacheClient cache = client.cache(SERVER_TEST.getMethodName());
    for (int i = 0; i < NUM_PUTS; i++) {
        RestResponse putResp = sync(cache.put("k" + i, "v" + i));
        assertEquals(204, putResp.getStatus());
    }
    try (RestResponse response = sync(metricsClient.metrics())) {
        assertEquals(200, response.getStatus());
        checkIsPrometheus(response.contentType());
        String body = response.getBody();
        assertThat(body).contains("base", "vendor", metricName);
        checkRule(body, "vendor_" + metricName, (stringValue) -> {
            double parsed = Double.parseDouble(stringValue);
            assertThat(parsed).isPositive();
        });
    }
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) RestCacheClient(org.infinispan.client.rest.RestCacheClient) RestMetricsClient(org.infinispan.client.rest.RestMetricsClient) Test(org.junit.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