Search in sources :

Example 1 with MapClientEntityFields

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

the class JpaClientDelegateProvider method getDelegate.

@Override
public MapClientEntity getDelegate(boolean isRead, Enum<? extends EntityField<MapClientEntity>> field, Object... parameters) {
    if (getDelegate().isMetadataInitialized())
        return getDelegate();
    if (isRead) {
        if (field instanceof MapClientEntityFields) {
            switch((MapClientEntityFields) field) {
                case ID:
                case REALM_ID:
                case CLIENT_ID:
                case PROTOCOL:
                case ENABLED:
                    return getDelegate();
                case ATTRIBUTES:
                    CriteriaBuilder cb = em.getCriteriaBuilder();
                    CriteriaQuery<JpaClientEntity> query = cb.createQuery(JpaClientEntity.class);
                    Root<JpaClientEntity> root = query.from(JpaClientEntity.class);
                    root.fetch("attributes", JoinType.LEFT);
                    query.select(root).where(cb.equal(root.get("id"), UUID.fromString(getDelegate().getId())));
                    setDelegate(em.createQuery(query).getSingleResult());
                    break;
                default:
                    setDelegate(em.find(JpaClientEntity.class, UUID.fromString(getDelegate().getId())));
            }
        } else {
            throw new IllegalStateException("Not a valid client field: " + field);
        }
    } else {
        setDelegate(em.find(JpaClientEntity.class, UUID.fromString(getDelegate().getId())));
    }
    return getDelegate();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JpaClientEntity(org.keycloak.models.map.storage.jpa.client.entity.JpaClientEntity) MapClientEntityFields(org.keycloak.models.map.client.MapClientEntityFields)

Example 2 with MapClientEntityFields

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

the class TreeStorageNodePrescriptionTest method testPrimarySourceForBasicSetId.

/**
 * Test a node that has PRIMARY_SOURCE_FOR set to only ID field with no specialization (i.e. {@code null}),
 * and no PRIMARY_SOURCE_FOR_EXCLUDED is set.
 * <p>
 * Represents e.g. a node in the tree that stores only ID (maintains existence check of an object)
 */
@Test
public void testPrimarySourceForBasicSetId() {
    Map<String, Object> nodeProperties = new HashMap<>();
    EnumMap<MapClientEntityFields, Collection<String>> primarySourceFor = new EnumMap<>(MapClientEntityFields.class);
    nodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR, primarySourceFor);
    primarySourceFor.put(MapClientEntityFields.ID, null);
    TreeStorageNodePrescription n = new TreeStorageNodePrescription(nodeProperties, null, null);
    for (MapClientEntityFields field : MapClientEntityFields.values()) {
        assertThat(n.isPrimarySourceFor(field, null), is(field == MapClientEntityFields.ID ? FieldContainedStatus.FULLY : FieldContainedStatus.NOT_CONTAINED));
    }
}
Also used : HashMap(java.util.HashMap) MapClientEntityFields(org.keycloak.models.map.client.MapClientEntityFields) Collection(java.util.Collection) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 3 with MapClientEntityFields

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

the class TreeStorageNodePrescriptionTest method testPrimarySourceForBasicSet.

/**
 * Test a node that has PRIMARY_SOURCE_FOR set to all fields with no specialization (i.e. {@code null}),
 * and no PRIMARY_SOURCE_FOR_EXCLUDED is set.
 * <p>
 * Represents e.g. a node in the tree that stores all fields.
 */
@Test
public void testPrimarySourceForBasicSet() {
    Map<String, Object> nodeProperties = new HashMap<>();
    EnumMap<MapClientEntityFields, Collection<String>> primarySourceFor = new EnumMap<>(MapClientEntityFields.class);
    for (MapClientEntityFields field : MapClientEntityFields.values()) {
        primarySourceFor.put(field, null);
    }
    nodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR, primarySourceFor);
    TreeStorageNodePrescription n = new TreeStorageNodePrescription(nodeProperties, null, null);
    for (MapClientEntityFields field : MapClientEntityFields.values()) {
        assertThat("Field " + field + " has primary source in this node", n.isPrimarySourceFor(field, null), is(FieldContainedStatus.FULLY));
    }
}
Also used : HashMap(java.util.HashMap) MapClientEntityFields(org.keycloak.models.map.client.MapClientEntityFields) Collection(java.util.Collection) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 4 with MapClientEntityFields

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

the class TreeStorageNodePrescriptionTest method testPrimarySourceForWithExcluded.

/**
 * Test a node that has no PRIMARY_SOURCE_FOR set,
 * and PRIMARY_SOURCE_FOR_EXCLUDED is set to ATTRIBUTES field with no specialization (i.e. {@code null}).
 * <p>
 * Represents e.g. a node in the tree that stores all attributes apart from attributes
 */
@Test
public void testPrimarySourceForWithExcluded() {
    Map<String, Object> nodeProperties = new HashMap<>();
    EnumMap<MapClientEntityFields, Collection<String>> primarySourceForExcluded = new EnumMap<>(MapClientEntityFields.class);
    nodeProperties.put(NodeProperties.PRIMARY_SOURCE_FOR_EXCLUDED, primarySourceForExcluded);
    primarySourceForExcluded.put(MapClientEntityFields.ATTRIBUTES, null);
    // node is primary for all fields apart from all attributes
    TreeStorageNodePrescription n = new TreeStorageNodePrescription(nodeProperties, null, null);
    for (MapClientEntityFields field : MapClientEntityFields.values()) {
        assertThat(n.isPrimarySourceFor(field, null), is(field == MapClientEntityFields.ATTRIBUTES ? FieldContainedStatus.NOT_CONTAINED : FieldContainedStatus.FULLY));
    }
}
Also used : HashMap(java.util.HashMap) MapClientEntityFields(org.keycloak.models.map.client.MapClientEntityFields) Collection(java.util.Collection) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 5 with MapClientEntityFields

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

the class TreeStorageNodePrescriptionTest method testPrimarySourceForBasicUnset.

/**
 * Test a node that has neither PRIMARY_SOURCE_FOR nor PRIMARY_SOURCE_FOR_EXCLUDED set.
 * <p>
 * Represents e.g. a node in the tree that stores all fields.
 */
@Test
public void testPrimarySourceForBasicUnset() {
    Map<String, Object> nodeProperties = new HashMap<>();
    TreeStorageNodePrescription n = new TreeStorageNodePrescription(nodeProperties, null, null);
    for (MapClientEntityFields field : MapClientEntityFields.values()) {
        assertThat("Field " + field + " has primary source in this node", n.isPrimarySourceFor(field, null), is(FieldContainedStatus.FULLY));
    }
}
Also used : HashMap(java.util.HashMap) MapClientEntityFields(org.keycloak.models.map.client.MapClientEntityFields) Test(org.junit.Test)

Aggregations

MapClientEntityFields (org.keycloak.models.map.client.MapClientEntityFields)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Collection (java.util.Collection)3 EnumMap (java.util.EnumMap)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 JpaClientEntity (org.keycloak.models.map.storage.jpa.client.entity.JpaClientEntity)1