text:
package org.apache.camel.test.junit5;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.Message;
import org.apache.camel.NamedNode;
import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.Service;
import org.apache.camel.ServiceStatus;
import org.apache.camel.api.management.ManagedCamelContext;
import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.debugger.DefaultDebugger;
import org.apache.camel.model.Model;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.spi.CamelBeanPostProcessor;
import org.apache.camel.spi.Language;
import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.PropertiesSource;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.BreakpointSupport;
import org.apache.camel.support.EndpointHelper;
import org.apache.camel.test.CamelRouteCoverageDumper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.URISupport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * A useful base class which creates a {@link org.apache.camel.CamelContext} with some routes along with a
 * {@link org.apache.camel.ProducerTemplate} for use in the test case Do <tt>not</tt> use this class for Spring Boot
 * testing.
 */
public abstract class CamelTestSupport
        implements BeforeEachCallback, AfterEachCallback, AfterAllCallback, BeforeAllCallback, BeforeTestExecutionCallback,
        AfterTestExecutionCallback {

    /**
     * JVM system property which can be set to true to turn on dumping route coverage statistics.
     */
    public static final String ROUTE_COVERAGE_ENABLED = "CamelTestRouteCoverage";

    // CHECKSTYLE:OFF
    private static final Logger LOG = LoggerFactory.getLogger(CamelTestSupport.class);
    private static ThreadLocal<ModelCamelContext> threadCamelContext = new ThreadLocal<>();
    private static ThreadLocal<ProducerTemplate> threadTemplate = new ThreadLocal<>();
    private static ThreadLocal<FluentProducerTemplate> threadFluentTemplate = new ThreadLocal<>();
    private static ThreadLocal<ConsumerTemplate> threadConsumer = new ThreadLocal<>();
    private static ThreadLocal<Service> threadService = new ThreadLocal<>();
    protected Properties extra;
    protected volatile ModelCamelContext context;
    protected volatile ProducerTemplate template;
    protected volatile FluentProducerTemplate fluentTemplate;
    protected volatile ConsumerTemplate consumer;
    protected volatile Service camelContextService;
    @RegisterExtension
    protected CamelTestSupport camelTestSupportExtension = this;
    private boolean useRouteBuilder = true;
    private final DebugBreakpoint breakpoint = new DebugBreakpoint();
    private final StopWatch watch = new StopWatch();
    private final Map<String, String> fromEndpoints = new HashMap<>();
    private static final ThreadLocal<AtomicInteger> TESTS = new ThreadLocal<>();
    private static final ThreadLocal<CamelTestSupport> INSTANCE = new ThreadLocal<>();
    private String currentTestName;
    private boolean isCreateCamelContextPerClass = false;
    private CamelRouteCoverageDumper routeCoverageDumper = new CamelRouteCoverageDumper();
    private ExtensionContext.Store globalStore;
    private boolean testDirectoryCleaned;
    // CHECKSTYLE:ON

    @Override
    public void afterTestExecution(ExtensionContext context) throws Exception {
        watch.taken();
    }

    @Override
    public void beforeTestExecution(ExtensionContext context) throws Exception {
        watch.restart();
    }

    public long timeTaken() {
        return watch.taken();
    }

    @Override
    public void beforeEach(ExtensionContext context) throws Exception {
        currentTestName = context.getDisplayName();
        globalStore = context.getStore(ExtensionContext.Namespace.GLOBAL);
    }

    @Override
    public void afterEach(ExtensionContext context) throws Exception {
        DefaultCamelContext.clearOptions();
    }

    @Override
    public void beforeAll(ExtensionContext context) {
        isCreateCamelContextPerClass
                = context.getTestInstanceLifecycle().filter(lc -> lc.equals(Lifecycle.PER_CLASS)).isPresent();
    }

    @Override
    public void afterAll(ExtensionContext context) {
        CamelTestSupport support = INSTANCE.get();
        if (support != null && support.isCreateCamelContextPerClass()) {
            try {
                support.tearDownCreateCamelContextPerClass();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Use the RouteBuilder or not
     *
     * @return <tt>true</tt> then {@link CamelContext} will be auto started, <tt>false</tt> then {@link CamelContext}
     *         will not be auto started (you will have to start it manually)
     */
    public boolean isUseRouteBuilder() {
        return useRouteBuilder;
    }

    public void setUseRouteBuilder(boolean useRouteBuilder) {
        this.useRouteBuilder = useRouteBuilder;
    }

    /**
     * Whether to dump route coverage stats at the end of the test.
     *

     * This allows tooling or manual inspection of the stats, so you can generate a route trace diagram of which EIPs
     * have been in use and which have not. Similar concepts as a code coverage report.
     *

     * You can also turn on route coverage globally via setting JVM system property
     * <tt>CamelTestRouteCoverage=true</tt>.
     *
     * @return <tt>true</tt> to write route coverage status in an xml file in the <tt>target/camel-route-coverage</tt>
     *         directory after the test has finished.
     */
    public boolean isDumpRouteCoverage() {
        return false;
    }

    /**
     * Override when using advice with and return <tt>true</tt>.
     * This helps knowing advice with is to be used, and {@link CamelContext} will not be started before the advice with
     * takes place. This helps by ensuring the advice with has been property setup before the {@link CamelContext} is
     * started
     *

     * Important: Its important to start {@link CamelContext} manually from the unit test after you are done
     * doing all the advice with.
     *
     * @return <tt>true</tt> if you use advice with in your unit tests.
     */
    public boolean isUseAdviceWith() {
        return false;
    }

    /**
     * Tells whether {@link CamelContext} should be setup per test or per class.
     *

     * By default it will be setup/teardown per test method. This method returns true when the camel test
     * class is annotated with @TestInstance(TestInstance.Lifecycle.PER_CLASS).
     *

     * Important: Use this with care as the {@link CamelContext} will carry over state from previous tests, such
     * as endpoints, components etc. So you cannot use this in all your tests.
     *

     * Setting up {@link CamelContext} uses the {@link #doPreSetup()}, {@link #doSetUp()}, and {@link #doPostSetup()}
     * methods in that given order.
     *
     * @return <tt>true</tt> per class, <tt>false</tt> per test.
     */
    public final boolean isCreateCamelContextPerClass() {
        return isCreateCamelContextPerClass;
    }

    /**
     * Override to enable auto mocking endpoints based on the pattern.
     *

     * Return <tt>*</tt> to mock all endpoints.
     *
     * @see EndpointHelper#matchEndpoint(CamelContext, String, String)
     */
    public String isMockEndpoints() {
        return null;
    }

    /**
     * Override to enable auto mocking endpoints based on the pattern, and skip sending to original endpoint.
     *

     * Return <tt>*</tt> to mock all endpoints.
     *
     * @see EndpointHelper#matchEndpoint(CamelContext, String, String)
     */
    public String isMockEndpointsAndSkip() {
        return null;
    }

    public void replaceRouteFromWith(String routeId, String fromEndpoint) {
        fromEndpoints.put(routeId, fromEndpoint);
    }

    /**
     * Used for filtering routes matching the given pattern, which follows the following rules:
     *

     * - Match by route id - Match by route input endpoint uri
     *

     * The matching is using exact match, by wildcard and regular expression.
     *

     * For example to only include routes which starts with foo in their route id's, use: include=foo* And to
     * exclude routes which starts from JMS endpoints, use: exclude=jms:*
     *

     * Multiple patterns can be separated by comma, for example to exclude both foo and bar routes, use:
     * exclude=foo*,bar*
     *

     * Exclude takes precedence over include.
     */
    public String getRouteFilterIncludePattern() {
        return null;
    }

    /**
     * Used for filtering routes matching the given pattern, which follows the following rules:
     *

     * - Match by route id - Match by route input endpoint uri
     *

     * The matching is using exact match, by wildcard and regular expression.
     *

     * For example to only include routes which starts with foo in their route id's, use: include=foo* And to
     * exclude routes which starts from JMS endpoints, use: exclude=jms:*
     *

     * Multiple patterns can be separated by comma, for example to exclude both foo and bar routes, use:
     * exclude=foo*,bar*
     *

     * Exclude takes precedence over include.
     */
    public String getRouteFilterExcludePattern() {
        return null;
    }

    /**
     * Gets the name of the current test being executed.
     */
    public final String getCurrentTestName() {
        return currentTestName;
    }

    /**
     * Override to enable debugger
     *

     * Is default <tt>false</tt>
     */
    public boolean isUseDebugger() {
        return false;
    }

    public Service getCamelContextService() {
        return camelContextService;
    }

    public Service camelContextService() {
        return camelContextService;
    }

    public CamelContext context() {
        return context;
    }

    public ProducerTemplate template() {
        return template;
    }

    public FluentProducerTemplate fluentTemplate() {
        return fluentTemplate;
    }

    public ConsumerTemplate consumer() {
        return consumer;
    }

    /**
     * Allows a service to be registered a separate lifecycle service to start and stop the context; such as for Spring
     * when the ApplicationContext is started and stopped, rather than directly stopping the CamelContext
     */
    public void setCamelContextService(Service service) {
        camelContextService = service;
        threadService.set(camelContextService);
    }

    @BeforeEach
    public void setUp() throws Exception {
        LOG.info("********************************************************************************");
        LOG.info("Testing: {} ({})", currentTestName, getClass().getName());
        LOG.info("********************************************************************************");

        if (isCreateCamelContextPerClass()) {
            INSTANCE.set(this);
            AtomicInteger v = TESTS.get();
            if (v == null) {
                v = new AtomicInteger();
                TESTS.set(v);
            }
            if (v.getAndIncrement() == 0) {
                LOG.debug("Setup CamelContext before running first test");
                // test is per class, so only setup once (the first time)
                doSpringBootCheck();
                setupResources();
                doPreSetup();
                doSetUp();
                doPostSetup();
            } else {
                LOG.debug("Reset between test methods");
                // and in between tests we must do IoC and reset mocks
                postProcessTest();
                resetMocks();
            }
        } else {
            // test is per test so always setup
            doSpringBootCheck();
            setupResources();
            doPreSetup();
            doSetUp();
            doPostSetup();
        }

        // only start timing after all the setup
        watch.restart();
    }

    /**
     * Strategy to perform any pre setup, before {@link CamelContext} is created
     */
    protected void doPreSetup() throws Exception {
        // noop
    }

    /**
     * Strategy to perform any post setup after {@link CamelContext} is created
     */
    protected void doPostSetup() throws Exception {
        // noop
    }

    /**
     * Detects if this is a Spring-Boot test and throws an exception, as these base classes is not intended for testing
     * Camel on Spring Boot.
     */
    protected void doSpringBootCheck() {
        boolean springBoot = hasClassAnnotation("org.springframework.boot.test.context.SpringBootTest");
        if (springBoot) {
            throw new RuntimeException(
                    "Spring Boot detected: The CamelTestSupport/CamelSpringTestSupport class is not intended for Camel testing with Spring Boot.");
        }
    }

    private void doSetUp() throws Exception {
        LOG.debug("setUp test");
        // jmx is enabled if we have configured to use it, or if dump route
        // coverage is enabled (it requires JMX)
        boolean jmx = useJmx() || isRouteCoverageEnabled();
        if (jmx) {
            enableJMX();
        } else {
            disableJMX();
        }

        context = (ModelCamelContext) createCamelContext();
        threadCamelContext.set(context);

        assertNotNull(context, "No context found!");

        // add custom beans
        bindToRegistry(context.getRegistry());

        // reduce default shutdown timeout to avoid waiting for 300 seconds
        context.getShutdownStrategy().setTimeout(getShutdownTimeout());

        // set debugger if enabled
        if (isUseDebugger()) {
            if (context.getStatus().equals(ServiceStatus.Started)) {
                LOG.info("Cannot setting the Debugger to the starting CamelContext, stop the CamelContext now.");
                // we need to stop the context first to setup the debugger
                context.stop();
            }
            context.setDebugger(new DefaultDebugger());
            context.getDebugger().addBreakpoint(breakpoint);
            // when stopping CamelContext it will automatic remove the
            // breakpoint
        }

        template = context.createProducerTemplate();
        template.start();
        fluentTemplate = context.createFluentProducerTemplate();
        fluentTemplate.start();
        consumer = context.createConsumerTemplate();
        consumer.start();

        threadTemplate.set(template);
        threadFluentTemplate.set(fluentTemplate);
        threadConsumer.set(consumer);

        // enable auto mocking if enabled
        String pattern = isMockEndpoints();
        if (pattern != null) {
            context.adapt(ExtendedCamelContext.class)
                    .registerEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
        }
        pattern = isMockEndpointsAndSkip();
        if (pattern != null) {
            context.adapt(ExtendedCamelContext.class)
                    .registerEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern, true));
        }

        // configure properties component (mandatory for testing)
        PropertiesComponent pc = context.getPropertiesComponent();
        if (extra == null) {
            extra = useOverridePropertiesWithPropertiesComponent();
        }
        if (extra != null && !extra.isEmpty()) {
            pc.setOverrideProperties(extra);
        }
        pc.addPropertiesSource(new PropertiesSource() {
            @Override
            public String getName() {
                return "junit-store";
            }

            @Override
            public String getProperty(String name) {
                return globalStore.get(name, String.class);
            }
        });
        Boolean ignore = ignoreMissingLocationWithPropertiesComponent();
        if (ignore != null) {
            pc.setIgnoreMissingLocation(ignore);
        }

        String include = getRouteFilterIncludePattern();
        String exclude = getRouteFilterExcludePattern();
        if (include != null || exclude != null) {
            LOG.info("Route filtering pattern: include={}, exclude={}", include, exclude);
            context.getExtension(Model.class).setRouteFilterPattern(include, exclude);
        }

        // prepare for in-between tests
        postProcessTest();

        if (isUseRouteBuilder()) {
            RoutesBuilder[] builders = createRouteBuilders();
            for (RoutesBuilder builder : builders) {
                LOG.debug("Using created route builder: {}", builder);
                context.addRoutes(builder);
            }
            replaceFromEndpoints();
            boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
            if (skip) {
                LOG.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
            } else if (isUseAdviceWith()) {
                LOG.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
            } else {
                startCamelContext();
            }
        } else {
            replaceFromEndpoints();
            LOG.debug("Using route builder from the created context: {}", context);
        }
        LOG.debug("Routing Rules are: {}", context.getRoutes());

        assertValidContext(context);
    }

    private void replaceFromEndpoints() throws Exception {
        for (final Map.Entry<String, String> entry : fromEndpoints.entrySet()) {
            AdviceWith.adviceWith(context.getRouteDefinition(entry.getKey()), context, new AdviceWithRouteBuilder() {
                @Override
                public void configure() throws Exception {
                    replaceFromWith(entry.getValue());
                }
            });
        }
    }

    private boolean isRouteCoverageEnabled() {
        return System.getProperty(ROUTE_COVERAGE_ENABLED, "false").equalsIgnoreCase("true") || isDumpRouteCoverage();
    }

    @AfterEach
    public void tearDown() throws Exception {
        long time = watch.taken();

        LOG.info("********************************************************************************");
        LOG.info("Testing done: {} ({})", currentTestName, getClass().getName());
        LOG.info("Took: {} ({} millis)", TimeUtils.printDuration(time), time);

        // if we should dump route stats, then write that to a file
        if (isRouteCoverageEnabled()) {
            String className = this.getClass().getSimpleName();
            String dir = "target/camel-route-coverage";
            String name = className + "-" + StringHelper.before(currentTestName, "(") + ".xml";

            ManagedCamelContext mc = context != null ? context.getExtension(ManagedCamelContext.class) : null;
            ManagedCamelContextMBean managedCamelContext = mc != null ? mc.getManagedCamelContext() : null;
            if (managedCamelContext == null) {
                LOG.warn("Cannot dump route coverage to file as JMX is not enabled. "
                         + "Add camel-management JAR as dependency and/or override useJmx() method to enable JMX in the unit test classes.");
            } else {
                routeCoverageDumper.dump(managedCamelContext, context, dir, name, getClass().getName(), currentTestName,
                        timeTaken());
            }
        }
        LOG.info("********************************************************************************");

        if (isCreateCamelContextPerClass()) {
            // will tear down test specially in afterAll callback
        } else {
            LOG.debug("tearDown()");
            doStopTemplates(consumer, template, fluentTemplate);
            doStopCamelContext(context, camelContextService);
            doPostTearDown();
            cleanupResources();
        }
        testDirectoryCleaned = false;
    }

    void tearDownCreateCamelContextPerClass() throws Exception {
        LOG.debug("tearDownCreateCamelContextPerClass()");
        TESTS.remove();
        doStopTemplates(threadConsumer.get(), threadTemplate.get(), threadFluentTemplate.get());
        doStopCamelContext(threadCamelContext.get(), threadService.get());
        doPostTearDown();
        cleanupResources();
    }

    /**
     * Strategy to perform any post action, after {@link CamelContext} is stopped
     */
    protected void doPostTearDown() throws Exception {
        // noop
    }

    /**
     * Strategy to perform resources setup, before {@link CamelContext} is created
     */
    protected void setupResources() throws Exception {
        deleteTestDirectory();
    }

    /**
     * Strategy to perform resources cleanup, after {@link CamelContext} is stopped
     */
    protected void cleanupResources() throws Exception {
        // noop
    }

    /**
     * Returns the timeout to use when shutting down (unit in seconds).
     *

     * Will default use 10 seconds.
     *
     * @return the timeout to use
     */
    protected int getShutdownTimeout() {
        return 10;
    }

    /**
     * Whether or not JMX should be used during testing.
     *
     * @return <tt>false</tt> by default.
     */
    protected boolean useJmx() {
        return false;
    }

    /**
     * Override this method to include and override properties with the Camel {@link PropertiesComponent}.
     *
     * @return additional properties to add/override.
     */
    protected Properties useOverridePropertiesWithPropertiesComponent() {
        return null;
    }

    /**
     * Whether to ignore missing locations with the {@link PropertiesComponent}. For example when unit testing you may
     * want to ignore locations that are not available in the environment used for testing.
     *
     * @return <tt>true</tt> to ignore, <tt>false</tt> to not ignore, and <tt>null</tt> to leave as configured on the
     *         {@link PropertiesComponent}
     */
    protected Boolean ignoreMissingLocationWithPropertiesComponent() {
        return null;
    }

    protected void postProcessTest() throws Exception {
        context = threadCamelContext.get();
        template = threadTemplate.get();
        fluentTemplate = threadFluentTemplate.get();
        consumer = threadConsumer.get();
        camelContextService = threadService.get();
        applyCamelPostProcessor();
    }

    /**
     * Applies the {@link CamelBeanPostProcessor} to this instance.
     *

     * Derived classes using IoC / DI frameworks may wish to turn this into a NoOp such as for CDI we would just use CDI
     * to inject this
     */
    protected void applyCamelPostProcessor() throws Exception {
        // use the bean post processor if the test class is not dependency
        // injected already by Spring Framework
        boolean spring = hasClassAnnotation("org.springframework.boot.test.context.SpringBootTest",
                "org.springframework.context.annotation.ComponentScan");
        if (!spring) {
            context.getExtension(ExtendedCamelContext.class).getBeanPostProcessor().postProcessBeforeInitialization(this,
                    getClass().getName());
            context.getExtension(ExtendedCamelContext.class).getBeanPostProcessor().postProcessAfterInitialization(this,
                    getClass().getName());
        }
    }

    /**
     * Does this test class have any of the following annotations on the class-level.
     */
    protected boolean hasClassAnnotation(String... names) {
        for (String name : names) {
            for (Annotation ann : getClass().getAnnotations()) {
                String annName = ann.annotationType().getName();
                if (annName.equals(name)) {
                    return true;
                }
            }
        }
        return false;
    }

    protected void stopCamelContext() throws Exception {
        doStopCamelContext(context, camelContextService);
    }

    private static void doStopCamelContext(CamelContext context, Service camelContextService) {
        if (camelContextService != null) {
            if (camelContextService == threadService.get()) {
                threadService.remove();
            }
            camelContextService.stop();
        } else {
            if (context != null) {
                if (context == threadCamelContext.get()) {
                    threadCamelContext.remove();
                }
                context.stop();
            }
        }
    }

    private static void doStopTemplates(
            ConsumerTemplate consumer, ProducerTemplate template, FluentProducerTemplate fluentTemplate) {
        if (consumer != null) {
            if (consumer == threadConsumer.get()) {
                threadConsumer.remove();
            }
            consumer.stop();
        }
        if (template != null) {
            if (template == threadTemplate.get()) {
                threadTemplate.remove();
            }
            template.stop();
        }
        if (fluentTemplate != null) {
            if (fluentTemplate == threadFluentTemplate.get()) {
                threadFluentTemplate.remove();
            }
            fluentTemplate.stop();
        }
    }

    protected void startCamelContext() throws Exception {
        if (camelContextService != null) {
            camelContextService.start();
        } else {
            if (context instanceof DefaultCamelContext) {
                DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context;
                if (!defaultCamelContext.isStarted()) {
                    defaultCamelContext.start();
                }
            } else {
                context.start();
            }
        }
    }

    protected CamelContext createCamelContext() throws Exception {
        Registry registry = createCamelRegistry();

        CamelContext retContext;
        if (registry != null) {
            retContext = new DefaultCamelContext(registry);
        } else {
            retContext = new DefaultCamelContext();
        }

        return retContext;
    }

    /**
     * Allows to bind custom beans to the Camel {@link Registry}.
     */
    protected void bindToRegistry(Registry registry) throws Exception {
        // noop
    }

    /**
     * Override to use a custom {@link Registry}.
     *

     * However if you need to bind beans to the registry then this is possible already with the bind method on registry,
     * and there is no need to override this method.
     */
    protected Registry createCamelRegistry() throws Exception {
        return null;
    }

    /**
     * Factory method which derived classes can use to create a {@link RouteBuilder} to define the routes for testing
     */
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() {
                // no routes added by default
            }
        };
    }

    /**
     * Factory method which derived classes can use to create an array of {@link org.apache.camel.builder.RouteBuilder}s
     * to define the routes for testing
     *
     * @see #createRouteBuilder()
     */
    protected RoutesBuilder[] createRouteBuilders() throws Exception {
        return new RoutesBuilder[] { createRouteBuilder() };
    }

    /**
     * Resolves a mandatory endpoint for the given URI or an exception is thrown
     *
     * @param  uri the Camel URI to use to create or resolve an endpoint
     * @return     the endpoint
     */
    protected Endpoint resolveMandatoryEndpoint(String uri) {
        return TestSupport.resolveMandatoryEndpoint(context, uri);
    }

    /**
     * Resolves a mandatory endpoint for the given URI and expected type or an exception is thrown
     *
     * @param  uri the Camel URI to use to create or resolve an endpoint
     * @return     the endpoint
     */
    protected <T extends Endpoint> T resolveMandatoryEndpoint(String uri, Class<T> endpointType) {
        return TestSupport.resolveMandatoryEndpoint(context, uri, endpointType);
    }

    /**
     * Resolves the mandatory Mock endpoint using a URI of the form mock:someName
     *
     * @param  uri the URI which typically starts with "mock:" and has some name
     * @return     the mandatory mock endpoint or an exception is thrown if it could not be resolved
     */
    protected MockEndpoint getMockEndpoint(String uri) {
        return getMockEndpoint(uri, true);
    }

    /**
     * Resolves the {@link MockEndpoint} using a URI of the form mock:someName, optionally creating it if
     * it does not exist. This implementation will lookup existing mock endpoints and match on the mock queue name, eg
     * mock:foo and mock:foo?retainFirst=5 would match as the queue name is foo.
     *
     * @param  uri                     the URI which typically starts with "mock:" and has some name
     * @param  create                  whether or not to allow the endpoint to be created if it doesn't exist
     * @return                         the mock endpoint or an {@link NoSuchEndpointException} is thrown if it could not
     *                                 be resolved
     * @throws NoSuchEndpointException is the mock endpoint does not exists
     */
    protected MockEndpoint getMockEndpoint(String uri, boolean create) throws NoSuchEndpointException {
        // look for existing mock endpoints that have the same queue name, and
        // to
        // do that we need to normalize uri and strip out query parameters and
        // whatnot
        String n;
        try {
            n = URISupport.normalizeUri(uri);
        } catch (Exception e) {
            throw RuntimeCamelException.wrapRuntimeException(e);
        }
        // strip query
        int idx = n.indexOf('?');
        if (idx != -1) {
            n = n.substring(0, idx);
        }
        final String target = n;

        // lookup endpoints in registry and try to find it
        MockEndpoint found = (MockEndpoint) context.getEndpointRegistry().values().stream()
                .filter(e -> e instanceof MockEndpoint).filter(e -> {
                    String t = e.getEndpointUri();
                    // strip query
                    int idx2 = t.indexOf('?');
                    if (idx2 != -1) {
                        t = t.substring(0, idx2);
                    }
                    return t.equals(target);
                }).findFirst().orElse(null);

        if (found != null) {
            return found;
        }

        if (create) {
            return resolveMandatoryEndpoint(uri, MockEndpoint.class);
        } else {
            throw new NoSuchEndpointException(String.format("MockEndpoint %s does not exist.", uri));
        }
    }

    /**
     * Sends a message to the given endpoint URI with the body value
     *
     * @param endpointUri the URI of the endpoint to send to
     * @param body        the body for the message
     */
    protected void sendBody(String endpointUri, final Object body) {
        template.send(endpointUri, exchange -> {
            Message in = exchange.getIn();
            in.setBody(body);
        });
    }

    /**
     * Sends a message to the given endpoint URI with the body value and specified headers
     *
     * @param endpointUri the URI of the endpoint to send to
     * @param body        the body for the message
     * @param headers     any headers to set on the message
     */
    protected void sendBody(String endpointUri, final Object body, final Map<String, Object> headers) {
        template.send(endpointUri, exchange -> {
            Message in = exchange.getIn();
            in.setBody(body);
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                in.setHeader(entry.getKey(), entry.getValue());
            }
        });
    }

    /**
     * Sends messages to the given endpoint for each of the specified bodies
     *
     * @param endpointUri the endpoint URI to send to
     * @param bodies      the bodies to send, one per message
     */
    protected void sendBodies(String endpointUri, Object... bodies) {
        for (Object body : bodies) {
            sendBody(endpointUri, body);
        }
    }

    /**
     * Creates an exchange with the given body
     */
    protected Exchange createExchangeWithBody(Object body) {
        return TestSupport.createExchangeWithBody(context, body);
    }

    /**
     * Asserts that the given language name and expression evaluates to the given value on a specific exchange
     */
    protected void assertExpression(Exchange exchange, String languageName, String expressionText, Object expectedValue) {
        Language language = assertResolveLanguage(languageName);

        Expression expression = language.createExpression(expressionText);
        assertNotNull(expression, "No Expression could be created for text: " + expressionText + " language: " + language);

        TestSupport.assertExpression(expression, exchange, expectedValue);
    }

    /**
     * Asserts that the given language name and predicate expression evaluates to the expected value on the message
     * exchange
     */
    protected void assertPredicate(String languageName, String expressionText, Exchange exchange, boolean expected) {
        Language language = assertResolveLanguage(languageName);

        Predicate predicate = language.createPredicate(expressionText);
        assertNotNull(predicate, "No Predicate could be created for text: " + expressionText + " language: " + language);

        TestSupport.assertPredicate(predicate, exchange, expected);
    }

    /**
     * Asserts that the language name can be resolved
     */
    protected Language assertResolveLanguage(String languageName) {
        Language language = context.resolveLanguage(languageName);
        assertNotNull(language, "No language found for name: " + languageName);
        return language;
    }

    /**
     * Asserts that all the expectations of the Mock endpoints are valid
     */
    protected void assertMockEndpointsSatisfied() throws InterruptedException {
        MockEndpoint.assertIsSatisfied(context);
    }

    /**
     * Asserts that all the expectations of the Mock endpoints are valid
     */
    protected void assertMockEndpointsSatisfied(long timeout, TimeUnit unit) throws InterruptedException {
        MockEndpoint.assertIsSatisfied(context, timeout, unit);
    }

    /**
     * Reset all Mock endpoints.
     */
    protected void resetMocks() {
        MockEndpoint.resetMocks(context);
    }

    protected void assertValidContext(CamelContext context) {
        assertNotNull(context, "No context found!");
    }

    protected <T extends Endpoint> T getMandatoryEndpoint(String uri, Class<T> type) {
        T endpoint = context.getEndpoint(uri, type);
        assertNotNull(endpoint, "No endpoint found for uri: " + uri);
        return endpoint;
    }

    protected Endpoint getMandatoryEndpoint(String uri) {
        Endpoint endpoint = context.getEndpoint(uri);
        assertNotNull(endpoint, "No endpoint found for uri: " + uri);
        return endpoint;
    }

    /**
     * Disables the JMX agent. Must be called before the {@link #setUp()} method.
     */
    protected void disableJMX() {
        DefaultCamelContext.setDisableJmx(true);
    }

    /**
     * Enables the JMX agent. Must be called before the {@link #setUp()} method.
     */
    protected void enableJMX() {
        DefaultCamelContext.setDisableJmx(false);
    }

    /**
     * Single step debugs and Camel invokes this method before entering the given processor
     */
    protected void debugBefore(
            Exchange exchange, Processor processor, ProcessorDefinition<?> definition, String id, String label) {
    }

    /**
     * Single step debugs and Camel invokes this method after processing the given processor
     */
    protected void debugAfter(
            Exchange exchange, Processor processor, ProcessorDefinition<?> definition, String id, String label,
            long timeTaken) {
    }

    /**
     * To easily debug by overriding the <tt>debugBefore</tt> and <tt>debugAfter</tt> methods.
     */
    private class DebugBreakpoint extends BreakpointSupport {

        @Override
        public void beforeProcess(Exchange exchange, Processor processor, NamedNode definition) {
            CamelTestSupport.this.debugBefore(exchange, processor, (ProcessorDefinition<?>) definition, definition.getId(),
                    definition.getLabel());
        }

        @Override
        public void afterProcess(Exchange exchange, Processor processor, NamedNode definition, long timeTaken) {
            CamelTestSupport.this.debugAfter(exchange, processor, (ProcessorDefinition<?>) definition, definition.getId(),
                    definition.getLabel(), timeTaken);
        }
    }

    protected Path testDirectory() {
        return testDirectory(false);
    }

    protected Path testDirectory(boolean create) {
        Class<?> testClass = getClass();
        return testDirectory(testClass, create);
    }

    public static Path testDirectory(Class<?> testClass, boolean create) {
        Path dir = Paths.get("target", "data", testClass.getSimpleName());
        if (create) {
            try {
                Files.createDirectories(dir);
            } catch (IOException e) {
                throw new IllegalStateException("Unable to create test directory: " + dir, e);
            }
        }
        return dir;
    }

    protected Path testFile(String dir) {
        return testDirectory().resolve(dir);
    }

    protected Path testDirectory(String dir) {
        return testDirectory(dir, false);
    }

    protected Path testDirectory(String dir, boolean create) {
        Path f = testDirectory().resolve(dir);
        if (create) {
            try {
                Files.createDirectories(f);
            } catch (IOException e) {
                throw new IllegalStateException("Unable to create test directory: " + dir, e);
            }
        }
        return f;
    }

    protected String fileUri() {
        return "file:" + testDirectory();
    }

    protected String fileUri(String query) {
        return "file:" + testDirectory() + (query.startsWith("?") ? "" : "/") + query;
    }

    public void deleteTestDirectory() {
        if (!testDirectoryCleaned) {
            deleteDirectory(testDirectory());
            testDirectoryCleaned = true;
        }
    }

}


