Search in sources :

Example 1 with PkgSupplement

use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.

the class PkgIconController method handleHeadOrGetPkgIcon.

private void handleHeadOrGetPkgIcon(RequestMethod requestMethod, HttpServletResponse response, Integer size, String format, String pkgName, Boolean fallback) throws IOException {
    if (null == format) {
        throw new MissingOrBadFormat();
    }
    if (Strings.isNullOrEmpty(pkgName) || !Pkg.PATTERN_NAME.matcher(pkgName).matches()) {
        throw new MissingPkgName();
    }
    ObjectContext context = serverRuntime.newContext();
    // cached
    Optional<Pkg> pkg = Pkg.tryGetByName(context, pkgName);
    if (!pkg.isPresent()) {
        LOGGER.debug("request for icon for package '{}', but no such package was able to be found", pkgName);
        throw new PkgNotFound();
    }
    PkgSupplement pkgSupplement = pkg.get().getPkgSupplement();
    switch(format) {
        case org.haiku.haikudepotserver.dataobjects.MediaType.EXTENSION_HAIKUVECTORICONFILE:
            Optional<PkgIcon> hvifPkgIcon = pkg.get().getPkgSupplement().getPkgIcon(org.haiku.haikudepotserver.dataobjects.MediaType.getByExtension(context, format).get(), null);
            if (hvifPkgIcon.isPresent()) {
                byte[] data = hvifPkgIcon.get().getPkgIconImage().getData();
                response.setContentType(org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_HAIKUVECTORICONFILE);
                outputToResponse(response, pkgSupplement, data, requestMethod == RequestMethod.GET);
            } else {
                throw new PkgIconNotFound();
            }
            break;
        case org.haiku.haikudepotserver.dataobjects.MediaType.EXTENSION_PNG:
            if (null == size) {
                throw new IllegalArgumentException("the size must be provided when requesting a PNG");
            }
            size = normalizeSize(size);
            Optional<byte[]> pngImageData = renderedPkgIconRepository.render(size, context, pkg.get().getPkgSupplement());
            if (!pngImageData.isPresent()) {
                if ((null == fallback) || !fallback) {
                    throw new PkgIconNotFound();
                }
                handleGenericHeadOrGet(requestMethod, response, size, true);
            } else {
                byte[] data = pngImageData.get();
                response.setContentType(MediaType.PNG.toString());
                outputToResponse(response, pkgSupplement, data, requestMethod == RequestMethod.GET);
            }
            break;
        default:
            throw new IllegalStateException("unexpected format; " + format);
    }
}
Also used : Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) PkgIcon(org.haiku.haikudepotserver.dataobjects.PkgIcon) ObjectContext(org.apache.cayenne.ObjectContext)

Example 2 with PkgSupplement

use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.

the class PkgApiIT method testGetPkgScreenshots.

/**
 * <p>This test depends on the sample package pkg1 having some screenshots associated with it.</p>
 */
