Search in sources :

Example 1 with ConcurrentHashMapStorage

use of org.keycloak.models.map.storage.chm.ConcurrentHashMapStorage in project keycloak by keycloak.

the class ConcurrentHashMapStorageTest method testStorageSeparation.

@Test
@SuppressWarnings("unchecked")
public <K, K1, K2> void testStorageSeparation() {
    String component1Id = createMapStorageComponent("component1", "keyType", "ulong");
    String component2Id = createMapStorageComponent("component2", "keyType", "string");
    String[] ids = withRealm(realmId, (session, realm) -> {
        ConcurrentHashMapStorage<K, MapClientEntity, ClientModel> storageMain = (ConcurrentHashMapStorage<K, MapClientEntity, ClientModel>) (MapStorage) session.getProvider(MapStorageProvider.class, ConcurrentHashMapStorageProviderFactory.PROVIDER_ID).getStorage(ClientModel.class);
        ConcurrentHashMapStorage<K1, MapClientEntity, ClientModel> storage1 = (ConcurrentHashMapStorage<K1, MapClientEntity, ClientModel>) (MapStorage) session.getComponentProvider(MapStorageProvider.class, component1Id).getStorage(ClientModel.class);
        ConcurrentHashMapStorage<K2, MapClientEntity, ClientModel> storage2 = (ConcurrentHashMapStorage<K2, MapClientEntity, ClientModel>) (MapStorage) session.getComponentProvider(MapStorageProvider.class, component2Id).getStorage(ClientModel.class);
        // Assert that the map storage can be used both as a standalone store and a component
        assertThat(storageMain, notNullValue());
        assertThat(storage1, notNullValue());
        assertThat(storage2, notNullValue());
        final StringKeyConvertor<K> kcMain = storageMain.getKeyConvertor();
        final StringKeyConvertor<K1> kc1 = storage1.getKeyConvertor();
        final StringKeyConvertor<K2> kc2 = storage2.getKeyConvertor();
        String idMain = kcMain.keyToString(kcMain.yieldNewUniqueKey());
        String id1 = kc1.keyToString(kc1.yieldNewUniqueKey());
        String id2 = kc2.keyToString(kc2.yieldNewUniqueKey());
        assertThat(idMain, notNullValue());
        assertThat(id1, notNullValue());
        assertThat(id2, notNullValue());
        // Assert that the stores do not contain the to-be-created clients
        assertThat(storageMain.read(idMain), nullValue());
        assertThat(storage1.read(id1), nullValue());
        assertThat(storage2.read(id2), nullValue());
        assertClientDoesNotExist(storageMain, id1, kc1, kcMain);
        assertClientDoesNotExist(storageMain, id2, kc2, kcMain);
        assertClientDoesNotExist(storage1, idMain, kcMain, kc1);
        assertClientDoesNotExist(storage1, id2, kc2, kc1);
        assertClientDoesNotExist(storage2, idMain, kcMain, kc2);
        assertClientDoesNotExist(storage2, id1, kc1, kc2);
        MapClientEntity clientMain = new MapClientEntityImpl();
        clientMain.setId(idMain);
        clientMain.setRealmId(realmId);
        MapClientEntity client1 = new MapClientEntityImpl();
        client1.setId(id1);
        client1.setRealmId(realmId);
        MapClientEntity client2 = new MapClientEntityImpl();
        client2.setId(id2);
        client2.setRealmId(realmId);
        clientMain = storageMain.create(clientMain);
        client1 = storage1.create(client1);
        client2 = storage2.create(client2);
        return new String[] { clientMain.getId(), client1.getId(), client2.getId() };
    });
    String idMain = ids[0];
    String id1 = ids[1];
    String id2 = ids[2];
    LOG.debugf("Object IDs: %s, %s, %s", idMain, id1, id2);
    assertClientsPersisted(component1Id, component2Id, idMain, id1, id2);
    // Invalidate one component and check that the storage still contains what it should
    getFactory().invalidate(ObjectType.COMPONENT, component1Id);
    assertClientsPersisted(component1Id, component2Id, idMain, id1, id2);
    // Invalidate whole realm and check that the storage still contains what it should
    getFactory().invalidate(ObjectType.REALM, realmId);
    assertClientsPersisted(component1Id, component2Id, idMain, id1, id2);
    // Refresh factory (akin server restart) and check that the storage still contains what it should
    reinitializeKeycloakSessionFactory();
    assertClientsPersisted(component1Id, component2Id, idMain, id1, id2);
}
Also used : MapClientEntity(org.keycloak.models.map.client.MapClientEntity) ClientModel(org.keycloak.models.ClientModel) MapClientEntityImpl(org.keycloak.models.map.client.MapClientEntityImpl) ConcurrentHashMapStorage(org.keycloak.models.map.storage.chm.ConcurrentHashMapStorage) MapStorageProvider(org.keycloak.models.map.storage.MapStorageProvider) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 ClientModel (org.keycloak.models.ClientModel)1 MapClientEntity (org.keycloak.models.map.client.MapClientEntity)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