Search in sources :

Example 1 with RepositorySourceMirror

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

the class RepositoryApiImpl method createRepositorySourceMirror.

@Override
public CreateRepositorySourceMirrorResult createRepositorySourceMirror(CreateRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositorySourceCode), "the code for the new repository source mirror");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.countryCode), "the country code should be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.baseUrl), "the base url should be supplied");
    final ObjectContext context = serverRuntime.newContext();
    Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
    RepositorySource repositorySource = getRepositorySourceOrThrow(context, request.repositorySourceCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("the repository [" + repositorySource.getRepository() + "] is not able to be edited");
    }
    if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySource.getCode(), request.baseUrl).isPresent()) {
        LOGGER.info("attempt to add a repository source mirror for a url [{}] that is " + " already in use", request.baseUrl);
        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
    }
    // if there is no other mirror then this should be the primary.
    RepositorySourceMirror mirror = context.newObject(RepositorySourceMirror.class);
    mirror.setIsPrimary(repositorySource.tryGetPrimaryMirror().isEmpty());
    mirror.setBaseUrl(request.baseUrl);
    mirror.setRepositorySource(repositorySource);
    mirror.setCountry(country);
    mirror.setDescription(StringUtils.trimToNull(request.description));
    mirror.setCode(UUID.randomUUID().toString());
    repositorySource.getRepository().setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did add mirror [{}] to repository source [{}]", country.getCode(), repositorySource.getCode());
    CreateRepositorySourceMirrorResult result = new CreateRepositorySourceMirrorResult();
    result.code = mirror.getCode();
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) Country(org.haiku.haikudepotserver.dataobjects.Country) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 2 with RepositorySourceMirror

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

the class RepositoryApiImpl method getRepositorySourceMirror.

@Override
public GetRepositorySourceMirrorResult getRepositorySourceMirror(GetRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be provided");
    Preconditions.checkArgument(StringUtils.isNotBlank(request.code), "a mirror code must be provided");
    final ObjectContext context = serverRuntime.newContext();
    RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
    GetRepositorySourceMirrorResult result = new GetRepositorySourceMirrorResult();
    result.active = repositorySourceMirror.getActive();
    result.baseUrl = repositorySourceMirror.getBaseUrl();
    result.code = repositorySourceMirror.getCode();
    result.countryCode = repositorySourceMirror.getCountry().getCode();
    result.createTimestamp = repositorySourceMirror.getCreateTimestamp().getTime();
    result.modifyTimestamp = repositorySourceMirror.getModifyTimestamp().getTime();
    result.description = repositorySourceMirror.getDescription();
    result.isPrimary = repositorySourceMirror.getIsPrimary();
    result.repositorySourceCode = repositorySourceMirror.getRepositorySource().getCode();
    return result;
}
Also used : GetRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceMirrorResult) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 3 with RepositorySourceMirror

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

the class RepositoryApiIT method testCreateRepositorySourceMirror.

@Test
public void testCreateRepositorySourceMirror() {
    integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    CreateRepositorySourceMirrorRequest request = new CreateRepositorySourceMirrorRequest();
    request.baseUrl = "http://testtest.haiku-os.org";
    request.countryCode = "DE";
    request.description = "Landkarte";
    request.repositorySourceCode = "testreposrc_xyz";
    // ------------------------------------
    String code = repositoryApi.createRepositorySourceMirror(request).code;
    // ------------------------------------
    {
        ObjectContext context = serverRuntime.newContext();
        RepositorySourceMirror mirror = RepositorySourceMirror.tryGetByCode(context, code).get();
        Assertions.assertThat(mirror.getActive()).isTrue();
        Assertions.assertThat(mirror.getBaseUrl()).isEqualTo("http://testtest.haiku-os.org");
        Assertions.assertThat(mirror.getDescription()).isEqualTo("Landkarte");
    }
}
Also used : RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) CreateRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorRequest) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with RepositorySourceMirror

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

the class RepositoryApiIT method testUpdateRepositorySourceMirror.

