|
| 1 | +package it.aboutbits.springboot.testing.testdata.base; |
| 2 | + |
| 3 | +import it.aboutbits.springboot.testing.spring.BeanAccessor; |
| 4 | +import jakarta.persistence.EntityManager; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.transaction.support.TransactionTemplate; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.BiFunction; |
| 11 | +import java.util.function.Consumer; |
| 12 | +import java.util.function.ObjIntConsumer; |
| 13 | +import java.util.function.UnaryOperator; |
| 14 | + |
| 15 | +@SuppressWarnings("unchecked") |
| 16 | +@Slf4j |
| 17 | +public abstract class ModifyableTestDataCreator<CREATOR extends ModifyableTestDataCreator<CREATOR, ITEM, PARAMETER>, ITEM, PARAMETER> extends TestDataCreator<ITEM> { |
| 18 | + private boolean mutatorSet = false; |
| 19 | + private boolean mutatorCalled = false; |
| 20 | + |
| 21 | + protected BiFunction<PARAMETER, Integer, PARAMETER> parameterMutator = (parameter, index) -> { |
| 22 | + mutatorCalled = true; |
| 23 | + return parameter; |
| 24 | + }; |
| 25 | + protected ObjIntConsumer<ITEM> resultMutator = (item, index) -> { |
| 26 | + }; |
| 27 | + |
| 28 | + protected ModifyableTestDataCreator(int count) { |
| 29 | + super(count); |
| 30 | + } |
| 31 | + |
| 32 | + public CREATOR modifyParameter(BiFunction<PARAMETER, Integer, PARAMETER> parameterMutator) { |
| 33 | + this.parameterMutator = (parameter, index) -> { |
| 34 | + mutatorCalled = true; |
| 35 | + return parameterMutator.apply(parameter, index); |
| 36 | + }; |
| 37 | + mutatorSet = true; |
| 38 | + return (CREATOR) this; |
| 39 | + } |
| 40 | + |
| 41 | + public CREATOR modifyParameter(UnaryOperator<PARAMETER> parameterMutator) { |
| 42 | + this.parameterMutator = (parameter, index) -> { |
| 43 | + mutatorCalled = true; |
| 44 | + return parameterMutator.apply(parameter); |
| 45 | + }; |
| 46 | + mutatorSet = true; |
| 47 | + return (CREATOR) this; |
| 48 | + } |
| 49 | + |
| 50 | + public CREATOR modifyResult(ObjIntConsumer<ITEM> resultMutator) { |
| 51 | + this.resultMutator = resultMutator; |
| 52 | + return (CREATOR) this; |
| 53 | + } |
| 54 | + |
| 55 | + public CREATOR modifyResult(Consumer<ITEM> resultMutator) { |
| 56 | + this.resultMutator = (item, index) -> resultMutator.accept(item); |
| 57 | + return (CREATOR) this; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected List<ITEM> create() { |
| 62 | + var result = new ArrayList<ITEM>(); |
| 63 | + |
| 64 | + for (var index = 0; index < numberOfItems; index++) { |
| 65 | + var item = create(index); |
| 66 | + |
| 67 | + resultMutator.accept(item, index); |
| 68 | + |
| 69 | + result.add( |
| 70 | + saveMutation(item) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + if (mutatorSet && !mutatorCalled) { |
| 75 | + log.error("Parameter-mutation is defined but was never called."); |
| 76 | + throw new IllegalStateException("Parameter-mutation is defined but was never called."); |
| 77 | + } |
| 78 | + |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | + protected ITEM saveMutation(ITEM item) { |
| 83 | + var entityManager = BeanAccessor.getBean(EntityManager.class); |
| 84 | + var transactionTemplate = BeanAccessor.getBean(TransactionTemplate.class); |
| 85 | + |
| 86 | + return transactionTemplate.execute(status -> { |
| 87 | + try { |
| 88 | + return entityManager.merge(item); |
| 89 | + } catch (Exception e) { |
| 90 | + status.setRollbackOnly(); |
| 91 | + throw e; |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
0 commit comments