Search in sources :

Example 46 with Response

use of org.apache.hadoop.hbase.rest.client.Response in project hbase by apache.

the class TestNamespacesInstanceResource method testInvalidNamespacePostsAndPuts.

@Test
public void testInvalidNamespacePostsAndPuts() throws IOException, JAXBException {
    String namespacePath1 = "/namespaces/" + NAMESPACE1;
    String namespacePath2 = "/namespaces/" + NAMESPACE2;
    String namespacePath3 = "/namespaces/" + NAMESPACE3;
    NamespacesInstanceModel model1;
    NamespacesInstanceModel model2;
    NamespacesInstanceModel model3;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE1));
    assertNull(findNamespace(admin, NAMESPACE2));
    assertNull(findNamespace(admin, NAMESPACE3));
    model1 = testNamespacesInstanceModel.buildTestModel(NAMESPACE1, NAMESPACE1_PROPS);
    testNamespacesInstanceModel.checkModel(model1, NAMESPACE1, NAMESPACE1_PROPS);
    model2 = testNamespacesInstanceModel.buildTestModel(NAMESPACE2, NAMESPACE2_PROPS);
    testNamespacesInstanceModel.checkModel(model2, NAMESPACE2, NAMESPACE2_PROPS);
    model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
    testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);
    // Try REST post and puts with invalid content.
    response = client.post(namespacePath1, Constants.MIMETYPE_JSON, toXML(model1));
    assertEquals(400, response.getCode());
    String jsonString = jsonMapper.writeValueAsString(model2);
    response = client.put(namespacePath2, Constants.MIMETYPE_XML, Bytes.toBytes(jsonString));
    assertEquals(400, response.getCode());
    response = client.post(namespacePath3, Constants.MIMETYPE_PROTOBUF, toXML(model1));
    assertEquals(500, response.getCode());
    NamespaceDescriptor nd1 = findNamespace(admin, NAMESPACE1);
    NamespaceDescriptor nd2 = findNamespace(admin, NAMESPACE2);
    NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
    assertNull(nd1);
    assertNull(nd2);
    assertNull(nd3);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 47 with Response

use of org.apache.hadoop.hbase.rest.client.Response in project hbase by apache.

the class TestNamespacesInstanceResource method testGetNamespaceTablesAndCannotDeleteNamespace.