@Test
public void testUpdateRepositorySourceMirror() {
    integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    UpdateRepositorySourceMirrorRequest request = new UpdateRepositorySourceMirrorRequest();
    request.code = "testreposrc_xyz_m_notpri";
    request.isPrimary = true;
    request.baseUrl = "http://www.example.com/changed";
    request.description = "Cheramoia";
    request.countryCode = "DE";
    request.filter = ImmutableList.of(UpdateRepositorySourceMirrorRequest.Filter.DESCRIPTION, UpdateRepositorySourceMirrorRequest.Filter.IS_PRIMARY, UpdateRepositorySourceMirrorRequest.Filter.BASE_URL, UpdateRepositorySourceMirrorRequest.Filter.COUNTRY);
    // ------------------------------------
    repositoryApi.updateRepositorySourceMirror(request);
    // ------------------------------------
    {
        ObjectContext context = serverRuntime.newContext();
        RepositorySourceMirror mirror = RepositorySourceMirror.tryGetByCode(context, "testreposrc_xyz_m_notpri").get();
        Assertions.assertThat(mirror.getActive()).isTrue();
        Assertions.assertThat(mirror.getDescription()).isEqualTo("Cheramoia");
        Assertions.assertThat(mirror.getBaseUrl()).isEqualTo("http://www.example.com/changed");
        Assertions.assertThat(mirror.getDescription()).isEqualTo("Cheramoia");
        Assertions.assertThat(mirror.getIsPrimary()).isEqualTo(true);
    }
    {
        ObjectContext context = serverRuntime.newContext();
        RepositorySourceMirror mirror = RepositorySourceMirror.tryGetByCode(context, "testreposrc_xyz_m_pri").get();
        Assertions.assertThat(mirror.getIsPrimary()).isEqualTo(false);
    }
}
Also used : RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) UpdateRepositorySourceMirrorRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with RepositorySourceMirror

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

the class RepositoryHpkrIngressServiceIT method testImportThenCheck.

