Search in sources :

Example 26 with RestClient

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

the class CacheResourceTest method testCORSAllOrigins.

@Test
public void testCORSAllOrigins() throws IOException {
    RestServerHelper restServerHelper = null;
    RestClient client = null;
    try {
        RestServerConfigurationBuilder restBuilder = new RestServerConfigurationBuilder();
        restBuilder.cors().addNewRule().allowOrigins(new String[] { "*" });
        restBuilder.host("localhost").port(0);
        restServerHelper = RestServerHelper.defaultRestServer();
        RestServerConfiguration build = restBuilder.build();
        restServerHelper.withConfiguration(build).start("test");
        client = restServerHelper.createClient();
        RestResponse response = join(client.cache("default").get("test", singletonMap(ORIGIN.toString(), "http://host.example.com:5576")));
        assertThat(response).containsAllHeaders("access-control-allow-origin");
    } finally {
        client.close();
        if (restServerHelper != null)
            restServerHelper.stop();
    }
}
Also used : RestServerConfigurationBuilder(org.infinispan.rest.configuration.RestServerConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) RestServerConfiguration(org.infinispan.rest.configuration.RestServerConfiguration) RestServerHelper(org.infinispan.rest.helper.RestServerHelper) Test(org.testng.annotations.Test)

Example 27 with RestClient

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

the class PipelineInitializationTest method testInitializationRules.

@Test
public void testInitializationRules() throws InterruptedException, ExecutionException {
    int numTasks = 5;
    ExecutorService executorService = Executors.newFixedThreadPool(numTasks);
    CountDownLatch startLatch = new CountDownLatch(1);
    List<Supplier<Integer>> suppliers = new ArrayList<>();
    for (int i = 0; i < numTasks; i++) {
        RestClient client = i % 2 == 0 ? client1 : client2;
        suppliers.add(createTask(client, startLatch));
    }
    List<CompletableFuture<Integer>> results = suppliers.stream().map(s -> supplyAsync(s, executorService)).collect(Collectors.toList());
    startLatch.countDown();
    executorService.shutdown();
    assertTrue(executorService.awaitTermination(5, TimeUnit.SECONDS));
    for (CompletableFuture<Integer> result : results) {
        assertFalse(result.isCompletedExceptionally());
        assertEquals((int) result.get(), 200);
    }
}
Also used : TestResourceTracker(org.infinispan.commons.test.TestResourceTracker) Assert.assertEquals(org.testng.Assert.assertEquals) CompletableFuture(java.util.concurrent.CompletableFuture) RestClient(org.infinispan.client.rest.RestClient) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) ExecutorService(java.util.concurrent.ExecutorService) RestResponse(org.infinispan.client.rest.RestResponse) FunctionalTestUtils.await(org.infinispan.functional.FunctionalTestUtils.await) BeforeMethod(org.testng.annotations.BeforeMethod) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) RestServerHelper(org.infinispan.rest.helper.RestServerHelper) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertTrue(org.testng.Assert.assertTrue) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) RestClient(org.infinispan.client.rest.RestClient) Supplier(java.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Example 28 with RestClient

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

the class NativeCliIT method testCliBatch.

@Test
public void testCliBatch() throws Exception {
    RestClient client = SERVER_TEST.rest().create();
    RestCacheManagerClient cm = client.cacheManager("default");
    RestResponse restResponse = sync(cm.healthStatus());
    assertEquals(200, restResponse.getStatus());
    assertEquals("HEALTHY", restResponse.getBody());
    String cliPath = resource("ispn-cli");
    String batchFile = resource("batch.file");
    String hostname = SERVERS.getTestServer().getDriver().getServerAddress(0).getHostAddress() + ":11222";
    String cliCmd = String.format("%s --connect %s --file=%s", cliPath, hostname, batchFile);
    ProcessBuilder pb = new ProcessBuilder("bash", "-c", cliCmd);
    pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    Process p = pb.start();
    StringBuilder sb = new StringBuilder();
    new Thread(() -> {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }).start();
    p.waitFor(5, TimeUnit.SECONDS);
    assertEquals(0, p.exitValue());
    String stderr = sb.toString();
    if (!stderr.isEmpty()) {
        System.err.println(stderr);
        fail("Unexpected CLI output in stderr");
    }
    restResponse = sync(client.cache("mybatch").exists());
    assertEquals(204, restResponse.getStatus());
}
Also used : InputStreamReader(java.io.InputStreamReader) RestCacheManagerClient(org.infinispan.client.rest.RestCacheManagerClient) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Test(org.junit.Test)

Example 29 with RestClient

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

the class RestCacheManagerResource method testCacheInfo.

@Test
public void testCacheInfo() throws Exception {
    RestClient client = SERVER_TEST.rest().create();
    String cacheName = "test";
    RestResponse restResponse = sync(client.cache(cacheName).createWithTemplate("org.infinispan.LOCAL"));
    assertEquals(200, restResponse.getStatus());
    restResponse = sync(client.raw().get("/rest/v2/cache-managers/default/caches"));
    assertEquals(200, restResponse.getStatus());
    ArrayNode cacheArray = (ArrayNode) mapper.readTree(restResponse.getBody());
    assertNotNull(cacheArray);
    cacheArray.forEach(cache -> {
        assertEquals("RUNNING", cache.get("status").asText());
        assertEquals("HEALTHY", cache.get("health").asText());
        if (cacheName.equals(cache.get("name").asText())) {
            assertEquals("local-cache", cache.get("type").asText());
            assertFalse(cache.get("simple_cache").asBoolean());
            assertFalse(cache.get("transactional").asBoolean());
            assertFalse(cache.get("persistent").asBoolean());
            assertFalse(cache.get("bounded").asBoolean());
            assertFalse(cache.get("indexed").asBoolean());
            assertFalse(cache.get("secured").asBoolean());
            assertFalse(cache.get("has_remote_backup").asBoolean());
        }
    });
}
Also used : RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.Test)

