@Nonnull public static ConsumableTestDefinition convertToConsumableTestDefinition( @Nonnull final TestDefinition td) { final Map<String, Object> specialConstants = td.getSpecialConstants(); final List<String> ruleComponents = Lists.newArrayList(); //noinspection unchecked final List<String> countries = (List<String>) specialConstants.get("__COUNTRIES"); if (countries != null) { ruleComponents.add("proctor:contains(__COUNTRIES, country)"); } if (td.getRule() != null) { ruleComponents.add(td.getRule()); } final String rule; if (ruleComponents.isEmpty()) { rule = null; } else { final StringBuilder ruleBuilder = new StringBuilder("${"); for (int i = 0; i < ruleComponents.size(); i++) { if (i != 0) { ruleBuilder.append(" && "); } ruleBuilder.append(ruleComponents.get(i)); } ruleBuilder.append("}"); rule = ruleBuilder.toString(); } final List<Allocation> allocations = td.getAllocations(); for (final Allocation alloc : allocations) { final String allocRule = alloc.getRule(); if (allocRule != null && !(allocRule.startsWith("${") && allocRule.endsWith("}"))) { final String newAllocRule = "${" + allocRule + "}"; alloc.setRule(newAllocRule); } } final Map<String, Object> constants = Maps.newLinkedHashMap(); constants.putAll(td.getConstants()); constants.putAll(specialConstants); return new ConsumableTestDefinition( td.getVersion(), rule, td.getTestType(), td.getSalt(), td.getBuckets(), allocations, constants, td.getDescription()); }