@Test
public void create_cache() throws Exception {
// ImportBeanを生成
Map<String, ImportTargetTableBean> targetTable = new LinkedHashMap<String, ImportTargetTableBean>();
ImportTargetTableBean tableBean1 = new ImportTargetTableBean();
tableBean1.setDfsFilePath("/${user}/${execution_id}/import/c1");
tableBean1.setImportTargetType(ImportTarget1.class);
tableBean1.setCacheId("c1");
targetTable.put("IMPORT_TARGET1", tableBean1);
ImportTargetTableBean tableBean2 = new ImportTargetTableBean();
tableBean2.setDfsFilePath("/${user}/${execution_id}/import/c2");
tableBean2.setImportTargetType(ImportTarget1.class);
tableBean2.setCacheId("c2");
targetTable.put("IMPORT_TARGET2", tableBean2);
ImportBean bean = new ImportBean();
bean.setTargetTable(targetTable);
bean.setExecutionId(executionId);
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
FileList.Writer writer = FileList.createWriter(buffer, true);
final CacheInfo info = new CacheInfo("a", "c1", Calendar.getInstance(), "IMPORT_TARGET1", Arrays.asList("a", "b"), "X", 0);
writer.openNext(new FileProtocol(
FileProtocol.Kind.CREATE_CACHE,
tableBean1.getDfsFilePath(),
info)
).close();
writer.openNext(new FileProtocol(
FileProtocol.Kind.CONTENT,
FileNameUtil.createSendImportFileName("IMPORT_TARGET2"),
null)
).close();
writer.close();
final File output = folder.newFolder("output");
final List<String> files = new ArrayList<String>();
final List<String> builders = new ArrayList<String>();
// テスト対象クラス実行
DummyHdfsFileImport fileImport = new DummyHdfsFileImport(0) {
@Override
protected InputStream getInputStream() {
return new ByteArrayInputStream(buffer.toByteArray());
}
@Override
protected URI resolveLocation(ImportBean _, String user, String location) throws BulkLoaderSystemException {
return new File(output, location).toURI();
}
@Override
protected <T> long write(
Class<T> targetTableModel,
URI dfsFilePath,
InputStream inputStream) throws BulkLoaderSystemException {
try {
inputStream.close();
files.add(new File(dfsFilePath).getPath());
} catch (Exception e) {
throw new AssertionError(e);
}
return 1;
}
@Override
protected Callable<?> createCacheBuilder(
String subcommand,
ImportBean _,
URI location,
final CacheInfo target) throws IOException {
assertThat(subcommand, is(CacheBuildClient.SUBCOMMAND_CREATE));
assertThat(target, is(info));
return new Callable<Void>() {
@Override
public Void call() throws Exception {
builders.add(target.getId());
return null;
}
};
}
};
boolean result = fileImport.importFile(bean, "hadoop");
assertTrue(result);
assertThat(files.size(), is(2));
assertThat(files.get(0), endsWith("c1/PATCH/part-0"));
assertThat(files.get(1), endsWith(tableBean2.getDfsFilePath()));
Collections.sort(builders);
assertThat(builders.size(), is(1));
assertThat(builders.get(0), is("c1"));
}