// Copyright (C) 2014 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.reviewdb.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class PatchSetTest {
  @Test
  public void parseRefNames() {
    assertRef(1, 1, "refs/changes/01/1/1");
    assertRef(1234, 56, "refs/changes/34/1234/56");

    // Not even close.
    assertNotRef(null);
    assertNotRef("");
    assertNotRef("01/1/1");
    assertNotRef("HEAD");
    assertNotRef("refs/tags/v1");

    // Invalid characters.
    assertNotRef("refs/changes/0x/1/1");
    assertNotRef("refs/changes/01/x/1");
    assertNotRef("refs/changes/01/1/x");

    // Truncations.
    assertNotRef("refs/changes/");
    assertNotRef("refs/changes/1");
    assertNotRef("refs/changes/01");
    assertNotRef("refs/changes/01/");
    assertNotRef("refs/changes/01/1/");
    assertNotRef("refs/changes/01/1/1/");
    assertNotRef("refs/changes/01//1/1");

    // Leading zeroes.
    assertNotRef("refs/changes/01/01/1");
    assertNotRef("refs/changes/01/1/01");

    // Mismatched last 2 digits.
    assertNotRef("refs/changes/35/1234/56");

    // Something other than patch set after change.
    assertNotRef("refs/changes/34/1234/0");
    assertNotRef("refs/changes/34/1234/foo");
    assertNotRef("refs/changes/34/1234|56");
    assertNotRef("refs/changes/34/1234foo");
  }

  private static void assertRef(int changeId, int psId, String refName) {
    assertTrue(PatchSet.isRef(refName));
    assertEquals(new PatchSet.Id(new Change.Id(changeId), psId),
        PatchSet.Id.fromRef(refName));
  }

  private static void assertNotRef(String refName) {
    assertFalse(PatchSet.isRef(refName));
    assertNull(PatchSet.Id.fromRef(refName));
  }
}

@@ -664,164 +664,45 @@
 tic 
-org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert
+com.google.common.truth.Truth
 .assertT
 rue;
@@ -697,19 +697,19 @@
 .assertT
-rue
+hat
 ;%0A%0Aimpor
@@ -2005,11 +2005,11 @@
 ertT
-rue
+hat
 (Pat
@@ -2021,32 +2021,41 @@
 .isRef(refName))
+.isTrue()
 ;%0A    assertEqua
@@ -2054,14 +2054,61 @@
 sert
+That(PatchSet.Id.fromRef(refName))%0A        .is
 Equal
-s
+To
 (new
@@ -2154,46 +2154,8 @@
 sId)
-,%0A        PatchSet.Id.fromRef(refName)
 );%0A 
@@ -2225,13 +2225,12 @@
 sert
-False
+That
 (Pat
@@ -2246,24 +2246,34 @@
 ef(refName))
+.isFalse()
 ;%0A    assert
@@ -2276,12 +2276,12 @@
 sert
-Null
+That
 (Pat
@@ -2306,16 +2306,25 @@
 efName))
+.isNull()
 ;%0A  %7D%0A%7D%0A

Convert to Truth