text:
The Python Script uses troposphere to create the Template.

I tried creating the script using the AWS Web Tool, but it was quite difficult to get the Template I wanted.

There are some pretty good examples on creating various components on troposphere github.

The Python Script does some error checking; however, it isn't perfect.  For example, when I created the route for the NAT Gateway I accidentally used GatewayID instead of NatGatewayId and it created the json; however, I got a cryptic error message from Cloudformation.

Right now the script only support a single master and single public agent. At the top you can change num_agents to generate a script for various numbers of agents.

I've only been working with CloudFormation for about a week, so the script will be improved as I learn more.

Steps
- Run Script to Create CloudFormation Template with your desired number of private agents
- Copy your private key and install_dcos.sh to the boot server
- Become root and copy the private key and installer to root's home
- Run the installer (e.g. bash install_dcos.sh 1 3 1)

# Certificates in AWS

Browsers often block access the Master URL via HTTPS.

Issue: In Load Balancer I configured a TCP listener for port 443.  In browsers I could see the certificate as the one generated by the Mesosphere. Firefox and IE both rejected and even though you say ignore and proceed you usually cannot access the site.  Chrome would sometimes work after a few tries.

## Created a certificate

From this [instructions](http://msnider.github.io/blog/2013/12/05/creating-a-self-signed-ssl-certificate-for-an-amazon-elb/)

1. Generate a Private Key The Private Key is used to decrypt messages sent to the server. Keep this safe and secret! Use any password when prompted (we'll remove it later). **openssl genrsa -des3 -out domain.key 4096**

2. Generate a Certificate Signing Request (CSR) The CSR contains the Public Key, used to encrypt messages, and information about the application so the end user (visitor) can see, so enter the information accordingly. **openssl req -nodes -newkey rsa:2048 -keyout domain.key -out domain.csr**

3. Remove the Password from the Private Key
- **cp domain.key domain.key.password**
- **openssl rsa -in domain.key.password -out domain.key**

4. Generate the Certificate Make sure to set the number of days before expiration, set to 1 year below. **openssl x509 -req -days 365 -in domain.csr -signkey domain.key -out domain.crt**

5. Open the AWS Console to upload your certificate
- Goto ACM Page
- Click Import Certificate
- Output the certificate to the command line **openssl x509 -inform PEM -in domain.crt**
- Copy everything from the "Begin" to the "End" tags (inclusive)
- Output the private key to the command line **openssl rsa -in domain.key -text**
- Copy everything from the "---BEGIN RSA PRIVATE KEY ----" to the "End" tags (inclusive)
- Click Review and Import
- Click Import

6. Configure Load Balancer
- Select Load Balancer
- Under Actions Edit Listeners
- Certificate Type: Click Choose existing certificate from ACM
- Select the Certicate.  **NOTE: I had to refresh my Browser to get the certificate to show up in list**
- Click OK

After making this configuration change I could access from any browser after accepting the normal warnings about invalid certificate (associated with using a self-signed certificate).

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace App\Controller;

use Cake\Event\Event;

/**
 * Error Handling Controller
 *
 * Controller used by ExceptionRenderer to render error responses.
 */
class ErrorController extends AppController
{
    /**
     * Initialization hook method.
     *
     * @return void
     */
    public function initialize()
    {
        $this->loadComponent('RequestHandler');
    }

    /**
     * beforeFilter callback.
     *
     * @param \Cake\Event\Event $event Event.
     * @return \Cake\Network\Response|null|void
     */
    public function beforeFilter(Event $event)
    {
    }

    /**
     * beforeRender callback.
     *
     * @param \Cake\Event\Event $event Event.
     * @return \Cake\Network\Response|null|void
     */
    public function beforeRender(Event $event)
    {
        parent::beforeRender($event);

        $this->viewBuilder()->setTemplatePath('Error');
    }

    /**
     * afterFilter callback.
     *
     * @param \Cake\Event\Event $event Event.
     * @return \Cake\Network\Response|null|void
     */
    public function afterFilter(Event $event)
    {
    }
}


text:
export default class ImmutableAccessor {
  _value: any
  path: Array<any>
  constructor(_value: any, path?: any) {
    this._value = _value
    this.path = path || []
  }
  containerType() {
    if (Array.isArray(this._value)) {
      return 'array'
    } else if (this._value !== null && typeof this._value == 'object') {
      return 'object'
    }
    return 'primitive'
  }

  // Common reader, supported by all containers
  get(): any {
    return this._value
  }

  // Array reader
  length(): number {
    if (this.containerType() !== 'array') {
      throw new Error("Won't return length of non-indexable _value")
    }
    return this._value.length
  }
  getIndex(i: number): any {
    if (this.containerType() !== 'array') {
      return false
    }
    if (i >= this.length()) {
      return null
    }
    return new ImmutableAccessor(this._value[i], this.path.concat(i))
  }

  // Object reader
  hasAttribute(key: string): boolean {
    if (this.containerType() !== 'object') {
      return false
    }
    return this._value.hasOwnProperty(key)
  }
  attributeKeys(): Array<string> {
    if (this.containerType() !== 'object') {
      return []
    }
    return Object.keys(this._value)
  }
  getAttribute(key: string): any {
    if (this.containerType() !== 'object') {
      throw new Error('getAttribute only applies to plain objects')
    }
    if (!this.hasAttribute(key)) {
      return null
    }
    return new ImmutableAccessor(this._value[key], this.path.concat(key))
  }

  // Common writer, supported by all containers
  set(value) {
    if (value === this._value) {
      return this
    }
    return new ImmutableAccessor(value, this.path)
  }

  setAccessor(accessor) {
    return accessor
  }

  // array writer interface
  setIndex(i: number, value: any) {
    if (value === this._value[i]) {
      return this
    }
    const nextValue = this._value.slice()
    nextValue[i] = value
    return new ImmutableAccessor(nextValue, this.path)
  }
  setIndexAccessor(i: number, accessor) {
    return this.setIndex(i, accessor.get())
  }
  unsetIndices(indices: Array<number>) {
    const length = this._value.length
    const nextValue = []
    // Copy every _value _not_ in the indices array over to the newValue
    for (let i = 0; i < length; i++) {
      if (indices.indexOf(i) === -1) {
        nextValue.push(this._value[i])
      }
    }
    return new ImmutableAccessor(nextValue, this.path)
  }
  insertItemsAt(pos: number, items: Array<any>) {
    let nextValue
    if (this.length() === 0 && pos === 0) {
      nextValue = items
    } else {
      nextValue = this._value.slice(0, pos).concat(items).concat(this._value.slice(pos))
    }
    return new ImmutableAccessor(nextValue, this.path)
  }

  // Object writer interface
  setAttribute(key: string, value: any) {
    if (this.containerType() !== 'object') {
      throw new Error('Unable to set attribute of non-object container')
    }
    if (value === this._value[key]) {
      return this
    }
    const nextValue = Object.assign({}, this._value)
    nextValue[key] = value
    return new ImmutableAccessor(nextValue, this.path)
  }
  setAttributeAccessor(key: string, accessor) {
    return this.setAttribute(key, accessor.get())
  }
  unsetAttribute(key: string) {
    if (this.containerType() != 'object') {
      throw new Error('Unable to unset attribute of non-object container')
    }
    const nextValue = Object.assign({}, this._value)
    delete nextValue[key]
    return new ImmutableAccessor(nextValue, this.path)
  }

  // primitive writer interface
  mutate(fn: Function) {
    if (this.containerType() != 'primitive') {
      throw new Error("Won't mutate container types")
    }
    const nextValue = fn(this._value)
    return new ImmutableAccessor(nextValue, this.path)
  }
}


text:
from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Comment',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('object_id', models.PositiveIntegerField(db_index=True)),
                ('description', models.CharField(max_length=140)),
            ],
            options={
                'ordering': ['created'],
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Flag',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('object_id', models.PositiveIntegerField()),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Follow',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('object_id', models.PositiveIntegerField(db_index=True)),
            ],
            options={
                'ordering': ['created'],
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Like',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('object_id', models.PositiveIntegerField(db_index=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Share',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('object_id', models.PositiveIntegerField()),
                ('content_type', models.ForeignKey(to='contenttypes.ContentType', on_delete=models.CASCADE)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Tag',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True, null=True)),
                ('name', models.CharField(unique=True, max_length=75)),
            ],
            options={
                'abstract': False,
            },
            bases=(models.Model,),
        ),
    ]


