Search in sources :

Example 36 with RestClient

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

the class ProtocolManagementIT method testIpFilter.

@Test
public void testIpFilter() throws IOException {
    NetworkAddress loopback = NetworkAddress.loopback("loopback");
    RestClientConfigurationBuilder loopbackBuilder = new RestClientConfigurationBuilder();
    loopbackBuilder.addServer().host(loopback.getAddress().getHostAddress()).port(11222);
    RestClient loopbackClient = SERVER_TEST.rest().withClientConfiguration(loopbackBuilder).get();
    assertStatus(200, loopbackClient.server().connectorNames());
    NetworkAddress siteLocal = NetworkAddress.match("sitelocal", iF -> !iF.getName().startsWith("docker"), InetAddress::isSiteLocalAddress);
    RestClientConfigurationBuilder siteLocalBuilder0 = new RestClientConfigurationBuilder();
    siteLocalBuilder0.addServer().host(siteLocal.getAddress().getHostAddress()).port(11222);
    RestClient siteLocalClient0 = SERVER_TEST.rest().withClientConfiguration(siteLocalBuilder0).get();
    assertStatus(200, siteLocalClient0.server().connectorNames());
    RestClientConfigurationBuilder siteLocalBuilder1 = new RestClientConfigurationBuilder();
    siteLocalBuilder1.addServer().host(siteLocal.getAddress().getHostAddress()).port(11322);
    RestClient siteLocalClient1 = SERVER_TEST.rest().withClientConfiguration(siteLocalBuilder1).get();
    assertStatus(200, siteLocalClient1.server().connectorNames());
    List<IpFilterRule> rules = new ArrayList<>();
    rules.add(new IpFilterRule(IpFilterRule.RuleType.REJECT, siteLocal.cidr()));
    assertStatus(204, loopbackClient.server().connectorIpFilterSet("endpoint-default", rules));
    Exceptions.expectException(RuntimeException.class, ExecutionException.class, SocketException.class, () -> sync(siteLocalClient0.server().connectorNames()));
    Exceptions.expectException(RuntimeException.class, ExecutionException.class, SocketException.class, () -> sync(siteLocalClient1.server().connectorNames()));
    assertStatus(204, loopbackClient.server().connectorIpFiltersClear("endpoint-default"));
    assertStatus(200, siteLocalClient0.server().connectorNames());
    assertStatus(200, siteLocalClient1.server().connectorNames());
    // Attempt to lock ourselves out
    assertStatus(409, siteLocalClient0.server().connectorIpFilterSet("endpoint-default", rules));
    // Apply the filter just on the Hot Rod endpoint
    assertStatus(204, loopbackClient.server().connectorIpFilterSet("HotRod-hotrod", rules));
    ConfigurationBuilder hotRodSiteLocalBuilder = new ConfigurationBuilder();
    hotRodSiteLocalBuilder.addServer().host(siteLocal.getAddress().getHostAddress()).port(11222).clientIntelligence(ClientIntelligence.BASIC);
    RemoteCacheManager siteLocalRemoteCacheManager = SERVER_TEST.hotrod().withClientConfiguration(hotRodSiteLocalBuilder).createRemoteCacheManager();
    Exceptions.expectException(TransportException.class, siteLocalRemoteCacheManager::getCacheNames);
    // REST should still work, so let's clear the rules
    assertStatus(204, siteLocalClient0.server().connectorIpFiltersClear("HotRod-hotrod"));
    // And retry
    assertNotNull(siteLocalRemoteCacheManager.getCacheNames());
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) ConfigurationBuilder(org.infinispan.client.hotrod.configuration.ConfigurationBuilder) RemoteCacheManager(org.infinispan.client.hotrod.RemoteCacheManager) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) NetworkAddress(org.infinispan.server.network.NetworkAddress) RestClient(org.infinispan.client.rest.RestClient) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) IpFilterRule(org.infinispan.client.rest.IpFilterRule) Test(org.junit.Test)

Example 37 with RestClient

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

the class ProtocolManagementIT method testConnectorStartStop.

@Test
public void testConnectorStartStop() throws IOException {
    NetworkAddress loopback = NetworkAddress.loopback("loopback");
    RestClientConfigurationBuilder defaultBuilder = new RestClientConfigurationBuilder();
    defaultBuilder.addServer().host(loopback.getAddress().getHostAddress()).port(11222);
    RestClient defaultClient = SERVER_TEST.rest().withClientConfiguration(defaultBuilder).get();
    assertStatus(200, defaultClient.caches());
    RestClientConfigurationBuilder alternateBuilder = new RestClientConfigurationBuilder();
    alternateBuilder.addServer().host(loopback.getAddress().getHostAddress()).port(11223);
    RestClient alternateClient = SERVER_TEST.rest().withClientConfiguration(alternateBuilder).get();
    assertStatus(200, alternateClient.caches());
    assertStatus(204, defaultClient.server().connectorStop("endpoint-alternate-1"));
    Exceptions.expectException(RuntimeException.class, ExecutionException.class, SocketException.class, () -> sync(alternateClient.caches()));
    assertStatus(204, defaultClient.server().connectorStart("endpoint-alternate-1"));
    assertStatus(200, alternateClient.caches());
    // Attempt to lock ourselves out
    assertStatus(409, defaultClient.server().connectorStop("endpoint-default"));
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) NetworkAddress(org.infinispan.server.network.NetworkAddress) RestClient(org.infinispan.client.rest.RestClient) Test(org.junit.Test)

Example 38 with RestClient

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

the class RestMetricsResource method testMetrics.

@Test
public void testMetrics() throws Exception {
    RestClient client = SERVER_TEST.rest().create();
    RestMetricsClient metricsClient = client.metrics();
    String metricName = "cache_manager_default_cache_" + SERVER_TEST.getMethodName() + "_statistics_stores";
    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).isEqualTo(10.0);
        });
    }
    // delete cache and check that the metric is gone
    sync(client.cache(SERVER_TEST.getMethodName()).delete());
    try (RestResponse response = sync(metricsClient.metrics())) {
        assertEquals(200, response.getStatus());
        checkIsPrometheus(response.contentType());
        String body = response.getBody();
        assertThat(body).contains("base", "vendor");
        // metric is not present anymore:
        assertThat(body).doesNotContain(metricName);
    }
}
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 39 with RestClient

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

the class RestMetricsResource method testMetricsMetadata.

@Test
public void testMetricsMetadata() throws Exception {
    RestClient client = SERVER_TEST.rest().create();
    RestMetricsClient metricsClient = client.metrics();
    String metricName = "cache_manager_default_cache_" + SERVER_TEST.getMethodName() + "_statistics_stores";
    try (RestResponse response = sync(metricsClient.metricsMetadata())) {
        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();
        });
    }
    // delete cache and check that the metric is gone
    sync(client.cache(SERVER_TEST.getMethodName()).delete());
    try (RestResponse response = sync(metricsClient.metricsMetadata())) {
        assertEquals(200, response.getStatus());
        checkIsPrometheus(response.contentType());
        String body = response.getBody();
        assertThat(body).contains("base", "vendor");
        // metric is not present anymore:
        assertThat(body).doesNotContain(metricName);
    }
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) RestMetricsClient(org.infinispan.client.rest.RestMetricsClient) Test(org.junit.Test)

Example 40 with RestClient

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

the class RestRouter method testRestRouting.

@Test
public void testRestRouting() throws IOException {
    Function<String, RestClientConfigurationBuilder> cfgFromCtx = c -> new RestClientConfigurationBuilder().contextPath(c);
    try (RestClient restCtx = SERVER_TEST.newRestClient(cfgFromCtx.apply("/rest"));
        RestClient invalidCtx = SERVER_TEST.newRestClient(cfgFromCtx.apply("/invalid"));
        RestClient emptyCtx = SERVER_TEST.newRestClient(cfgFromCtx.apply("/"))) {
        String body = sync(restCtx.server().info()).getBody();
        assertTrue(body, body.contains("version"));
        assertEquals(404, sync(emptyCtx.server().info()).getStatus());
        assertEquals(404, sync(invalidCtx.server().info()).getStatus());
    }
}
Also used : RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) InfinispanServerTestMethodRule(org.infinispan.server.test.junit4.InfinispanServerTestMethodRule) Rule(org.junit.Rule) InfinispanServerRule(org.infinispan.server.test.junit4.InfinispanServerRule) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) RestClient(org.infinispan.client.rest.RestClient) Test(org.junit.Test) ClassRule(org.junit.ClassRule) Function(java.util.function.Function) Common.sync(org.infinispan.server.test.core.Common.sync) Assert.assertEquals(org.junit.Assert.assertEquals) 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