Search in sources :

Example 1 with CreateRepositorySourceMirrorRequest

use of org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorRequest 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 CreateRepositorySourceMirrorRequest

use of org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorRequest 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)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)2 RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror)2 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)1 CreateRepositorySourceMirrorRequest (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorRequest)1 CreateRepositorySourceMirrorResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult)1 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)1 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)1 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)1 Country (org.haiku.haikudepotserver.dataobjects.Country)1 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)1 org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror)1 Test (org.junit.jupiter.api.Test)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1