text:
module.exports.init = require('./tasks/init');
module.exports.clean = require('./tasks/clean');
module.exports.build = require('./tasks/build');


text:
<?php
	namespace Register\AuthenticationService;

	class OAuth2 Extends Base {
		private $app_id;
		private $app_secret;
		private $redirect_uri = "/_register/oauth_callback";
		private $scopes;
		private $authority;
		private $authorize_endpoint = "/oauth2/v2.0/authorize";
		private $token_endpoint = "/oauth2/v2.0/token";
		public $authState;

		public function authClient() {
			return new \Microsoft\Model\GenericProvider([
				'clientId'					=> $this->app_id,
				'clientSecret'				=> $this->app_secret,
				'redirectUri'				=> $this->redirect_uri,
				'urlAuthorize'				=> $this->authority.$this->authorize_endpoint,
				'urlAccessToken'			=> $this->authority.$this->token_endpoint,
				'urlResourceOwnerDetails'	=> '',
				'scopes'					=> $this->scopes
			]);
		}
		public function authenticate($login,$password) {
			if (! $login) {
				app_log("No 'login' for authentication");
				return false;
			}

			$oAuthClient = $this->authClient();

			$authUrl = $oAuthClient->getAuthorizationUrl();
			$authState = $oAuthClient->getState();
			return true;
		}

		public function callBack() {
			$expectedState = $authState;
			unset($authState);

			if (!isset($_GET['state']) || !isset($_GET['code'])) {
				$this->error("No OAuth Session");
				return false;
			}

			$providedState = $_GET['state'];

			if (!isset($expectedState)) {
				$this->error("Expected State Not Available");
				return false;
			}

			if (!isset($providedState) || $expectedState != $providedState) {
				$this->error("State Does Not Match");
				return false;
			}

			$authCode = $_GET['code'];
			if (isset($authCode)) {
				$oAuthClient = new GenericProvider([
					'clientId'					=> $this->app_id,
					'clientSecret'				=> $this->app_secret,
					'redirectUri'				=> $this->redirect_uri,
					'urlAuthorize'				=> $this->authority.$this->authorize_endpoint,
					'urlAccessToken'			=> $this->authority.$this->token_endpoint,
					'urlResourceOwnerDetails'	=> '',
					'scopes'					=> $this->scopes
				]);
			}

			$accessToken = null;
			try {
				// Make the token request
				$accessToken = $oAuthClient->getAccessToken('authorization_code', [ 'code' => $authCode ]);
			}
			catch (\League\OAuth2\Client\Provider\Exception\IdentityPrividerException $e) {
				$this->error(urlencode($e->getMessage()));
				return false;
			}

			$user = [];
			if (null !== $accessToken) {
				$graph = new Graph();
				$graph->setAccessToken($accessToken->getToken());
				try {
					$azureUser = $graph->createRequest('GET','/me?$select=displayName,mail,userPrincipalName')
						->setReturnType(Model\User::class);

				}
				catch (Exception $exception) {
					$this->error('Unable to get user details: '.$exception->getMessage());
					return false;
				}

				$user = [
					'name'	=> $azureUser->getDisplayName(),
					'email'	=> $azureUser->getMail()
				];
			}
			return true;
		}

		public function changePassword($login,$password) {
			app_log($GLOBALS['_SESSION_']->customer->login." changing password for ".$this->login,'info');

			$this->error("OAuth2 password change not supported");
			return false;
		}
	}


