use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest 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);
}
}
use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceMirrorRequest in project haikudepotserver by haiku.
the class RepositoryApiImpl method updateRepositorySourceMirror.
@Override
public UpdateRepositorySourceMirrorResult updateRepositorySourceMirror(UpdateRepositorySourceMirrorRequest request) {
Preconditions.checkArgument(null != request, "the request must be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the mirror to update");
final ObjectContext context = serverRuntime.newContext();
RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySourceMirror.getRepositorySource().getRepository(), Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("the repository [" + repositorySourceMirror.getRepositorySource().getRepository() + "] is unable to be edited");
}
for (UpdateRepositorySourceMirrorRequest.Filter filter : CollectionUtils.emptyIfNull(request.filter)) {
switch(filter) {
case ACTIVE:
if (repositorySourceMirror.getIsPrimary()) {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.ACTIVE.getName(), "confict"));
}
repositorySourceMirror.setActive(null != request.active && request.active);
break;
case BASE_URL:
if (StringUtils.isBlank(request.baseUrl)) {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "required"));
}
if (!repositorySourceMirror.getBaseUrl().equals(request.baseUrl)) {
if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySourceMirror.getRepositorySource().getCode(), request.baseUrl).isPresent()) {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
}
repositorySourceMirror.setBaseUrl(request.baseUrl);
}
break;
case COUNTRY:
Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
repositorySourceMirror.setCountry(country);
break;
case IS_PRIMARY:
boolean isPrimary = null != request.isPrimary && request.isPrimary;
if (isPrimary != repositorySourceMirror.getIsPrimary()) {
if (isPrimary) {
// in this case, the former primary should loose it's primary
// status so that it can be swapped to this one.
repositorySourceMirror.getRepositorySource().getPrimaryMirror().setIsPrimary(false);
repositorySourceMirror.setIsPrimary(true);
} else {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.IS_PRIMARY.getName(), "confict"));
}
}
break;
case DESCRIPTION:
repositorySourceMirror.setDescription(StringUtils.trimToNull(request.description));
break;
default:
throw new IllegalStateException("unknown change filter for mirror [" + filter + "]");
}
}
if (context.hasChanges()) {
repositorySourceMirror.getRepositorySource().getRepository().setModifyTimestamp();
}
context.commitChanges();
LOGGER.info("did update mirror [{}]", repositorySourceMirror.getCode());
return new UpdateRepositorySourceMirrorResult();
}
Aggregations