/*
 * 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.ehcache.internal.tier;

import org.ehcache.exceptions.CacheAccessException;
import org.ehcache.expiry.Expirations;
import org.ehcache.spi.cache.Store;
import org.ehcache.spi.cache.tiering.AuthoritativeTier;
import org.ehcache.spi.test.After;
import org.ehcache.spi.test.Before;
import org.ehcache.spi.test.LegalSPITesterException;
import org.ehcache.spi.test.SPITest;

import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Test the {@link AuthoritativeTier#flush(Object, Store.ValueHolder)} contract of the
 * {@link AuthoritativeTier AuthoritativeTier} interface.
 * <p/>
 *
 * @author Aurelien Broszniowski
 */

public class AuthoritativeTierFlush<K, V> extends SPIAuthoritativeTierTester<K, V> {

  protected AuthoritativeTier<K, V> tier;

  public AuthoritativeTierFlush(final AuthoritativeTierFactory<K, V> factory) {
    super(factory);
  }

  @Before
  public void setUp() {
  }

  @After
  public void tearDown() {
    if (tier != null) {
//      tier.close();
      tier = null;
    }
  }

  @SPITest
  @SuppressWarnings("unchecked")
  public void entryIsFlushed() throws LegalSPITesterException {
    K key = factory.createKey(1);
    final V value = factory.createValue(1);
    Store.ValueHolder<V> valueHolder = mock(Store.ValueHolder.class);
    when(valueHolder.expirationTime(any(TimeUnit.class))).thenReturn(1L);

    tier = factory.newStore(factory.newConfiguration(factory.getKeyType(), factory.getValueType(),
        1L, null, null, Expirations.noExpiration()));

    try {
      tier.put(key, value);
      final Store.ValueHolder<V> fault = tier.getAndFault(key);
      when(valueHolder.getId()).thenReturn(fault.getId());
    } catch (CacheAccessException e) {
      throw new LegalSPITesterException("Warning, an exception is thrown due to the SPI test");
    }

    assertThat(tier.flush(key, valueHolder), is(equalTo(true)));
  }

  @SPITest
  @SuppressWarnings("unchecked")
  public void entryIsNotFlushed() throws LegalSPITesterException {
    K key = factory.createKey(1);
    final V value = factory.createValue(1);
    Store.ValueHolder<V> valueHolder = mock(Store.ValueHolder.class);
    when(valueHolder.expirationTime(any(TimeUnit.class))).thenReturn(1L);

    tier = factory.newStore(factory.newConfiguration(factory.getKeyType(), factory.getValueType(),
        1L, null, null, Expirations.noExpiration()));

    try {
      tier.put(key, value);
    } catch (CacheAccessException e) {
      throw new LegalSPITesterException("Warning, an exception is thrown due to the SPI test");
    }

    assertThat(tier.flush(key, valueHolder), is(equalTo(false)));
  }

  @SPITest
  @SuppressWarnings("unchecked")
  public void entryDoesNotExist() {
    K key = factory.createKey(1);
    Store.ValueHolder<V> valueHolder = mock(Store.ValueHolder.class);
    when(valueHolder.expirationTime(any(TimeUnit.class))).thenReturn(1L);

    tier = factory.newStore(factory.newConfiguration(factory.getKeyType(), factory.getValueType(),
        1L, null, null, Expirations.noExpiration()));

    assertThat(tier.flush(key, valueHolder), is(equalTo(false)));
  }

  @SPITest
  @SuppressWarnings("unchecked")
  public void exceptionWhenValueHolderIsNotAnInstanceFromTheCachingTier() throws LegalSPITesterException {
    K key = factory.createKey(1);
    final V value = factory.createValue(1);

    tier = factory.newStore(factory.newConfiguration(factory.getKeyType(), factory.getValueType(),
        1L, null, null, Expirations.noExpiration()));

    Store.ValueHolder<V> valueHolder = null;
    try {
      tier.put(key, value);
      valueHolder = tier.get(key);
    } catch (CacheAccessException e) {
      throw new LegalSPITesterException("Warning, an exception is thrown due to the SPI test");
    }

    try {
      tier.flush(key, valueHolder);
      throw new AssertionError();
    } catch (IllegalArgumentException e) {
      // expected
    }
  }
}

@@ -3940,801 +3940,6 @@
  %7D%0A%0A
-  @SPITest%0A  @SuppressWarnings(%22unchecked%22)%0A  public void exceptionWhenValueHolderIsNotAnInstanceFromTheCachingTier() throws LegalSPITesterException %7B%0A    K key = factory.createKey(1);%0A    final V value = factory.createValue(1);%0A%0A    tier = factory.newStore(factory.newConfiguration(factory.getKeyType(), factory.getValueType(),%0A        1L, null, null, Expirations.noExpiration()));%0A%0A    Store.ValueHolder%3CV%3E valueHolder = null;%0A    try %7B%0A      tier.put(key, value);%0A      valueHolder = tier.get(key);%0A    %7D catch (CacheAccessException e) %7B%0A      throw new LegalSPITesterException(%22Warning, an exception is thrown due to the SPI test%22);%0A    %7D%0A%0A    try %7B%0A      tier.flush(key, valueHolder);%0A      throw new AssertionError();%0A    %7D catch (IllegalArgumentException e) %7B%0A      // expected%0A    %7D%0A  %7D%0A
 %7D%0A

remove invalid SPI test that doesn't actually test the SPI contract but some impl detail