/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *   https://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.apache.accumulo.tserver.tablet;

import static org.easymock.EasyMock.mock;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.accumulo.core.constraints.Violations;
import org.apache.accumulo.core.data.Mutation;
import org.junit.jupiter.api.Test;

public class TabletMutationPrepAttemptTest {

  @Test
  public void ensureTabletClosed() {
    PreparedMutations prepared = new PreparedMutations();
    assertTrue(prepared.tabletClosed());
  }

  @Test
  public void callGetSessionWhenClosed() {
    PreparedMutations prepared = new PreparedMutations();
    assertThrows(IllegalStateException.class, prepared::getCommitSession);
  }

  @Test
  public void callGetNonViolatorsWhenClosed() {
    PreparedMutations prepared = new PreparedMutations();
    assertThrows(IllegalStateException.class, prepared::getNonViolators);
  }

  @Test
  public void callGetViolatorsWhenClosed() {
    PreparedMutations prepared = new PreparedMutations();
    assertThrows(IllegalStateException.class, prepared::getViolators);
  }

  @Test
  public void callGetViolationsWhenClosed() {
    PreparedMutations prepared = new PreparedMutations();
    assertThrows(IllegalStateException.class, prepared::getViolations);
  }

  @Test
  public void testTabletOpen() {
    CommitSession cs = mock(CommitSession.class);
    List<Mutation> nonViolators = new ArrayList<>();
    Violations violations = new Violations();
    Set<Mutation> violators = new HashSet<>();

    PreparedMutations prepared = new PreparedMutations(cs, nonViolators, violations, violators);

    assertFalse(prepared.tabletClosed());
    assertSame(cs, prepared.getCommitSession());
    assertSame(nonViolators, prepared.getNonViolators());
    assertSame(violations, prepared.getViolations());
    assertSame(violators, prepared.getViolators());
  }
}
