Search in sources :

Example 6 with RestClient

use of org.infinispan.client.rest.RestClient 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 7 with RestClient

use of org.infinispan.client.rest.RestClient 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 8 with RestClient

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

the class TestClient method newRestClient.

public RestClient newRestClient(RestClientConfigurationBuilder restClientConfigurationBuilder) {
    RestClient restClient = testServer.newRestClient(restClientConfigurationBuilder);
    registerResource(restClient);
    return restClient;
}
Also used : RestClient(org.infinispan.client.rest.RestClient)

Example 9 with RestClient

use of org.infinispan.client.rest.RestClient 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 10 with RestClient

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

the class AbstractAuthorization method testRestAdminsShouldBeAbleToAdminServer.

@Test
public void testRestAdminsShouldBeAbleToAdminServer() {
    RestClientConfigurationBuilder adminConfig = restBuilders.get(TestUser.ADMIN);
    RestClient client = getServerTest().rest().withClientConfiguration(adminConfig).get();
    assertStatus(NO_CONTENT, client.server().connectorStop("endpoint-alternate-1"));
    assertStatus(NO_CONTENT, client.server().connectorStart("endpoint-alternate-1"));
    assertStatus(NO_CONTENT, client.server().connectorIpFilterSet("endpoint-alternate-1", Collections.emptyList()));
    assertStatus(NO_CONTENT, client.server().connectorIpFiltersClear("endpoint-alternate-1"));
    assertStatus(OK, client.server().memory());
    assertStatus(OK, client.server().env());
    assertStatus(OK, client.server().configuration());
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestClient(org.infinispan.client.rest.RestClient) Test(org.junit.Test)

Aggregations

RestClient (org.infinispan.client.rest.RestClient)65 Test (org.junit.Test)41 RestResponse (org.infinispan.client.rest.RestResponse)37 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)26 Json (org.infinispan.commons.dataconversion.internal.Json)15 RestCacheClient (org.infinispan.client.rest.RestCacheClient)11 Test (org.testng.annotations.Test)10 IOException (java.io.IOException)6 AbstractMultipleSitesTest (org.infinispan.xsite.AbstractMultipleSitesTest)6 ArrayList (java.util.ArrayList)4 RestServerHelper (org.infinispan.rest.helper.RestServerHelper)4 AbstractRestResourceTest (org.infinispan.rest.resources.AbstractRestResourceTest)4 InetSocketAddress (java.net.InetSocketAddress)3 List (java.util.List)3 RestCacheManagerClient (org.infinispan.client.rest.RestCacheManagerClient)3 RestMetricsClient (org.infinispan.client.rest.RestMetricsClient)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 TestUser (org.infinispan.server.test.api.TestUser)3 File (java.io.File)2 UncheckedIOException (java.io.UncheckedIOException)2