use of org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunner method run.
@Override
public void run(JobService jobService, PkgScreenshotImportArchiveJobSpecification specification) throws IOException, JobRunnerException {
Preconditions.checkArgument(null != jobService);
Preconditions.checkArgument(null != specification);
Preconditions.checkArgument(null != specification.getInputDataGuid(), "missing input data guid on specification");
Preconditions.checkArgument(null != specification.getImportStrategy(), "missing import strategy on specification");
// this will register the outbound data against the job.
JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
Optional<JobDataWithByteSource> jobDataWithByteSourceOptional = jobService.tryObtainData(specification.getInputDataGuid());
if (!jobDataWithByteSourceOptional.isPresent()) {
throw new IllegalStateException("the job data was not able to be found for guid; " + specification.getInputDataGuid());
}
if (!serverRuntime.performInTransaction(() -> {
try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
Map<String, ScreenshotImportMetadatas> metadatas = new HashMap<>();
writer.writeNext(new String[] { "path", "pkg-name", "action", "message", "code" });
// sweep through and collect meta-data about the packages in the tar file.
LOGGER.info("will collect data about packages' screenshots from the archive", metadatas.size());
consumeScreenshotArchiveEntries(jobDataWithByteSourceOptional.get().getByteSource(), (ae) -> collectScreenshotMetadataFromArchive(metadatas, ae.getArchiveInputStream(), ae.getArchiveEntry(), ae.getPkgName(), ae.getOrder()));
LOGGER.info("did collect data about {} packages' screenshots from the archive", metadatas.size());
LOGGER.info("will collect data about persisted packages' screenshots");
collectPersistedScreenshotMetadata(metadatas);
LOGGER.info("did collect data about persisted packages' screenshots");
if (specification.getImportStrategy() == PkgScreenshotImportArchiveJobSpecification.ImportStrategy.REPLACE) {
LOGGER.info("will delete persisted screenshots that are absent from the archive");
int deleted = deletePersistedScreenshotsThatAreNotPresentInArchiveAndReport(writer, metadatas.values());
LOGGER.info("did delete {} persisted screenshots that are absent from the archive", deleted);
}
blendInArtificialOrderings(metadatas.values());
// sweep through the archive again and load in those screenshots that are not already present.
// The ordering of the inbound data should be preserved.
LOGGER.info("will load screenshots from archive", metadatas.size());
consumeScreenshotArchiveEntries(jobDataWithByteSourceOptional.get().getByteSource(), (ae) -> importScreenshotsFromArchiveAndReport(writer, metadatas.get(ae.getPkgName()), ae.getArchiveInputStream(), ae.getArchiveEntry(), ae.getPkgName(), ae.getOrder()));
LOGGER.info("did load screenshots from archive", metadatas.size());
return true;
} catch (IOException e) {
LOGGER.error("unable to complete the job", e);
}
return false;
})) {
throw new JobRunnerException("unable to complete job");
}
}
use of org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunnerIT method testImport_augment.
/**
* <p>The package 'pkg1' has screenshots loaded already.</p>
*/
@Test
public void testImport_augment() throws Exception {
integrationTestSupportService.createStandardTestData();
// now load in the data to the job's storage system.
PkgScreenshotImportArchiveJobSpecification spec = new PkgScreenshotImportArchiveJobSpecification();
spec.setInputDataGuid(jobService.storeSuppliedData("sample-pkgscreenshotimportarchive-supplied.tgz", MediaType.TAR.toString(), getResourceByteSource("sample-pkgscreenshotimportarchive-supplied.tgz")).getGuid());
spec.setImportStrategy(PkgScreenshotImportArchiveJobSpecification.ImportStrategy.AUGMENT);
// run the job to import the data
// ------------------------------------
String jobGuid = jobService.immediate(spec, false);
// ------------------------------------
JobSnapshot snapshot = jobService.tryGetJob(jobGuid).get();
Assertions.assertThat(snapshot.getStatus()).isEqualTo(JobSnapshot.Status.FINISHED);
// check that the pkg1 is now loaded-up with screenshots from the tar-ball.
assertScreenshotHashes(new String[] { "741dcbf503ddf034db0c41cb0fc9e3f82b20a5b6", "199a10b2b498e07e5bde31d816b560b19ed76ca6", "1a7be578f99d815084d32487d0e74626fe489967", "cba98ed83e44c2b07710f78c95568a3b2e3a081e", "fd1616327841ee2be21a16e330c57daa613580fa" });
// check that the output report is as expected.
{
List<String> outputLines = getOutputLines(snapshot);
// "path","pkg-name","action","message","code"
// "hscr/pkg1/200.png","pkg1","ADDED","","34e796f2-27ff-4139-a0eb-b3b0e8238e96"
// "hscr/pkg1/201.png","pkg1","ADDED","","91da40b7-762d-4d43-a0a0-4b4f31e585ae"
// "hscr/pkg1/202.png","pkg1","INVALID",,""
// "hscr/pkg1/23.png","pkg1","PRESENT","","2ef86afa-4da3-4f0c-9be6-d2b54ff099cb"
// "hscr/notexists/3.png","notexists","NOTFOUND","",""
// compare actual generated with expected.
Assertions.assertThat(outputLines).hasSize(6);
Assertions.assertThat(outputLines.get(1)).startsWith("\"hscr/pkg1/200.png\",\"pkg1\",\"ADDED\",\"\",\"");
Assertions.assertThat(outputLines.get(2)).startsWith("\"hscr/pkg1/201.png\",\"pkg1\",\"ADDED\",\"\",\"");
Assertions.assertThat(outputLines.get(3)).isEqualTo("\"hscr/pkg1/202.png\",\"pkg1\",\"INVALID\",,\"\"");
Assertions.assertThat(outputLines.get(4)).startsWith("\"hscr/pkg1/23.png\",\"pkg1\",\"PRESENT\",\"\",\"");
Assertions.assertThat(outputLines.get(5)).isEqualTo("\"hscr/notexists/3.png\",\"notexists\",\"NOTFOUND\",\"\",\"\"");
}
}
use of org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunnerIT method testImport_replace.
/**
* <p>The package 'pkg1' has screenshots loaded already.</p>
*/
@Test
public void testImport_replace() throws Exception {
integrationTestSupportService.createStandardTestData();
// now load in the data to the job's storage system.
PkgScreenshotImportArchiveJobSpecification spec = new PkgScreenshotImportArchiveJobSpecification();
spec.setInputDataGuid(jobService.storeSuppliedData("sample-pkgscreenshotimportarchive-supplied.tgz", MediaType.TAR.toString(), getResourceByteSource("sample-pkgscreenshotimportarchive-supplied.tgz")).getGuid());
spec.setImportStrategy(PkgScreenshotImportArchiveJobSpecification.ImportStrategy.REPLACE);
// run the job to import the data
// ------------------------------------
String jobGuid = jobService.immediate(spec, false);
// ------------------------------------
JobSnapshot snapshot = jobService.tryGetJob(jobGuid).get();
Assertions.assertThat(snapshot.getStatus()).isEqualTo(JobSnapshot.Status.FINISHED);
// check that the pkg1 is now loaded-up with screenshots from the tar-ball.
assertScreenshotHashes(new String[] { "741dcbf503ddf034db0c41cb0fc9e3f82b20a5b6", "cba98ed83e44c2b07710f78c95568a3b2e3a081e", "fd1616327841ee2be21a16e330c57daa613580fa" });
// check that the output report is as expected.
{
List<String> outputLines = getOutputLines(snapshot);
// "path","pkg-name","action","message","code"
// "","pkg1","REMOVED","","cc0bbb79-7cc1-45b2-83a0-c53b77d3d228"
// "","pkg1","REMOVED","","52433d05-9583-4419-9033-ea6e59b0e171"
// "hscr/pkg1/200.png","pkg1","ADDED","","b1731520-575a-4cca-8a31-afbfb561d184"
// "hscr/pkg1/201.png","pkg1","ADDED","","7a203288-1c9e-4fa6-b055-1be62b5db2fe"
// "hscr/pkg1/202.png","pkg1","INVALID",,""
// "hscr/pkg1/23.png","pkg1","PRESENT","","eb5e9cf5-ca10-4f43-80c4-3ea51cde5132"
// "hscr/notexists/3.png","notexists","NOTFOUND","",""
// compare actual generated with expected.
Assertions.assertThat(outputLines.size()).isEqualTo(8);
Assertions.assertThat(outputLines.get(1)).startsWith("\"\",\"pkg1\",\"REMOVED\",\"\",\"");
Assertions.assertThat(outputLines.get(2)).startsWith("\"\",\"pkg1\",\"REMOVED\",\"\",\"");
Assertions.assertThat(outputLines.get(3)).startsWith("\"hscr/pkg1/200.png\",\"pkg1\",\"ADDED\",\"\",\"");
Assertions.assertThat(outputLines.get(4)).startsWith("\"hscr/pkg1/201.png\",\"pkg1\",\"ADDED\",\"\",\"");
Assertions.assertThat(outputLines.get(5)).isEqualTo("\"hscr/pkg1/202.png\",\"pkg1\",\"INVALID\",,\"\"");
Assertions.assertThat(outputLines.get(6)).startsWith("\"hscr/pkg1/23.png\",\"pkg1\",\"PRESENT\",\"\",\"");
Assertions.assertThat(outputLines.get(7)).isEqualTo("\"hscr/notexists/3.png\",\"notexists\",\"NOTFOUND\",\"\",\"\"");
}
}
Aggregations