Search in sources :

Example 11 with HttpServiceRequest

use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.

the class TestHttpService method testListLedgerService.

/**
 * Create ledgers, then test ListLedgerService.
 */
@Test
public void testListLedgerService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    BookKeeper.DigestType digestType = BookKeeper.DigestType.CRC32;
    int numLedgers = 430;
    LedgerHandle[] lh = new LedgerHandle[numLedgers];
    // create ledgers
    for (int i = 0; i < numLedgers; i++) {
        lh[i] = bkc.createLedger(digestType, "password".getBytes());
    }
    HttpEndpointService listLedgerService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.LIST_LEDGER);
    // 1,  null parameters, should print ledger ids, without metadata
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response1 = listLedgerService.handle(request1);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response1.getStatusCode());
    // get response , expected get all ledgers, and without metadata
    @SuppressWarnings("unchecked") LinkedHashMap<String, String> respBody = JsonUtil.fromJson(response1.getBody(), LinkedHashMap.class);
    assertEquals(numLedgers, respBody.size());
    for (int i = 0; i < numLedgers; i++) {
        assertEquals(true, respBody.containsKey(Long.valueOf(lh[i].getId()).toString()));
        assertEquals(null, respBody.get(Long.valueOf(lh[i].getId()).toString()));
    }
    // 2, parameter: print_metadata=true, should print ledger ids, with metadata
    HashMap<String, String> params = Maps.newHashMap();
    params.put("print_metadata", "true");
    HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.GET, params);
    HttpServiceResponse response2 = listLedgerService.handle(request2);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response2.getStatusCode());
    // get response, expected get all ledgers, and with metadata
    @SuppressWarnings("unchecked") LinkedHashMap<String, String> respBody2 = JsonUtil.fromJson(response2.getBody(), LinkedHashMap.class);
    assertEquals(numLedgers, respBody2.size());
    for (int i = 0; i < numLedgers; i++) {
        assertEquals(true, respBody2.containsKey(Long.valueOf(lh[i].getId()).toString()));
        assertNotNull(respBody2.get(Long.valueOf(lh[i].getId()).toString()));
    }
    // 3, parameter: print_metadata=true&page=5,
    // since each page contains 100 ledgers, page=5 should print ledger ids, with metadata for(400--430)
    HashMap<String, String> params3 = Maps.newHashMap();
    params3.put("print_metadata", "true");
    params3.put("page", "5");
    HttpServiceRequest request3 = new HttpServiceRequest(null, HttpServer.Method.GET, params3);
    HttpServiceResponse response3 = listLedgerService.handle(request3);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
    // get response, expected get 4 ledgers, and with metadata
    @SuppressWarnings("unchecked") LinkedHashMap<String, String> respBody3 = JsonUtil.fromJson(response3.getBody(), LinkedHashMap.class);
    assertEquals(31, respBody3.size());
    for (int i = 400; i < 430; i++) {
        assertEquals(true, respBody3.containsKey(Long.valueOf(lh[i].getId()).toString()));
        assertNotNull(respBody3.get(Long.valueOf(lh[i].getId()).toString()));
    }
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 12 with HttpServiceRequest

use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.

the class TestHttpService method testLostBookieRecoveryDelayService.

@Test
public void testLostBookieRecoveryDelayService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    HttpEndpointService lostBookieRecoveryDelayService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.LOST_BOOKIE_RECOVERY_DELAY);
    // 1,  PUT with null, should return error, because should contains {"delay_seconds": <delay_seconds>}.
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.PUT, null);
    HttpServiceResponse response1 = lostBookieRecoveryDelayService.handle(request1);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
    // 2,  GET, should meet exception when get delay seconds
    HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response2 = lostBookieRecoveryDelayService.handle(request2);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response2.getStatusCode());
    // 3, PUT, with body, should success
    String putBody3 = "{\"delay_seconds\": 17 }";
    HttpServiceRequest request3 = new HttpServiceRequest(putBody3, HttpServer.Method.PUT, null);
    HttpServiceResponse response3 = lostBookieRecoveryDelayService.handle(request3);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 13 with HttpServiceRequest

use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.

the class TestHttpService method testConfigServicePut.

@Test
public void testConfigServicePut() throws Exception {
    // test config service
    HttpEndpointService configService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.SERVER_CONFIG);
    // properties to be set
    String putBody = "{\"TEST_PROPERTY1\": \"TEST_VALUE1\", \"TEST_PROPERTY2\": 2,  \"TEST_PROPERTY3\": true }";
    // null body, should return NOT_FOUND
    HttpServiceRequest putRequest1 = new HttpServiceRequest(null, HttpServer.Method.PUT, null);
    HttpServiceResponse putResponse1 = configService.handle(putRequest1);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), putResponse1.getStatusCode());
    // Method DELETE, should return NOT_FOUND
    HttpServiceRequest putRequest2 = new HttpServiceRequest(putBody, HttpServer.Method.DELETE, null);
    HttpServiceResponse putResponse2 = configService.handle(putRequest2);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), putResponse2.getStatusCode());
    // Normal PUT, should success, then verify using get method
    HttpServiceRequest putRequest3 = new HttpServiceRequest(putBody, HttpServer.Method.PUT, null);
    HttpServiceResponse putResponse3 = configService.handle(putRequest3);
    assertEquals(HttpServer.StatusCode.OK.getValue(), putResponse3.getStatusCode());
    // Get all the config
    HttpServiceRequest getRequest = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response = configService.handle(getRequest);
    Map configMap = JsonUtil.fromJson(response.getBody(), Map.class);
    // verify response code
    assertEquals(HttpServer.StatusCode.OK.getValue(), response.getStatusCode());
    // verify response body
    assertEquals("TEST_VALUE1", configMap.get("TEST_PROPERTY1"));
    assertEquals("2", configMap.get("TEST_PROPERTY2"));
    assertEquals("true", configMap.get("TEST_PROPERTY3"));
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with HttpServiceRequest

use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.

the class TestHttpService method testGetLedgerMetaService.

@Test
public void testGetLedgerMetaService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    BookKeeper.DigestType digestType = BookKeeper.DigestType.CRC32;
    int numLedgers = 4;
    int numMsgs = 100;
    LedgerHandle[] lh = new LedgerHandle[numLedgers];
    // create ledgers
    for (int i = 0; i < numLedgers; i++) {
        lh[i] = bkc.createLedger(digestType, "password".getBytes());
    }
    String content = "Apache BookKeeper is cool!";
    // add entries
    for (int i = 0; i < numMsgs; i++) {
        for (int j = 0; j < numLedgers; j++) {
            lh[j].addEntry(content.getBytes());
        }
    }
    // close ledgers
    for (int i = 0; i < numLedgers; i++) {
        lh[i].close();
    }
    HttpEndpointService getLedgerMetaService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.GET_LEDGER_META);
    // 1,  null parameters of GET, should return NOT_FOUND
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response1 = getLedgerMetaService.handle(request1);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
    // 2,  parameters for GET first ledger, should return OK, and contains metadata
    HashMap<String, String> params = Maps.newHashMap();
    Long ledgerId = Long.valueOf(lh[0].getId());
    params.put("ledger_id", ledgerId.toString());
    HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.GET, params);
    HttpServiceResponse response2 = getLedgerMetaService.handle(request2);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response2.getStatusCode());
    @SuppressWarnings("unchecked") HashMap<String, String> respBody = JsonUtil.fromJson(response2.getBody(), HashMap.class);
    assertEquals(1, respBody.size());
    // verify LedgerMetadata content is equal
    assertTrue(respBody.get(ledgerId.toString()).toString().equals(new String(lh[0].getLedgerMetadata().serialize())));
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 15 with HttpServiceRequest

use of org.apache.bookkeeper.http.service.HttpServiceRequest in project bookkeeper by apache.

the class TestHttpService method testListBookieInfoService.

@Test
public void testListBookieInfoService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    HttpEndpointService listBookieInfoService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.LIST_BOOKIE_INFO);
    // 1,  PUT method, should return NOT_FOUND
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.PUT, null);
    HttpServiceResponse response1 = listBookieInfoService.handle(request1);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
    // 2, GET method, expected get 6 bookies info and the cluster total info
    HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response2 = listBookieInfoService.handle(request2);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response2.getStatusCode());
    @SuppressWarnings("unchecked") LinkedHashMap<String, String> respBody = JsonUtil.fromJson(response2.getBody(), LinkedHashMap.class);
    assertEquals(numberOfBookies + 1, respBody.size());
    for (int i = 0; i < numberOfBookies; i++) {
        assertEquals(true, respBody.containsKey(getBookie(i).toString()));
    }
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Aggregations

HttpServiceRequest (org.apache.bookkeeper.http.service.HttpServiceRequest)19 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)19 HttpEndpointService (org.apache.bookkeeper.http.service.HttpEndpointService)17 Test (org.junit.Test)15 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)7 BookKeeper (org.apache.bookkeeper.client.BookKeeper)6 Map (java.util.Map)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ErrorHttpService (org.apache.bookkeeper.http.service.ErrorHttpService)2 LedgerUnderreplicationManager (org.apache.bookkeeper.meta.LedgerUnderreplicationManager)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 Response (com.twitter.finagle.http.Response)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1