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 2019/04/06 08:09:32 UTC

[isis] branch 2033-IoC_spring updated: ISIS-2033: fixes the basic transactional/async example tests

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

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


The following commit(s) were added to refs/heads/2033-IoC_spring by this push:
     new 5d6076b  ISIS-2033: fixes the basic transactional/async example tests
5d6076b is described below

commit 5d6076bf23278b804cb9c3756cdfba24241d4452
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat Apr 6 10:09:25 2019 +0200

    ISIS-2033: fixes the basic transactional/async example tests
    
    - still, there is yet too much boilerplate required
    
    Task-Url: https://issues.apache.org/jira/browse/ISIS-2033
---
 .../java/springapp/dom/email/EmailRepository.java  |  5 +-
 .../dom/email/EmailVerificationService.java        | 38 ++++++++++------
 .../java/springapp/tests/command/CommandTest.java  | 53 ++++++++++++----------
 3 files changed, 56 insertions(+), 40 deletions(-)

diff --git a/example/application/springapp/src/main/java/springapp/dom/email/EmailRepository.java b/example/application/springapp/src/main/java/springapp/dom/email/EmailRepository.java
index 57ea186..e80a545 100644
--- a/example/application/springapp/src/main/java/springapp/dom/email/EmailRepository.java
+++ b/example/application/springapp/src/main/java/springapp/dom/email/EmailRepository.java
@@ -2,9 +2,10 @@ package springapp.dom.email;
 
 import java.util.List;
 
-import org.springframework.data.repository.CrudRepository;
+import org.springframework.data.jpa.repository.JpaRepository;
+//import org.springframework.data.repository.CrudRepository;
 
