Search in sources :

Example 1 with GetPkgRequest

use of org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest in project haikudepotserver by haiku.

the class PkgApiIT method testGetPkg_notFound.

@Test
public void testGetPkg_notFound() {
    integrationTestSupportService.createStandardTestData();
    GetPkgRequest request = new GetPkgRequest();
    request.architectureCode = "x86_64";
    request.name = "pkg9";
    request.versionType = PkgVersionType.LATEST;
    request.naturalLanguageCode = NaturalLanguage.CODE_GERMAN;
    request.repositoryCode = "testrepo";
    try {
        // ------------------------------------
        pkgApi.getPkg(request);
        // ------------------------------------
        org.junit.jupiter.api.Assertions.fail("expected an instance of " + ObjectNotFoundException.class.getSimpleName() + " to be thrown, but was not");
    } catch (ObjectNotFoundException onfe) {
        Assertions.assertThat(onfe.getEntityName()).isEqualTo(Pkg.class.getSimpleName());
        Assertions.assertThat(onfe.getIdentifier()).isEqualTo("pkg9");
    } catch (Throwable th) {
        org.junit.jupiter.api.Assertions.fail("expected an instance of " + ObjectNotFoundException.class.getSimpleName() + " to be thrown, but " + th.getClass().getSimpleName() + " was instead");
    }
}
Also used : GetPkgRequest(org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with GetPkgRequest

use of org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest in project haikudepotserver by haiku.

the class PkgApiIT method testGetPkg_found_specific.

@Test
public void testGetPkg_found_specific() {
    integrationTestSupportService.createStandardTestData();
    GetPkgRequest request = new GetPkgRequest();
    request.architectureCode = "x86_64";
    request.name = "pkg1";
    request.repositoryCode = "testrepo";
    request.versionType = PkgVersionType.SPECIFIC;
    request.naturalLanguageCode = NaturalLanguage.CODE_ENGLISH;
    request.major = "1";
    request.micro = "2";
    request.minor = null;
    request.preRelease = null;
    request.revision = 4;
    // ------------------------------------
    GetPkgResult result = pkgApi.getPkg(request);
    // ------------------------------------
    Assertions.assertThat(result.name).isEqualTo("pkg1");
    Assertions.assertThat(result.versions.size()).isEqualTo(1);
    Assertions.assertThat(result.versions.get(0).title).isEqualTo("Package 1");
    Assertions.assertThat(result.versions.get(0).architectureCode).isEqualTo("x86_64");
    Assertions.assertThat(result.versions.get(0).major).isEqualTo("1");
    Assertions.assertThat(result.versions.get(0).micro).isEqualTo("2");
    Assertions.assertThat(result.versions.get(0).revision).isEqualTo(4);
    Assertions.assertThat(result.versions.get(0).description).isEqualTo("pkg1Version2DescriptionEnglish_rockmelon");
    Assertions.assertThat(result.versions.get(0).summary).isEqualTo("pkg1Version2SummaryEnglish_persimon");
}
Also used : GetPkgRequest(org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest) GetPkgResult(org.haiku.haikudepotserver.api1.model.pkg.GetPkgResult) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with GetPkgRequest

use of org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest in project haikudepotserver by haiku.

the class PkgApiIT method testGetPkg_found_latest.

/**
 * <p>In this test, an German localization is requested, but there is no localization present for German so it will
 * fall back English.</p>
 */
@Test
public void testGetPkg_found_latest() {
    integrationTestSupportService.createStandardTestData();
    GetPkgRequest request = new GetPkgRequest();
    request.architectureCode = "x86_64";
    request.name = "pkg1";
    request.repositoryCode = "testrepo";
    request.versionType = PkgVersionType.LATEST;
    request.naturalLanguageCode = NaturalLanguage.CODE_GERMAN;
    // ------------------------------------
    GetPkgResult result = pkgApi.getPkg(request);
    // ------------------------------------
    Assertions.assertThat(result.name).isEqualTo("pkg1");
    Assertions.assertThat(result.versions.size()).isEqualTo(1);
    Assertions.assertThat(result.versions.get(0).architectureCode).isEqualTo("x86_64");
    Assertions.assertThat(result.versions.get(0).major).isEqualTo("1");
    Assertions.assertThat(result.versions.get(0).micro).isEqualTo("2");
    Assertions.assertThat(result.versions.get(0).revision).isEqualTo(4);
    Assertions.assertThat(result.versions.get(0).description).isEqualTo("pkg1Version2DescriptionEnglish_rockmelon");
    Assertions.assertThat(result.versions.get(0).summary).isEqualTo("pkg1Version2SummaryEnglish_persimon");
}
Also used : GetPkgRequest(org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest) GetPkgResult(org.haiku.haikudepotserver.api1.model.pkg.GetPkgResult) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with GetPkgRequest

use of org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest in project haikudepotserver by haiku.

the class PkgApiImpl method getPkg.

@Override
public GetPkgResult getPkg(GetPkgRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.name), "request pkg name is required");
    Preconditions.checkNotNull(request.versionType);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.naturalLanguageCode));
    Preconditions.checkArgument(EnumSet.of(PkgVersionType.NONE, PkgVersionType.ALL).contains(request.versionType) || !Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied of the version request is not ALL or NONE");
    final ObjectContext context = serverRuntime.newContext();
    Optional<Architecture> architectureOptional = Optional.empty();
    if (!Strings.isNullOrEmpty(request.architectureCode)) {
        architectureOptional = Architecture.tryGetByCode(context, request.architectureCode);
    }
    Pkg pkg = getPkg(context, request.name);
    Repository repository = null;
    if (!Strings.isNullOrEmpty(request.repositoryCode)) {
        repository = getRepository(context, request.repositoryCode);
    }
    final NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
    GetPkgResult result = new GetPkgResult();
    result.name = pkg.getName();
    result.modifyTimestamp = pkg.getModifyTimestamp().getTime();
    result.vanityLinkUrl = pkgService.createVanityLinkUrl(pkg);
    result.hasChangelog = pkg.getPkgSupplement().getPkgChangelog().isPresent();
    result.pkgCategoryCodes = pkg.getPkgSupplement().getPkgPkgCategories().stream().map(ppc -> ppc.getPkgCategory().getCode()).collect(Collectors.toList());
    if (null != repository) {
        Optional<PkgUserRatingAggregate> userRatingAggregate = pkg.getPkgUserRatingAggregate(repository);
        if (userRatingAggregate.isPresent()) {
            result.derivedRating = userRatingAggregate.get().getDerivedRating();
            result.derivedRatingSampleSize = userRatingAggregate.get().getDerivedRatingSampleSize();
        }
    }
    if (null != repository) {
        result.prominenceOrdering = pkg.getPkgProminence(repository).map(pp -> pp.getProminence().getOrdering()).orElse(null);
    }
    switch(request.versionType) {
        case ALL:
            {
                List<PkgVersion> allVersions;
                if (null == repository) {
                    // active only
                    allVersions = PkgVersion.getForPkg(context, pkg, false);
                } else {
                    // active only
                    allVersions = PkgVersion.getForPkg(context, pkg, repository, false);
                }
                if (architectureOptional.isPresent()) {
                    final Architecture a = architectureOptional.get();
                    allVersions = allVersions.stream().filter(v -> v.getArchitecture().equals(a)).collect(Collectors.toList());
                }
                // now sort those.
                final VersionCoordinatesComparator vcc = new VersionCoordinatesComparator();
                Collections.sort(allVersions, (pv1, pv2) -> ComparisonChain.start().compare(pv1.getArchitecture().getCode(), pv2.getArchitecture().getCode()).compare(pv1.toVersionCoordinates(), pv2.toVersionCoordinates(), vcc).result());
                result.versions = allVersions.stream().map(v -> createGetPkgResultPkgVersion(context, v, naturalLanguage)).collect(Collectors.toList());
            }
            break;
        case SPECIFIC:
            {
                if (architectureOptional.isEmpty()) {
                    throw new IllegalStateException("the specified architecture was not able to be found; " + request.architectureCode);
                }
                VersionCoordinates coordinates = new VersionCoordinates(request.major, request.minor, request.micro, request.preRelease, request.revision);
                PkgVersion pkgVersion = PkgVersion.getForPkg(context, pkg, repository, architectureOptional.get(), coordinates).filter(_PkgVersion::getActive).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), ""));
                if (null != request.incrementViewCounter && request.incrementViewCounter) {
                    incrementCounter(pkgVersion);
                }
                result.versions = Collections.singletonList(createGetPkgResultPkgVersion(context, pkgVersion, naturalLanguage));
            }
            break;
        case LATEST:
            {
                if (architectureOptional.isEmpty()) {
                    throw new IllegalStateException("the specified architecture was not able to be found; " + request.architectureCode);
                }
                PkgVersion pkgVersion = pkgService.getLatestPkgVersionForPkg(context, pkg, repository, ImmutableList.of(architectureOptional.get(), Architecture.tryGetByCode(context, Architecture.CODE_ANY).get(), Architecture.tryGetByCode(context, Architecture.CODE_SOURCE).get())).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), request.name));
                if (null != request.incrementViewCounter && request.incrementViewCounter) {
                    incrementCounter(pkgVersion);
                }
                result.versions = Collections.singletonList(createGetPkgResultPkgVersion(context, pkgVersion, naturalLanguage));
            }
            break;
        case // no version is actually required.
        NONE:
            break;
        default:
            throw new IllegalStateException("unhandled version type in request");
    }
    return result;
}
Also used : PkgVersionUrl(org.haiku.haikudepotserver.api1.model.pkg.PkgVersionUrl) ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) Permission(org.haiku.haikudepotserver.security.model.Permission) org.haiku.haikudepotserver.support(org.haiku.haikudepotserver.support) PkgIcon(org.haiku.haikudepotserver.api1.model.pkg.PkgIcon) FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) PkgLocalization(org.haiku.haikudepotserver.dataobjects.PkgLocalization) BadPkgIconException(org.haiku.haikudepotserver.api1.support.BadPkgIconException) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) PkgVersionLocalization(org.haiku.haikudepotserver.dataobjects.PkgVersionLocalization) org.haiku.haikudepotserver.pkg.model(org.haiku.haikudepotserver.pkg.model) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) org.haiku.haikudepotserver.api1.model.pkg(org.haiku.haikudepotserver.api1.model.pkg) Logger(org.slf4j.Logger) AutoJsonRpcServiceImpl(com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl) PermissionEvaluator(org.haiku.haikudepotserver.security.PermissionEvaluator) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) PkgVersionType(org.haiku.haikudepotserver.api1.model.PkgVersionType) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) org.haiku.haikudepotserver.dataobjects(org.haiku.haikudepotserver.dataobjects) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) Preconditions(com.google.common.base.Preconditions) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ImmutableList(com.google.common.collect.ImmutableList) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)3 GetPkgRequest (org.haiku.haikudepotserver.api1.model.pkg.GetPkgRequest)3 Test (org.junit.jupiter.api.Test)3 GetPkgResult (org.haiku.haikudepotserver.api1.model.pkg.GetPkgResult)2 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)2 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 ComparisonChain (com.google.common.collect.ComparisonChain)1 ImmutableList (com.google.common.collect.ImmutableList)1 AutoJsonRpcServiceImpl (com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)1