/*
 * Copyright Terracotta, Inc.
 *
 * 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.terracotta.auditor;

import org.junit.Test;
import org.terracotta.auditor.operations.Operations;
import org.terracotta.auditor.verifier.RecordValue;
import org.terracotta.auditor.verifier.Verifier;

import java.io.StringReader;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author Ludovic Orban
 */
public class VerifierTest {

  @Test
  public void orderedConcurrentAddNoError() throws Exception {
    String journalContents =
        "1;2;Add;1;true\n" +
        "1;2;Add;1;false\n";

    Verifier verifier = new Verifier(new StringReader(journalContents), 10, Operations.parser());
    List<String> errors = verifier.verify((results) -> {
      Set<RecordValue> recordValues = results.get("1");
      assertThat(recordValues.size(), is(1));
      RecordValue recordValue = recordValues.iterator().next();
      assertThat(recordValue.isUnknown(), is(true));
      assertThat(recordValue.isAbsent(), is(false));
      return Collections.emptyList();
    });
    assertThat(errors.size(), is(0));
  }

  @Test
  public void unorderedConcurrentAddNoError() throws Exception {
    String journalContents =
        "1;2;Add;1;false\n" +
        "1;2;Add;1;true\n";

    Verifier verifier = new Verifier(new StringReader(journalContents), 10, Operations.parser());
    List<String> errors = verifier.verify((results) -> {
      Set<RecordValue> recordValues = results.get("1");
      assertThat(recordValues.size(), is(1));
      RecordValue recordValue = recordValues.iterator().next();
      assertThat(recordValue.isUnknown(), is(true));
      assertThat(recordValue.isAbsent(), is(false));
      return Collections.emptyList();
    });
    assertThat(errors.size(), is(0));
  }

  @Test
  public void concurrentAddError() throws Exception {
    String journalContents =
        "1;2;Add;1;true\n" +
        "1;2;Add;1;true\n";

    Verifier verifier = new Verifier(new StringReader(journalContents), 10, Operations.parser());
    List<String> errors = verifier.verify((results) -> {
      Set<RecordValue> recordValues = results.get("1");
      assertThat(recordValues.size(), is(2));
      Iterator<RecordValue> it = recordValues.iterator();
      RecordValue absent = it.next();
      assertThat(absent.isUnknown(), is(false));
      assertThat(absent.isAbsent(), is(true));
      RecordValue unknown = it.next();
      assertThat(unknown.isUnknown(), is(true));
      assertThat(unknown.isAbsent(), is(false));
      return Collections.emptyList();
    });
    assertThat(errors.size(), is(1));
  }

}

@@ -2911,22 +2911,27 @@
 rdValue 
-absent
+recordValue
  = it.ne
@@ -2946,105 +2946,272 @@
     
-assertThat(absent.isUnknown(), is(false));%0A      assertThat(absent.isAbsent(), is(true));%0A      R
+if (recordValue.isAbsent()) %7B%0A        assertThat(recordValue.isUnknown(), is(false));%0A        recordValue = it.next();%0A        assertThat(recordValue.isAbsent(), is(false));%0A        assertThat(recordValue.isUnknown(), is(true));%0A      %7D else %7B%0A        assertThat(r
 ecor
@@ -3220,16 +3220,52 @@
 alue
- u
+.isU
 nknown
+(), is(true));%0A        recordValue
  = i
@@ -3272,32 +3272,34 @@
 t.next();%0A      
+  
 assertThat(unkno
@@ -3297,25 +3297,28 @@
 hat(
-unknown.isUnknown
+recordValue.isAbsent
 (), 
@@ -3326,32 +3326,34 @@
 s(true));%0A      
+  
 assertThat(unkno
@@ -3351,24 +3351,29 @@
 hat(
-unknown.isAbsent
+recordValue.isUnknown
 (), 
@@ -3376,32 +3376,40 @@
 (), is(false));%0A
+      %7D%0A
       return Col

fix test