Search in sources :

Example 1 with JpaConnectionProvider

use of org.keycloak.connections.jpa.JpaConnectionProvider in project keycloak by keycloak.

the class MigrationModelTest method test.

@Test
public void test() {
    inComittedTransaction(1, (session, i) -> {
        String currentVersion = Version.VERSION_KEYCLOAK.replaceAll("^(\\d+(?:\\.\\d+){0,2}).*$", "$1");
        JpaConnectionProvider p = session.getProvider(JpaConnectionProvider.class);
        EntityManager em = p.getEntityManager();
        List<MigrationModelEntity> l = em.createQuery("select m from MigrationModelEntity m ORDER BY m.updatedTime DESC", MigrationModelEntity.class).getResultList();
        Assert.assertEquals(1, l.size());
        Assert.assertTrue(l.get(0).getId().matches("[\\da-z]{5}"));
        Assert.assertEquals(currentVersion, l.get(0).getVersion());
        MigrationModel m = session.getProvider(DeploymentStateProvider.class).getMigrationModel();
        Assert.assertEquals(currentVersion, m.getStoredVersion());
        Assert.assertEquals(m.getResourcesTag(), l.get(0).getId());
        Time.setOffset(-60000);
        session.getProvider(DeploymentStateProvider.class).getMigrationModel().setStoredVersion("6.0.0");
        em.flush();
        Time.setOffset(0);
        l = em.createQuery("select m from MigrationModelEntity m ORDER BY m.updatedTime DESC", MigrationModelEntity.class).getResultList();
        Assert.assertEquals(2, l.size());
        Logger.getLogger(MigrationModelTest.class).info("MigrationModelEntity entries: ");
        Logger.getLogger(MigrationModelTest.class).info("--id: " + l.get(0).getId() + "; " + l.get(0).getVersion() + "; " + l.get(0).getUpdateTime());
        Logger.getLogger(MigrationModelTest.class).info("--id: " + l.get(1).getId() + "; " + l.get(1).getVersion() + "; " + l.get(1).getUpdateTime());
        Assert.assertTrue(l.get(0).getId().matches("[\\da-z]{5}"));
        Assert.assertEquals(currentVersion, l.get(0).getVersion());
        Assert.assertTrue(l.get(1).getId().matches("[\\da-z]{5}"));
        Assert.assertEquals("6.0.0", l.get(1).getVersion());
        m = session.getProvider(DeploymentStateProvider.class).getMigrationModel();
        Assert.assertEquals(l.get(0).getId(), m.getResourcesTag());
        Assert.assertEquals(currentVersion, m.getStoredVersion());
        em.remove(l.get(1));
        return null;
    });
}
Also used : EntityManager(javax.persistence.EntityManager) MigrationModel(org.keycloak.migration.MigrationModel) MigrationModelEntity(org.keycloak.models.jpa.entities.MigrationModelEntity) JpaConnectionProvider(org.keycloak.connections.jpa.JpaConnectionProvider) DeploymentStateProvider(org.keycloak.models.DeploymentStateProvider) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1 Test (org.junit.Test)1 JpaConnectionProvider (org.keycloak.connections.jpa.JpaConnectionProvider)1 MigrationModel (org.keycloak.migration.MigrationModel)1 DeploymentStateProvider (org.keycloak.models.DeploymentStateProvider)1 MigrationModelEntity (org.keycloak.models.jpa.entities.MigrationModelEntity)1