@Test
public void testImportThenCheck() throws Exception {
    File temporaryDir;
    File temporaryRepoFile = null;
    File temporaryRepoInfoFile = null;
    try {
        temporaryDir = Files.createTempDir();
        temporaryRepoFile = new File(temporaryDir, "repo");
        temporaryRepoInfoFile = new File(temporaryDir, "repo.info");
        // get the test hpkr data and copy it into a temporary file that can be used as a source
        // for a repository.
        Files.write(getResourceData("sample-repo.info"), temporaryRepoInfoFile);
        Files.write(getResourceData("sample-repo.hpkr"), temporaryRepoFile);
        // first setup a fake repository to import that points at the local test HPKR file.
        {
            ObjectContext context = serverRuntime.newContext();
            Repository repository = context.newObject(Repository.class);
            repository.setCode("test");
            repository.setName("Test Repository");
            RepositorySource repositorySource = context.newObject(RepositorySource.class);
            repositorySource.setCode("testsrc_xyz");
            repositorySource.setIdentifier("file://" + temporaryDir.getAbsolutePath());
            repository.addToManyTarget(Repository.REPOSITORY_SOURCES.getName(), repositorySource, true);
            RepositorySourceMirror repositorySourceMirror = context.newObject(RepositorySourceMirror.class);
            repositorySourceMirror.setBaseUrl("file://" + temporaryDir.getAbsolutePath());
            repositorySourceMirror.setIsPrimary(true);
            repositorySourceMirror.setCode("testsrc_xyz_mirror");
            repositorySourceMirror.setCountry(Country.getByCode(context, Country.CODE_NZ));
            repositorySource.addToManyTarget(RepositorySource.REPOSITORY_SOURCE_MIRRORS.getName(), repositorySourceMirror, true);
            context.commitChanges();
        }
        // setup another repository that is not related to the import test to check some stuff...
        {
            ObjectContext context = serverRuntime.newContext();
            Repository repository = context.newObject(Repository.class);
            repository.setCode("test2");
            repository.setName("Test 2");
            RepositorySource repositorySource = context.newObject(RepositorySource.class);
            repositorySource.setCode("testsrc2_xyz");
            // just after epoc second.
            repositorySource.setLastImportTimestamp(new java.sql.Timestamp(12345L));
            repository.addToManyTarget(Repository.REPOSITORY_SOURCES.getName(), repositorySource, true);
            RepositorySourceMirror repositorySourceMirror = context.newObject(RepositorySourceMirror.class);
            repositorySourceMirror.setBaseUrl("file://does-not-exist/path");
            repositorySourceMirror.setIsPrimary(true);
            repositorySourceMirror.setCode("testsrc2_xyz_mirror");
            repositorySourceMirror.setCountry(Country.getByCode(context, Country.CODE_NZ));
            repositorySource.addToManyTarget(RepositorySource.REPOSITORY_SOURCE_MIRRORS.getName(), repositorySourceMirror, true);
            context.commitChanges();
        }
        // add a package version from this repository that is known not to be in that example and then
        // latterly check that the package version is no longer active.
        {
            ObjectContext context = serverRuntime.newContext();
            Pkg pkg = integrationTestSupportService.createPkg(context, "taranaki");
            pkgService.ensurePkgProminence(context, pkg, Repository.tryGetByCode(context, "test").get());
            pkgService.ensurePkgProminence(context, pkg, Repository.tryGetByCode(context, "test2").get());
            // this one should get deactivated
            {
                PkgVersion pkgVersion = context.newObject(PkgVersion.class);
                pkgVersion.setPkg(pkg);
                pkgVersion.setMajor("1");
                pkgVersion.setMinor("2");
                pkgVersion.setArchitecture(Architecture.tryGetByCode(context, "x86_64").get());
                pkgVersion.setIsLatest(true);
                pkgVersion.setRepositorySource(RepositorySource.tryGetByCode(context, "testsrc_xyz").get());
            }
            // this one should remain
            {
                PkgVersion pkgVersion = context.newObject(PkgVersion.class);
                pkgVersion.setPkg(pkg);
                pkgVersion.setMajor("1");
                pkgVersion.setMinor("3");
                pkgVersion.setArchitecture(Architecture.tryGetByCode(context, "x86_64").get());
                pkgVersion.setIsLatest(true);
                pkgVersion.setRepositorySource(RepositorySource.tryGetByCode(context, "testsrc2_xyz").get());
            }
            context.commitChanges();
        }
        // add an inactive package version from this repository that is known to be in the repository.  This
        // package should be activated and re-used.
        ObjectId originalFfmpegPkgOid;
        {
            ObjectContext context = serverRuntime.newContext();
            Pkg pkg = integrationTestSupportService.createPkg(context, "ffmpeg");
            pkgService.ensurePkgProminence(context, pkg, Repository.tryGetByCode(context, "test").get());
            pkgService.ensurePkgProminence(context, pkg, Repository.tryGetByCode(context, "test2").get());
            PkgVersion pkgVersion = context.newObject(PkgVersion.class);
            pkgVersion.setPkg(pkg);
            pkgVersion.setMajor("3");
            pkgVersion.setMinor("3");
            pkgVersion.setMicro("2");
            pkgVersion.setRevision(1);
            pkgVersion.setArchitecture(Architecture.tryGetByCode(context, "x86_64").get());
            pkgVersion.setIsLatest(true);
            // to be sure!
            pkgVersion.setActive(false);
            pkgVersion.setRepositorySource(RepositorySource.tryGetByCode(context, "testsrc_xyz").get());
            PkgVersionUrl pkgVersionUrl = context.newObject(PkgVersionUrl.class);
            pkgVersionUrl.setPkgUrlType(PkgUrlType.getByCode(context, org.haiku.pkg.model.PkgUrlType.HOMEPAGE.name().toLowerCase()).get());
            pkgVersionUrl.setUrl("http://noop");
            pkgVersion.addToManyTarget(PkgVersion.PKG_VERSION_URLS.getName(), pkgVersionUrl, true);
            PkgVersionCopyright pkgVersionCopyright = context.newObject(PkgVersionCopyright.class);
            pkgVersionCopyright.setBody("Norfolk pine");
            pkgVersion.addToManyTarget(PkgVersion.PKG_VERSION_COPYRIGHTS.getName(), pkgVersionCopyright, true);
            PkgVersionLicense pkgVersionLicense = context.newObject(PkgVersionLicense.class);
            pkgVersionLicense.setBody("Punga");
            pkgVersion.addToManyTarget(PkgVersion.PKG_VERSION_LICENSES.getName(), pkgVersionLicense, true);
            context.commitChanges();
            originalFfmpegPkgOid = pkgVersion.getObjectId();
        }
        // do the import.
        String guid = jobService.submit(new RepositoryHpkrIngressJobSpecification("test"), JobSnapshot.COALESCE_STATUSES_NONE);
        // wait for it to finish.
        {
            long startMs = System.currentTimeMillis();
            while (Jobs.isQueuedOrStarted(jobService.tryGetJob(guid).get()) && (System.currentTimeMillis() - startMs) < DELAY_PROCESSSUBMITTEDTESTJOB) {
                Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
            }
            if (Jobs.isQueuedOrStarted(jobService.tryGetJob(guid).get())) {
                throw new IllegalStateException("test processing of the sample repo has taken > " + DELAY_PROCESSSUBMITTEDTESTJOB + "ms");
            }
        }
        // check that the sample url is loaded into the repository source.
        {
            ObjectContext context = serverRuntime.newContext();
            RepositorySource repositorySource = RepositorySource.tryGetByCode(context, "testsrc_xyz").get();
            Assertions.assertThat(repositorySource.getIdentifier()).isEqualTo("f0c086e5-e096-429c-b38d-57beabd764e9");
            // ^^ as defined in the repo info file.
            Assertions.assertThat(repositorySource.getArchitecture().getCode()).isEqualTo("x86_gcc2");
        // ^^ as defined in the repo info file.
        }
        // now pull out some known packages and make sure they are imported correctly.
        // TODO - this is a fairly simplistic test; do some more checks.
        {
            ObjectContext context = serverRuntime.newContext();
            verifyPackage(context, "apr");
            verifyPackage(context, "schroedinger");
            // this one is not in the import and so should be inactive afterwards.
            List<PkgVersion> pkgVersions = ObjectSelect.query(PkgVersion.class).where(PkgVersion.PKG.dot(Pkg.NAME).eq("taranaki")).select(context);
            Assertions.assertThat(pkgVersions.size()).isEqualTo(2);
            for (PkgVersion pkgVersion : pkgVersions) {
                boolean isTestRepository = pkgVersion.getRepositorySource().getRepository().getCode().equals("test");
                Assertions.assertThat(pkgVersion.getActive()).isEqualTo(!isTestRepository);
            }
            // check that the ffmpeg package was re-used and populated; as an example.
            {
                PkgVersion pkgVersion = PkgVersion.get(context, originalFfmpegPkgOid);
                Assertions.assertThat(pkgVersion.getActive()).isTrue();
                Assertions.assertThat(pkgVersion.getIsLatest()).isTrue();
                Assertions.assertThat(PkgVersion.getForPkg(context, pkgVersion.getPkg(), Repository.tryGetByCode(context, "test").get(), true).size()).isEqualTo(// include inactive
                1);
                PkgVersionLocalization localization = pkgVersion.getPkgVersionLocalization(NaturalLanguage.getByCode(context, NaturalLanguage.CODE_ENGLISH)).get();
                Assertions.assertThat(localization.getDescription().get()).startsWith("FFmpeg is a complete, cro");
                Assertions.assertThat(localization.getSummary().get()).startsWith("Audio and video rec");
                // the former rubbish copyright is removed
                List<String> copyrights = pkgVersion.getCopyrights();
                Assertions.assertThat(copyrights.size()).isEqualTo(2);
                Assertions.assertThat(ImmutableSet.copyOf(copyrights)).containsOnly("2000-2003 Fabrice Bellard", "2003-2017 the FFmpeg developers");
                // the former rubbish license is removed
                List<String> licenses = pkgVersion.getLicenses();
                Assertions.assertThat(licenses.size()).isEqualTo(2);
                Assertions.assertThat(ImmutableSet.copyOf(licenses)).containsOnly("GNU LGPL v2.1", "GNU GPL v2");
                Optional<PkgVersionUrl> pkgVersionUrlOptional = pkgVersion.getPkgVersionUrlForType(PkgUrlType.getByCode(context, org.haiku.pkg.model.PkgUrlType.HOMEPAGE.name().toLowerCase()).get());
                Assertions.assertThat(pkgVersionUrlOptional.isPresent()).isTrue();
                Assertions.assertThat(pkgVersionUrlOptional.get().getUrl()).isEqualTo("https://ffmpeg.org/");
            }
        }
    } finally {
        if (null != temporaryRepoFile) {
            if (!temporaryRepoFile.delete()) {
                LOGGER.warn("unable to delete the temporary 'repo' file");
            }
        }
        if (null != temporaryRepoInfoFile) {
            if (!temporaryRepoInfoFile.delete()) {
                LOGGER.warn("unable to delete the temporary 'repo.info' file");
            }
        }
    }
}
Also used : PkgVersionLocalization(org.haiku.haikudepotserver.dataobjects.PkgVersionLocalization) PkgVersionLicense(org.haiku.haikudepotserver.dataobjects.PkgVersionLicense) RepositoryHpkrIngressJobSpecification(org.haiku.haikudepotserver.repository.model.RepositoryHpkrIngressJobSpecification) ObjectId(org.apache.cayenne.ObjectId) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Repository(org.haiku.haikudepotserver.dataobjects.Repository) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) PkgVersionUrl(org.haiku.haikudepotserver.dataobjects.PkgVersionUrl) PkgVersionCopyright(org.haiku.haikudepotserver.dataobjects.PkgVersionCopyright) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) List(java.util.List) ObjectContext(org.apache.cayenne.ObjectContext) File(java.io.File) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)7 RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror)7 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)4 org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror)4 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)3 Test (org.junit.jupiter.api.Test)3 UpdateRepositorySourceMirrorRequest (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest)2 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)2 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)2 Country (org.haiku.haikudepotserver.dataobjects.Country)2 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 File (java.io.File)1 List (java.util.List)1 ObjectId (org.apache.cayenne.ObjectId)1 CreateRepositorySourceMirrorRequest (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorRequest)1 CreateRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult)1 GetRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceMirrorResult)1 RemoveRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.RemoveRepositorySourceMirrorResult)1 UpdateRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorResult)1