You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/11/06 16:57:50 UTC

[isis] branch master updated: ISIS-2441: fixes AuditerServiceTest (and moves it to stable)

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new e9f886c  ISIS-2441: fixes AuditerServiceTest (and moves it to stable)
e9f886c is described below

commit e9f886c663ae338e0ff2ce883f6a49f25b430e74
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Nov 6 17:57:38 2020 +0100

    ISIS-2441: fixes AuditerServiceTest (and moves it to stable)
---
 .../changetracking/ChangedObjectsService.java      |   4 -
 .../entitychangetracking}/AuditerServiceTest.java  | 132 ++++++++++++---------
 .../entitychangetracking/PublisherServiceTest.java |  16 +--
 3 files changed, 84 insertions(+), 68 deletions(-)

diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/changetracking/ChangedObjectsService.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/changetracking/ChangedObjectsService.java
index cc5fde4..1a42d4b 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/changetracking/ChangedObjectsService.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/changetracking/ChangedObjectsService.java
@@ -44,7 +44,6 @@ import org.apache.isis.applib.services.metrics.MetricsService;
 import org.apache.isis.commons.internal.base._Lazy;
 import org.apache.isis.commons.internal.collections._Maps;
 import org.apache.isis.commons.internal.collections._Sets;
-import org.apache.isis.commons.internal.debug._Probe;
 import org.apache.isis.commons.internal.exceptions._Exceptions;
 import org.apache.isis.commons.internal.factory._InstanceUtil;
 import org.apache.isis.core.metamodel.facets.object.callbacks.CallbackFacet;
@@ -172,9 +171,6 @@ implements
     @Override
     public void onTransactionEnding() {
         log.debug("purging data");
-        
-        _Probe.errOut("clearing change data");
-        
         enlistedObjectProperties.clear();
         changeKindByEnlistedAdapter.clear();
         changedObjectPropertiesRef.clear();
diff --git a/regressiontests/incubating/src/test/java/org/apache/isis/testdomain/auditing/AuditerServiceTest.java b/regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/AuditerServiceTest.java
similarity index 56%
rename from regressiontests/incubating/src/test/java/org/apache/isis/testdomain/auditing/AuditerServiceTest.java
rename to regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/AuditerServiceTest.java
index 7c595df..6f28b81 100644
--- a/regressiontests/incubating/src/test/java/org/apache/isis/testdomain/auditing/AuditerServiceTest.java
+++ b/regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/AuditerServiceTest.java
@@ -16,7 +16,7 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-package org.apache.isis.testdomain.auditing;
+package org.apache.isis.testdomain.entitychangetracking;
 
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
@@ -25,14 +25,15 @@ import java.util.concurrent.TimeoutException;
 import javax.inject.Inject;
 
 import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
 import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.TestPropertySource;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.support.TransactionTemplate;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
@@ -40,13 +41,16 @@ import org.apache.isis.applib.services.repository.RepositoryService;
 import org.apache.isis.applib.services.wrapper.DisabledException;
 import org.apache.isis.applib.services.wrapper.WrapperFactory;
 import org.apache.isis.applib.services.wrapper.control.AsyncControl;
-import org.apache.isis.applib.services.wrapper.control.ExceptionHandlerAbstract;
-import org.apache.isis.applib.services.xactn.TransactionService;
-import org.apache.isis.commons.internal.base._Blackhole;
+import org.apache.isis.applib.services.wrapper.control.SyncControl;
+import org.apache.isis.commons.collections.Can;
+import org.apache.isis.commons.internal.debug._Probe;
 import org.apache.isis.core.config.presets.IsisPresets;
+import org.apache.isis.testdomain.auditing.AuditerServiceForTesting;
+import org.apache.isis.testdomain.auditing.Configuration_usingAuditing;
 import org.apache.isis.testdomain.conf.Configuration_usingJdo;
 import org.apache.isis.testdomain.jdo.JdoTestDomainPersona;
 import org.apache.isis.testdomain.jdo.entities.JdoBook;
+import org.apache.isis.testdomain.util.CollectionAssertions;
 import org.apache.isis.testdomain.util.kv.KVStoreForTesting;
 import org.apache.isis.testing.fixtures.applib.fixturescripts.FixtureScripts;
 import org.apache.isis.testing.integtestsupport.applib.IsisIntegrationTestAbstract;
@@ -64,21 +68,16 @@ import lombok.val;
                 "logging.level.org.apache.isis.testdomain.util.rest.KVStoreForTesting=DEBUG"
         })
 @TestPropertySource({
-    IsisPresets.SilenceWicket
-    ,IsisPresets.UseLog4j2Test
+    IsisPresets.UseLog4j2Test
 })
