/*
 * -\-\-
 * Spotify Styx Scheduler Service
 * --
 * Copyright (C) 2018 Spotify AB
 * --
 * 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.spotify.styx.storage;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.spotify.styx.model.WorkflowInstance;
import com.spotify.styx.state.RunState;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AggregateStorageTest {

  private static final String COMPONENT = "test-component";

  @Mock BigtableStorage bigtable;
  @Mock DatastoreStorage datastore;
  @Mock WorkflowInstance workflowInstance;
  @Mock RunState runState;

  private AggregateStorage sut;

  @Before
  public void setUp() throws Exception {
    sut = new AggregateStorage(bigtable, datastore);
  }

  @After
  public void tearDown() throws Exception {
    sut.close();
  }

  @Test
  public void shouldCloseDatastore() throws IOException {
    sut.close();
    verify(datastore).close();
  }

  @Test
  public void readActiveWorkflowInstances() throws Exception {
    final Map<WorkflowInstance, RunState> activeStates =
        Map.of(workflowInstance, runState);
    when(datastore.readActiveStates()).thenReturn(activeStates);
    assertThat(sut.readActiveStates(), is(activeStates));
    verify(datastore).readActiveStates();
  }

  @Test
  public void readActiveWorkflowInstance() throws Exception {
    when(datastore.readActiveState(workflowInstance)).thenReturn(Optional.of(runState));
    assertThat(sut.readActiveState(workflowInstance), is(Optional.of(runState)));
    verify(datastore).readActiveState(workflowInstance);
  }

  @Test
  public void readActiveWorkflowInstancesForComponent() throws Exception {
    final Map<WorkflowInstance, RunState> activeStates =
        Map.of(workflowInstance, runState);
    when(datastore.readActiveStates(COMPONENT)).thenReturn(activeStates);
    assertThat(sut.readActiveStates(COMPONENT), is(activeStates));
    verify(datastore).readActiveStates(COMPONENT);
  }

  @Test
  public void writeActiveState() throws Exception {
    sut.writeActiveState(workflowInstance, runState);
    verify(datastore).writeActiveState(workflowInstance, runState);
  }
}

@@ -774,16 +774,107 @@
 rtThat;%0A
+import static org.mockito.ArgumentMatchers.any;%0Aimport static org.mockito.Mockito.doThrow;%0A
 import s
@@ -948,16 +948,70 @@
 .when;%0A%0A
+import com.google.cloud.datastore.DatastoreException;%0A
 import c
@@ -1233,20 +1233,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0Aimport
@@ -1541,16 +1541,88 @@
 nent%22;%0A%0A
+  @Rule public ExpectedException exception = ExpectedException.none();%0A%0A
   @Mock 
@@ -3301,11 +3301,428 @@
 te);%0A  %7D
+%0A%0A  @Test%0A  public void shouldPropagateTransactionFailure() throws IOException %7B%0A    var rootCause = new DatastoreException(1, %22fail%22, %22error%22);%0A    var cause = new DatastoreIOException(rootCause);%0A    doThrow(cause).when(datastore).runInTransactionWithRetries(any());%0A    exception.expect(TransactionException.class);%0A    exception.expectCause(is(rootCause));%0A    sut.runInTransactionWithRetries(tx -%3E %22foobar%22);%0A  %7D
 %0A%7D%0A

add test