@Test
public void testGetPkgScreenshots() {
    IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
    // ------------------------------------
    GetPkgScreenshotsResult result = pkgApi.getPkgScreenshots(new GetPkgScreenshotsRequest(data.pkg1.getName()));
    // ------------------------------------
    PkgSupplement pkgSupplement = data.pkg1.getPkgSupplement();
    Assertions.assertThat(result.items.size()).isEqualTo(pkgSupplement.getPkgScreenshots().size());
    List<org.haiku.haikudepotserver.dataobjects.PkgScreenshot> sortedScreenshots = pkgSupplement.getSortedPkgScreenshots();
    Assertions.assertThat(sortedScreenshots).hasSize(3);
    int[] widths = { 320, 240, 320 };
    int[] heights = { 240, 320, 240 };
    int[] lengths = { 41296, 28303, 33201 };
    for (int i = 0; i < sortedScreenshots.size(); i++) {
        org.haiku.haikudepotserver.dataobjects.PkgScreenshot pkgScreenshot = sortedScreenshots.get(i);
        PkgScreenshot apiPkgScreenshot = result.items.get(i);
        Assertions.assertThat(pkgScreenshot.getCode()).isEqualTo(apiPkgScreenshot.code);
        Assertions.assertThat(pkgScreenshot.getWidth()).isEqualTo(apiPkgScreenshot.width);
        Assertions.assertThat(pkgScreenshot.getHeight()).isEqualTo(apiPkgScreenshot.height);
        Assertions.assertThat(pkgScreenshot.getLength()).isEqualTo(apiPkgScreenshot.length);
        Assertions.assertThat(apiPkgScreenshot.width).isEqualTo(widths[i]);
        Assertions.assertThat(apiPkgScreenshot.height).isEqualTo(heights[i]);
        Assertions.assertThat(apiPkgScreenshot.length).isEqualTo(lengths[i]);
    }
}
Also used : PkgScreenshot(org.haiku.haikudepotserver.api1.model.pkg.PkgScreenshot) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) GetPkgScreenshotsResult(org.haiku.haikudepotserver.api1.model.pkg.GetPkgScreenshotsResult) IntegrationTestSupportService(org.haiku.haikudepotserver.IntegrationTestSupportService) GetPkgScreenshotsRequest(org.haiku.haikudepotserver.api1.model.pkg.GetPkgScreenshotsRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with PkgSupplement

use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.

the class PkgApiIT method testConfigurePkgIcon_ok_bitmap.

/**
 * <p>This test will configure the icons for the package.</p>
 */
@Test
public void testConfigurePkgIcon_ok_bitmap() throws Exception {
    setAuthenticatedUserToRoot();
    integrationTestSupportService.createStandardTestData();
    byte[] sample16 = getResourceData("sample-16x16.png");
    byte[] sample32 = getResourceData("sample-32x32.png");
    byte[] sample64 = getResourceData("sample-64x64.png");
    ConfigurePkgIconRequest request = new ConfigurePkgIconRequest();
    request.pkgName = "pkg1";
    request.pkgIcons = ImmutableList.of(new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 16, Base64.getEncoder().encodeToString(sample16)), new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 32, Base64.getEncoder().encodeToString(sample32)), new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 64, Base64.getEncoder().encodeToString(sample64)));
    // ------------------------------------
    pkgApi.configurePkgIcon(request);
    // ------------------------------------
    {
        ObjectContext objectContext = serverRuntime.newContext();
        Pkg pkgAfter = Pkg.getByName(objectContext, "pkg1");
        PkgSupplement pkgSupplementAfter = pkgAfter.getPkgSupplement();
        org.haiku.haikudepotserver.dataobjects.MediaType mediaTypePng = org.haiku.haikudepotserver.dataobjects.MediaType.getByCode(objectContext, MediaType.PNG.toString());
        Assertions.assertThat(pkgSupplementAfter.getPkgIcons().size()).isEqualTo(3);
        Optional<PkgIcon> pkgIcon16Optional = pkgSupplementAfter.getPkgIcon(mediaTypePng, 16);
        Assertions.assertThat(pkgIcon16Optional.get().getPkgIconImage().getData()).isEqualTo(sample16);
        Optional<PkgIcon> pkgIcon32Optional = pkgSupplementAfter.getPkgIcon(mediaTypePng, 32);
        Assertions.assertThat(pkgIcon32Optional.get().getPkgIconImage().getData()).isEqualTo(sample32);
        Optional<PkgIcon> pkgIcon64Optional = pkgSupplementAfter.getPkgIcon(mediaTypePng, 64);
        Assertions.assertThat(pkgIcon64Optional.get().getPkgIconImage().getData()).isEqualTo(sample64);
    }
}
Also used : PkgIcon(org.haiku.haikudepotserver.dataobjects.PkgIcon) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Optional(java.util.Optional) ConfigurePkgIconRequest(org.haiku.haikudepotserver.api1.model.pkg.ConfigurePkgIconRequest) MediaType(com.google.common.net.MediaType) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with PkgSupplement

use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.

the class PkgApiIT method testUpdatePkgChangelog_withNoContent.

/**
 * <p>Writing in no content will mean that the change log that was there is now removed.</p>
 */
