use of org.haiku.haikudepotserver.job.model.JobSnapshot in project haikudepotserver by haiku.
the class LocalJobServiceIT method testHappyDays.
/**
* <p>This will be a bit of an unstable test (non-repeatable) because it is
* going to drive jobs into the job service and see them all run correctly.
* It introduces some random delays.</p>
*/
@Test
public void testHappyDays() {
// -------------------------
List<String> guids = IntStream.of(1, 2, 3, 4).mapToObj((i) -> new TestNumberedLinesJobSpecification(3, 500L)).map((spec) -> jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_NONE)).collect(Collectors.toList());
String immediateGuid = jobService.immediate(new TestNumberedLinesJobSpecification(3, 500L), false);
// -------------------------
Stream.concat(guids.stream(), Stream.of(immediateGuid)).forEach((guid) -> {
jobService.awaitJobFinishedUninterruptibly(guid, TimeUnit.SECONDS.toMillis(15));
try {
Optional<? extends JobSnapshot> jobSnapshotOptional = jobService.tryGetJob(guid);
Assertions.assertThat(jobSnapshotOptional.isPresent()).isTrue();
Assertions.assertThat(jobSnapshotOptional.get().getStatus()).isEqualTo(JobSnapshot.Status.FINISHED);
Set<String> dataGuids = jobSnapshotOptional.get().getGeneratedDataGuids();
Assertions.assertThat(dataGuids.size()).isEqualTo(1);
String dataGuid = dataGuids.iterator().next();
Optional<JobDataWithByteSource> jobDataOptional = jobService.tryObtainData(dataGuid);
Assertions.assertThat(jobDataOptional.isPresent()).isTrue();
try (InputStream inputStream = jobDataOptional.get().getByteSource().openStream();
Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8)) {
Assertions.assertThat(CharStreams.toString(reader)).isEqualTo("0\n1\n2\n");
}
} catch (IOException ioe) {
new UncheckedIOException(ioe);
}
});
}
use of org.haiku.haikudepotserver.job.model.JobSnapshot in project haikudepotserver by haiku.
the class PkgIconImportArchiveJobRunnerIT method testImport.
/**
* <p>The package 'pkg2' has initially no icons associated with it, but one 16x16 icons is then added. The
* tar-ball is loaded and in doing so, two new icons are populated for 'pkg2', but the old 16x16 is not
* present as importing for a package will remove any previously present icon data.</p>
*/
@Test
public void testImport() throws Exception {
integrationTestSupportService.createStandardTestData();
// check that there are no icons stored for pkg2.
{
ObjectContext context = serverRuntime.newContext();
Assertions.assertThat(Pkg.getByName(context, "pkg2").getPkgSupplement().getPkgIcons()).hasSize(0);
}
try (InputStream iconInputStream = Resources.asByteSource(Resources.getResource("sample-16x16-2.png")).openStream()) {
ObjectContext context = serverRuntime.newContext();
pkgIconService.storePkgIconImage(iconInputStream, org.haiku.haikudepotserver.dataobjects.MediaType.tryGetByCode(context, org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_PNG).get(), // expected size along both axiis
16, context, Pkg.getByName(context, "pkg2").getPkgSupplement());
}
// now load in the data to the job's storage system.
PkgIconImportArchiveJobSpecification spec = new PkgIconImportArchiveJobSpecification();
spec.setInputDataGuid(jobService.storeSuppliedData("sample-pkgiconimportarchive-supplied.tgz", MediaType.TAR.toString(), getResourceByteSource("sample-pkgiconimportarchive-supplied.tgz")).getGuid());
// 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 pkg2 is now loaded-up with icons from the tar-ball.
{
ObjectContext context = serverRuntime.newContext();
Pkg pkg2 = Pkg.getByName(context, "pkg2");
Assertions.assertThat(pkg2.getPkgSupplement().getPkgIcons()).hasSize(2);
Assertions.assertThat(pkg2.getPkgSupplement().getPkgIcon(org.haiku.haikudepotserver.dataobjects.MediaType.getByCode(context, org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_HAIKUVECTORICONFILE), null).isPresent()).isTrue();
Assertions.assertThat(pkg2.getPkgSupplement().getPkgIcon(org.haiku.haikudepotserver.dataobjects.MediaType.getByCode(context, org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_PNG), 32).isPresent()).isTrue();
}
// check that the output report is as expected.
{
String dataGuid = snapshot.getGeneratedDataGuids().stream().collect(SingleCollector.single());
JobDataWithByteSource jobSource = jobService.tryObtainData(dataGuid).get();
ByteSource expectedByteSource = getResourceByteSource("sample-pkgiconimportarchive-generated.csv");
// write the report to the console in order to help with diagnosis
LOGGER.info("actual output;\n{}", jobSource.getByteSource().asCharSource(Charsets.UTF_8).read());
try (BufferedReader jobReader = jobSource.getByteSource().asCharSource(Charsets.UTF_8).openBufferedStream();
BufferedReader sampleReader = expectedByteSource.asCharSource(Charsets.UTF_8).openBufferedStream()) {
assertEqualsLineByLine(sampleReader, jobReader);
}
}
}
use of org.haiku.haikudepotserver.job.model.JobSnapshot 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.job.model.JobSnapshot 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\",\"\",\"\"");
}
}
use of org.haiku.haikudepotserver.job.model.JobSnapshot in project haikudepotserver by haiku.
the class JobApiImpl method getJob.
@Override
public GetJobResult getJob(GetJobRequest request) {
Preconditions.checkArgument(null != request);
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.guid));
final ObjectContext context = serverRuntime.newContext();
JobSnapshot job = jobService.tryGetJob(request.guid).orElseThrow(() -> new ObjectNotFoundException(JobSnapshot.class.getSimpleName(), request.guid));
if (Strings.isNullOrEmpty(job.getOwnerUserNickname())) {
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.JOBS_VIEW)) {
throw new AccessDeniedException("attempt to access jobs view");
}
} else {
User ownerUser = User.tryGetByNickname(context, job.getOwnerUserNickname()).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), job.getOwnerUserNickname()));
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
throw new AccessDeniedException("attempt to access jobs view for [" + job + "]");
}
}
// now output the result.
GetJobResult result = new GetJobResult();
result.guid = job.getGuid();
result.jobStatus = JobStatus.valueOf(job.getStatus().name());
result.jobTypeCode = job.getJobTypeCode();
result.ownerUserNickname = job.getOwnerUserNickname();
result.startTimestamp = null == job.getStartTimestamp() ? null : job.getStartTimestamp().getTime();
result.finishTimestamp = null == job.getFinishTimestamp() ? null : job.getFinishTimestamp().getTime();
result.queuedTimestamp = null == job.getQueuedTimestamp() ? null : job.getQueuedTimestamp().getTime();
result.failTimestamp = null == job.getFailTimestamp() ? null : job.getFailTimestamp().getTime();
result.cancelTimestamp = null == job.getCancelTimestamp() ? null : job.getCancelTimestamp().getTime();
result.progressPercent = job.getProgressPercent();
result.generatedDatas = new ArrayList<>();
for (String guid : job.getGeneratedDataGuids()) {
JobData jobData = jobService.tryGetData(guid).orElseThrow(() -> new ObjectNotFoundException(JobData.class.getSimpleName(), guid));
GetJobResult.JobData resultJobData = new GetJobResult.JobData();
resultJobData.useCode = jobData.getUseCode();
resultJobData.guid = jobData.getGuid();
resultJobData.mediaTypeCode = jobData.getMediaTypeCode();
resultJobData.filename = jobService.deriveDataFilename(guid);
result.generatedDatas.add(resultJobData);
}
return result;
}
Aggregations