/*
 *  Copyright (c) 2016 Savoir Technologies
 *
 *  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 com.savoirtech.json;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
 * Integrated test of the JsonComparator.
 *
 * Created by art on 5/4/16.
 */
public class JsonComparatorIT {

  private JsonComparator comparator;

  @Before
  public void setupTest() {
    this.comparator = new JsonComparatorBuilder().build();
  }

  @Test
  public void testCompare01() throws Exception {
    String actual01 = IOUtils.toString(JsonComparatorIT.class.getResourceAsStream("spec01.json"));
    String rules01 = IOUtils.toString(JsonComparatorIT.class.getResourceAsStream("rules01.json"));

    JsonComparatorResult result = this.comparator.compare(rules01, actual01);

    assertTrue("expect match; error=" + result.getErrorMessage(), result.isMatch());
  }

  @Test
  public void testReadmeExample01() {

    JsonComparator comparator = new JsonComparatorBuilder().build();

    String comparisonSpec = "{ \"templateJson\": [ 1, 2, 3 ] }";
    String actualJson = "[ 1, 2, 3 ]";

    JsonComparatorResult result = comparator.compare(comparisonSpec, actualJson);

    assertTrue(result.isMatch());

    // Same thing, except include the error message on mismatches:
    assertTrue(result.getErrorMessage(), result.isMatch());
  }

  @Test
  public void testReadmeExample02() {
    JsonComparator comparator = new JsonComparatorBuilder().build();

    String
        comparisonSpec =
        "{ \"rules\": [ { \"selector\": { \"path\": \"$[2]\" }, \"action\": \"matches\", \"pattern\": \"[3-5]\" } ], \"templateJson\": [ 1, 2, 3 ] }";
    String actualJson = "[ 1, 2, 4 ]";

    JsonComparatorResult result = comparator.compare(comparisonSpec, actualJson);

    assertTrue(result.getErrorMessage(), result.isMatch());
  }
}

@@ -724,16 +724,148 @@
 .Test;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNull;%0A
 import s
@@ -899,16 +899,16 @@
 rtTrue;%0A
-
 %0A/**%0A * 
@@ -1567,32 +1567,71 @@
 ult.isMatch());%0A
+    assertNull(result.getErrorPath());%0A
   %7D%0A%0A  @Test%0A  p
@@ -2082,24 +2082,63 @@
 isMatch());%0A
+    assertNull(result.getErrorPath());%0A
   %7D%0A%0A  @Test
@@ -2548,32 +2548,32 @@
 , actualJson);%0A%0A
-
     assertTrue(r
@@ -2612,18 +2612,666 @@
 isMatch());%0A
+    assertNull(result.getErrorPath());%0A  %7D%0A%0A  @Test%0A  public void testFailureCase() %7B%0A    JsonComparator comparator = new JsonComparatorBuilder().build();%0A%0A    String%0A        comparisonSpec =%0A        %22%7B %5C%22rules%5C%22: %5B %7B %5C%22selector%5C%22: %7B %5C%22path%5C%22: %5C%22$%5B2%5D%5C%22 %7D, %5C%22action%5C%22: %5C%22matches%5C%22, %5C%22pattern%5C%22: %5C%22%5B3-5%5D%5C%22 %7D %5D, %5C%22templateJson%5C%22: %5B 2, 4, 6 %5D %7D%22;%0A    String actualJson = %22%5B 2, 4, 6 %5D%22;%0A%0A    JsonComparatorResult result = comparator.compare(comparisonSpec, actualJson);%0A%0A    assertFalse(result.isMatch());%0A    assertEquals(%22value at path $%5B2%5D does not match '%5B3-5%5D': value=6%22, result.getErrorMessage());%0A    assertEquals(%22$%5B2%5D%22, result.getErrorPath());%0A
   %7D%0A%7D%0A

Add integration test for failure scenario.