@Test
public void testELValidity_atTestMatrixLoadTime() throws IncompatibleTestMatrixException, IOException {
final List<TestBucket> buckets = fromCompactBucketFormat("inactive:-1,control:0,test:1");
{
//testing recognition of test constants
final ConsumableTestDefinition testDefValConstants = constructDefinition(buckets,
fromCompactAllocationFormat("${proctor:now()>time}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
final Map<String, Object> providedConstantsVal = new HashMap<String, Object>();
providedConstantsVal.put("time", "1");
testDefValConstants.setConstants(providedConstantsVal);
final Map<String, String> providedContext = Collections.emptyMap();
ProctorUtils.verifyInternallyConsistentDefinition("testELevalwithcontext", "test context recognition", testDefValConstants, RuleEvaluator.FUNCTION_MAPPER,
ProctorUtils.convertContextToTestableMap(providedContext));
}
{//test if the providedContext is read in correctly
final ConsumableTestDefinition testDefValConstants2 = constructDefinition(buckets,
fromCompactAllocationFormat("${proctor:now()>time}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
final ObjectMapper objectMapper = new ObjectMapper();
final ProctorSpecification spec = objectMapper.readValue(getClass().getResourceAsStream("no-context-specification.json"), ProctorSpecification.class);
final Map<String, String> providedContext2 = spec.getProvidedContext(); //needs to read in empty provided context as Collections.emptyMap() and not null
try {
ProctorUtils.verifyInternallyConsistentDefinition("testELevalwithcontext", "test context recognition", testDefValConstants2, RuleEvaluator.FUNCTION_MAPPER,
ProctorUtils.convertContextToTestableMap(providedContext2));
fail("expected IncompatibleTestMatrixException");
} catch (IncompatibleTestMatrixException e) {
//expected
}
}
{//test that an error is thrown with missing providedContext
final ConsumableTestDefinition testDef = constructDefinition(buckets,
fromCompactAllocationFormat("${time eq ''}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
try {
ProctorUtils.verifyInternallyConsistentDefinition("testProvidedContextMissing", "test Provided Context Missing", testDef, RuleEvaluator.FUNCTION_MAPPER, new ProvidedContext(ProvidedContext.EMPTY_CONTEXT,true));
//checking to make sure it can evaluate with converted provided context
fail("expected IncompatibleTestMatrixException due to missing provided Context");
} catch (IncompatibleTestMatrixException e) {
//expected
}
}
{//testing recognition of providedContext in testRule
final Map<String, String> providedContextVal = new HashMap<String, String>();
providedContextVal.put("time", "Integer");
ConsumableTestDefinition testDefValContextTestRule = constructDefinition(buckets,
fromCompactAllocationFormat("${proctor:now()>-1}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
testDefValContextTestRule.setRule("${proctor:now()>time}");
ProctorUtils.verifyInternallyConsistentDefinition("testELevalwithcontext", "test context recognition in test rule", testDefValContextTestRule, RuleEvaluator.FUNCTION_MAPPER,
ProctorUtils.convertContextToTestableMap(providedContextVal));
}
{ //testing that invalid properties are recognized
final ConsumableTestDefinition testDef = constructDefinition(buckets,
fromCompactAllocationFormat("${ua.iPad}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
final Map<String, String> providedContextClass = new HashMap<String, String>();
providedContextClass.put("ua", "com.indeed.proctor.common.TestRulesClass");
final ProvidedContext providedContext = ProctorUtils.convertContextToTestableMap(providedContextClass);
try {
ProctorUtils.verifyInternallyConsistentDefinition("testProvidedContextConversion", "test Provided Context Conversion Class", testDef, RuleEvaluator.FUNCTION_MAPPER, providedContext);
fail("expected IncompatibleTestMatrixException due to missing attribute");
} catch (IncompatibleTestMatrixException e) {
// expected due to incorrect spelling
}
}
{ //testing that valid properties are recognized
final ConsumableTestDefinition testDef = constructDefinition(buckets,
fromCompactAllocationFormat("${ua.IPad}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
final Map<String, String> providedContextClass = new HashMap<String, String>();
providedContextClass.put("ua", "com.indeed.proctor.common.TestRulesClass");
final ProvidedContext providedContext = ProctorUtils.convertContextToTestableMap(providedContextClass);
ProctorUtils.verifyInternallyConsistentDefinition("testProvidedContextConversion", "test Provided Context Conversion Class", testDef, RuleEvaluator.FUNCTION_MAPPER, providedContext);
}
{ //testing that invalid functions are recognized
final ConsumableTestDefinition testDef = constructDefinition(buckets,
fromCompactAllocationFormat("${proctor:notafunction(5)}|-1:0.5,0:0.5,1:0.0", "-1:0.25,0:0.5,1:0.25"));
try {
ProctorUtils.verifyInternallyConsistentDefinition("testProvidedContextConversion", "test Provided Context Conversion Class", testDef, RuleEvaluator.FUNCTION_MAPPER, new ProvidedContext(ProvidedContext.EMPTY_CONTEXT,true));
fail("expected IncompatibleTestMatrixException due to missing function");