text:
<?php

/**
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2015
 * $Id$
 *
 */

/**
 * This class provides the functionality for batch profile update for members
 */
class CRM_Member_Form_Task_Batch extends CRM_Member_Form_Task {

  /**
   * The title of the group.
   *
   * @var string
   */
  protected $_title;

  /**
   * Maximum profile fields that will be displayed.
   */
  protected $_maxFields = 9;

  /**
   * Variable to store redirect path.
   */
  protected $_userContext;

  /**
   * Build all the data structures needed to build the form.
   *
   * @return void
   */
  public function preProcess() {
    // initialize the task and row fields
    parent::preProcess();

    //get the contact read only fields to display.
    $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
      CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'contact_autocomplete_options',
        TRUE, NULL, FALSE, 'name', TRUE
      )
    );
    //get the read only field data.
    $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
    $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_memberIds,
      'CiviMember', $returnProperties
    );
    $this->assign('contactDetails', $contactDetails);
    $this->assign('readOnlyFields', $readOnlyFields);
  }

  /**
   * Build the form object.
   *
   *
   * @return void
   */
  public function buildQuickForm() {
    $ufGroupId = $this->get('ufGroupId');

    if (!$ufGroupId) {
      CRM_Core_Error::fatal('ufGroupId is missing');
    }
    $this->_title = ts('Batch Update for Members') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
    CRM_Utils_System::setTitle($this->_title);

    $this->addDefaultButtons(ts('Save'));
    $this->_fields = array();
    $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);

    // remove file type field and then limit fields
    $suppressFields = FALSE;
    $removehtmlTypes = array('File', 'Autocomplete-Select');
    foreach ($this->_fields as $name => $field) {
      if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
        in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
      ) {
        $suppressFields = TRUE;
        unset($this->_fields[$name]);
      }

      //fix to reduce size as we are using this field in grid
      if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) {
        //shrink class to "form-text-medium"
        $this->_fields[$name]['attributes']['size'] = 19;
      }
    }

    $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);

    $this->addButtons(array(
        array(
          'type' => 'submit',
          'name' => ts('Update Members(s)'),
          'isDefault' => TRUE,
        ),
        array(
          'type' => 'cancel',
          'name' => ts('Cancel'),
        ),
      )
    );

    $this->assign('profileTitle', $this->_title);
    $this->assign('componentIds', $this->_memberIds);
    $fileFieldExists = FALSE;

    //load all campaigns.
    if (array_key_exists('member_campaign_id', $this->_fields)) {
      $this->_componentCampaigns = array();
      CRM_Core_PseudoConstant::populate($this->_componentCampaigns,
        'CRM_Member_DAO_Membership',
        TRUE, 'campaign_id', 'id',
        ' id IN (' . implode(' , ', array_values($this->_memberIds)) . ' ) '
      );
    }

    $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
    foreach ($this->_memberIds as $memberId) {
      $typeId = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $memberId, 'membership_type_id');
      foreach ($this->_fields as $name => $field) {
        if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
          $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
          if (!empty($customValue['extends_entity_column_value'])) {
            $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
              $customValue['extends_entity_column_value']
            );
          }
          if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) ||
            CRM_Utils_System::isNull($entityColumnValue[$typeId])
          ) {
            CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId);
          }
        }
        else {
          // handle non custom fields
          CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId);
        }
      }
    }

    $this->assign('fields', $this->_fields);

    // don't set the status message when form is submitted.
    $buttonName = $this->controller->getButtonName('submit');

    if ($suppressFields && $buttonName != '_qf_Batch_next') {
      CRM_Core_Session::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Unsupported Field Type'), 'error');
    }

    $this->addDefaultButtons(ts('Update Memberships'));
  }

  /**
   * Set default values for the form.
   *
   *
   * @return void
   */
  public function setDefaultValues() {
    if (empty($this->_fields)) {
      return;
    }

    $defaults = array();
    foreach ($this->_memberIds as $memberId) {
      $details[$memberId] = array();
      CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $memberId, 'Membership');
    }

    return $defaults;
  }

  /**
   * Process the form after the input has been submitted and validated.
   *
   *
   * @return void
   */
  public function postProcess() {
    $params = $this->exportValues();
    $dates = array(
      'join_date',
      'membership_start_date',
      'membership_end_date',
    );
    if (isset($params['field'])) {
      $customFields = array();
      foreach ($params['field'] as $key => $value) {
        $ids['membership'] = $key;
        if (!empty($value['membership_source'])) {
          $value['source'] = $value['membership_source'];
        }

        if (!empty($value['membership_type'])) {
          $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
        }

        unset($value['membership_source']);
        unset($value['membership_type']);

        //Get the membership status
        $value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
        unset($value['membership_status']);
        foreach ($dates as $val) {
          if (isset($value[$val])) {
            $value[$val] = CRM_Utils_Date::processDate($value[$val]);
          }
        }
        if (empty($customFields)) {
          if (empty($value['membership_type_id'])) {
            $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
          }

          // membership type custom data
          $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);

          $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
            CRM_Core_BAO_CustomField::getFields('Membership',
              FALSE, FALSE, NULL, NULL, TRUE
            )
          );
        }
        //check for custom data
        $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
          $customFields,
          $key,
          'Membership',
          $membershipTypeId
        );

        $membership = CRM_Member_BAO_Membership::add($value, $ids);

        // add custom field values
        if (!empty($value['custom']) &&
          is_array($value['custom'])
        ) {
          CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
        }
      }

      CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
    }
    else {
      CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
    }
  }

}


