All Downloads are FREE. Search and download functionalities are using the official Maven repository.

restx.specs.GivenUUIDGeneratorRecorder Maven / Gradle / Ivy

There is a newer version: 1.2.0-rc2
Show newest version
package restx.specs;

import com.google.common.collect.ImmutableList;
import restx.CoreModule;
import restx.RestxContext;
import restx.common.UUIDGenerator;
import restx.factory.*;

import java.util.*;

/**
 * @author fcamblor
 */
@Component
public class GivenUUIDGeneratorRecorder implements RestxSpecRecorder.GivenRecorder {
    @Override
    public AutoCloseable recordIn(final Map givens) {
        final Tape tape = new Tape(givens);
        Factory.LocalMachines.threadLocal().addMachine(
                new SingleNameFactoryMachine<>(0, new NoDepsMachineEngine(
                        Name.of(ComponentCustomizerEngine.class, "UUIDGeneratorSequenceSupplier"),
                        BoundlessComponentBox.FACTORY) {
                    @Override
                    protected ComponentCustomizerEngine doNewComponent(SatisfiedBOM satisfiedBOM) {
                        return new SingleComponentClassCustomizerEngine(0, UUIDGenerator.class) {
                            @Override
                            public NamedComponent customize(final NamedComponent namedComponent) {
                                return new NamedComponent<>(namedComponent.getName(), new UUIDGenerator() {
                                    @Override
                                    public String doGenerate() {
                                        String uuid = namedComponent.getComponent().doGenerate();
                                        tape.recordGeneratedId(uuid);
                                        return uuid;
                                    }
                                });
                            }
                        };
                    }
                }));
        return tape;
    }

    private static class Tape implements AutoCloseable {
        private final Map givens;
        private GivenUUIDGenerator givenUUIDGenerator;

        private Tape(Map givens) {
            this.givens = givens;
            givenUUIDGenerator = new GivenUUIDGenerator(ImmutableList.of());
        }

        @Override
        public void close() throws Exception {
            if (!givenUUIDGenerator.getPlaybackUUIDs().isEmpty()) {
                givens.put("uuids", givenUUIDGenerator);
            }
        }

        private void recordGeneratedId(String uuid) {
            givenUUIDGenerator = givenUUIDGenerator.concat(uuid);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy