package owltools.gaf.rules;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;

import owltools.gaf.GafDocument;
import owltools.gaf.GafObjectsBuilder;
import owltools.gaf.rules.go.GoAnnotationRulesFactoryImpl;

public class AnnotationRulesEngineTest {

	private static final String LOCATION = "src/test/resources/rules/";
	private static AnnotationRulesEngine engine = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		String qcfile = LOCATION + "annotation_qc.xml";
		String xrfabbslocation = LOCATION + "GO.xrf_abbs";
		String ontologylocation = LOCATION + "taxon/gene_ontology_ext.obo";
		List<String> taxonomylocation = Arrays.asList(LOCATION + "taxon/gene_ontology_ext.obo", 
				LOCATION + "taxon/ncbi_taxon_slim.obo",
				LOCATION + "taxon/taxon_go_triggers.obo", 
				LOCATION + "taxon/taxon_union_terms.obo");
		
		AnnotationRulesFactory rulesFactory = new GoAnnotationRulesFactoryImpl(
				qcfile, xrfabbslocation, ontologylocation, taxonomylocation);
		engine = new AnnotationRulesEngine(-1, rulesFactory);
	}

	@Test
	public void testValidateAnnotations() throws Exception {
		GafObjectsBuilder builder = new GafObjectsBuilder();
		GafDocument gafdoc = builder.buildDocument("src/test/resources/test_gene_association_mgi.gaf");			
		Map<String, List<AnnotationRuleViolation>> allViolations = engine.validateAnnotations(gafdoc);
		
		assertFalse(allViolations.isEmpty());
		renderViolations(allViolations);
	}

	private static void renderViolations(Map<String, List<AnnotationRuleViolation>> allViolations) {
		System.out.println("------------");
		List<String> ruleIds = new ArrayList<String>(allViolations.keySet());
		Collections.sort(ruleIds);
		for (String ruleId : ruleIds) {
			List<AnnotationRuleViolation> violationList = allViolations.get(ruleId);
			System.out.println(ruleId + "  count: "+ violationList.size());
			for (AnnotationRuleViolation violation : violationList) {
				StringBuilder sb = new StringBuilder("Line ");
				sb.append(violation.getLineNumber());
				sb.append(": ");
				sb.append(violation.getMessage());
				System.out.println(sb);
			}
			System.out.println("------------");
		}
	}

}

@@ -414,16 +414,66 @@
 Test %7B%0A%0A
+%09private static boolean renderViolations = false;%0A
 %09private
@@ -1597,40 +1597,346 @@
 sert
-False(allViolations.isEmpty());%0A
+Equals(4, allViolations.size()); // 4 types of rule violations%0A%09%09assertEquals(2, allViolations.get(%22GO_AR:0000001%22).size());%0A%09%09assertEquals(9, allViolations.get(%22GO_AR:0000003%22).size());%0A%09%09assertEquals(7, allViolations.get(%22GO_AR:0000013%22).size());%0A%09%09assertEquals(1, allViolations.get(%22GO_AR:0000014%22).size());%0A%09%09if (renderViolations) %7B%0A%09
 %09%09re
@@ -1966,16 +1966,20 @@
 tions);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pri

update test case