Search in sources :

Example 1 with MapClientEntity

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

the class PerFieldDelegateProviderCacheTest method testGet_CacheForExcluded.

@Test
public void testGet_CacheForExcluded() {
    // 
    // High-level perspective: listed fields exclusively available in the lower layer (e.g. LDAP),
    // never stored nor cached in the top layer (e.g. database).
    // All other fields are stored in the top layer.
    // 
    // Upper node is a primary source for all fields apart from cached ones. It only 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.
    upperCacheForExcluded.put(MapClientEntityFields.CLIENT_ID, null);
    upperCacheForExcluded.put(MapClientEntityFields.ATTRIBUTES, Arrays.asList("attr2", "attr3", "attr4"));
    upperNodeProperties.put(NodeProperties.CACHE_FOR_EXCLUDED, upperCacheForExcluded);
    upperNodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR_EXCLUDED, upperCacheForExcluded);
    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"), nullValue());
    assertThat(lowerEntSupplierCallCount.get(), is(1));
    assertThat(ent.getAttribute("attr3"), contains("lower-value-3"));
    assertThat(ent.getAttribute("attr4"), contains("lower-value-4"));
    assertThat(ent.getClientId(), is("lower-clientId-1"));
    assertThat(ent.getAttributes().keySet(), containsInAnyOrder("attr1", "attr3", "attr4"));
    assertThat(ent.getAttributes(), hasEntry("attr1", Arrays.asList("upper-value-1")));
    assertThat(ent.getAttributes(), hasEntry("attr3", Arrays.asList("lower-value-3")));
    assertThat(ent.getAttributes(), hasEntry("attr4", Arrays.asList("lower-value-4")));
    assertThat(lowerEntSupplierCallCount.get(), is(1));
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) Test(org.junit.Test)

Example 2 with MapClientEntity

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

the class PerFieldDelegateProviderCacheTest 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 3 with MapClientEntity

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

the class PerFieldDelegateProviderCacheTest method testSet_CacheFor.

@Test
public void testSet_CacheFor() {
    // 
    // High-level perspective: fields available in the lower layer (e.g. LDAP) cached in a faster layer (e.g. database)
    // 
    // Upper node is a primary source for all fields apart from cached ones. It only 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.PROTOCOL, null);
    upperCacheFor.put(MapClientEntityFields.ATTRIBUTES, Arrays.asList("attr2", "attr3", "attr4"));
    upperNodeProperties.put(NodeProperties.CACHE_FOR, upperCacheFor);
    upperNodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR_EXCLUDED, upperCacheFor);
    // When there is primary source in the node properties, named properties are considered as owned by this entity, and
    // all other are considered as owned by child entity.
    // Thus there is no call to the child entity creator.
    MapClientEntity ent = prepareEntityAndTreeNodeInstances();
    // When
    // modification in: upper and lower
    ent.setProtocol("modified-protocol");
    // modification in: upper
    ent.setAttribute("attr1", Arrays.asList("modified-value-1"));
    // 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("upper-clientId-1"));
    assertThat(upperEnt.getClientId(), is("upper-clientId-1"));
    assertThat(lowerEnt.getClientId(), is("lower-clientId-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("modified-protocol"));
    assertThat(ent.getAttribute("attr1"), contains("modified-value-1"));
    assertThat(upperEnt.getAttribute("attr1"), contains("modified-value-1"));
    assertThat(lowerEnt.getAttribute("attr1"), contains("lower-value-1"));
    assertThat(ent.getAttribute("attr3"), contains("upper-value-3"));
    assertThat(upperEnt.getAttribute("attr3"), contains("upper-value-3"));
    assertThat(lowerEnt.getAttribute("attr3"), contains("lower-value-3"));
    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 upper, since it is a "cached" value (deliberately a stale cache here, it is not in lower)
    "attr2", // From upper
    "attr3", // From upper
    "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 4 with MapClientEntity

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

the class PerFieldDelegateProviderCacheTest method testSet_CacheForExcluded.

@Test
public void testSet_CacheForExcluded() {
    // 
    // High-level perspective: listed fields exclusively available in the lower layer (e.g. LDAP),
    // never stored nor cached in the top layer (e.g. database).
    // All other fields are stored in the top layer.
    // 
    // 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.
    upperCacheForExcluded.put(MapClientEntityFields.CLIENT_ID, null);
    upperCacheForExcluded.put(MapClientEntityFields.ATTRIBUTES, Arrays.asList("attr2", "attr3", "attr4"));
    upperNodeProperties.put(NodeProperties.CACHE_FOR_EXCLUDED, upperCacheForExcluded);
    upperNodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR_EXCLUDED, upperCacheForExcluded);
    // 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 upper
    "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 5 with MapClientEntity

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

the class PerFieldDelegateProviderPrimarySourceTest method testGet_PrimarySourceForExcluded.

@Test
public void testGet_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);
    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"), nullValue());
    assertThat(lowerEntSupplierCallCount.get(), is(1));
    assertThat(ent.getAttribute("attr3"), contains("lower-value-3"));
    assertThat(ent.getAttribute("attr4"), contains("lower-value-4"));
    assertThat(ent.getClientId(), is("lower-clientId-1"));
    assertThat(ent.getAttributes().keySet(), containsInAnyOrder("attr1", "attr3", "attr4"));
    assertThat(ent.getAttributes(), hasEntry("attr1", Arrays.asList("upper-value-1")));
    assertThat(ent.getAttributes(), hasEntry("attr3", Arrays.asList("lower-value-3")));
    assertThat(ent.getAttributes(), hasEntry("attr4", Arrays.asList("lower-value-4")));
    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