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

com.indeed.proctor.common.model.ConsumableTestDefinition Maven / Gradle / Ivy

The newest version!
package com.indeed.proctor.common.model;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.indeed.proctor.common.ProctorUtils;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Models a single test
 *
 * @author ketan
 */
public class ConsumableTestDefinition {
    @Nonnull private Map constants = Collections.emptyMap();
    private String version;
    @Nullable private String salt;
    @Nullable private String rule;
    @Nonnull private List buckets = Collections.emptyList();
    @Nonnull private List allocations = Collections.emptyList();
    private boolean silent = false;

    @Nonnull private TestType testType;
    @Nullable private String description;
    @Nonnull private List metaTags = Collections.emptyList();

    /** @see TestDefinition#getDependsOn() */
    @Nullable private TestDependency dependsOn;

    private boolean isDynamic = false;
    private boolean evaluateForIncognitoUsers = false;
    private boolean enableUnitlessAllocations = false;
    private boolean containsUnitlessAllocation = false;
    private boolean forceLogging = false;

    public ConsumableTestDefinition() {
        /* intentionally empty */
    }

    /**
     * @deprecated Use {@link #fromTestDefinition(TestDefinition)} and {@link
     *     TestDefinition#builder()}
     */
    @Deprecated
    public ConsumableTestDefinition(
            final String version,
            @Nullable final String rule,
            @Nonnull final TestType testType,
            @Nullable final String salt,
            @Nonnull final List buckets,
            @Nonnull final List allocations,
            @Nonnull final Map constants,
            @Nullable final String description) {
        this(
                version,
                rule,
                testType,
                salt,
                buckets,
                allocations,
                false,
                constants,
                description,
                Collections.emptyList());
    }

    /**
     * @deprecated Use {@link #fromTestDefinition(TestDefinition)} and {@link
     *     TestDefinition#builder()}
     */
    @Deprecated
    public ConsumableTestDefinition(
            final String version,
            @Nullable final String rule,
            @Nonnull final TestType testType,
            @Nullable final String salt,
            @Nonnull final List buckets,
            @Nonnull final List allocations,
            final boolean silent,
            @Nonnull final Map constants,
            @Nullable final String description) {
        this(
                version,
                rule,
                testType,
                salt,
                buckets,
                allocations,
                silent,
                constants,
                description,
                Collections.emptyList());
    }

    /**
     * @deprecated Use {@link #fromTestDefinition(TestDefinition)} and {@link
     *     TestDefinition#builder()}
     */
    @Deprecated
    public ConsumableTestDefinition(
            final String version,
            @Nullable final String rule,
            @Nonnull final TestType testType,
            @Nullable final String salt,
            @Nonnull final List buckets,
            @Nonnull final List allocations,
            final boolean silent,
            @Nonnull final Map constants,
            @Nullable final String description,
            @Nonnull final List metaTags) {
        this.constants = constants;
        this.version = version;
        this.salt = salt;
        this.rule = rule;
        this.buckets = buckets;
        this.allocations = allocations;
        this.silent = silent;
        this.testType = testType;
        this.description = description;
        this.metaTags = metaTags;
    }

    // intentionally private to avoid creating deprecated constructors
    private ConsumableTestDefinition(
            final String version,
            @Nullable final String rule,
            @Nonnull final TestType testType,
            @Nullable final String salt,
            @Nonnull final List buckets,
            @Nonnull final List allocations,
            final boolean silent,
            @Nonnull final Map constants,
            @Nullable final String description,
            @Nonnull final List metaTags,
            @Nullable final TestDependency dependsOn,
            final boolean evaluateForIncognitoUsers,
            final boolean enableUnitlessAllocations,
            final boolean containsUnitlessAllocation,
            final boolean forceLogging) {
        this.constants = constants;
        this.version = version;
        this.salt = salt;
        this.rule = rule;
        this.buckets = buckets;
        this.allocations = allocations;
        this.silent = silent;
        this.testType = testType;
        this.description = description;
        this.metaTags = metaTags;
        this.dependsOn = dependsOn;
        this.evaluateForIncognitoUsers = evaluateForIncognitoUsers;
        this.enableUnitlessAllocations = enableUnitlessAllocations;
        this.containsUnitlessAllocation = containsUnitlessAllocation;
        this.forceLogging = forceLogging;
    }

    @Nonnull
    public Map getConstants() {
        return constants;
    }

    public void setConstants(@Nonnull final Map constants) {
        this.constants = constants;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(final String version) {
        this.version = version;
    }

    @Nullable
    public String getRule() {
        return rule;
    }

    public void setRule(@Nullable final String rule) {
        this.rule = rule;
    }

    @Nullable
    public String getSalt() {
        return salt;
    }

    public void setSalt(@Nullable final String salt) {
        this.salt = salt;
    }

    @Nonnull
    public List getBuckets() {
        return buckets;
    }

    public void setBuckets(@Nonnull final List buckets) {
        this.buckets = buckets;
    }

    @Nonnull
    public List getAllocations() {
        return allocations;
    }

    public void setAllocations(@Nonnull final List allocations) {
        this.allocations = allocations;
    }

    public void setSilent(final boolean silent) {
        this.silent = silent;
    }

    public boolean getSilent() {
        return silent;
    }

    public void setDynamic(final boolean dynamic) {
        this.isDynamic = dynamic;
    }

    public boolean getDynamic() {
        return isDynamic;
    }

    @Nonnull
    public TestType getTestType() {
        return testType;
    }

    public void setTestType(@Nonnull final TestType testType) {
        this.testType = testType;
    }

    @Nullable
    public String getDescription() {
        return description;
    }

    public void setDescription(@Nullable final String description) {
        this.description = description;
    }

    /** metaTags allow to group and filter tests. */
    @Nonnull
    public List getMetaTags() {
        return this.metaTags;
    }

    public void setMetaTags(final List metaTags) {
        this.metaTags = metaTags;
    }

    /** @see TestDefinition#getDependsOn() */
    @Nullable
    public TestDependency getDependsOn() {
        return dependsOn;
    }

    public void setDependsOn(@Nullable final TestDependency dependsOn) {
        this.dependsOn = dependsOn;
    }

    public void setEvaluateForIncognitoUsers(final boolean evaluateForIncognitoUsers) {
        this.evaluateForIncognitoUsers = evaluateForIncognitoUsers;
    }

    public boolean getEvaluateForIncognitoUsers() {
        return evaluateForIncognitoUsers;
    }

    public boolean getEnableUnitlessAllocations() {
        return enableUnitlessAllocations;
    }

    public void setEnableUnitlessAllocations(final boolean enableUnitlessAllocations) {
        this.enableUnitlessAllocations = enableUnitlessAllocations;
    }

    public boolean getContainsUnitlessAllocation() {
        return containsUnitlessAllocation;
    }

    public void setContainsUnitlessAllocation(final boolean containsUnitlessAllocation) {
        this.containsUnitlessAllocation = containsUnitlessAllocation;
    }

    public boolean getForceLogging() {
        return forceLogging;
    }

    public void setForceLogging(final boolean forceLogging) {
        this.forceLogging = forceLogging;
    }

    @Nonnull
    public static ConsumableTestDefinition fromTestDefinition(@Nonnull final TestDefinition td) {
        final Map specialConstants = td.getSpecialConstants();

        final List ruleComponents = Lists.newArrayList();
        //noinspection unchecked
        final List countries = (List) specialConstants.get("__COUNTRIES");
        if (countries != null) {
            ruleComponents.add("proctor:contains(__COUNTRIES, country)");
        }
        final String rawRule = ProctorUtils.removeElExpressionBraces(td.getRule());
        if (!StringUtils.isBlank(rawRule)) {
            ruleComponents.add(rawRule);
        }

        final String rule;
        if (ruleComponents.isEmpty()) {
            rule = null;
        } else {
            rule = "${" + String.join(" && ", ruleComponents) + '}';
        }

        final List allocations = td.getAllocations();
        for (final Allocation alloc : allocations) {
            final String rawAllocRule = ProctorUtils.removeElExpressionBraces(alloc.getRule());
            if (StringUtils.isBlank(rawAllocRule)) {
                alloc.setRule(null);
            } else {
                // ensure that all rules in the generated test-matrix are wrapped in "${" ... "}"
                if (!(rawAllocRule.startsWith("${") && rawAllocRule.endsWith("}"))) {
                    final String newAllocRule = "${" + rawAllocRule + "}";
                    alloc.setRule(newAllocRule);
                }
            }
        }
        final boolean containsUnitlessAllocation = ProctorUtils.containsUnitlessAllocation(td);

        final Map constants = Maps.newLinkedHashMap();
        constants.putAll(td.getConstants());
        constants.putAll(specialConstants);

        return new ConsumableTestDefinition(
                td.getVersion(),
                rule,
                td.getTestType(),
                td.getSalt(),
                td.getBuckets(),
                allocations,
                td.getSilent(),
                constants,
                td.getDescription(),
                td.getMetaTags(),
                td.getDependsOn(),
                td.getEvaluateForIncognitoUsers(),
                td.getEnableUnitlessAllocations(),
                containsUnitlessAllocation,
                td.getForceLogging());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy