Skip to content

Commit

Permalink
Merge pull request #42 from aservo/bugfix/false-unsupported-error
Browse files Browse the repository at this point in the history
Fix bug of wrong unsupported directory bean error, improve handling
  • Loading branch information
pathob authored Nov 3, 2023
2 parents f25d47d + 3da34e0 commit e2fea16
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -32,6 +33,15 @@ public class DirectoryBeanUtil {

public static final String ATTRIBUTE_USE_NESTED_GROUPS = "useNestedGroups";

private static final Set<Class<? extends AbstractDirectoryBean>> SUPPORTED_DIRECTORY_BEAN_TYPES;

static {
final Set<Class<? extends AbstractDirectoryBean>> supportedDirectoryBeanTypes = new HashSet<>();
supportedDirectoryBeanTypes.add(DirectoryInternalBean.class);
supportedDirectoryBeanTypes.add(DirectoryDelegatingBean.class);
SUPPORTED_DIRECTORY_BEAN_TYPES = Collections.unmodifiableSet(supportedDirectoryBeanTypes);
}

/*
* Methods for converting directories to directory beans.
*/
Expand Down Expand Up @@ -154,7 +164,7 @@ private static DirectoryGenericBean toDirectoryGenericBean(
*/
@Nonnull
public static Directory toDirectory(
@Nonnull final AbstractDirectoryBean directoryBean) {
@Nonnull final AbstractDirectoryBean directoryBean) throws UnsupportedDirectoryBeanException {

final ImmutableDirectory.Builder directoryBuilder = ImmutableDirectory.builder(
directoryBean.getName(), toDirectoryType(directoryBean), toDirectoryImplClass(directoryBean));
Expand All @@ -171,7 +181,11 @@ public static Directory toDirectory(
@Nonnull
public static Directory toDirectory(
@Nonnull final AbstractDirectoryBean directoryBean,
@Nonnull final Directory directory) {
@Nonnull final Directory directory) throws UnsupportedDirectoryBeanException {

if (!SUPPORTED_DIRECTORY_BEAN_TYPES.contains(directoryBean.getClass())) {
throw new UnsupportedDirectoryBeanException(directoryBean.getClass());
}

final ImmutableDirectory.Builder directoryBuilder = ImmutableDirectory.builder(directory);

Expand Down Expand Up @@ -467,6 +481,12 @@ public static DirectoryDelegatingConnectorTypeImplClass fromImplClass(String imp
}
}

public static class UnsupportedDirectoryBeanException extends Exception {
public UnsupportedDirectoryBeanException(Class<? extends AbstractDirectoryBean> directoryBeanClass) {
super(directoryBeanClass.getName());
}
}

private DirectoryBeanUtil() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import de.aservo.confapi.commons.exception.ServiceUnavailableException;
import de.aservo.confapi.commons.model.AbstractDirectoryBean;
import de.aservo.confapi.commons.model.DirectoriesBean;
import de.aservo.confapi.commons.model.DirectoryInternalBean;
import de.aservo.confapi.commons.service.api.DirectoriesService;
import de.aservo.confapi.crowd.model.util.DirectoryBeanUtil;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -89,17 +88,15 @@ public AbstractDirectoryBean setDirectory(
@NotNull final AbstractDirectoryBean directoryBean,
final boolean testConnection) {

if (!(directoryBean instanceof DirectoryInternalBean)) {
throw new BadRequestException(String.format(
"Setting directory type '%s' is not supported (yet)", directoryBean.getClass()));
}

final Directory existingDirectory = findDirectory(id);

try {
final Directory mergedDirectory = DirectoryBeanUtil.toDirectory(directoryBean, existingDirectory);
final Directory updatedDirectory = directoryManager.updateDirectory(mergedDirectory);
return DirectoryBeanUtil.toDirectoryInternalBean(updatedDirectory);
} catch (DirectoryBeanUtil.UnsupportedDirectoryBeanException e) {
throw new BadRequestException(String.format(
"Setting directory type '%s' is not supported (yet)", e.getMessage()));
} catch (DirectoryNotFoundException e) {
// this should not happen
throw new InternalServerErrorException(String.format(
Expand All @@ -112,15 +109,13 @@ public AbstractDirectoryBean addDirectory(
final @NotNull AbstractDirectoryBean directoryBean,
final boolean testConnection) {

if (!(directoryBean instanceof DirectoryInternalBean)) {
throw new BadRequestException(String.format(
"Adding directory type '%s' is not supported (yet)", directoryBean.getClass()));
}

try {
final Directory directory = DirectoryBeanUtil.toDirectory(directoryBean);
final Directory addedDirectory = directoryManager.addDirectory(directory);
return DirectoryBeanUtil.toDirectoryBean(addedDirectory);
} catch (DirectoryBeanUtil.UnsupportedDirectoryBeanException e) {
throw new BadRequestException(String.format(
"Adding directory type '%s' is not supported (yet)", e.getMessage()));
} catch (DirectoryInstantiationException e) {
throw new InternalServerErrorException(String.format("Could not create directory '%s'", directoryBean.getName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import de.aservo.confapi.commons.model.DirectoryInternalBean;
import de.aservo.confapi.commons.model.type.DirectoryPermissions;
import de.aservo.confapi.crowd.model.util.DirectoryBeanUtil.DirectoryDelegatingConnectorTypeImplClass;
import de.aservo.confapi.crowd.model.util.DirectoryBeanUtil.UnsupportedDirectoryBeanException;
import de.aservo.confapi.crowd.util.AssertUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void testDirectoryToDirectoryGenericBean() {
}

@Test
public void testDirectoryInternalBeanToDirectory() {
public void testDirectoryInternalBeanToDirectory() throws UnsupportedDirectoryBeanException {
final DirectoryInternalBean directoryInternalBean = DirectoryInternalBean.EXAMPLE_1;

final Directory directory = DirectoryBeanUtil.toDirectory(directoryInternalBean);
Expand All @@ -72,7 +73,7 @@ public void testDirectoryInternalBeanToDirectory() {
}

@Test
public void testDirectoryDelegatingBeanToDirectory() {
public void testDirectoryDelegatingBeanToDirectory() throws UnsupportedDirectoryBeanException {
final DirectoryDelegatingBean directoryDelegatingBean = DirectoryDelegatingBean.EXAMPLE_1;

final Directory directory = DirectoryBeanUtil.toDirectory(directoryDelegatingBean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void testSetDirectoriesSetExistingUnsupportedType() {
final Directory directoryAzureAd = getTestDirectoryAzureAd();
final DirectoriesServiceImpl spy = spy(directoriesService);
doReturn(List.of(directoryInternal, directoryAzureAd)).when(spy).findAllDirectories();
doReturn(directoryAzureAd).when(spy).findDirectory(directoryAzureAd.getId());

final AbstractDirectoryBean directoryBean = DirectoryBeanUtil.toDirectoryBean(directoryAzureAd);
final DirectoriesBean directoriesBean = new DirectoriesBean(Collections.singletonList(directoryBean));
Expand Down

0 comments on commit e2fea16

Please sign in to comment.