Search in sources :

Example 31 with RestResponse

use of org.infinispan.client.rest.RestResponse 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)

Example 32 with RestResponse

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

the class RestMetricsResource method testBaseAndVendorMetrics.

@Test
public void testBaseAndVendorMetrics() throws Exception {
    RestMetricsClient metricsClient = SERVER_TEST.rest().create().metrics();
    try (RestResponse response = sync(metricsClient.metrics())) {
        assertEquals(200, response.getStatus());
        // that is the default
        checkIsPrometheus(response.contentType());
        String body = response.getBody();
        checkRule(body, "base_classloader_loadedClasses_count", (stringValue) -> {
            double parsed = Double.parseDouble(stringValue);
            assertThat(parsed).isPositive();
        });
        checkRule(body, "vendor_memoryPool_Metaspace_usage_bytes", (stringValue) -> {
            double parsed = Double.parseDouble(stringValue);
            assertThat(parsed).isPositive();
        });
    }
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestMetricsClient(org.infinispan.client.rest.RestMetricsClient) Test(org.junit.Test)

Example 33 with RestResponse

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

the class RestOperations method testCounter.

@Test
public void testCounter() {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    builder.protocol(protocol);
    RestClient client = SERVER_TEST.rest().withClientConfiguration(builder).create();
    CounterConfiguration configuration = CounterConfiguration.builder(CounterType.WEAK).initialValue(5).concurrencyLevel(1).build();
    AbstractCounterConfiguration config = ConvertUtil.configToParsedConfig("test-counter", configuration);
    String configJson = AbstractRestResourceTest.counterConfigToJson(config);
    RestCounterClient counter = client.counter("test");
    RestResponse rsp = sync(counter.create(RestEntity.create(MediaType.APPLICATION_JSON, configJson)));
    assertEquals(HttpResponseStatus.OK.code(), rsp.getStatus());
    rsp = sync(counter.get());
    assertEquals("5", rsp.getBody());
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) AbstractRestResourceTest(org.infinispan.rest.resources.AbstractRestResourceTest) Test(org.junit.Test)

Example 34 with RestResponse

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

the class KeyCloakServerRule method getAccessTokenForCredentials.

public String getAccessTokenForCredentials(String realm, String client, String secret, String username, String password, Path trustStore, String trustStorePassword) {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    int port;
    if (trustStore != null) {
        builder.security().ssl().trustStoreFileName(trustStore.toString()).trustStorePassword(trustStorePassword.toCharArray()).hostnameVerifier((hostname, session) -> true);
        port = 8443;
    } else {
        port = 8080;
    }
    builder.addServer().host(container.getContainerIpAddress()).port(container.getMappedPort(port)).connectionTimeout(5000).socketTimeout(5000);
    try (RestClient c = RestClient.forConfiguration(builder.build())) {
        String url = String.format("/auth/realms/%s/protocol/openid-connect/token", realm);
        Map<String, List<String>> form = new HashMap<>();
        form.put("client_id", Collections.singletonList(client));
        form.put("client_secret", Collections.singletonList(secret));
        form.put("username", Collections.singletonList(username));
        form.put("password", Collections.singletonList(password));
        form.put("grant_type", Collections.singletonList("password"));
        RestResponse response = c.raw().postForm(url, Collections.singletonMap("Content-Type", "application/x-www-form-urlencoded"), form).toCompletableFuture().get(5, TimeUnit.SECONDS);
        Map<String, Json> map = Json.read(response.getBody()).asJsonMap();
        return map.get("access_token").asString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) HashMap(java.util.HashMap) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) ArrayList(java.util.ArrayList) List(java.util.List) Json(org.infinispan.commons.dataconversion.internal.Json) IOException(java.io.IOException)

Example 35 with RestResponse

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

the class Common method awaitStatus.

public static RestResponse awaitStatus(Supplier<CompletionStage<RestResponse>> request, int pendingStatus, int completeStatus) {
    int count = 0;
    RestResponse response;
    while ((response = await(request.get())).getStatus() == pendingStatus || count++ < 100) {
        response.close();
        TestingUtil.sleepThread(10);
    }
    assertEquals(completeStatus, response.getStatus());
    return response;
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse)

Aggregations

RestResponse (org.infinispan.client.rest.RestResponse)233 Test (org.testng.annotations.Test)108 Json (org.infinispan.commons.dataconversion.internal.Json)52 RestCacheClient (org.infinispan.client.rest.RestCacheClient)42 Test (org.junit.Test)41 Util.getResourceAsString (org.infinispan.commons.util.Util.getResourceAsString)39 RestClient (org.infinispan.client.rest.RestClient)37 RestEntity (org.infinispan.client.rest.RestEntity)36 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)23 HashMap (java.util.HashMap)8 RestCounterClient (org.infinispan.client.rest.RestCounterClient)8 TestClass (org.infinispan.rest.TestClass)8 RestSchemaClient (org.infinispan.client.rest.RestSchemaClient)7 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)7 RestCacheManagerClient (org.infinispan.client.rest.RestCacheManagerClient)6 RestMetricsClient (org.infinispan.client.rest.RestMetricsClient)6 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)6 Cache (org.infinispan.Cache)5 RestRawClient (org.infinispan.client.rest.RestRawClient)5 MultipleCacheManagersTest (org.infinispan.test.MultipleCacheManagersTest)5