Wire local sonar and extract method for findById

This commit is contained in:
Alex Morel 2024-06-17 15:27:14 +02:00
parent 154bb34980
commit 3ef6c81e98
3 changed files with 15 additions and 3 deletions

View file

@ -1,6 +1,7 @@
plugins { plugins {
id 'java' id 'java'
id 'com.github.johnrengelman.shadow' version '8.1.1' id 'com.github.johnrengelman.shadow' version '8.1.1'
id "org.sonarqube" version "5.0.0.4638"
} }
group = 'sh.libre.scim' group = 'sh.libre.scim'

View file

@ -13,6 +13,12 @@ import sh.libre.scim.jpa.ScimResource;
import java.util.stream.Stream; import java.util.stream.Stream;
/**
* Abstract class for converting a Keycloack {@link RoleMapperModel} into a SCIM {@link ResourceNode}.
*
* @param <M> The Keycloack {@link RoleMapperModel} (e.g. GroupModel, UserModel)
* @param <S> the SCIM {@link ResourceNode} (e.g. Group, User)
*/
public abstract class Adapter<M extends RoleMapperModel, S extends ResourceNode> { public abstract class Adapter<M extends RoleMapperModel, S extends ResourceNode> {
protected final Logger logger; protected final Logger logger;

View file

@ -14,6 +14,7 @@ import io.github.resilience4j.retry.RetryConfig;
import io.github.resilience4j.retry.RetryRegistry; import io.github.resilience4j.retry.RetryRegistry;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException; import jakarta.persistence.NoResultException;
import jakarta.persistence.TypedQuery;
import jakarta.ws.rs.ProcessingException; import jakarta.ws.rs.ProcessingException;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.component.ComponentModel; import org.keycloak.component.ComponentModel;
@ -91,6 +92,10 @@ public class ScimClient implements AutoCloseable {
return new ScimClient(scimRequestBuilder, retryRegistry, session, model); return new ScimClient(scimRequestBuilder, retryRegistry, session, model);
} }
private static <M extends RoleMapperModel, S extends ResourceNode, A extends Adapter<M, S>> TypedQuery<ScimResource> findById(A adapter) {
return adapter.query("findById", adapter.getId());
}
protected EntityManager getEntityManager() { protected EntityManager getEntityManager() {
return session.getProvider(JpaConnectionProvider.class).getEntityManager(); return session.getProvider(JpaConnectionProvider.class).getEntityManager();
} }
@ -116,7 +121,7 @@ public class ScimClient implements AutoCloseable {
if (adapter.skip) if (adapter.skip)
return; return;
// If mapping exist then it was created by import so skip. // If mapping exist then it was created by import so skip.
if (!adapter.query("findById", adapter.getId()).getResultList().isEmpty()) { if (!findById(adapter).getResultList().isEmpty()) {
return; return;
} }
Retry retry = registry.retry("create-" + adapter.getId()); Retry retry = registry.retry("create-" + adapter.getId());
@ -148,7 +153,7 @@ public class ScimClient implements AutoCloseable {
adapter.apply(kcModel); adapter.apply(kcModel);
if (adapter.skip) if (adapter.skip)
return; return;
ScimResource resource = adapter.query("findById", adapter.getId()).getSingleResult(); ScimResource resource = findById(adapter).getSingleResult();
adapter.apply(resource); adapter.apply(resource);
Retry retry = registry.retry("replace-" + adapter.getId()); Retry retry = registry.retry("replace-" + adapter.getId());
ServerResponse<S> response = retry.executeSupplier(() -> { ServerResponse<S> response = retry.executeSupplier(() -> {
@ -178,7 +183,7 @@ public class ScimClient implements AutoCloseable {
adapter.setId(id); adapter.setId(id);
try { try {
ScimResource resource = adapter.query("findById", adapter.getId()).getSingleResult(); ScimResource resource = findById(adapter).getSingleResult();
adapter.apply(resource); adapter.apply(resource);
Retry retry = registry.retry("delete-" + id); Retry retry = registry.retry("delete-" + id);