Search in sources :

Example 11 with MapClientEntity

use of org.keycloak.models.map.client.MapClientEntity in project keycloak by keycloak.

the class DictTest method testDictClientFromMap.

@Test
public void testDictClientFromMap() {
    MapClientEntity mce = Dict.clientDelegate();
    assertThat(mce.getClientId(), nullValue());
    assertThat(mce.isEnabled(), nullValue());
    assertThat(mce.getAttribute("logo"), nullValue());
    assertThat(mce.getAttributes().keySet(), is(empty()));
    Dict.asDict(mce).put(Dict.CLIENT_FIELD_NAME, "name");
    Dict.asDict(mce).put(Dict.CLIENT_FIELD_ENABLED, false);
    Dict.asDict(mce).put(Dict.CLIENT_FIELD_LOGO, "thisShouldBeBase64Logo");
    Dict.asDict(mce).put("nonexistent", "nonexistent");
    assertThat(mce.getId(), is("name"));
    assertThat(mce.getClientId(), is("name"));
    assertThat(mce.isEnabled(), is(false));
    assertThat(mce.getAttribute("logo"), hasItems("thisShouldBeBase64Logo"));
    assertThat(mce.getAttributes().keySet(), hasItems("logo"));
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) Test(org.junit.Test)

Example 12 with MapClientEntity

use of org.keycloak.models.map.client.MapClientEntity in project keycloak by keycloak.

the class PerFieldDelegateProviderPrimarySourceTest method testGet_NoPrimarySource.

@Test
public void testGet_NoPrimarySource() {
    // When there is no primary source (exclusion) in the node properties, all properties are considered as owned by this entity.
    // Thus there is no call to the child entity creator.
    MapClientEntity ent = prepareEntityAndTreeNodeInstances();
    assertThat(lowerEntSupplierCallCount.get(), is(0));
    assertThat(ent.getProtocol(), is("upper-protocol"));
    assertThat(ent.getAttribute("attr1"), contains("upper-value-1"));
    assertThat(lowerEntSupplierCallCount.get(), is(0));
    assertThat(ent.getAttribute("attr2"), contains("upper-value-2"));
    assertThat(ent.getAttribute("attr3"), contains("upper-value-3"));
    assertThat(ent.getAttribute("attr4"), nullValue());
    assertThat(lowerEntSupplierCallCount.get(), is(0));
    assertThat(ent.getClientId(), is("upper-clientId-1"));
    assertThat(ent.getAttributes().keySet(), containsInAnyOrder("attr1", "attr2", "attr3"));
    assertThat(ent.getAttributes(), hasEntry("attr1", Arrays.asList("upper-value-1")));
    assertThat(ent.getAttributes(), hasEntry("attr2", Arrays.asList("upper-value-2")));
    assertThat(ent.getAttributes(), hasEntry("attr3", Arrays.asList("upper-value-3")));
    assertThat(lowerEntSupplierCallCount.get(), is(0));
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) Test(org.junit.Test)

Example 13 with MapClientEntity

use of org.keycloak.models.map.client.MapClientEntity in project keycloak by keycloak.

the class PerFieldDelegateProviderPrimarySourceTest method testSet_PrimarySourceForExcluded.

