blob: 826a409e9fb468e5882a6fbf959f580e3bfee5b2 [file] [log] [blame]
// Copyright (C) 2023 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.googlesource.gerrit.plugins.spannerrefdb;
import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.entities.Project;
import java.util.Optional;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SpannerRefDatabaseTest implements RefFixture {
private EmulatedSpannerRefDb emulator;
private SpannerRefDatabase refdb;
@Before
public void setup() throws Exception {
emulator = new EmulatedSpannerRefDb();
refdb = emulator.getSpannerRefDatabase();
}
@After
public void cleanup() {
emulator.cleanup();
}
@Test
public void testCreateNewRef() throws Exception {
createNewRef(PROJECT_NAME_KEY, REF_NAME, OBJECT_ID_1);
createNewRef(PROJECT_NAME_KEY, PATCHSET_REF_NAME, OBJECT_ID_2);
createNewRef(PROJECT_NAME_KEY, MULTISITE_VERSION_VALUE_REF_NAME, UNIX_TIMESTAMP_1.toString());
}
@Test
public void testUpdateRef() throws Exception {
createNewRef(PROJECT_NAME_KEY, REF_NAME, OBJECT_ID_1);
updateRef(PROJECT_NAME_KEY, REF_NAME, OBJECT_ID_1, OBJECT_ID_2);
createNewRef(PROJECT_NAME_KEY, PATCHSET_REF_NAME, OBJECT_ID_2);
updateRef(PROJECT_NAME_KEY, PATCHSET_REF_NAME, OBJECT_ID_2, OBJECT_ID_3);
createNewRef(PROJECT_NAME_KEY, MULTISITE_VERSION_VALUE_REF_NAME, UNIX_TIMESTAMP_1.toString());
updateRef(
PROJECT_NAME_KEY,
MULTISITE_VERSION_VALUE_REF_NAME,
UNIX_TIMESTAMP_1.toString(),
UNIX_TIMESTAMP_2.toString());
}
@Test
public void testDeleteRef() throws Exception {
createNewRef(PROJECT_NAME_KEY, REF_NAME, OBJECT_ID_1);
deleteRef(PROJECT_NAME_KEY, REF_NAME, OBJECT_ID_1);
createNewRef(PROJECT_NAME_KEY, PATCHSET_REF_NAME, OBJECT_ID_2);
deleteRef(PROJECT_NAME_KEY, PATCHSET_REF_NAME, OBJECT_ID_2);
createNewRef(PROJECT_NAME_KEY, MULTISITE_VERSION_VALUE_REF_NAME, UNIX_TIMESTAMP_1.toString());
deleteRef(PROJECT_NAME_KEY, MULTISITE_VERSION_VALUE_REF_NAME, UNIX_TIMESTAMP_1.toString());
}
private void createNewRef(Project.NameKey project, String refName, ObjectId newValue)
throws Exception {
Ref ref = nullRef(refName);
try (AutoCloseable lock = refdb.lockRef(project, refName)) {
assertThat(refdb.compareAndPut(project, ref, newValue)).isTrue();
Optional<ObjectId> oid = refdb.getObjectId(project, refName);
assertThat(oid.isEmpty()).isFalse();
assertThat(newValue).isEqualTo(oid.get());
}
}
private void updateRef(
Project.NameKey project, String refName, ObjectId oldValue, ObjectId newValue)
throws Exception {
Ref expecRef = newRef(refName, oldValue);
try (AutoCloseable lock = refdb.lockRef(project, refName)) {
assertThat(refdb.compareAndPut(project, expecRef, newValue)).isTrue();
assertThat(newValue).isEqualTo(refdb.getObjectId(project, refName).get());
}
}
private void deleteRef(Project.NameKey project, String refName, ObjectId oldValue)
throws Exception {
Ref expecRef = newRef(refName, oldValue);
try (AutoCloseable lock = refdb.lockRef(project, refName)) {
assertThat(refdb.compareAndPut(project, expecRef, ObjectId.zeroId())).isTrue();
assertThat(refdb.getObjectId(project, refName).isEmpty()).isTrue();
}
}
private void createNewRef(Project.NameKey project, String refName, String newValue)
throws Exception {
try (AutoCloseable lock = refdb.lockRef(project, refName)) {
assertThat(refdb.compareAndPut(project, refName, ObjectId.zeroId().name(), newValue))
.isTrue();
assertThat(refdb.get(project, refName)).isNotNull();
assertThat(newValue).isEqualTo(refdb.get(project, refName));
}
}
private void updateRef(Project.NameKey project, String refName, String oldValue, String newValue)
throws Exception {
try (AutoCloseable lock = refdb.lockRef(project, refName)) {
assertThat(refdb.compareAndPut(project, refName, oldValue, newValue)).isTrue();
assertThat(newValue).isEqualTo(refdb.get(project, refName));
}
}
private void deleteRef(Project.NameKey project, String refName, String oldValue)
throws Exception {
try (AutoCloseable lock = refdb.lockRef(project, refName)) {
assertThat(refdb.compareAndPut(project, refName, oldValue, ObjectId.zeroId().name()))
.isTrue();
assertThat(refdb.get(project, refName)).isNull();
}
}
}