text:
Writing Skia Tests
==================

+   [Unit Tests](#test)
+   [Rendering Tests](#gm)
+   [Benchmark Tests](#bench)

We assume you have already synced Skia's dependecies and set up Skia's build system.

    python tools/git-sync-deps
    bin/gn gen out/Debug
    bin/gn gen out/Release --args='is_debug=false'

Writing a Unit Test
-------------------

1.  Add a file `tests/NewUnitTest.cpp`:

        #include "Test.h"
        DEF_TEST(NewUnitTest, reporter) {
            if (1 + 1 != 2) {
                ERRORF(reporter, "%d + %d != %d", 1, 1, 2);
            }
            bool lifeIsGood = true;
            REPORTER_ASSERT(reporter, lifeIsGood);
        }

2.  Add `NewUnitTest.cpp` to `gn/tests.gni`.

3.  Recompile and run test:

        ninja -C out/Debug dm
        out/Debug/dm --match NewUnitTest

Writing a Rendering Test
------------------------

1.  Add a file `gm/newgmtest.cpp`:

        /*
         * Copyright ........
         *
         * Use of this source code is governed by a BSD-style license
         * that can be found in the LICENSE file.
         */
        #include "gm.h"
        DEF_SIMPLE_GM(newgmtest, canvas, 128, 128) {
            canvas->clear(SK_ColorWHITE);
            SkPaint p;
            p.setStrokeWidth(2);
            canvas->drawLine(16, 16, 112, 112, p);
        }

2.  Add `newgmtest.cpp` to `gn/gm.gni`.

3.  Recompile and run test:

        ninja -C out/Debug dm
        out/Debug/dm --match newgmtest

4.  Run the GM inside Viewer:

        ninja -C out/Debug viewer
        out/Debug/viewer --slide GM_newgmtest

Writing a Benchmark Test
------------------------

1.  Add a file `bench/FooBench.cpp`:

        /*
         * Copyright ........
         *
         * Use of this source code is governed by a BSD-style license
         * that can be found in the LICENSE file.
         */
        #include "Benchmark.h"
        #include "SkCanvas.h"
        namespace {
        class FooBench : public Benchmark {
        public:
            FooBench() {}
            virtual ~FooBench() {}
        protected:
            const char* onGetName() override { return "Foo"; }
            SkIPoint onGetSize() override { return SkIPoint{100, 100}; }
            void onDraw(int loops, SkCanvas* canvas) override {
                while (loops-- > 0) {
                    canvas->drawLine(0.0f, 0.0f, 100.0f, 100.0f, SkPaint());
                }
            }
        };
        }  // namespace
        DEF_BENCH(return new FooBench;)

2.  Add `FooBench.cpp` to `gn/bench.gni`.

3.  Recompile and run nanobench:

        ninja -C out/Release nanobench
        out/Release/nanobench --match Foo


text:
class ProtectedMediaIdentifierPermissionContext;
class Profile;

class ProtectedMediaIdentifierPermissionContextFactory
    : public BrowserContextKeyedServiceFactory {
 public:
  static ProtectedMediaIdentifierPermissionContext* GetForProfile(
      Profile* profile);

  static ProtectedMediaIdentifierPermissionContextFactory* GetInstance();

 private:
  friend struct base::DefaultSingletonTraits<
      ProtectedMediaIdentifierPermissionContextFactory>;

  ProtectedMediaIdentifierPermissionContextFactory();
  ~ProtectedMediaIdentifierPermissionContextFactory() override;

  // BrowserContextKeyedBaseFactory methods:
  KeyedService* BuildServiceInstanceFor(
      content::BrowserContext* profile) const override;
  void RegisterProfilePrefs(
      user_prefs::PrefRegistrySyncable* registry) override;
  content::BrowserContext* GetBrowserContextToUse(
      content::BrowserContext* context) const override;

  DISALLOW_COPY_AND_ASSIGN(
      ProtectedMediaIdentifierPermissionContextFactory);
};

#endif  // CHROME_BROWSER_MEDIA_PROTECTED_MEDIA_IDENTIFIER_PERMISSION_CONTEXT_FACTORY_H_


text:
@protocol BCOVPlaybackController;

@class BCOVCatalogService;
@class BCOVMediaRequestFactory;

@interface BCOVPlayerController : BaseViewController<BCOVPlaybackControllerDelegate>
{

}

@property (strong, nonatomic) id<BCOVPlaybackController> controller;

@property (strong, nonatomic) BCOVCatalogService *catalog;
@property (strong, nonatomic) BCOVMediaRequestFactory *mediaRequestFactory;

- (id) initWithVideoURL:(NSString *) url videoId:(NSString*) videoId;

@end


text:
title: "PoW vs. PoS"
date: "2018-11-09T11:00:13.121Z"
layout: post
draft: false
path: "/posts/sixty-fourth-post/"
category: "Blockchain"
tags:
  - "Bitcoin"
  - "Blockchain"
  - "P2P"
description: "This is a post about Ethereum Proof of Work versus Proof of Stake mining processes."
---

This is a post about the difference between Proof of Work versus Proof of Stake processes.

## Blockchain Basics: Consensus

Consensus is the state of agreement among each node in the blockchain network. In order to reach consensus, there are two main strategies employed: proof-of-work (PoW) and proof-of-stake (PoS).

## PoW vs. PoS: The Basics

I recommend watching this quick video: [PoW vs. PoS Video Explainer][https://www.youtube.com/watch?v=ASCGQFZgcT8&feature=youtu.be]

Proof of Work (PoW) is the method by which miners reach consensus by providing proof of solving increasingly complex mathematical problems. [PoW vs. PoS][https://www.ethnews.com/proof-of-work-vs-proof-of-stake-explained]

> Proof of Work (PoW)
>- Particpants only accept valid block when blockhash is less than target number
>- Miners in a network repeatedly hash the block contents and check the output to see if it matches some specific criteria.
>- When the correct block hash is found it is broadcasted throughout the entire network and the miner is awarded.
>- Blocks are mined a deterministic rate (every 15 seconds in the Ethereum Network)

[Consensys Academy Notes][https://github.com/ScottWorks/ConsenSys-Academy-Notes/blob/master/Part%201%20-%20Review%20of%20Blockchain%20Technology.md]

Proof of Stake (PoS) is when the creator of a new block is chosen via various combinations of random selection and wealth or age [Proof of Stake (PoS) - Wikipedia][https://en.wikipedia.org/wiki/Proof-of-stake].

> Proof of Stake (PoS)
>- Currency holders stake some amount of the currency for the chance to validate a block.
>- The block validator is randomly selected by the network but the chance of selection is proportional to the stake.

[Consensys Academy Notes][https://github.com/ScottWorks/ConsenSys-Academy-Notes/blob/master/Part%201%20-%20Review%20of%20Blockchain%20Technology.md]

For more in depth Proof of Work explainer video, watch this video from [Khan Academy][https://www.youtube.com/watch?v=9V1bipPkCTU&feature=youtu.be].

## PoW: Mining

>- In the Proof-of-Work consensus scheme, mining is the process of validating transaction blocks through hashing the block contents. This process makes it incredibly difficult to guess the correct answer but very easy to verify the correct guess has been made.
>- Each time a node wins the block they suggest the next state of the system and broadcast their solution to the network. Other nodes verify that the solution is correct and update their ledgers such that the validator recieves the block reward.
>- The difficulty is adjusted based on how quickly the previous block was mined.
>- Each block contains a list of transactions, the hash of the most recent block, nonce, and block reward.
>- The Ethereum proof-of-work algorithm abides by three main rules:
>>1.  Rejects invalid blocks
>>2.  Requires proof of work to contain a hash less than the target block #
>>3.  Longest chain is always considered to be the most up-to-date/ trusted

[Consensys Academy Notes][https://github.com/ScottWorks/ConsenSys-Academy-Notes/blob/master/Part%201%20-%20Review%20of%20Blockchain%20Technology.md]

## Issues with PoW: 51% attack && The Environment

### 51% attack

This occurs when the network that computes the transactions is controlled by a mining pool that does more then 51% of the mining for block transactions. This goes against decentralization and creates a control over the transactions being processed in the hands of a small number of individuals.

With this power, the mining pool could collectively invalidate transactions and double spend, which defeats the trustless protocols purpose.

For more on 51% attacks, read [PoS vs. PoW: 51% attack section][https://www.ethnews.com/proof-of-work-vs-proof-of-stake-explained].

### The Environment

It can require tremendous computing power to provide proof of work. This limits participation in mining, as well as creates negative externalities for the environment.

## Who uses PoW?

5 cryptocurrencies that use PoW:

[Ether][https://www.ethereum.org/ether]

[Bitcoin][https://bitcoin.org/en/]

[BitcoinCash][https://cryptoslate.com/coins/bitcoin-cash/]

[LiteCoin][https://cryptoslate.com/coins/litecoin/]

[Verge][https://cryptoslate.com/coins/verge/]

## Who uses PoS?

5 cryptocurrencies that use PoS:

[Dash][https://www.dash.org/]

[PeerCoin][https://peercoin.net/]

[NEO][https://en.wikipedia.org/wiki/NEO_(cryptocurrency)]

[Factom][https://www.factom.com/]

[PIVX][https://pivx.org/]

## Conclusion

Both Proof of Work (PoW) and Proof of Stake (PoS) are different methods for obtaining the same thing: consensus among nodes in a blockchain. The main difference is that instead of solving complex mathematical problems to validate blocks (PoW), PoS requires that one have a significant stake in the blockchain network chosen randomly to validate a block.

Thanks so much for reading! Now, you know the difference between proof of work and proof of stake!


text:
import { SearchBoxComponent } from './searchBox.component';

interface ITestObject {
	prop: string;
}

describe('SearchBoxComponent', () => {
	let searchBox: SearchBoxComponent<ITestObject>;
	let emitSpy: Sinon.SinonSpy;

	beforeEach(() => {
		searchBox = new SearchBoxComponent<ITestObject>();
		emitSpy = sinon.spy();
		searchBox.filteredItems = <any>{ emit: emitSpy };
	});

	it('should filter the items and emit the results', () => {
		const items: ITestObject[] = [
			{ prop: 'something' },
			{ prop: 'other' },
			{ prop: 'something 2' },
		];
		const expectedResults: ITestObject[] = [items[0], items[2]];
		searchBox.items = items;

		searchBox.search('something');

		sinon.assert.calledOnce(emitSpy);
		sinon.assert.calledWith(emitSpy, expectedResults);
	});
});


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PCDataDLL
{
    public class DBResult
    {
        private Dictionary<string, string> resultRow;

        public DBResult(Dictionary<string, string> results)
        {
            resultRow = results;
        }

        public string Get(string column)
        {
            if (resultRow.ContainsKey(column))
                return resultRow[column];
            if (resultRow.ContainsKey(column.ToUpper()))
                return resultRow[column.ToUpper()];
            return null;
        }

        public bool Exists()
        {
            return resultRow.Keys.Count > 0;
        }

        public int KeyCount()
        {
            return resultRow.Keys.Count;
        }

        public Dictionary<string, string>.KeyCollection GetKeyCollection()
        {
            return resultRow.Keys;
        }
    }
}


text:
package org.elasticsearch.snapshots;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRunnable;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.repositories.Repository;
import org.elasticsearch.repositories.RepositoryData;
import org.elasticsearch.repositories.RepositoryException;
import org.elasticsearch.repositories.ShardGenerations;
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
import org.elasticsearch.threadpool.ThreadPool;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.Locale;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;

public class CorruptedBlobStoreRepositoryIT extends AbstractSnapshotIntegTestCase {

    public void testConcurrentlyChangeRepositoryContents() throws Exception {
        Client client = client();

        Path repo = randomRepoPath();
        final String repoName = "test-repo";
        logger.info("-->  creating repository at {}", repo.toAbsolutePath());
        assertAcked(client.admin().cluster().preparePutRepository(repoName)
            .setType("fs").setSettings(Settings.builder()
                .put("location", repo)
                .put("compress", false)
                // Don't cache repository data because the test manually modifies the repository data
                .put(BlobStoreRepository.CACHE_REPOSITORY_DATA.getKey(), false)
                .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));

        createIndex("test-idx-1", "test-idx-2");
        logger.info("--> indexing some data");
        indexRandom(true,
            client().prepareIndex("test-idx-1").setSource("foo", "bar"),
            client().prepareIndex("test-idx-2").setSource("foo", "bar"));

        final String snapshot = "test-snap";

        logger.info("--> creating snapshot");
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapshot)
            .setWaitForCompletion(true).setIndices("test-idx-*").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(),
            equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));

        logger.info("--> move index-N blob to next generation");
        final RepositoryData repositoryData =
            getRepositoryData(internalCluster().getMasterNodeInstance(RepositoriesService.class).repository(repoName));
        Files.move(repo.resolve("index-" + repositoryData.getGenId()), repo.resolve("index-" + (repositoryData.getGenId() + 1)));

        assertRepositoryBlocked(client, repoName, snapshot);

        if (randomBoolean()) {
            logger.info("--> move index-N blob back to initial generation");
            Files.move(repo.resolve("index-" + (repositoryData.getGenId() + 1)), repo.resolve("index-" + repositoryData.getGenId()));

            logger.info("--> verify repository remains blocked");
            assertRepositoryBlocked(client, repoName, snapshot);
        }

        logger.info("--> remove repository");
        assertAcked(client.admin().cluster().prepareDeleteRepository(repoName));

        logger.info("--> recreate repository");
        assertAcked(client.admin().cluster().preparePutRepository(repoName)
            .setType("fs").setSettings(Settings.builder()
                .put("location", repo)
                .put("compress", false)
                .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));

        logger.info("--> delete snapshot");
        client.admin().cluster().prepareDeleteSnapshot(repoName, snapshot).get();

        logger.info("--> make sure snapshot doesn't exist");
        expectThrows(SnapshotMissingException.class, () -> client.admin().cluster().prepareGetSnapshots(repoName)
            .addSnapshots(snapshot).get().getSnapshots(repoName));
    }

    public void testFindDanglingLatestGeneration() throws Exception {
        Path repo = randomRepoPath();
        final String repoName = "test-repo";
        logger.info("-->  creating repository at {}", repo.toAbsolutePath());
        assertAcked(client().admin().cluster().preparePutRepository(repoName)
            .setType("fs").setSettings(Settings.builder()
                .put("location", repo)
                .put("compress", false)
                .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));

        createIndex("test-idx-1", "test-idx-2");
        logger.info("--> indexing some data");
        indexRandom(true,
            client().prepareIndex("test-idx-1").setSource("foo", "bar"),
            client().prepareIndex("test-idx-2").setSource("foo", "bar"));

        final String snapshot = "test-snap";

        logger.info("--> creating snapshot");
        CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshot)
            .setWaitForCompletion(true).setIndices("test-idx-*").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(),
            equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));

        final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);

        logger.info("--> move index-N blob to next generation");
        final RepositoryData repositoryData = getRepositoryData(repository);
        final long beforeMoveGen = repositoryData.getGenId();
        Files.move(repo.resolve("index-" + beforeMoveGen), repo.resolve("index-" + (beforeMoveGen + 1)));

        logger.info("--> set next generation as pending in the cluster state");
        final PlainActionFuture<Void> csUpdateFuture = PlainActionFuture.newFuture();
        internalCluster().getCurrentMasterNodeInstance(ClusterService.class).submitStateUpdateTask("set pending generation",
            new ClusterStateUpdateTask() {
                @Override
                public ClusterState execute(ClusterState currentState) {
                    return ClusterState.builder(currentState).metadata(Metadata.builder(currentState.getMetadata())
                        .putCustom(RepositoriesMetadata.TYPE,
                            currentState.metadata().<RepositoriesMetadata>custom(RepositoriesMetadata.TYPE).withUpdatedGeneration(
                                repository.getMetadata().name(), beforeMoveGen, beforeMoveGen + 1)).build()).build();
                }

                @Override
                public void onFailure(String source, Exception e) {
                    csUpdateFuture.onFailure(e);
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                    csUpdateFuture.onResponse(null);
                }
            }
        );
        csUpdateFuture.get();

        logger.info("--> full cluster restart");
        internalCluster().fullRestart();
        ensureGreen();

        Repository repositoryAfterRestart = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);

        logger.info("--> verify index-N blob is found at the new location");
        assertThat(getRepositoryData(repositoryAfterRestart).getGenId(), is(beforeMoveGen + 1));

        logger.info("--> delete snapshot");
        client().admin().cluster().prepareDeleteSnapshot(repoName, snapshot).get();

        logger.info("--> verify index-N blob is found at the expected location");
        assertThat(getRepositoryData(repositoryAfterRestart).getGenId(), is(beforeMoveGen + 2));

        logger.info("--> make sure snapshot doesn't exist");
        expectThrows(SnapshotMissingException.class, () -> client().admin().cluster().prepareGetSnapshots(repoName)
            .addSnapshots(snapshot).get().getSnapshots(repoName));
    }

    public void testHandlingMissingRootLevelSnapshotMetadata() throws Exception {
        Path repo = randomRepoPath();
        final String repoName = "test-repo";
        logger.info("-->  creating repository at {}", repo.toAbsolutePath());
        assertAcked(client().admin().cluster().preparePutRepository(repoName)
            .setType("fs").setSettings(Settings.builder()
                .put("location", repo)
                .put("compress", false)
                // Don't cache repository data because the test manually modifies the repository data
                .put(BlobStoreRepository.CACHE_REPOSITORY_DATA.getKey(), false)
                .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));

        final String snapshotPrefix = "test-snap-";
        final int snapshots = randomIntBetween(1, 2);
        logger.info("--> creating [{}] snapshots", snapshots);
        for (int i = 0; i < snapshots; ++i) {
            // Workaround to simulate BwC situation: taking a snapshot without indices here so that we don't create any new version shard
            // generations (the existence of which would short-circuit checks for the repo containing old version snapshots)
            CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, snapshotPrefix + i)
                .setIndices().setWaitForCompletion(true).get();
            assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), is(0));
            assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(),
                equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
        }
        final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);
        final RepositoryData repositoryData = getRepositoryData(repository);

        final SnapshotId snapshotToCorrupt = randomFrom(repositoryData.getSnapshotIds());
        logger.info("--> delete root level snapshot metadata blob for snapshot [{}]", snapshotToCorrupt);
        Files.delete(repo.resolve(String.format(Locale.ROOT, BlobStoreRepository.SNAPSHOT_NAME_FORMAT, snapshotToCorrupt.getUUID())));

        logger.info("--> strip version information from index-N blob");
        final RepositoryData withoutVersions = new RepositoryData(repositoryData.getGenId(),
            repositoryData.getSnapshotIds().stream().collect(Collectors.toMap(
                SnapshotId::getUUID, Function.identity())),
            repositoryData.getSnapshotIds().stream().collect(Collectors.toMap(
                SnapshotId::getUUID, repositoryData::getSnapshotState)),
            Collections.emptyMap(), Collections.emptyMap(), ShardGenerations.EMPTY);

        Files.write(repo.resolve(BlobStoreRepository.INDEX_FILE_PREFIX + withoutVersions.getGenId()),
            BytesReference.toBytes(BytesReference.bytes(withoutVersions.snapshotsToXContent(XContentFactory.jsonBuilder(),
                true))), StandardOpenOption.TRUNCATE_EXISTING);

        logger.info("--> verify that repo is assumed in old metadata format");
        final SnapshotsService snapshotsService = internalCluster().getCurrentMasterNodeInstance(SnapshotsService.class);
        final ThreadPool threadPool = internalCluster().getCurrentMasterNodeInstance(ThreadPool.class);
        assertThat(PlainActionFuture.get(f -> threadPool.generic().execute(
            ActionRunnable.supply(f, () ->
                snapshotsService.minCompatibleVersion(Version.CURRENT, repoName, getRepositoryData(repository), null)))),
            is(SnapshotsService.OLD_SNAPSHOT_FORMAT));

        logger.info("--> verify that snapshot with missing root level metadata can be deleted");
        assertAcked(client().admin().cluster().prepareDeleteSnapshot(repoName, snapshotToCorrupt.getName()).get());

        logger.info("--> verify that repository is assumed in new metadata format after removing corrupted snapshot");
        assertThat(PlainActionFuture.get(f -> threadPool.generic().execute(
            ActionRunnable.supply(f, () ->
                snapshotsService.minCompatibleVersion(Version.CURRENT, repoName, getRepositoryData(repository), null)))),
            is(Version.CURRENT));
        final RepositoryData finalRepositoryData = getRepositoryData(repository);
        for (SnapshotId snapshotId : finalRepositoryData.getSnapshotIds()) {
            assertThat(finalRepositoryData.getVersion(snapshotId), is(Version.CURRENT));
        }
    }

    public void testMountCorruptedRepositoryData() throws Exception {
        disableRepoConsistencyCheck("This test intentionally corrupts the repository contents");
        Client client = client();

        Path repo = randomRepoPath();
        final String repoName = "test-repo";
        logger.info("-->  creating repository at {}", repo.toAbsolutePath());
        assertAcked(client.admin().cluster().preparePutRepository(repoName)
            .setType("fs").setSettings(Settings.builder()
                .put("location", repo)
                // Don't cache repository data because the test manually modifies the repository data
                .put(BlobStoreRepository.CACHE_REPOSITORY_DATA.getKey(), false)
                .put("compress", false)));

        final String snapshot = "test-snap";

        logger.info("--> creating snapshot");
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapshot)
            .setWaitForCompletion(true).setIndices("test-idx-*").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(),
            equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));

        logger.info("--> corrupt index-N blob");
        final Repository repository = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(repoName);
        final RepositoryData repositoryData = getRepositoryData(repository);
        Files.write(repo.resolve("index-" + repositoryData.getGenId()), randomByteArrayOfLength(randomIntBetween(1, 100)));

        logger.info("--> verify loading repository data throws RepositoryException");
        expectThrows(RepositoryException.class, () -> getRepositoryData(repository));

        logger.info("--> mount repository path in a new repository");
        final String otherRepoName = "other-repo";
        assertAcked(client.admin().cluster().preparePutRepository(otherRepoName)
            .setType("fs").setSettings(Settings.builder()
                .put("location", repo)
                .put("compress", false)));
        final Repository otherRepo = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class).repository(otherRepoName);

        logger.info("--> verify loading repository data from newly mounted repository throws RepositoryException");
        expectThrows(RepositoryException.class, () -> getRepositoryData(otherRepo));
    }

    private void assertRepositoryBlocked(Client client, String repo, String existingSnapshot) {
        logger.info("--> try to delete snapshot");
        final RepositoryException repositoryException3 = expectThrows(RepositoryException.class,
            () -> client.admin().cluster().prepareDeleteSnapshot(repo, existingSnapshot).execute().actionGet());
        assertThat(repositoryException3.getMessage(),
            containsString("Could not read repository data because the contents of the repository do not match its expected state."));

        logger.info("--> try to create snapshot");
        final RepositoryException repositoryException4 = expectThrows(RepositoryException.class,
            () -> client.admin().cluster().prepareCreateSnapshot(repo, existingSnapshot).execute().actionGet());
        assertThat(repositoryException4.getMessage(),
            containsString("Could not read repository data because the contents of the repository do not match its expected state."));
    }
}


text: import typeof {typeof string} from "foo";


text:
layout: indianlands
title: "Federal Recognition of Tribe Granted"
year: "2007-2016"
description: "Indian <strike>Lands</strike>"
date: 2017-08-27 15:59:15 -0700
categories: project
tags: aftermath, fallout, tribe without tribal land, coming full circle, tribal court, Tale of Tisquantum, medicine circle
---
- Newspaper article
- Federal Recognition Granted
- 2008 Ruling on Amelia Bingham Class Action Against the Commonwealth of Massachusetts: No Individual Grounds for Harm

The aftermath of 400 years of arbitrary, legal fashion that have lead to cultural genocide, ecocide,
displacement, colonization of the mind, and lateral violence can hardly have been said to have come full circle
when the Mashpee-Wampanoag finally did receive Federal Recognition as a Tribe in 2007. Not only
was a massive campaign to collect every Wampanoag's birth certificate to show that, racially, yes the
majority were Indian and Wampanoag (not 'triracial isolates', but also many Wampanoag women's rights,
dignity, and cultural practices were sacrificed in the process. A key example is Sturgis's family. But another
is the cases shown by Amelia Bingham - a key player in organizing the tribal council, the Wampanoag
museum, establishing a Massachusetts Commissioner on Indian Affairs, and re-writing the tribal history in
line with both ethnohistorical accounts, oral traditions, and actual archival documents. Bingham's case is
exceptional not only because her successes irritated the newly formed tribal council of Wampanoag men - who failing in their quest for tribal recognition - proceeded to issue a 'formal ban' on her presence in
"tribal affairs". But it is also interesting because of the grounds on which she, a culturally and financially
powerful female Wampanoag, was both advised to and those grounds lost her class action case. The
grounds were: she was an 'individual' (not endorsed by the all-male Wampanoag council) and she showed
no "harm" for lands lost.

Class Action cases are meant to be taken up for collective rights. It's true, but having no council to back
her, Bingham took the case anyway on behalf of the tribe (it says nowhere one has to be the employer or
hold a very specific office to take a class action on behalf of others - just that they share a class of
relations e.g. are all Indian, or all employees). But the tribal council of men saw it as against their interests
in power, not being Indian, to not back Bingham so they objected. But aside from that key detail about
the internal, class and gender politics within a tribe, Bingham herself had allotment land and was in no
way suffering. She and members of her family ended up on the right side of history given the fall-outs
that happened to Sturgis's following 1977. However, she still invested heavily in a lawyer to take the case
because of a sense of injustice happening to and within Mashpee-Wampanoag worlds - a sense held
collectively, but not as forcefully as Bingham. The important thing to note, however, in terms of justice
coming full-circle if it ever is to do so - is that Sturgis' herself and family has suffered harm as a direct
consequence of being displaced from their family allotment land - given to them because they were
Indian, but not protected no doubt also because of racism, classism, sexism, and because they were Indian - by the Commonwealth of Massachusetts.

So how do we take the individual evidence of "harm" - linked to wider, demographic studies of historical
and intergenerational trauma in Indian Country - as well as often healed by 'culture' - as a capacity for
culture as a path forward to justice?


text:
<?php

/**
 * @file
 * Test if Upload progress is available and working.
 */

$success = FALSE;

// Check uploadprogress extension is loaded.
if (extension_loaded('uploadprogress')) {
  $success = TRUE;
  print "Upload progress working properly.\r\n";
  exit(0);
}

if (!$success) {
  print "Upload progress not working properly.\r\n";
  exit(1);
}


text:
@interface Test : NSObject

@end


text:
require 'spec_helper'

describe "TicketKinds" do
end