Example 30 with RestClient

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

the class XSiteResourceTest method testGetStatusAllCaches.

@Test
public void testGetStatusAllCaches() {
    RestClient restClient = clientPerSite.get(LON);
    assertAllSitesOnline(restClient);
    assertSuccessful(restClient.cache(CACHE_2).takeSiteOffline(NYC));
    Json json = jsonResponseBody(restClient.cacheManager(CACHE_MANAGER).backupStatuses());
    assertEquals(json.at(NYC).at("status").asString(), "mixed");
    assertEquals(json.at(NYC).at("online").asJsonList().iterator().next().asString(), CACHE_1);
    assertEquals(json.at(NYC).at("offline").asJsonList().iterator().next().asString(), CACHE_2);
    json = jsonResponseBody(restClient.cacheManager(CACHE_MANAGER).backupStatus(NYC));
    assertEquals(json.at("status").asString(), "mixed");
    assertEquals(json.at("online").asJsonList().iterator().next().asString(), CACHE_1);
    assertEquals(json.at("offline").asJsonList().iterator().next().asString(), CACHE_2);
    assertSuccessful(restClient.cache(CACHE_2).bringSiteOnline(NYC));
    assertAllSitesOnline(restClient);
    // add a second node
    TestSite site = site(LON);
    EmbeddedCacheManager cm = site.addCacheManager(null, defaultGlobalConfigurationForSite(site.getSiteIndex()), defaultConfigurationForSite(site.getSiteIndex()), true);
    site.waitForClusterToForm(CACHE_1);
    site.waitForClusterToForm(CACHE_2);
    TakeOfflineManager takeOfflineManager = TestingUtil.extractComponent(cm.getCache(CACHE_1), TakeOfflineManager.class);
    takeOfflineManager.takeSiteOffline(NYC);
    json = jsonResponseBody(restClient.cacheManager(CACHE_MANAGER).backupStatuses());
    assertEquals(json.at(NYC).at("status").asString(), "mixed");
    assertEquals(json.at(NYC).at("online").asJsonList().iterator().next().asString(), CACHE_2);
    assertTrue(json.at(NYC).at("offline").asJsonList().isEmpty());
    assertEquals(json.at(NYC).at("mixed").asJsonList().iterator().next().asString(), CACHE_1);
    json = jsonResponseBody(restClient.cacheManager(CACHE_MANAGER).backupStatus(NYC));
    assertEquals(json.at("status").asString(), "mixed");
    assertEquals(json.at("online").asJsonList().iterator().next().asString(), CACHE_2);
    assertTrue(json.at("offline").asJsonList().isEmpty());
    assertEquals(json.at("mixed").asJsonList().iterator().next().asString(), CACHE_1);
    takeOfflineManager.bringSiteOnline(NYC);
}
Also used : RestClient(org.infinispan.client.rest.RestClient) Json(org.infinispan.commons.dataconversion.internal.Json) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) TakeOfflineManager(org.infinispan.xsite.status.TakeOfflineManager) Test(org.testng.annotations.Test) AbstractMultipleSitesTest(org.infinispan.xsite.AbstractMultipleSitesTest)

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