Search in sources :

Example 1 with CreateRepositoryRequest

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

the class RepositoryApiImpl method createRepository.

@Override
public CreateRepositoryResult createRepository(CreateRepositoryRequest createRepositoryRequest) {
    Preconditions.checkNotNull(createRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_ADD)) {
        throw new AccessDeniedException("unable to add a repository");
    }
    if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
        throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
    }
    // check to see if there is an existing repository with the same code; non-unique.
    {
        Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, createRepositoryRequest.code);
        if (repositoryOptional.isPresent()) {
            throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "unique"));
        }
    }
    Repository repository = context.newObject(Repository.class);
    repository.setCode(createRepositoryRequest.code);
    repository.setName(createRepositoryRequest.name);
    repository.setInformationUrl(createRepositoryRequest.informationUrl);
    context.commitChanges();
    return new CreateRepositoryResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) Optional(java.util.Optional) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 2 with CreateRepositoryRequest

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

the class RepositoryApiIT method testCreateRepository_ok.

@Test
public void testCreateRepository_ok() {
    setAuthenticatedUserToRoot();
    CreateRepositoryRequest request = new CreateRepositoryRequest();
    request.code = "differentrepo";
    request.name = "Different Repo";
    request.informationUrl = "http://zink.haiku.org";
    // ------------------------------------
    repositoryApi.createRepository(request);
    // ------------------------------------
    ObjectContext context = serverRuntime.newContext();
    Optional<Repository> repositoryAfterOptional = Repository.tryGetByCode(context, "differentrepo");
    Assertions.assertThat(repositoryAfterOptional.get().getActive()).isTrue();
    Assertions.assertThat(repositoryAfterOptional.get().getCode()).isEqualTo("differentrepo");
    Assertions.assertThat(repositoryAfterOptional.get().getName()).isEqualTo("Different Repo");
    Assertions.assertThat(repositoryAfterOptional.get().getInformationUrl()).isEqualTo("http://zink.haiku.org");
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with CreateRepositoryRequest

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

the class RepositoryApiIT method testCreateRepository_codeNotUnique.

@Test
public void testCreateRepository_codeNotUnique() {
    IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    CreateRepositoryRequest request = new CreateRepositoryRequest();
    request.code = data.repository.getCode();
    request.informationUrl = "http://zink.haiku.org";
    try {
        // ------------------------------------
        repositoryApi.createRepository(request);
        // ------------------------------------
        org.junit.jupiter.api.Assertions.fail("the repository should not have been able to be created against an already existing repository code");
    } catch (ValidationException ve) {
        Assertions.assertThat(ve.getValidationFailures().size()).isEqualTo(1);
        Assertions.assertThat(ve.getValidationFailures().get(0).getMessage()).isEqualTo("unique");
        Assertions.assertThat(ve.getValidationFailures().get(0).getProperty()).isEqualTo(Repository.CODE.getName());
    }
}
Also used : ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) CreateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryRequest) IntegrationTestSupportService(org.haiku.haikudepotserver.IntegrationTestSupportService) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

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