@Test
public void testSet_PrimarySourceForExcluded() {
    // 
    // High-level perspective: all but the listed fields are available in upper entity (e.g. JPA),
    // the listed ones are stored in the lower entity (e.g. LDAP)
    // 
    // When there is are primary source exclusion in the node properties, all properties apart from those enumerated
    // are considered as owned by this entity. Those enumerated are obtained from the child entity.
    // Thus there must be a single call to the child entity creator.
    upperPrimarySourceForExcluded.put(MapClientEntityFields.CLIENT_ID, null);
    upperPrimarySourceForExcluded.put(MapClientEntityFields.ATTRIBUTES, Arrays.asList("attr2", "attr3", "attr4"));
    upperNodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR_EXCLUDED, upperPrimarySourceForExcluded);
    // When there is primary source exclusion in the node properties, listed properties are considered as owned by the child
    // entity, and all other are considered as owned by this entity.
    // Thus there is no call to the child entity creator.
    MapClientEntity ent = prepareEntityAndTreeNodeInstances();
    // When
    // modification in: lower
    ent.setClientId("modified-client-id-1");
    // modification in: upper
    ent.setProtocol("modified-protocol");
    // modification in: lower
    ent.setAttribute("attr4", Arrays.asList("modified-value-4"));
    // modification in: upper
    ent.setAttribute("attrX", Arrays.asList("modified-value-X"));
    // modification in: upper
    ent.addRedirectUri("added-redirectUri");
    // modification in: upper
    ent.removeRedirectUri("upper-redirectUri-2");
    // modification in: upper
    ent.removeRedirectUri("lower-redirectUri-2");
    // Then
    assertThat(lowerEntSupplierCallCount.get(), is(1));
    assertThat(ent.getClientId(), is("modified-client-id-1"));
    assertThat(upperEnt.getClientId(), is("upper-clientId-1"));
    assertThat(lowerEnt.getClientId(), is("modified-client-id-1"));
    assertThat(ent.getRedirectUris(), containsInAnyOrder("upper-redirectUri-1", "added-redirectUri"));
    assertThat(upperEnt.getRedirectUris(), containsInAnyOrder("upper-redirectUri-1", "added-redirectUri"));
    assertThat(lowerEnt.getRedirectUris(), containsInAnyOrder("lower-redirectUri-1", "lower-redirectUri-2"));
    assertThat(ent.getProtocol(), is("modified-protocol"));
    assertThat(upperEnt.getProtocol(), is("modified-protocol"));
    assertThat(lowerEnt.getProtocol(), is("lower-protocol"));
    assertThat(ent.getAttribute("attr1"), contains("upper-value-1"));
    assertThat(upperEnt.getAttribute("attr1"), contains("upper-value-1"));
    assertThat(lowerEnt.getAttribute("attr1"), contains("lower-value-1"));
    assertThat(ent.getAttribute("attr2"), nullValue());
    assertThat(upperEnt.getAttribute("attr2"), contains("upper-value-2"));
    assertThat(lowerEnt.getAttribute("attr2"), nullValue());
    assertThat(ent.getAttribute("attr3"), contains("lower-value-3"));
    assertThat(upperEnt.getAttribute("attr3"), contains("upper-value-3"));
    assertThat(lowerEnt.getAttribute("attr3"), contains("lower-value-3"));
    assertThat(ent.getAttribute("attr4"), contains("modified-value-4"));
    assertThat(upperEnt.getAttribute("attr4"), nullValue());
    assertThat(lowerEnt.getAttribute("attr4"), contains("modified-value-4"));
    assertThat(ent.getAttribute("attrX"), contains("modified-value-X"));
    assertThat(upperEnt.getAttribute("attrX"), contains("modified-value-X"));
    assertThat(lowerEnt.getAttribute("attrX"), nullValue());
    assertThat(ent.getAttributes().keySet(), containsInAnyOrder(// From upper
    "attr1", // From lower
    "attr3", // From lower
    "attr4", // From lower
    "attrX"));
    assertThat(upperEnt.getAttributes().keySet(), containsInAnyOrder("attr1", "attr2", "attr3", "attrX"));
    assertThat(lowerEnt.getAttributes().keySet(), containsInAnyOrder("attr1", "attr3", "attr4"));
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) Test(org.junit.Test)

Example 14 with MapClientEntity

use of org.keycloak.models.map.client.MapClientEntity in project keycloak by keycloak.

the class PerFieldDelegateProviderPrimarySourceTest method prepareEntityAndTreeNodeInstances.

