Search in sources :

Example 1 with PkgIconImportArchiveJobSpecification

use of org.haiku.haikudepotserver.pkg.model.PkgIconImportArchiveJobSpecification 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)

Aggregations

ByteSource (com.google.common.io.ByteSource)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 ObjectContext (org.apache.cayenne.ObjectContext)1 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)1 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)1 JobDataWithByteSource (org.haiku.haikudepotserver.job.model.JobDataWithByteSource)1 JobSnapshot (org.haiku.haikudepotserver.job.model.JobSnapshot)1 PkgIconImportArchiveJobSpecification (org.haiku.haikudepotserver.pkg.model.PkgIconImportArchiveJobSpecification)1 Test (org.junit.jupiter.api.Test)1