Search in sources :

Example 1 with JobSnapshot

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);
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) TestLockableJobSpecification(org.haiku.haikudepotserver.job.model.TestLockableJobSpecification) TestNumberedLinesJobSpecification(org.haiku.haikudepotserver.job.model.TestNumberedLinesJobSpecification) CharStreams(com.google.common.io.CharStreams) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) JobService(org.haiku.haikudepotserver.job.model.JobService) TestConfig(org.haiku.haikudepotserver.config.TestConfig) Charsets(com.google.common.base.Charsets) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Assertions(org.fest.assertions.Assertions) Resource(javax.annotation.Resource) Set(java.util.Set) IOException(java.io.IOException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Lock(java.util.concurrent.locks.Lock) Stream(java.util.stream.Stream) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Optional(java.util.Optional) InputStream(java.io.InputStream) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) TestNumberedLinesJobSpecification(org.haiku.haikudepotserver.job.model.TestNumberedLinesJobSpecification) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with JobSnapshot

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);
        }
    }
}
Also used : PkgIconImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgIconImportArchiveJobSpecification) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) ByteSource(com.google.common.io.ByteSource) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with JobSnapshot

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\",\"\",\"\"");
    }
}
Also used : PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) List(java.util.List) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with JobSnapshot

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\",\"\",\"\"");
    }
}
Also used : PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) List(java.util.List) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with JobSnapshot

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;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) ObjectContext(org.apache.cayenne.ObjectContext) JobData(org.haiku.haikudepotserver.job.model.JobData)

Aggregations

JobSnapshot (org.haiku.haikudepotserver.job.model.JobSnapshot)5 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)4 Test (org.junit.jupiter.api.Test)4 List (java.util.List)3 InputStream (java.io.InputStream)2 ObjectContext (org.apache.cayenne.ObjectContext)2 JobDataWithByteSource (org.haiku.haikudepotserver.job.model.JobDataWithByteSource)2 PkgScreenshotImportArchiveJobSpecification (org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification)2 Charsets (com.google.common.base.Charsets)1 ByteSource (com.google.common.io.ByteSource)1 CharStreams (com.google.common.io.CharStreams)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UncheckedIOException (java.io.UncheckedIOException)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 Lock (java.util.concurrent.locks.Lock)1