-@DirtiesContext // because of the temporary installed AuditerServiceProbe ?
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 class AuditerServiceTest extends IsisIntegrationTestAbstract {
 
     @Inject private RepositoryService repository;
     @Inject private FixtureScripts fixtureScripts;
     @Inject private WrapperFactory wrapper;
     @Inject private KVStoreForTesting kvStore;
-    
-    @Configuration
-    public class Config {
-        // so that we get a new ApplicationContext.
-    }
+    @Inject private PlatformTransactionManager txMan;
 
     @BeforeEach
     void setUp() {
@@ -89,19 +88,18 @@ class AuditerServiceTest extends IsisIntegrationTestAbstract {
         fixtureScripts.runPersona(JdoTestDomainPersona.InventoryWith1Book);
     }
 
-    @Test @Tag("Incubating")
+    @Test @Order(1)
     void auditerService_shouldBeAwareOfInventoryChanges() {
 
         // given
-        val books = repository.allInstances(JdoBook.class);
-        assertEquals(1, books.size());
-        val book = books.listIterator().next();
-        
-        kvStore.clear(AuditerServiceForTesting.class);
+        val book = repository.allInstances(JdoBook.class).listIterator().next();
+        AuditerServiceForTesting.clearAuditEntries(kvStore);
+
+        _Probe.errOut("(1) BEFORE BOOK UPDATED");
 
         // when - running within its own transactional boundary
-//        val transactionTemplate = new TransactionTemplate(txMan);
-//        transactionTemplate.execute(status -> {
+        val transactionTemplate = new TransactionTemplate(txMan);
+        transactionTemplate.execute(status -> {
 
             book.setName("Book #2");
             repository.persist(book);
@@ -109,53 +107,68 @@ class AuditerServiceTest extends IsisIntegrationTestAbstract {
             // then - before the commit
             assertFalse(kvStore.get(AuditerServiceForTesting.class, "audit").isPresent());
 
-//            return null;
-//        });
-        transactionService.nextTransaction();
+            return null;
+        });
+
+        _Probe.errOut("(1) AFTER BOOK UPDATED");
+
+        // then - after the commit
+        assertHasAuditEntries(Can.of(
+                "Jdo Book/name: 'Sample Book' -> 'Book #2'"));
+    }
+
+    @Test @Order(2)
+    void auditerService_shouldBeAwareOfInventoryChanges_whenUsingSyncExecution() throws ExecutionException, InterruptedException, TimeoutException {
+
+        // given
+        val book = repository.allInstances(JdoBook.class).listIterator().next();
+        AuditerServiceForTesting.clearAuditEntries(kvStore);
+
+        _Probe.errOut("(2) BEFORE BOOK UPDATED");
+
+        // when - running synchronous
+        val control = SyncControl.control().withSkipRules();
+        wrapper.wrap(book, control).setName("Book #2");
+
+        _Probe.errOut("(2) AFTER BOOK UPDATED");
 
         // then - after the commit
-        assertEquals("targetClassName=Jdo Book,propertyName=name,preValue=Sample Book,postValue=Book #2;",
-                kvStore.get(AuditerServiceForTesting.class, "audit").orElse(null));
+        assertHasAuditEntries(Can.of(
+                "Jdo Book/name: 'Sample Book' -> 'Book #2'"));
     }
 
-    @Inject
-    TransactionService transactionService;
 
-    @Test @Tag("Incubating")
+    @Test @Order(3)
     void auditerService_shouldBeAwareOfInventoryChanges_whenUsingAsyncExecution() throws ExecutionException, InterruptedException, TimeoutException {
 
         // given
-        val books = repository.allInstances(JdoBook.class);
-        assertEquals(1, books.size());
-        val book = books.listIterator().next();
-        kvStore.clear(AuditerServiceForTesting.class);
+        val book = repository.allInstances(JdoBook.class).listIterator().next();
+        AuditerServiceForTesting.clearAuditEntries(kvStore);
+        val latch = kvStore.latch(AuditerServiceForTesting.class);
+
+        _Probe.errOut("(3) BEFORE BOOK UPDATED");
 
         // when - running within its own background task
         AsyncControl<Void> control = returningVoid().withSkipRules();
-        wrapper.asyncWrap(book, control.with(new ExceptionHandlerAbstract() {
-            @Override
-            public Object handle(Exception ex) throws Exception {
-                getLog().error(ex);
-                return null;
-            }
-        })).setName("Book #2");
+        wrapper.asyncWrap(book, control).setName("Book #2");
 
-        Void await = control.getFuture().get(10, TimeUnit.SECONDS); // blocks the current thread
-        _Blackhole.consume(await); // just use the value to suppress warnings
+        control.getFuture().get(10, TimeUnit.SECONDS); // blocks the current thread
+
+        latch.await(2, TimeUnit.SECONDS);
+
+        _Probe.errOut("(3) AFTER BOOK UPDATED");
 
         // then - after the commit
-        assertEquals("targetClassName=Jdo Book,propertyName=name,preValue=Sample Book,postValue=Book #2;",
-                kvStore.get(AuditerServiceForTesting.class, "audit").orElse(null));
+        assertHasAuditEntries(Can.of(
+                "Jdo Book/name: 'Sample Book' -> 'Book #2'"));
     }
-    
-    @Test @Tag("Incubating")
+
+    @Test @Order(4)
     void auditerService_shouldNotBeAwareOfInventoryChanges_whenUsingAsyncExecutionThatFails() {
 
         // given
-        val books = repository.allInstances(JdoBook.class);
-        assertEquals(1, books.size());
-        val book = books.listIterator().next();
-        kvStore.clear(AuditerServiceForTesting.class);
+        val book = repository.allInstances(JdoBook.class).listIterator().next();
+        AuditerServiceForTesting.clearAuditEntries(kvStore);
 
         // when - running within its own background task
         assertThrows(DisabledException.class, ()->{
@@ -163,11 +176,18 @@ class AuditerServiceTest extends IsisIntegrationTestAbstract {
             wrapper.asyncWrap(book, returningVoid()).setName("Book #2");
 
             returningVoid().getFuture().get(1000, TimeUnit.SECONDS);
-            
+
         });
-        
+
         // then - after the exception
-        assertFalse(kvStore.get(AuditerServiceForTesting.class, "audit").isPresent());
+        assertHasAuditEntries(Can.empty());
+    }
+
+    // -- HELPER
+
+    private void assertHasAuditEntries(Can<String> expectedAuditEntries) {
+        val actualAuditEntries = AuditerServiceForTesting.getAuditEntries(kvStore);
+        CollectionAssertions.assertComponentWiseEquals(expectedAuditEntries, actualAuditEntries);
     }
 
 
diff --git a/regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/PublisherServiceTest.java b/regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/PublisherServiceTest.java
index 89e3f93..e0e3e71 100644
--- a/regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/PublisherServiceTest.java
+++ b/regressiontests/stable/src/test/java/org/apache/isis/testdomain/entitychangetracking/PublisherServiceTest.java
@@ -93,7 +93,7 @@ class PublisherServiceTest extends IsisIntegrationTestAbstract {
 
         // given
         val book = repository.allInstances(JdoBook.class).listIterator().next();
-        kvStore.clear(PublisherServiceForTesting.class);
+        PublisherServiceForTesting.clearPublishedEntries(kvStore);
         
         _Probe.errOut("(1) BEFORE BOOK UPDATED");
         
@@ -114,8 +114,8 @@ class PublisherServiceTest extends IsisIntegrationTestAbstract {
         
         // this test does not trigger publishing 
         // because invocation happens directly rather than through the means of
-        // of an ActionInvocationFacet, which is required to creates CommandDtos 
-        // executions  
+        // of an ActionInvocationFacet, which is required to create a CommandDto 
+        // for each execution  
         
         // then - after the commit
         assertEquals(0, getCreated());
@@ -131,12 +131,12 @@ class PublisherServiceTest extends IsisIntegrationTestAbstract {
 
         // given
         val book = repository.allInstances(JdoBook.class).listIterator().next();
-        kvStore.clear(PublisherServiceForTesting.class);
+        PublisherServiceForTesting.clearPublishedEntries(kvStore);
 
         _Probe.errOut("(2) BEFORE BOOK UPDATED");
         
-        SyncControl syncControl = SyncControl.control().withSkipRules();
-        // when - running within its own background task
+        // when - running synchronous
+        val syncControl = SyncControl.control().withSkipRules();
         wrapper.wrap(book, syncControl).setName("Book #2"); // don't enforce rules for this test
 
         _Probe.errOut("(2) AFTER BOOK UPDATED");
@@ -157,7 +157,7 @@ class PublisherServiceTest extends IsisIntegrationTestAbstract {
 
         // given
         val book = repository.allInstances(JdoBook.class).listIterator().next();
-        kvStore.clear(PublisherServiceForTesting.class);
+        PublisherServiceForTesting.clearPublishedEntries(kvStore);
         val latch = kvStore.latch(PublisherServiceForTesting.class);
 
         _Probe.errOut("(3) BEFORE BOOK UPDATED");
@@ -188,7 +188,7 @@ class PublisherServiceTest extends IsisIntegrationTestAbstract {
 
         // given
         val book = repository.allInstances(JdoBook.class).listIterator().next();
-        kvStore.clear(PublisherServiceForTesting.class);
+        PublisherServiceForTesting.clearPublishedEntries(kvStore);
 
         // when - running within its own background task
         assertThrows(DisabledException.class, ()->{