HBaseDataStore implements DataStore { @Override @Timed public Document save(final Table table, Document document) { if(document == null || document.getData() == null || document.getId() == null) { throw FoxtrotExceptions.createBadRequestException(table.getName(), "Invalid Input Document"); } Document translatedDocument = null; try (org.apache.hadoop.hbase.client.Table hTable = tableWrapper.getTable(table)) { translatedDocument = translator.translate(table, document); hTable.put(getPutForDocument(translatedDocument)); } catch (JsonProcessingException e) { throw FoxtrotExceptions.createBadRequestException(table, e); } catch (IOException e) { throw FoxtrotExceptions.createConnectionException(table, e); } return translatedDocument; } @Inject  HBaseDataStore(HbaseTableConnection tableWrapper, ObjectMapper mapper, DocumentTranslator translator); @Override @Timed void initializeTable(Table table, boolean forceTableCreate); @Override @Timed Document save(final Table table, Document document); @Override @Timed List<Document> saveAll(final Table table, List<Document> documents); @Override @Timed Document get(final Table table, String id); @Override @Timed List<Document> getAll(final Table table, List<String> ids); @VisibleForTesting Put getPutForDocument(Document document); @Override @Timed void updateTable(final Table table);  }
@Test public void testSaveSingle() throws Exception { DocumentTranslator documentTranslator = new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV1()); hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, documentTranslator); Document expectedDocument = new Document(); expectedDocument.setId(UUID.randomUUID() .toString()); expectedDocument.setTimestamp(System.currentTimeMillis()); JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); expectedDocument.setData(data); hbaseDataStore.save(TEST_APP, expectedDocument); validateSave(v1FormatKey(expectedDocument.getId()), expectedDocument); documentTranslator = new DocumentTranslator(TestUtils.createTranslatorConfigWithRawKeyV2()); hbaseDataStore = new HBaseDataStore(hbaseTableConnection, mapper, documentTranslator); expectedDocument = new Document(); expectedDocument.setId(UUID.randomUUID() .toString()); expectedDocument.setTimestamp(System.currentTimeMillis()); data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); expectedDocument.setData(data); hbaseDataStore.save(TEST_APP, expectedDocument); validateSave(documentTranslator.translate(TEST_APP, expectedDocument) .getMetadata() .getRawStorageId(), expectedDocument); }
@Test public void testSaveSingleNullDocument() throws Exception { Document document = null; try { hbaseDataStore.save(TEST_APP, document); fail(); } catch (FoxtrotException ex) { assertEquals(ErrorCode.INVALID_REQUEST, ex.getCode()); } }
@Test public void testSaveSingleNullId() throws Exception { Document document = createDummyDocument(); document.setId(null); try { hbaseDataStore.save(TEST_APP, document); fail(); } catch (FoxtrotException ex) { assertEquals(ErrorCode.INVALID_REQUEST, ex.getCode()); } }
@Test public void testSaveSingleNullData() throws Exception { Document document = new Document(UUID.randomUUID() .toString(), System.currentTimeMillis(), null); try { hbaseDataStore.save(TEST_APP, document); fail(); } catch (FoxtrotException ex) { assertEquals(ErrorCode.INVALID_REQUEST, ex.getCode()); } }
@Test public void testSaveSingleHBaseWriteException() throws Exception { Document document = new Document(UUID.randomUUID() .toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) ); doThrow(new IOException()).when(tableInterface) .put(Matchers.<Put>any()); try { hbaseDataStore.save(TEST_APP, document); fail(); } catch (FoxtrotException ex) { assertEquals(ErrorCode.STORE_CONNECTION_ERROR, ex.getCode()); } }
@Test(expected = StoreConnectionException.class) public void testSaveSingleHBaseCloseException() throws Exception { Document document = new Document(UUID.randomUUID() .toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")) ); doThrow(new IOException()).when(tableInterface) .close(); hbaseDataStore.save(TEST_APP, document); }