| // 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 org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertFalse; |
| import static org.junit.Assert.assertNotNull; |
| import static org.junit.Assert.assertNull; |
| import static org.junit.Assert.assertTrue; |
| |
| 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 SpannerEmulatorContainer emulator; |
| private SpannerRefDatabase refdb; |
| |
| @Before |
| public void setup() throws Exception { |
| emulator = new SpannerEmulatorContainer(); |
| 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 updateRef() 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)) { |
| assertTrue(refdb.compareAndPut(project, ref, newValue)); |
| Optional<ObjectId> oid = refdb.getObjectId(project, refName); |
| assertFalse(oid.isEmpty()); |
| assertEquals(newValue, 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)) { |
| assertTrue(refdb.compareAndPut(project, expecRef, newValue)); |
| assertEquals(newValue, 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)) { |
| assertTrue(refdb.compareAndPut(project, expecRef, ObjectId.zeroId())); |
| assertTrue(refdb.getObjectId(project, refName).isEmpty()); |
| } |
| } |
| |
| private void createNewRef(Project.NameKey project, String refName, String newValue) |
| throws Exception { |
| try (AutoCloseable lock = refdb.lockRef(project, refName)) { |
| assertTrue(refdb.compareAndPut(project, refName, ObjectId.zeroId().name(), newValue)); |
| assertNotNull(refdb.get(project, refName)); |
| assertEquals(newValue, 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)) { |
| assertTrue(refdb.compareAndPut(project, refName, oldValue, newValue)); |
| assertEquals(newValue, refdb.get(project, refName)); |
| } |
| } |
| |
| private void deleteRef(Project.NameKey project, String refName, String oldValue) |
| throws Exception { |
| try (AutoCloseable lock = refdb.lockRef(project, refName)) { |
| assertTrue(refdb.compareAndPut(project, refName, oldValue, ObjectId.zeroId().name())); |
| assertNull(refdb.get(project, refName)); |
| } |
| } |
| } |