Search in sources :

Example 1 with CreateRepositorySourceRequest

use of org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceRequest in project haikudepotserver by haiku.

the class RepositoryApiImpl method createRepositorySource.

@Override
public CreateRepositorySourceResult createRepositorySource(CreateRepositorySourceRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the new repository source must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository for the new repository source must be identified");
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, request.repositoryCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    Optional<RepositorySource> existingRepositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
    if (existingRepositorySourceOptional.isPresent()) {
        throw new ValidationException(new ValidationFailure(RepositorySource.CODE.getName(), "unique"));
    }
    RepositorySource repositorySource = context.newObject(RepositorySource.class);
    repositorySource.setRepository(repository);
    repositorySource.setCode(request.code);
    repository.setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did create a new repository source '{}' on the repository '{}'", repositorySource, repository);
    return new CreateRepositorySourceResult();
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) CreateRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 2 with CreateRepositorySourceRequest

use of org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceRequest in project haikudepotserver by haiku.

the class RepositoryApiIT method testCreateRepositorySource.

@Test
public void testCreateRepositorySource() {
    integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    CreateRepositorySourceRequest request = new CreateRepositorySourceRequest();
    request.code = "testreposrcxx_xyz";
    request.repositoryCode = "testrepo";
    // ------------------------------------
    repositoryApi.createRepositorySource(request);
    // ------------------------------------
    {
        ObjectContext context = serverRuntime.newContext();
        Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, "testreposrcxx_xyz");
        Assertions.assertThat(repositorySourceOptional.get().getActive()).isTrue();
        Assertions.assertThat(repositorySourceOptional.get().getIdentifier()).isNull();
        Assertions.assertThat(repositorySourceOptional.get().getRepository().getCode()).isEqualTo("testrepo");
        Assertions.assertThat(repositorySourceOptional.get().getCode()).isEqualTo("testreposrcxx_xyz");
    }
}
Also used : Optional(java.util.Optional) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositorySourceRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)2 Optional (java.util.Optional)1 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)1 CreateRepositorySourceRequest (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceRequest)1 CreateRepositorySourceResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult)1 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)1 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)1 Repository (org.haiku.haikudepotserver.dataobjects.Repository)1 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)1 Test (org.junit.jupiter.api.Test)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1