From cf485c3fc92c076031855e301003f65dc53ceca9 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Fri, 17 Nov 2017 09:45:57 +0100 Subject: [PATCH] KEYCLOAK-5308 Fix updating protocol mappers on Oracle --- .../models/utils/RepresentationToModel.java | 2 +- .../testsuite/admin/IdentityProviderTest.java | 6 +- .../admin/client/ClientTemplateTest.java | 55 +++++++++++++++++-- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/server-spi-private/src/main/java/org/keycloak/models/utils/RepresentationToModel.java b/server-spi-private/src/main/java/org/keycloak/models/utils/RepresentationToModel.java index 99faf6b8f1..1448c5b6ba 100755 --- a/server-spi-private/src/main/java/org/keycloak/models/utils/RepresentationToModel.java +++ b/server-spi-private/src/main/java/org/keycloak/models/utils/RepresentationToModel.java @@ -1680,7 +1680,7 @@ public class RepresentationToModel { model.setConsentText(rep.getConsentText()); model.setProtocol(rep.getProtocol()); model.setProtocolMapper(rep.getProtocolMapper()); - model.setConfig(rep.getConfig()); + model.setConfig(removeEmptyString(rep.getConfig())); return model; } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/IdentityProviderTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/IdentityProviderTest.java index fdf0c14b4f..2ec225c2cd 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/IdentityProviderTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/IdentityProviderTest.java @@ -455,12 +455,12 @@ public class IdentityProviderTest extends AbstractAdminTest { // KEYCLOAK-4962 @Test public void testUpdateProtocolMappers() { - create(createRep("google", "google")); + create(createRep("google2", "google")); - IdentityProviderResource provider = realm.identityProviders().get("google"); + IdentityProviderResource provider = realm.identityProviders().get("google2"); IdentityProviderMapperRepresentation mapper = new IdentityProviderMapperRepresentation(); - mapper.setIdentityProviderAlias("google"); + mapper.setIdentityProviderAlias("google2"); mapper.setName("my_mapper"); mapper.setIdentityProviderMapper("oidc-hardcoded-role-idp-mapper"); Map config = new HashMap<>(); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/client/ClientTemplateTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/client/ClientTemplateTest.java index df57326e4b..21ff67c3ba 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/client/ClientTemplateTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/client/ClientTemplateTest.java @@ -19,8 +19,8 @@ package org.keycloak.testsuite.admin.client; import org.junit.Assert; import org.junit.Test; - import org.keycloak.admin.client.resource.ClientTemplatesResource; +import org.keycloak.admin.client.resource.ProtocolMappersResource; import org.keycloak.admin.client.resource.RoleMappingResource; import org.keycloak.events.admin.OperationType; import org.keycloak.events.admin.ResourceType; @@ -32,23 +32,27 @@ import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientTemplateRepresentation; import org.keycloak.representations.idm.ErrorRepresentation; import org.keycloak.representations.idm.MappingsRepresentation; +import org.keycloak.representations.idm.ProtocolMapperRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.testsuite.admin.ApiUtil; import org.keycloak.testsuite.util.AdminEventPaths; import org.keycloak.testsuite.util.Matchers; import javax.ws.rs.BadRequestException; +import javax.ws.rs.ClientErrorException; import javax.ws.rs.NotFoundException; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; -import javax.ws.rs.*; -import javax.ws.rs.core.Response.Status; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; /** * @author Marek Posolda @@ -315,7 +319,6 @@ public class ClientTemplateTest extends AbstractClientTest { return testRealmResource().roles().get(roleName).toRepresentation(); } - // KEYCLOAK-2844 @Test public void testRemoveTemplateInUse() { @@ -350,6 +353,48 @@ public class ClientTemplateTest extends AbstractClientTest { removeTemplate(templateId); } + // KEYCLOAK-5863 + @Test + public void testUpdateProtocolMappers() { + ClientTemplateRepresentation templateRep = new ClientTemplateRepresentation(); + templateRep.setName("testUpdateProtocolMappers"); + templateRep.setProtocol("openid-connect"); + + + String templateId = createTemplate(templateRep); + + ProtocolMapperRepresentation mapper = new ProtocolMapperRepresentation(); + mapper.setName("test"); + mapper.setProtocol("openid-connect"); + mapper.setProtocolMapper("oidc-usermodel-attribute-mapper"); + + Map m = new HashMap<>(); + m.put("user.attribute", "test"); + m.put("claim.name", ""); + m.put("jsonType.label", ""); + + mapper.setConfig(m); + + ProtocolMappersResource protocolMappers = clientTemplates().get(templateId).getProtocolMappers(); + + Response response = protocolMappers.createMapper(mapper); + String mapperId = ApiUtil.getCreatedId(response); + + mapper = protocolMappers.getMapperById(mapperId); + + mapper.getConfig().put("claim.name", "claim"); + + protocolMappers.update(mapperId, mapper); + + List mappers = protocolMappers.getMappers(); + assertEquals(1, mappers.size()); + assertEquals(2, mappers.get(0).getConfig().size()); + assertEquals("test", mappers.get(0).getConfig().get("user.attribute")); + assertEquals("claim", mappers.get(0).getConfig().get("claim.name")); + + clientTemplates().get(templateId).remove(); + } + private ClientTemplatesResource clientTemplates() { return testRealmResource().clientTemplates();