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();
}
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");
}
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());
}
}
Aggregations