@Test
public void testGetNamespaceTablesAndCannotDeleteNamespace() throws IOException, JAXBException {
    Admin admin = TEST_UTIL.getAdmin();
    String nsName = "TestNamespacesInstanceResource5";
    Response response;
    // Create namespace via admin.
    NamespaceDescriptor.Builder nsBuilder = NamespaceDescriptor.create(nsName);
    NamespaceDescriptor nsd = nsBuilder.build();
    nsd.setConfiguration("key1", "value1");
    admin.createNamespace(nsd);
    // Create two tables via admin.
    HColumnDescriptor colDesc = new HColumnDescriptor("cf1");
    TableName tn1 = TableName.valueOf(nsName + ":table1");
    HTableDescriptor table = new HTableDescriptor(tn1);
    table.addFamily(colDesc);
    admin.createTable(table);
    TableName tn2 = TableName.valueOf(nsName + ":table2");
    table = new HTableDescriptor(tn2);
    table.addFamily(colDesc);
    admin.createTable(table);
    Map<String, String> nsProperties = new HashMap<>();
    nsProperties.put("key1", "value1");
    List<String> nsTables = Arrays.asList("table1", "table2");
    // Check get namespace properties as XML, JSON and Protobuf.
    String namespacePath = "/namespaces/" + nsName;
    response = client.get(namespacePath);
    assertEquals(200, response.getCode());
    response = client.get(namespacePath, Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    NamespacesInstanceModel model = fromXML(response.getBody());
    checkNamespaceProperties(model.getProperties(), nsProperties);
    response = client.get(namespacePath, Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    model = jsonMapper.readValue(response.getBody(), NamespacesInstanceModel.class);
    checkNamespaceProperties(model.getProperties(), nsProperties);
    response = client.get(namespacePath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(200, response.getCode());
    model.getObjectFromMessage(response.getBody());
    checkNamespaceProperties(model.getProperties(), nsProperties);
    // Check get namespace tables as XML, JSON and Protobuf.
    namespacePath = "/namespaces/" + nsName + "/tables";
    response = client.get(namespacePath);
    assertEquals(200, response.getCode());
    response = client.get(namespacePath, Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    TableListModel tablemodel = fromXML(response.getBody());
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    response = client.get(namespacePath, Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    tablemodel = jsonMapper.readValue(response.getBody(), TableListModel.class);
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    response = client.get(namespacePath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(200, response.getCode());
    tablemodel.setTables(new ArrayList<>());
    tablemodel.getObjectFromMessage(response.getBody());
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    // Check cannot delete namespace via REST because it contains tables.
    response = client.delete(namespacePath);
    namespacePath = "/namespaces/" + nsName;
    assertEquals(503, response.getCode());
}
Also used : TableListModel(org.apache.hadoop.hbase.rest.model.TableListModel) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) HashMap(java.util.HashMap) Admin(org.apache.hadoop.hbase.client.Admin) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Response(org.apache.hadoop.hbase.rest.client.Response) TableName(org.apache.hadoop.hbase.TableName) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Test(org.junit.Test)

Example 48 with Response

use of org.apache.hadoop.hbase.rest.client.Response in project hbase by apache.

the class TestNamespacesInstanceResource method testNamespaceCreateAndDeletePBAndNoBody.

@Test
public void testNamespaceCreateAndDeletePBAndNoBody() throws IOException, JAXBException {
    String namespacePath3 = "/namespaces/" + NAMESPACE3;
    String namespacePath4 = "/namespaces/" + NAMESPACE4;
    NamespacesInstanceModel model3;
    NamespacesInstanceModel model4;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE3));
    assertNull(findNamespace(admin, NAMESPACE4));
    model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
    testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);
    model4 = testNamespacesInstanceModel.buildTestModel(NAMESPACE4, NAMESPACE4_PROPS);
    testNamespacesInstanceModel.checkModel(model4, NAMESPACE4, NAMESPACE4_PROPS);
    // Test cannot PUT (alter) non-existent namespace.
    response = client.put(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.put(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    // Test cannot create tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.put(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
    NamespaceDescriptor nd4 = findNamespace(admin, NAMESPACE4);
    assertNull(nd3);
    assertNull(nd4);
    conf.set("hbase.rest.readonly", "false");
    // Create namespace via no body and protobuf.
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(201, response.getCode());
    response = client.post(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(201, response.getCode());
    // Check that created namespaces correctly.
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNotNull(nd3);
    assertNotNull(nd4);
    checkNamespaceProperties(nd3, new HashMap<>());
    checkNamespaceProperties(nd4, NAMESPACE4_PROPS);
    // Check cannot post tables that already exist.
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.post(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    // Check cannot post tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.delete(namespacePath3);
    assertEquals(403, response.getCode());
    response = client.delete(namespacePath4);
    assertEquals(403, response.getCode());
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNotNull(nd3);
    assertNotNull(nd4);
    conf.set("hbase.rest.readonly", "false");
    // Delete namespaces via XML and JSON.
    response = client.delete(namespacePath3);
    assertEquals(200, response.getCode());
    response = client.delete(namespacePath4);
    assertEquals(200, response.getCode());
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNull(nd3);
    assertNull(nd4);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 49 with Response

use of org.apache.hadoop.hbase.rest.client.Response in project hbase by apache.

the class TestGetAndPutResource method testSuffixGlobbingXMLWithNewScanner.

@Test
public void testSuffixGlobbingXMLWithNewScanner() throws IOException, JAXBException {
    // deliberate nonexistent row
    String path = "/" + TABLE + "/fakerow";
    CellSetModel cellSetModel = new CellSetModel();
    RowModel rowModel = new RowModel(ROW_1);
    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_1)));
    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), Bytes.toBytes(VALUE_2)));
    cellSetModel.addRow(rowModel);
    rowModel = new RowModel(ROW_2);
    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_3)));
    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), Bytes.toBytes(VALUE_4)));
    cellSetModel.addRow(rowModel);
    StringWriter writer = new StringWriter();
    xmlMarshaller.marshal(cellSetModel, writer);
    Response response = client.put(path, Constants.MIMETYPE_XML, Bytes.toBytes(writer.toString()));
    Thread.yield();
    // make sure the fake row was not actually created
    response = client.get(path, Constants.MIMETYPE_XML);
    assertEquals(response.getCode(), 404);
    // check that all of the values were created
    StringBuilder query = new StringBuilder();
    query.append('/');
    query.append(TABLE);
    query.append('/');
    query.append("testrow*");
    response = client.get(query.toString(), Constants.MIMETYPE_XML);
    assertEquals(response.getCode(), 200);
    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
    CellSetModel cellSet = (CellSetModel) xmlUnmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
    assertTrue(cellSet.getRows().size() == 2);
    response = deleteRow(TABLE, ROW_1);
    assertEquals(response.getCode(), 200);
    response = deleteRow(TABLE, ROW_2);
    assertEquals(response.getCode(), 200);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Test(org.junit.Test)

Example 50 with Response

use of org.apache.hadoop.hbase.rest.client.Response in project hbase by apache.

the class TestGetAndPutResource method testSingleCellGetPutPB.

@Test
public void testSingleCellGetPutPB() throws IOException, JAXBException {
    Response response = getValuePB(TABLE, ROW_1, COLUMN_1);
    assertEquals(response.getCode(), 404);
    response = putValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
    assertEquals(response.getCode(), 200);
    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
    response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_2);
    assertEquals(response.getCode(), 200);
    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_2);
    response = checkAndPutValuePB(TABLE, ROW_1, COLUMN_1, VALUE_2, VALUE_3);
    assertEquals(response.getCode(), 200);
    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_3);
    response = checkAndPutValueXML(TABLE, ROW_1, COLUMN_1, VALUE_3, VALUE_4);
    assertEquals(response.getCode(), 200);
    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_4);
    response = deleteRow(TABLE, ROW_1);
    assertEquals(response.getCode(), 200);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) Test(org.junit.Test)

Aggregations

Response (org.apache.hadoop.hbase.rest.client.Response)91 Test (org.junit.Test)73 CellSetModel (org.apache.hadoop.hbase.rest.model.CellSetModel)39 RowModel (org.apache.hadoop.hbase.rest.model.RowModel)30 CellModel (org.apache.hadoop.hbase.rest.model.CellModel)24 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringWriter (java.io.StringWriter)16 ScannerModel (org.apache.hadoop.hbase.rest.model.ScannerModel)10 Admin (org.apache.hadoop.hbase.client.Admin)9 JAXBContext (javax.xml.bind.JAXBContext)7 Unmarshaller (javax.xml.bind.Unmarshaller)7 JacksonJaxbJsonProvider (org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)6 HashMap (java.util.HashMap)4 NamespaceDescriptor (org.apache.hadoop.hbase.NamespaceDescriptor)4 NamespacesInstanceModel (org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel)4 TestNamespacesInstanceModel (org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel)4 Header (org.apache.http.Header)4 StorageClusterVersionModel (org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel)3 TableListModel (org.apache.hadoop.hbase.rest.model.TableListModel)3