-public interface EmailRepository extends CrudRepository<Email, Long> {
+public interface EmailRepository extends JpaRepository<Email, Long> {
 	
 	List<Email> findByVerified(boolean verified);
 	long countByVerified(boolean verified);
diff --git a/example/application/springapp/src/main/java/springapp/dom/email/EmailVerificationService.java b/example/application/springapp/src/main/java/springapp/dom/email/EmailVerificationService.java
index 83f2238..3c5ad4c 100644
--- a/example/application/springapp/src/main/java/springapp/dom/email/EmailVerificationService.java
+++ b/example/application/springapp/src/main/java/springapp/dom/email/EmailVerificationService.java
@@ -2,32 +2,44 @@ package springapp.dom.email;
 
 import java.util.concurrent.CompletableFuture;
 
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
-import lombok.val;
+import lombok.Setter;
 import lombok.extern.slf4j.Slf4j;
 
 @Service @Slf4j
 public class EmailVerificationService {
+	
+	//TODO #boilerplate
+    @Setter(onMethod = @__({@PersistenceContext})) private EntityManager em;
 
     // -- BUSINESS LOGIC
 	
-	@Transactional(propagation = Propagation.REQUIRES_NEW)
+	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
 	public void startVerificationProcess(Email email) {
 		
 		log.info("start process for {}", toInfo(email));
 		
+		// we probably want 'email' passed over by the caller to be 'attached', but its 'detached'
+		email = em.merge(email); //TODO #boilerplate
+		
 		email.setVerified(true);
 		
+		// why is the 'commit' not handled by the current transaction?
+		em.persist(email); 	//TODO #boilerplate
+		
 		log.info("end process for {}", toInfo(email));
 	}
 	
 
-	@Async @Transactional(propagation = Propagation.REQUIRES_NEW)
+	@Async @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
 	public CompletableFuture<Void> startVerificationProcessAsync(Email email) {
 		
 		log.info("start process for {}", toInfo(email));
@@ -38,8 +50,14 @@ public class EmailVerificationService {
 			e.printStackTrace();
 		}
 		
+		// we probably want 'email' passed over by the caller to be 'attached', but its 'detached'
+		email = em.merge(email); //TODO #boilerplate
+		
 		email.setVerified(true);
 		
+		// why is the 'commit' not handled by the current transaction?
+		em.persist(email); 	//TODO #boilerplate
+		
 		log.info("end process for {}", toInfo(email));
 		
 		return new AsyncResult<Void>(null).completable();
@@ -48,16 +66,10 @@ public class EmailVerificationService {
 	// -- HELPER
 	
 	private String toInfo(Email email) {
-
-		val sb = new StringBuilder();
-		
-		//TODO what transaction are we currently in, if any?
-		
-		sb
-		.append(", email.verified=")
-		.append(email.isVerified());
-		
-		return sb.toString();
+		return new StringBuilder()
+		.append("email.verified=")
+		.append(email.isVerified())
+		.toString();
 	}
 	
 }
diff --git a/example/application/springapp/src/test/java/springapp/tests/command/CommandTest.java b/example/application/springapp/src/test/java/springapp/tests/command/CommandTest.java
index f293b12..8c05bd3 100644
--- a/example/application/springapp/src/test/java/springapp/tests/command/CommandTest.java
+++ b/example/application/springapp/src/test/java/springapp/tests/command/CommandTest.java
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import javax.inject.Inject;
 
 import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestMethodOrder;
@@ -46,8 +47,8 @@ class CommandTest {
 		commandDemoBean.cleanup();
 	}
 
-	@Test
-	void shouldProperlyCountVerifiedEmails_not_verified() {
+	@Test @Order(1)
+	void countEmails_not_verified() {
 
 		commandDemoBean.setUp();
 		
@@ -62,8 +63,8 @@ class CommandTest {
 			
 	}
 	
-	@Test
-	void shouldProperlyCountVerifiedEmails_verified() {
+	@Test @Order(2)
+	void countEmails_verified() {
 
 		commandDemoBean.setUpVerified();
 		
@@ -78,32 +79,21 @@ class CommandTest {
 			
 	}
 	
-	@Test
-	void shouldAllowTaskCancellation() {
+	@Test @Order(3)
+	void emailVerification_sync() {
 
 		commandDemoBean.setUp();
 		
-		val futures = new Futures<Void>();
-		
 		for(Email email: emailRepository.findByVerified(false)) {
 			
-			// we want this to run within its own transaction and in the background (async)
+			// we want this to run within its own transaction
 			// 'emailVerificationService' is managed by Spring, so the invocation honors any 
-			// @Transactional and @Async annotation present on the method that gets invoked.
-			val future = emailVerificationService.startVerificationProcessAsync(email);
-			
-			futures.add(future);
+			// @Transactional annotation present on the method that gets invoked.
+			emailVerificationService.startVerificationProcess(email);
 			
 			//backgroundService.execute(email).startVerificationProcess();
 		}
 		
-		log.info("join on the futures and wait for completion");
-		
-		// join on the futures and wait for completion
-		futures.combine().join();
-
-		log.info("all futures completed");
-		
 		{
 			val verifiedCount = emailRepository.countByVerified(true);
 			val notVerifiedCount = emailRepository.countByVerified(false);
@@ -114,25 +104,34 @@ class CommandTest {
 			assertEquals(0, notVerifiedCount);
 		}
 		
-
-		
 	}
 	
-	@Test
-	void emailVerificationNonAsync() {
+	@Test @Order(4)
+	void emailVerification_async() {
 
 		commandDemoBean.setUp();
 		
+		val futures = new Futures<Void>();
+		
 		for(Email email: emailRepository.findByVerified(false)) {
 			
 			// we want this to run within its own transaction and in the background (async)
 			// 'emailVerificationService' is managed by Spring, so the invocation honors any 
 			// @Transactional and @Async annotation present on the method that gets invoked.
-			emailVerificationService.startVerificationProcess(email);
+			val future = emailVerificationService.startVerificationProcessAsync(email);
+			
+			futures.add(future);
 			
 			//backgroundService.execute(email).startVerificationProcess();
 		}
 		
+		log.info("join on the futures and wait for completion");
+		
+		// join on the futures and wait for completion
+		futures.combine().join();
+
+		log.info("all futures completed");
+		
 		{
 			val verifiedCount = emailRepository.countByVerified(true);
 			val notVerifiedCount = emailRepository.countByVerified(false);
@@ -143,8 +142,12 @@ class CommandTest {
 			assertEquals(0, notVerifiedCount);
 		}
 		
+
+		
 	}
 	
 
+	
+
 
 }