|
| 1 | +package org.sterl.spring.persistent_tasks.task; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.ValueSource; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.transaction.annotation.Propagation; |
| 14 | +import org.springframework.transaction.annotation.Transactional; |
| 15 | +import org.springframework.transaction.support.DefaultTransactionDefinition; |
| 16 | +import org.sterl.spring.persistent_tasks.AbstractSpringTest; |
| 17 | +import org.sterl.spring.persistent_tasks.api.PersistentTask; |
| 18 | +import org.sterl.spring.persistent_tasks.api.TaskId.TaskTriggerBuilder; |
| 19 | +import org.sterl.spring.persistent_tasks.api.TransactionalTask; |
| 20 | +import org.sterl.spring.persistent_tasks.task.util.ReflectionUtil; |
| 21 | +import org.sterl.spring.sample_app.person.PersonBE; |
| 22 | +import org.sterl.spring.sample_app.person.PersonRepository; |
| 23 | + |
| 24 | +import lombok.RequiredArgsConstructor; |
| 25 | + |
| 26 | +class TaskTransactionTest extends AbstractSpringTest { |
| 27 | + |
| 28 | + @Component("transactionalClass") |
| 29 | + @Transactional(timeout = 5, propagation = Propagation.MANDATORY) |
| 30 | + @RequiredArgsConstructor |
| 31 | + static class TransactionalClass implements PersistentTask<String> { |
| 32 | + private final PersonRepository personRepository; |
| 33 | + @Override |
| 34 | + public void accept(String name) { |
| 35 | + personRepository.save(new PersonBE(name)); |
| 36 | + } |
| 37 | + } |
| 38 | + @Component("transactionalMethod") |
| 39 | + @RequiredArgsConstructor |
| 40 | + static class TransactionalMethod implements PersistentTask<String> { |
| 41 | + private final PersonRepository personRepository; |
| 42 | + @Transactional(timeout = 6, propagation = Propagation.MANDATORY) |
| 43 | + @Override |
| 44 | + public void accept(String name) { |
| 45 | + personRepository.save(new PersonBE(name)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * A closure cannot be annotated, so we use a anonymous class |
| 51 | + */ |
| 52 | + @Configuration |
| 53 | + static class Config { |
| 54 | + @Bean("transactionalAnonymous") |
| 55 | + PersistentTask<String> transactionalAnonymous(PersonRepository personRepository) { |
| 56 | + return new PersistentTask<String>() { |
| 57 | + @Transactional(timeout = 7, propagation = Propagation.MANDATORY) |
| 58 | + @Override |
| 59 | + public void accept(String name) { |
| 60 | + personRepository.save(new PersonBE(name)); |
| 61 | + } |
| 62 | + }; |
| 63 | + } |
| 64 | + @Bean("transactionalClosure") |
| 65 | + TransactionalTask<String> transactionalClosure(PersonRepository personRepository) { |
| 66 | + return name -> { |
| 67 | + personRepository.save(new PersonBE(name)); |
| 68 | + personRepository.save(new PersonBE(name)); |
| 69 | + }; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Autowired PersonRepository personRepository; |
| 74 | + |
| 75 | + @Autowired @Qualifier("transactionalClass") |
| 76 | + PersistentTask<String> transactionalClass; |
| 77 | + @Autowired @Qualifier("transactionalMethod") |
| 78 | + PersistentTask<String> transactionalMethod; |
| 79 | + @Autowired @Qualifier("transactionalAnonymous") |
| 80 | + PersistentTask<String> transactionalAnonymous; |
| 81 | + |
| 82 | + @Test |
| 83 | + void testFindTransactionAnnotation() { |
| 84 | + var a = ReflectionUtil.getAnnotation(transactionalClass, Transactional.class); |
| 85 | + assertThat(a).isNotNull(); |
| 86 | + assertThat(a.timeout()).isEqualTo(5); |
| 87 | + |
| 88 | + a = ReflectionUtil.getAnnotation(transactionalMethod, Transactional.class); |
| 89 | + assertThat(a).isNotNull(); |
| 90 | + assertThat(a.timeout()).isEqualTo(6); |
| 91 | + |
| 92 | + a = ReflectionUtil.getAnnotation(transactionalAnonymous, Transactional.class); |
| 93 | + assertThat(a).isNotNull(); |
| 94 | + assertThat(a.timeout()).isEqualTo(7); |
| 95 | + } |
| 96 | + |
| 97 | + @ParameterizedTest |
| 98 | + @ValueSource(strings = {"transactionalClass", "transactionalMethod", "transactionalClosure"}) |
| 99 | + void testTransactionalTask(String task) { |
| 100 | + // GIVEN |
| 101 | + var t = triggerService.queue(TaskTriggerBuilder |
| 102 | + .newTrigger(task, "test").build()); |
| 103 | + |
| 104 | + // WHEN |
| 105 | + personRepository.deleteAllInBatch(); |
| 106 | + hibernateAsserts.reset(); |
| 107 | + triggerService.run(t).get(); |
| 108 | + |
| 109 | + // THEN |
| 110 | + hibernateAsserts.assertTrxCount(1); |
| 111 | + assertThat(personRepository.count()).isEqualTo(2); |
| 112 | + } |
| 113 | + |
| 114 | + public static DefaultTransactionDefinition convertTransactionalToDefinition(Transactional transactional) { |
| 115 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); |
| 116 | + |
| 117 | + // Map Transactional attributes to DefaultTransactionDefinition |
| 118 | + def.setIsolationLevel(transactional.isolation().value()); |
| 119 | + def.setPropagationBehavior(transactional.propagation().value()); |
| 120 | + def.setTimeout(transactional.timeout()); |
| 121 | + def.setReadOnly(transactional.readOnly()); |
| 122 | + // No direct mapping for 'rollbackFor' or 'noRollbackFor' |
| 123 | + // Set a name if desired (e.g., based on transactional class/method) |
| 124 | + def.setName("TransactionalDefinition"); |
| 125 | + |
| 126 | + return def; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments