Search in sources :

Example 1 with EntityListener

use of org.molgenis.data.listeners.EntityListener in project molgenis by molgenis.

the class PlatformIT method testEntityListener.

@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testEntityListener() {
    populateUserPermissions();
    List<Entity> refEntities = testHarness.createTestRefEntities(refEntityTypeDynamic, 6);
    List<Entity> entities = testHarness.createTestEntities(entityTypeDynamic, 2, refEntities).collect(toList());
    runAsSystem(() -> {
        dataService.add(refEntityTypeDynamic.getId(), refEntities.stream());
        dataService.add(entityTypeDynamic.getId(), entities.stream());
        waitForIndexToBeStable(entityTypeDynamic, indexService, LOG);
    });
    AtomicInteger updateCalled = new AtomicInteger(0);
    EntityListener listener = new EntityListener() {

        @Override
        public Object getEntityId() {
            return entities.get(0).getIdValue();
        }

        @Override
        public void postUpdate(Entity entity) {
            updateCalled.incrementAndGet();
            assertEquals(entity.getIdValue(), entities.get(0).getIdValue());
        }
    };
    try {
        // Test that the listener is being called
        entityListenersService.addEntityListener(entityTypeDynamic.getId(), listener);
        dataService.update(entityTypeDynamic.getId(), entities.stream());
        assertEquals(updateCalled.get(), 1);
        waitForIndexToBeStable(entityTypeDynamic, indexService, LOG);
        assertPresent(entityTypeDynamic, entities);
    } finally {
        // Test that the listener is actually removed and not called anymore
        entityListenersService.removeEntityListener(entityTypeDynamic.getId(), listener);
        updateCalled.set(0);
        dataService.update(entityTypeDynamic.getId(), entities.stream());
        assertEquals(updateCalled.get(), 0);
        waitForIndexToBeStable(entityTypeDynamic, indexService, LOG);
        assertPresent(entityTypeDynamic, entities);
    }
}
Also used : EntityListener(org.molgenis.data.listeners.EntityListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WithMockUser(org.springframework.security.test.context.support.WithMockUser)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 EntityListener (org.molgenis.data.listeners.EntityListener)1 WithMockUser (org.springframework.security.test.context.support.WithMockUser)1