private MapClientEntity prepareEntityAndTreeNodeInstances() {
    TreeStorageNodePrescription upperTsnp = new TreeStorageNodePrescription(upperNodeProperties, null, null);
    TreeStorageNodePrescription lowerTsnp = new TreeStorageNodePrescription(lowerNodeProperties, null, null);
    upperTsni = new TreeStorageNodeInstance<>(null, upperTsnp);
    lowerTsni = new TreeStorageNodeInstance<>(null, lowerTsnp);
    PerFieldDelegateProvider<MapClientEntity> fieldProvider = new PerFieldDelegateProvider<>(upperTsni.new WithEntity(upperEnt), () -> {
        lowerEntSupplierCallCount.incrementAndGet();
        return lowerEnt;
    });
    return DeepCloner.DUMB_CLONER.entityFieldDelegate(MapClientEntity.class, fieldProvider);
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) TreeStorageNodePrescription(org.keycloak.models.map.storage.tree.TreeStorageNodePrescription)

Example 15 with MapClientEntity

use of org.keycloak.models.map.client.MapClientEntity in project keycloak by keycloak.

the class PerFieldDelegateProviderCacheTest method testGet_CacheFor.

@Test
public void testGet_CacheFor() {
    // 
    // High-level perspective: cache for listed fields, the primary source for values is in the lower entity.
    // 
    // Upper node is not a primary source for any fields, and caches only the enumerated fields
    // Lower node is a primary source for all fields
    // There is an (intentional) discrepancy between the field values in lowerEnt and upperEnt to be able to distinguish
    // which entity the return value was obtained from.
    upperCacheFor.put(MapClientEntityFields.CLIENT_ID, null);
    upperCacheFor.put(MapClientEntityFields.ATTRIBUTES, Arrays.asList("attr2", "attr3", "attr4"));
    upperNodeProperties.put(NodeProperties.CACHE_FOR, upperCacheFor);
    // set the upper object to be exclusively a cache - none of the fields is primary source
    upperNodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR, Collections.emptyMap());
    MapClientEntity ent = prepareEntityAndTreeNodeInstances();
    assertThat(lowerEntSupplierCallCount.get(), is(0));
    assertThat(ent.getClientId(), is("upper-clientId-1"));
    assertThat(ent.getAttribute("attr2"), contains("upper-value-2"));
    assertThat(ent.getAttribute("attr3"), contains("upper-value-3"));
    assertThat(ent.getAttribute("attr4"), nullValue());
    assertThat(lowerEntSupplierCallCount.get(), is(0));
    assertThat(ent.getProtocol(), is("lower-protocol"));
    assertThat(ent.getAttribute("attr1"), contains("lower-value-1"));
    assertThat(lowerEntSupplierCallCount.get(), is(1));
    assertThat(ent.getAttributes().keySet(), containsInAnyOrder("attr1", "attr2", "attr3"));
    assertThat(ent.getAttributes(), hasEntry("attr1", Arrays.asList("lower-value-1")));
    assertThat(ent.getAttributes(), hasEntry("attr2", Arrays.asList("upper-value-2")));
    assertThat(ent.getAttributes(), hasEntry("attr3", Arrays.asList("upper-value-3")));
    assertThat(lowerEntSupplierCallCount.get(), is(1));
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) Test(org.junit.Test)

Aggregations

MapClientEntity (org.keycloak.models.map.client.MapClientEntity)15 Test (org.junit.Test)13 TreeStorageNodePrescription (org.keycloak.models.map.storage.tree.TreeStorageNodePrescription)2 ClientModel (org.keycloak.models.ClientModel)1 MapClientEntityImpl (org.keycloak.models.map.client.MapClientEntityImpl)1 MapStorageProvider (org.keycloak.models.map.storage.MapStorageProvider)1 ConcurrentHashMapStorage (org.keycloak.models.map.storage.chm.ConcurrentHashMapStorage)1