use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceRequest in project haikudepotserver by haiku.
the class RepositoryApiIT method testUpdateRepositorySource.
@Test
public void testUpdateRepositorySource() {
integrationTestSupportService.createStandardTestData();
setAuthenticatedUserToRoot();
UpdateRepositorySourceRequest request = new UpdateRepositorySourceRequest();
request.code = "testreposrc_xyz";
request.active = false;
request.extraIdentifiers = List.of("birds:of:a:feather");
request.filter = List.of(UpdateRepositorySourceRequest.Filter.EXTRA_IDENTIFIERS, UpdateRepositorySourceRequest.Filter.ACTIVE);
// ------------------------------------
repositoryApi.updateRepositorySource(request);
// ------------------------------------
{
ObjectContext context = serverRuntime.newContext();
RepositorySource repositorySourceAfter = RepositorySource.tryGetByCode(context, "testreposrc_xyz").get();
// this url was set before and is retained after the update.
Assertions.assertThat(repositorySourceAfter.getIdentifier()).isEqualTo("http://www.example.com/test/identifier/url");
Assertions.assertThat(repositorySourceAfter.getActive()).isFalse();
Assertions.assertThat(repositorySourceAfter.getExtraIdentifiers()).contains("birds:of:a:feather");
}
}
use of org.haiku.haikudepotserver.api1.model.repository.UpdateRepositorySourceRequest in project haikudepotserver by haiku.
the class RepositoryApiImpl method updateRepositorySource.
@Override
public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourceRequest request) {
Preconditions.checkArgument(null != request);
Preconditions.checkArgument(StringUtils.isNotBlank(request.code), "a code is required to identify the repository source to update");
Preconditions.checkArgument(null != request.filter, "filters must be provided to specify what aspects of the repository source should be updated");
final ObjectContext context = serverRuntime.newContext();
RepositorySource repositorySource = getRepositorySourceOrThrow(context, request.code);
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("cannot edit the repository [" + repositorySource.getRepository() + "]");
}
for (UpdateRepositorySourceRequest.Filter filter : request.filter) {
switch(filter) {
case ACTIVE:
if (null == request.active) {
throw new IllegalArgumentException("the active field must be provided if the request requires it to be updated");
}
repositorySource.setActive(request.active);
LOGGER.info("did set the repository source {} active to {}", repositorySource, request.active);
break;
case FORCED_INTERNAL_BASE_URL:
repositorySource.setForcedInternalBaseUrl(StringUtils.trimToNull(request.forcedInternalBaseUrl));
LOGGER.info("did set the repository source forced internal base url");
break;
case EXTRA_IDENTIFIERS:
{
Set<String> existing = Set.copyOf(repositorySource.getExtraIdentifiers());
Set<String> desired = Set.copyOf(CollectionUtils.emptyIfNull(request.extraIdentifiers));
SetUtils.difference(existing, desired).stream().map(repositorySource::tryGetRepositorySourceExtraIdentifierForIdentifier).map(Optional::orElseThrow).forEach(rsei -> {
repositorySource.removeFromRepositorySourceExtraIdentifiers(rsei);
context.deleteObject(rsei);
});
SetUtils.difference(desired, existing).forEach(i -> {
RepositorySourceExtraIdentifier rsei = context.newObject(RepositorySourceExtraIdentifier.class);
rsei.setRepositorySource(repositorySource);
rsei.setIdentifier(i);
repositorySource.addToRepositorySourceExtraIdentifiers(rsei);
});
break;
}
default:
throw new IllegalStateException("unhandled filter; " + filter.name());
}
}
if (context.hasChanges()) {
repositorySource.getRepository().setModifyTimestamp();
context.commitChanges();
} else {
LOGGER.info("update repository source {} with no changes made", repositorySource);
}
return new UpdateRepositorySourceResult();
}
Aggregations