package edu.ucsd.sbrg.escher;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import edu.ucsd.sbrg.escher.utilities.Validation;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Created by Devesh Khandelwal on 27-06-2016.
 */
public class ValidationTest {

  public Validation validation;
  public File file;

  @Before
  public void setUp() {
    validation = null;
  }


  @Ignore("Any schema is valid as long as it has an 'id' field. So, this test is pointless right "
      + "now.")
  @Test(expected = IllegalArgumentException.class)
  public void failsOnInvalidJsonSchemaFile() throws IOException, ProcessingException {
    JsonNode jsonFile = JsonLoader.fromPath("data/e_coli_core.escher.json");

    validation = new Validation(jsonFile);
  }


  @Test
  public void canValidateJsonSchemaFile() throws IOException, ProcessingException {
    JsonNode jsonFile = JsonLoader.fromPath("data/cobra_json_schema.json");

    validation = new Validation(jsonFile);
  }


  @Test
  public void failsOnInvalidJsonEscherFile() throws IOException, ProcessingException {
    validation = new Validation();

    file = new File("data/TestMap_vcard.escher.json");

    assertFalse("failure - validation passes on invalid JSON", validation.validateEscher(file));
  }


  @Test
  public void validatesEscherJson() throws IOException, ProcessingException {
    validation = new Validation();
    file = new File("data/e_coli_core_metabolism.escher.json");

    assertTrue("failure - validation failing on valid JSON", validation.validateEscher(file));
  }


  @Ignore
  @Test
  public void validatesSbgnDocument() throws IOException, ProcessingException {
    validation = new Validation();
    file = new File("data/central_plant_metabolism.sbgn.xml");

    assertTrue("failure - validation failing on valid SBGN", validation.validateSbgnml(file));
  }


  @Test(expected = IllegalArgumentException.class)
  public void failsOnInvalidSbgnDocument() throws IOException, ProcessingException {
    validation = new Validation();
    file = new File("data/mapk_cascade.sbgn.xml");

    assertFalse("failure - validation passing on invalid SBGN", validation.validateSbgnml(file));
  }


  @Test(expected = IllegalArgumentException.class)
  public void failsWhenLanguageNotPresentOnSbgn() throws IOException, ProcessingException {
    validation = new Validation();
    file = new File("data/mapk_cascade.sbgn.xml");

    assertFalse("failure - validation passing w/o lang on SBGN", validation.validateSbgnml(file));
  }


  @Ignore
  @Test
  public void validatesSbmlDocument() {

  }


  @Ignore
  @Test
  public void failsOnInvalidSbmlDocument() {

  }

}

@@ -230,24 +230,93 @@
 Validation;%0A
+import junitparams.JUnitParamsRunner;%0Aimport junitparams.Parameters;%0A
 import org.j
@@ -405,16 +405,89 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.Parameterized;
 %0A%0Aimport
@@ -530,16 +530,61 @@
 ption;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -718,16 +718,50 @@
 16.%0A */%0A
+@RunWith(JUnitParamsRunner.class)%0A
 public c
@@ -1511,330 +1511,238 @@
 t%0A  
-public void failsOnInvalidJsonEscherFile() throws IOException, ProcessingException %7B%0A    validation = new Validation();%0A%0A    file = new File(%22data/TestMap_vcard.escher.json%22);%0A%0A    assertFalse(%22failure - validation passes on invalid JSON%22, validation.validateEscher(file));%0A  %7D%0A%0A%0A  @Test%0A  public void validatesEscherJson(
+@Parameters(%7B%0A      %22data/mapk_cascade.escher.json%7Cfalse%22,%0A      %22data/e_coli_core_metabolism.escher.json%7Ctrue%22,%0A      %22data/TestMap_vcard.escher.json%7Cfalse%22%0A  %7D)%0A  public void escherValidationTest(String filePath, boolean isValid
 ) th
@@ -1750,32 +1750,38 @@
 ows IOException,
+%0A     
  ProcessingExcep
@@ -1846,49 +1846,16 @@
 ile(
-%22data/e_coli_core_metabolism.escher.json%22
+filePath
 );%0A%0A
@@ -1856,36 +1856,38 @@
 th);%0A%0A    assert
-True
+Equals
 (%22failure - vali
@@ -1916,16 +1916,25 @@
 d JSON%22,
+ isValid,
  validat
@@ -1951,16 +1951,25 @@
 teEscher
+%0A        
 (file));
@@ -1973,39 +1973,205 @@
 ));%0A  %7D%0A%0A%0A  
-@Ignore%0A  @Test
+// TODO: Add more SBGN files to test validation.%0A  @Test%0A  @Ignore%0A  @Parameters(%7B%0A      %22data/mapk_cascade.sbgn.xml%7Cfalse%22,%0A      %22data/central_plant_metabolism.sbgn.xml%7Ctrue%22%0A  %7D)
 %0A  public vo
@@ -2177,30 +2177,59 @@
 oid 
-v
+sbgnV
 alidat
-esSbgnDocument(
+ionTest(String filePath, boolean isValid
 ) th
@@ -2237,32 +2237,38 @@
 ows IOException,
+%0A     
  ProcessingExcep
@@ -2333,48 +2333,16 @@
 ile(
-%22data/central_plant_metabolism.sbgn.xml%22
+filePath
 );%0A%0A
@@ -2351,20 +2351,22 @@
   assert
-True
+Equals
 (%22failur
@@ -2395,32 +2395,41 @@
  on valid SBGN%22,
+ isValid,
  validation.vali
@@ -2430,32 +2430,41 @@
 n.validateSbgnml
+%0A        
 (file));%0A  %7D%0A%0A%0A 

Add parameterized tests for validation.