// Copyright (C) 2018 The Android Open Source Project
//
// 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.google.gerrit.server.schema;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_VERSION;

import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.testing.InMemoryRepositoryManager;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Before;
import org.junit.Test;

public class NoteDbSchemaVersionManagerTest {
  private NoteDbSchemaVersionManager manager;
  private TestRepository<?> tr;

  @Before
  public void setUp() throws Exception {
    AllProjectsName allProjectsName = new AllProjectsName("The-Projects");
    GitRepositoryManager repoManager = new InMemoryRepositoryManager();
    tr = new TestRepository<>(repoManager.createRepository(allProjectsName));
    manager = new NoteDbSchemaVersionManager(allProjectsName, repoManager);
  }

  @Test
  public void readMissing() throws Exception {
    assertThat(manager.read()).isEqualTo(0);
  }

  @Test
  public void read() throws Exception {
    tr.update(REFS_VERSION, tr.blob("123"));
    assertThat(manager.read()).isEqualTo(123);
  }

  @Test
  public void readInvalid() throws Exception {
    ObjectId blobId = tr.blob(" 1 2 3 ");
    tr.update(REFS_VERSION, blobId);
    try {
      manager.read();
      assert_().fail("expected StorageException");
    } catch (StorageException e) {
      assertThat(e)
          .hasMessageThat()
          .isEqualTo("invalid value in refs/meta/version blob at " + blobId.name());
    }
  }

  @Test
  public void incrementFromMissing() throws Exception {
    manager.increment(123);
    assertThat(manager.read()).isEqualTo(124);
  }

  @Test
  public void increment() throws Exception {
    tr.update(REFS_VERSION, tr.blob("123"));
    manager.increment(123);
    assertThat(manager.read()).isEqualTo(124);
  }

  @Test
  public void incrementWrongOldVersion() throws Exception {
    tr.update(REFS_VERSION, tr.blob("123"));
    try {
      manager.increment(456);
      assert_().fail("expected StorageException");
    } catch (StorageException e) {
      assertThat(e)
          .hasMessageThat()
          .isEqualTo("Expected old version 456 for refs/meta/version, found 123");
    }
  }
}

@@ -729,105 +729,118 @@
 gle.
-common.truth.Truth.assert_;%0Aimport static com.google.gerrit.reviewdb.client.RefNames.REFS_VERSION
+gerrit.reviewdb.client.RefNames.REFS_VERSION;%0Aimport static com.google.gerrit.testing.GerritJUnit.assertThrows
 ;%0A%0Ai
@@ -2073,124 +2073,94 @@
     
-try %7B%0A      manager.read();%0A      assert_().fail(%22expected StorageException%22);%0A    %7D catch (StorageException e) %7B%0A  
+StorageException thrown = assertThrows(StorageException.class, () -%3E manager.read());%0A
     
@@ -2162,37 +2162,40 @@
 %0A    assertThat(
-e)%0A  
+thrown)%0A
         .hasMess
@@ -2196,34 +2196,32 @@
 asMessageThat()%0A
-  
         .isEqual
@@ -2287,22 +2287,16 @@
 ame());%0A
-    %7D%0A
   %7D%0A%0A  @
@@ -2735,132 +2735,102 @@
     
-try %7B%0A      manager.increment(456);%0A      assert_().fail(%22expected StorageException%22);%0A    %7D catch (StorageException e) %7B%0A  
+StorageException thrown = assertThrows(StorageException.class, () -%3E manager.increment(456));%0A
     
@@ -2844,13 +2844,16 @@
 hat(
-e)%0A  
+thrown)%0A
     
@@ -2870,26 +2870,24 @@
 ssageThat()%0A
-  
         .isE
@@ -2959,14 +2959,8 @@
 %22);%0A
-    %7D%0A
   %7D%0A

Migrate from try-catch-fail to assertThrows