|
| 1 | +package restx.specs; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import restx.common.UUIDGenerator; |
| 5 | +import restx.common.UUIDGenerators; |
| 6 | +import restx.factory.*; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author fcamblor |
| 12 | + */ |
| 13 | +@Component |
| 14 | +public class GivenUUIDGeneratorRecorder implements RestxSpecRecorder.GivenRecorder { |
| 15 | + Map<String, UUIDGenerators.OverridenMachineCleaner> namedUUIDGeneratorsCleaners = new HashMap<>(); |
| 16 | + |
| 17 | + @Override |
| 18 | + public void installRecording() { |
| 19 | + Set<NamedComponent<UUIDGenerator>> namedGenerators = UUIDGenerators.currentUUIDGenerators(); |
| 20 | + for(NamedComponent<UUIDGenerator> namedGenerator : namedGenerators){ |
| 21 | + namedUUIDGeneratorsCleaners.put(namedGenerator.getName().getName(), UUIDGenerators.overrideUUIDGenerator( |
| 22 | + NamedComponent.of(UUIDGenerator.class, namedGenerator.getName().getName(), new UUIDGenerator.RecordingUUIDGenerator()) |
| 23 | + )); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public AutoCloseable recordIn(final Map<String, RestxSpec.Given> givens) { |
| 29 | + final Set<NamedComponent<UUIDGenerator.RecordingUUIDGenerator>> recordingUUIDGenerators = recordingUUIDGenerators(); |
| 30 | + |
| 31 | + for(final NamedComponent<UUIDGenerator.RecordingUUIDGenerator> namedRecordingUUIDGenerator : recordingUUIDGenerators){ |
| 32 | + namedRecordingUUIDGenerator.getComponent().attachThreadedObserver(new UUIDGenerator.RecordingUUIDGenerator.UUIDGeneratedObserver() { |
| 33 | + public void uuidGenerated(String uuid) { |
| 34 | + String key = GivenUUIDGenerator.class.getSimpleName() + "/uuidsFor"+namedRecordingUUIDGenerator.getName().getName(); |
| 35 | + if (!givens.containsKey(key)) { |
| 36 | + givens.put(key, new GivenUUIDGenerator(namedRecordingUUIDGenerator.getName().getName(), Collections.<String>emptyList())); |
| 37 | + } |
| 38 | + givens.put(key, ((GivenUUIDGenerator)givens.get(key)).withAddedUUID(uuid)); |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + return new AutoCloseable() { |
| 44 | + @Override |
| 45 | + public void close() throws Exception { |
| 46 | + for(NamedComponent<UUIDGenerator.RecordingUUIDGenerator> namedRecordingUUIDGenerator : recordingUUIDGenerators){ |
| 47 | + // Removing attached observers |
| 48 | + namedRecordingUUIDGenerator.getComponent().detachThreadedObservers(); |
| 49 | + |
| 50 | + // No need to clean uuid generators machine because installRecording() will not be called |
| 51 | + // again before the next recordIn() call.. |
| 52 | + } |
| 53 | + } |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + private Set<NamedComponent<UUIDGenerator.RecordingUUIDGenerator>> recordingUUIDGenerators(){ |
| 58 | + Set<NamedComponent<UUIDGenerator.RecordingUUIDGenerator>> recordingUUIDGenerators = new HashSet<>(); |
| 59 | + for(Map.Entry<String,UUIDGenerators.OverridenMachineCleaner> namedUUIDGeneratorCleanerByName : namedUUIDGeneratorsCleaners.entrySet()){ |
| 60 | + UUIDGenerator currentGenerator = UUIDGenerators.currentGeneratorFor(namedUUIDGeneratorCleanerByName.getKey()); |
| 61 | + if(currentGenerator instanceof UUIDGenerator.RecordingUUIDGenerator){ |
| 62 | + recordingUUIDGenerators.add(NamedComponent.of(UUIDGenerator.RecordingUUIDGenerator.class, namedUUIDGeneratorCleanerByName.getKey(), (UUIDGenerator.RecordingUUIDGenerator) currentGenerator)); |
| 63 | + } |
| 64 | + } |
| 65 | + return recordingUUIDGenerators; |
| 66 | + } |
| 67 | +} |
0 commit comments