package org.hisp.dhis.rules;

import org.hisp.dhis.rules.models.Rule;
import org.hisp.dhis.rules.models.RuleAction;
import org.hisp.dhis.rules.models.RuleActionAssign;
import org.hisp.dhis.rules.models.RuleEffect;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static junit.framework.TestCase.fail;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class RuleEngineExecutionShould {

    @Mock
    private RuleExpressionEvaluator evaluator;

    @Mock
    private Rule rule;

    @Mock
    private List<RuleAction> actions;

    @Mock
    private RuleActionAssign ruleActionAssign;

    @Mock
    private HashMap<String, RuleVariableValue> variableMap;

    // Object to test
    private RuleEngineExecution ruleEngineExecution;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        when(rule.actions()).thenReturn(actions);
        when(actions.get(0)).thenReturn(RuleActionAssign.create("content", "data", "field"));

        ruleEngineExecution = new RuleEngineExecution(evaluator, Collections.singletonList(rule), variableMap);
    }

    @Test
    public void do_not_crash_on_malformed_expression() throws Exception {
        when(rule.condition()).thenReturn("d2:ceil(4 > 2");

        List<RuleEffect> effects = ruleEngineExecution.call();

        assertThat(effects).isEmpty();
    }

}

@@ -390,33 +390,8 @@
 s;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -463,107 +463,14 @@
 til.
-HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0Aimport static junit.framework.TestCase.fail;
+List;%0A
 %0Aimp
@@ -780,66 +780,8 @@
 s;%0A%0A
-    @Mock%0A    private RuleActionAssign ruleActionAssign;%0A%0A
     

remove unsed imports