Search in sources :

Example 26 with HttpServiceResponse

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

the class TestHttpService method testRecoveryBookieService.

@Test
public void testRecoveryBookieService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    HttpEndpointService recoveryBookieService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.RECOVERY_BOOKIE);
    // 1,  null body of GET, should return error
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response1 = recoveryBookieService.handle(request1);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
    // 2,  null body of PUT, should return error
    HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.PUT, null);
    HttpServiceResponse response2 = recoveryBookieService.handle(request2);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response2.getStatusCode());
    // 3, body with bookie_src, bookie_dest and delete_cookie of PUT, should success.
    String bookieSrc = getBookie(0).toString();
    String putBody3 = "{\"bookie_src\": [ \"" + bookieSrc + "\" ]," + "\"delete_cookie\": false }";
    HttpServiceRequest request3 = new HttpServiceRequest(putBody3, HttpServer.Method.PUT, null);
    HttpServiceResponse response3 = recoveryBookieService.handle(request3);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
    // 5, body with bookie_src of PUT, should success.
    String putBody5 = "{\"bookie_src\": [ \"" + bookieSrc + "\" ] }";
    HttpServiceRequest request5 = new HttpServiceRequest(putBody5, HttpServer.Method.PUT, null);
    HttpServiceResponse response5 = recoveryBookieService.handle(request5);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response5.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 27 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse 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 28 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse 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 29 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse 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 30 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse 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)

Aggregations

HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)35 HttpServiceRequest (org.apache.bookkeeper.http.service.HttpServiceRequest)19 HttpEndpointService (org.apache.bookkeeper.http.service.HttpEndpointService)18 Test (org.junit.Test)16 BookKeeper (org.apache.bookkeeper.client.BookKeeper)8 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)7 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)6 Map (java.util.Map)4 File (java.io.File)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LedgerManager (org.apache.bookkeeper.meta.LedgerManager)3 LedgerManagerFactory (org.apache.bookkeeper.meta.LedgerManagerFactory)3 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 ErrorHttpService (org.apache.bookkeeper.http.service.ErrorHttpService)2 LedgerUnderreplicationManager (org.apache.bookkeeper.meta.LedgerUnderreplicationManager)2 JsonUtil (org.apache.bookkeeper.util.JsonUtil)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1