mirror of
https://github.com/google/nomulus.git
synced 2025-05-03 13:37:51 +02:00
The dark lord Gosling designed the Java package naming system so that ownership flows from the DNS system. Since we own the domain name registry.google, it seems only appropriate that we should use google.registry as our package name.
212 lines
7.6 KiB
Java
212 lines
7.6 KiB
Java
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
|
//
|
|
// 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 google.registry.model.translators;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static google.registry.model.ofy.ObjectifyService.ofy;
|
|
import static org.joda.time.Duration.standardDays;
|
|
import static org.joda.time.Duration.standardHours;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableSortedMap;
|
|
|
|
import com.googlecode.objectify.ObjectifyService;
|
|
import com.googlecode.objectify.Ref;
|
|
import com.googlecode.objectify.VoidWork;
|
|
import com.googlecode.objectify.Work;
|
|
import com.googlecode.objectify.annotation.Entity;
|
|
|
|
import google.registry.config.TestRegistryConfig;
|
|
import google.registry.model.common.CrossTldSingleton;
|
|
import google.registry.model.ofy.CommitLogManifest;
|
|
import google.registry.model.ofy.Ofy;
|
|
import google.registry.testing.AppEngineRule;
|
|
import google.registry.testing.ExceptionRule;
|
|
import google.registry.testing.FakeClock;
|
|
import google.registry.testing.InjectRule;
|
|
import google.registry.testing.RegistryConfigRule;
|
|
|
|
import org.joda.time.DateTime;
|
|
import org.joda.time.Duration;
|
|
import org.junit.Before;
|
|
import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.JUnit4;
|
|
|
|
import java.util.List;
|
|
|
|
/** Unit tests for {@link CommitLogRevisionsTranslatorFactory}. */
|
|
@RunWith(JUnit4.class)
|
|
public class CommitLogRevisionsTranslatorFactoryTest {
|
|
|
|
private static final DateTime START_TIME = DateTime.parse("2000-01-01TZ");
|
|
|
|
@Entity
|
|
public static class TestObject extends CrossTldSingleton {
|
|
ImmutableSortedMap<DateTime, Ref<CommitLogManifest>> revisions = ImmutableSortedMap.of();
|
|
}
|
|
|
|
@Rule
|
|
public final AppEngineRule appEngine = AppEngineRule.builder()
|
|
.withDatastore()
|
|
.build();
|
|
|
|
@Rule
|
|
public final InjectRule inject = new InjectRule();
|
|
|
|
@Rule
|
|
public final ExceptionRule thrown = new ExceptionRule();
|
|
|
|
@Rule
|
|
public final RegistryConfigRule configRule = new RegistryConfigRule(
|
|
new TestRegistryConfig() {
|
|
@Override
|
|
public Duration getCommitLogDatastoreRetention() {
|
|
return Duration.standardDays(30);
|
|
}
|
|
});
|
|
|
|
private final FakeClock clock = new FakeClock(START_TIME);
|
|
|
|
@Before
|
|
public void before() throws Exception {
|
|
ObjectifyService.register(TestObject.class);
|
|
inject.setStaticField(Ofy.class, "clock", clock);
|
|
}
|
|
|
|
private void save(final TestObject object) {
|
|
ofy().transact(new VoidWork() {
|
|
@Override
|
|
public void vrun() {
|
|
ofy().save().entity(object);
|
|
}});
|
|
}
|
|
|
|
private TestObject reload() {
|
|
ofy().clearSessionCache();
|
|
return ofy().load().entity(new TestObject()).now();
|
|
}
|
|
|
|
@Test
|
|
public void testSave_doesNotMutateOriginalResource() throws Exception {
|
|
TestObject object = new TestObject();
|
|
save(object);
|
|
assertThat(object.revisions).isEmpty();
|
|
assertThat(reload().revisions).isNotEmpty();
|
|
}
|
|
|
|
@Test
|
|
public void testSave_translatorAddsReferenceToCommitLogToField() throws Exception {
|
|
save(new TestObject());
|
|
TestObject object = reload();
|
|
assertThat(object.revisions).hasSize(1);
|
|
assertThat(object.revisions).containsKey(START_TIME);
|
|
Ref<CommitLogManifest> ref = object.revisions.get(START_TIME);
|
|
CommitLogManifest commitLogManifest = ref.get();
|
|
assertThat(commitLogManifest.getCommitTime()).isEqualTo(START_TIME);
|
|
}
|
|
|
|
@Test
|
|
public void testSave_twoVersionsOnOneDay_referenceToLastCommitLogsGetsStored() throws Exception {
|
|
save(new TestObject());
|
|
clock.advanceBy(standardHours(1));
|
|
save(reload());
|
|
TestObject object = reload();
|
|
assertThat(object.revisions).hasSize(1);
|
|
assertThat(object.revisions).containsKey(START_TIME.plusHours(1));
|
|
}
|
|
|
|
@Test
|
|
public void testSave_twoVersionsOnTwoDays_referenceToBothCommitLogsGetsStored() throws Exception {
|
|
save(new TestObject());
|
|
clock.advanceBy(standardDays(1));
|
|
save(reload());
|
|
TestObject object = reload();
|
|
assertThat(object.revisions).hasSize(2);
|
|
assertThat(object.revisions).containsKey(START_TIME);
|
|
assertThat(object.revisions).containsKey(START_TIME.plusDays(1));
|
|
}
|
|
|
|
@Test
|
|
public void testSave_moreThanThirtyDays_truncatedAtThirtyPlusOne() throws Exception {
|
|
save(new TestObject());
|
|
for (int i = 0; i < 35; i++) {
|
|
clock.advanceBy(standardDays(1));
|
|
save(reload());
|
|
}
|
|
TestObject object = reload();
|
|
assertThat(object.revisions).hasSize(31);
|
|
assertThat(object.revisions.firstKey()).isEqualTo(clock.nowUtc().minusDays(30));
|
|
}
|
|
|
|
@Test
|
|
public void testSave_moreThanThirtySparse_keepsOneEntryPrecedingThirtyDays() throws Exception {
|
|
save(new TestObject());
|
|
assertThat(reload().revisions).hasSize(1);
|
|
assertThat(reload().revisions.firstKey()).isEqualTo(clock.nowUtc().minusDays(0));
|
|
clock.advanceBy(standardDays(29));
|
|
save(reload());
|
|
assertThat(reload().revisions).hasSize(2);
|
|
assertThat(reload().revisions.firstKey()).isEqualTo(clock.nowUtc().minusDays(29));
|
|
clock.advanceBy(standardDays(29));
|
|
save(reload());
|
|
assertThat(reload().revisions).hasSize(3);
|
|
assertThat(reload().revisions.firstKey()).isEqualTo(clock.nowUtc().minusDays(58));
|
|
clock.advanceBy(standardDays(29));
|
|
save(reload());
|
|
assertThat(reload().revisions).hasSize(3);
|
|
assertThat(reload().revisions.firstKey()).isEqualTo(clock.nowUtc().minusDays(58));
|
|
}
|
|
|
|
@Test
|
|
@SuppressWarnings("unchecked")
|
|
public void testRawEntityLayout() throws Exception {
|
|
save(new TestObject());
|
|
clock.advanceBy(standardDays(1));
|
|
com.google.appengine.api.datastore.Entity entity =
|
|
ofy().transactNewReadOnly(new Work<com.google.appengine.api.datastore.Entity>() {
|
|
@Override
|
|
public com.google.appengine.api.datastore.Entity run() {
|
|
return ofy().save().toEntity(reload());
|
|
}});
|
|
assertThat(entity.getProperties().keySet()).containsExactly("revisions.key", "revisions.value");
|
|
assertThat(entity.getProperties()).containsEntry(
|
|
"revisions.key", ImmutableList.of(START_TIME.toDate(), START_TIME.plusDays(1).toDate()));
|
|
assertThat(entity.getProperty("revisions.value")).isInstanceOf(List.class);
|
|
assertThat(((List<Object>) entity.getProperty("revisions.value")).get(0))
|
|
.isInstanceOf(com.google.appengine.api.datastore.Key.class);
|
|
}
|
|
|
|
@Test
|
|
public void testLoad_neverSaved_returnsNull() throws Exception {
|
|
assertThat(ofy().load().entity(new TestObject()).now()).isNull();
|
|
}
|
|
|
|
@Test
|
|
public void testLoad_missingRevisionRawProperties_createsEmptyObject() throws Exception {
|
|
com.google.appengine.api.datastore.Entity entity =
|
|
ofy().transactNewReadOnly(new Work<com.google.appengine.api.datastore.Entity>() {
|
|
@Override
|
|
public com.google.appengine.api.datastore.Entity run() {
|
|
return ofy().save().toEntity(new TestObject());
|
|
}});
|
|
entity.removeProperty("revisions.key");
|
|
entity.removeProperty("revisions.value");
|
|
TestObject object = ofy().load().fromEntity(entity);
|
|
assertThat(object.revisions).isNotNull();
|
|
assertThat(object.revisions).isEmpty();
|
|
}
|
|
}
|