/*
 * Copyright 2016 Thomas Krause.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.corpus_tools.graphannis;

import com.google.common.io.Files;
import java.io.File;
import org.corpus_tools.salt.SaltFactory;
import org.corpus_tools.salt.common.SDocument;
import org.corpus_tools.salt.samples.SampleGenerator;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.corpus_tools.graphannis.QueryToJSON.aqlToJSON;
import static org.junit.Assert.assertEquals;

/**
 *
 * @author thomas
 */
public class SaltImportTest
{
  private API.CorpusStorage storage;
  
  public SaltImportTest()
  {
  }
  
  @BeforeClass
  public static void setUpClass()
  {
  }
  
  @AfterClass
  public static void tearDownClass()
  {
  }
  
  @Before
  public void setUp()
  {
    File tmpDir = Files.createTempDir();
    
    storage = new API.CorpusStorage(tmpDir.getAbsolutePath());
  }
  
  @After
  public void tearDown()
  {
  }

  /**
   * Test of map method, of class SaltImport.
   */
  @Test
  public void testMapComplexExample()
  {
    System.out.println("map");
    SDocument doc = SaltFactory.createSDocument();
    
    SampleGenerator.createTokens(doc);
    SampleGenerator.createMorphologyAnnotations(doc);
    
    API.GraphUpdate result = new SaltImport().map(doc.getDocumentGraph()).finish();
    
    storage.applyUpdate("testCorpus", result);
    
    API.StringVector corpus = new API.StringVector("testCorpus");
    
    assertEquals(11, storage.count(corpus, aqlToJSON("node")));
    
    // test that the token are present and have the correct span values
    assertEquals(11, storage.count(corpus, aqlToJSON("tok")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"Is\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"this\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"example\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"more\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"complicated\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"than\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"it\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"appears\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"to\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"be\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("tok=\"?\"")));
    
    // test that the token annotations have been added
    assertEquals(2, storage.count(corpus, aqlToJSON("pos=\"VBZ\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"DT\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"NN\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"RBR\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"JJ\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"IN\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"PRP\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"TO\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\"VB\"")));
    assertEquals(1, storage.count(corpus, aqlToJSON("pos=\".\"")));
    
    // test that the precedence works for the token
    assertEquals(1, storage.count(corpus, 
      aqlToJSON("\"Is\" . \"this\" . \"example\" . \"more\" . \"complicated\" . \"than\" . \"it\" . \"appears\" . "
        + "\"to\" . \"be\" . \"?\"")));
  }
  
}

@@ -3094,17 +3094,17 @@
 tEquals(
-2
+1
 , storag
@@ -3141,16 +3141,27 @@
 =%5C%22VBZ%5C%22
+ _=_ %5C%22Is%5C%22
 %22)));%0A  
@@ -3221,16 +3221,29 @@
 s=%5C%22DT%5C%22
+ _=_ %5C%22this%5C%22
 %22)));%0A  
@@ -3303,16 +3303,32 @@
 s=%5C%22NN%5C%22
+ _=_ %5C%22example%5C%22
 %22)));%0A  
@@ -3389,16 +3389,29 @@
 =%5C%22RBR%5C%22
+ _=_ %5C%22more%5C%22
 %22)));%0A  
@@ -3471,16 +3471,36 @@
 s=%5C%22JJ%5C%22
+ _=_ %5C%22complicated%5C%22
 %22)));%0A  
@@ -3560,16 +3560,29 @@
 s=%5C%22IN%5C%22
+ _=_ %5C%22than%5C%22
 %22)));%0A  
@@ -3643,16 +3643,27 @@
 =%5C%22PRP%5C%22
+ _=_ %5C%22it%5C%22
 %22)));%0A  
@@ -3723,10 +3723,107 @@
 s=%5C%22
-TO
+VBZ%5C%22 _=_ %5C%22appears%5C%22%22)));%0A    assertEquals(1, storage.count(corpus, aqlToJSON(%22pos=%5C%22TO%5C%22 _=_ %5C%22to
 %5C%22%22)
@@ -3889,16 +3889,27 @@
 s=%5C%22VB%5C%22
+ _=_ %5C%22be%5C%22
 %22)));%0A  
@@ -3968,16 +3968,26 @@
 os=%5C%22.%5C%22
+ _=_ %5C%22?%5C%22
 %22)));%0A  

Make AQL in test case more specific.