@Test
public void testUpdatePkgChangelog_withNoContent() {
    integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    UpdatePkgChangelogRequest request = new UpdatePkgChangelogRequest();
    request.pkgName = "pkg1";
    request.content = "";
    // ------------------------------------
    pkgApi.updatePkgChangelog(request);
    // ------------------------------------
    {
        ObjectContext context = serverRuntime.newContext();
        PkgSupplement pkgSupplementAfter = Pkg.getByName(context, "pkg1").getPkgSupplement();
        Assertions.assertThat(pkgSupplementAfter.getPkgChangelog().isPresent()).isFalse();
    }
}
Also used : UpdatePkgChangelogRequest(org.haiku.haikudepotserver.api1.model.pkg.UpdatePkgChangelogRequest) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with PkgSupplement

use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.

the class PkgApiIT method testReorderPkgScreenshots.

/**
 * <p>This test assumes that the test data has a pkg1 with three screenshots associated with it.</p>
 */
@Test
public void testReorderPkgScreenshots() {
    setAuthenticatedUserToRoot();
    IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
    List<org.haiku.haikudepotserver.dataobjects.PkgScreenshot> sortedScreenshotsBefore = data.pkg1.getPkgSupplement().getSortedPkgScreenshots();
    if (3 != sortedScreenshotsBefore.size()) {
        throw new IllegalStateException("the test requires that pkg1 has three screenshots associated with it");
    }
    // ------------------------------------
    pkgApi.reorderPkgScreenshots(new ReorderPkgScreenshotsRequest(data.pkg1.getName(), ImmutableList.of(sortedScreenshotsBefore.get(2).getCode(), sortedScreenshotsBefore.get(0).getCode())));
    // ------------------------------------
    ObjectContext context = serverRuntime.newContext();
    PkgSupplement pkgSupplement = Pkg.getByName(context, data.pkg1.getName()).getPkgSupplement();
    List<org.haiku.haikudepotserver.dataobjects.PkgScreenshot> sortedScreenshotsAfter = pkgSupplement.getSortedPkgScreenshots();
    Assertions.assertThat(sortedScreenshotsAfter.size()).isEqualTo(3);
    Assertions.assertThat(sortedScreenshotsAfter.get(0).getCode()).isEqualTo(sortedScreenshotsBefore.get(2).getCode());
    Assertions.assertThat(sortedScreenshotsAfter.get(1).getCode()).isEqualTo(sortedScreenshotsBefore.get(0).getCode());
    Assertions.assertThat(sortedScreenshotsAfter.get(2).getCode()).isEqualTo(sortedScreenshotsBefore.get(1).getCode());
}
Also used : PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) ReorderPkgScreenshotsRequest(org.haiku.haikudepotserver.api1.model.pkg.ReorderPkgScreenshotsRequest) PkgScreenshot(org.haiku.haikudepotserver.api1.model.pkg.PkgScreenshot) ObjectContext(org.apache.cayenne.ObjectContext) IntegrationTestSupportService(org.haiku.haikudepotserver.IntegrationTestSupportService) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

PkgSupplement (org.haiku.haikudepotserver.dataobjects.PkgSupplement)17 ObjectContext (org.apache.cayenne.ObjectContext)15 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)10 Test (org.junit.jupiter.api.Test)10 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)5 CSVWriter (com.opencsv.CSVWriter)4 OutputStream (java.io.OutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 IntegrationTestSupportService (org.haiku.haikudepotserver.IntegrationTestSupportService)4 PkgScreenshot (org.haiku.haikudepotserver.api1.model.pkg.PkgScreenshot)4 PkgIcon (org.haiku.haikudepotserver.dataobjects.PkgIcon)4 Repository (org.haiku.haikudepotserver.dataobjects.Repository)4 MediaType (com.google.common.net.MediaType)3 Optional (java.util.Optional)3 ConfigurePkgIconRequest (org.haiku.haikudepotserver.api1.model.pkg.ConfigurePkgIconRequest)3 UpdatePkgChangelogRequest (org.haiku.haikudepotserver.api1.model.pkg.UpdatePkgChangelogRequest)3 JobDataWithByteSink (org.haiku.haikudepotserver.job.model.JobDataWithByteSink)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RemovePkgIconRequest (org.haiku.haikudepotserver.api1.model.pkg.RemovePkgIconRequest)2