// Copyright 2017 The Nomulus 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.rde.imports; import static com.google.common.truth.Truth.assertThat; import static google.registry.rde.imports.RdeImportsTestData.loadBytes; import static google.registry.testing.JUnitBackports.assertThrows; import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsService; import com.google.appengine.tools.cloudstorage.GcsServiceFactory; import com.google.appengine.tools.cloudstorage.RetryParams; import com.google.common.io.ByteSource; import com.google.common.io.ByteStreams; import google.registry.config.RegistryConfig.ConfigModule; import google.registry.gcs.GcsUtils; import google.registry.testing.AppEngineRule; import google.registry.xjc.JaxbFragment; import google.registry.xjc.rdehost.XjcRdeHost; import google.registry.xjc.rdehost.XjcRdeHostElement; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.NoSuchElementException; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Unit tests for {@link RdeHostReader} */ @RunWith(JUnit4.class) public class RdeHostReaderTest { private static final ByteSource DEPOSIT_1_HOST = loadBytes("deposit_1_host.xml"); private static final ByteSource DEPOSIT_3_HOST = loadBytes("deposit_3_host.xml"); private static final ByteSource DEPOSIT_4_HOST = loadBytes("deposit_4_host.xml"); private static final ByteSource DEPOSIT_10_HOST = loadBytes("deposit_10_host.xml"); private static final String IMPORT_BUCKET_NAME = "rde-import"; private static final String IMPORT_FILE_NAME = "escrow-file.xml"; private static final GcsService GCS_SERVICE = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); /** Reads at least one result at 0 offset 1 maxResults */ @Test public void testZeroOffsetOneResult_readsOne() throws Exception { pushToGcs(DEPOSIT_1_HOST); RdeHostReader reader = getReader(0, 1); checkHost(reader.next(), "ns1.example1.test", "Hns1_example1_test-TEST"); } /** Reads at most one at 0 offset 1 maxResults */ @Test public void testZeroOffsetOneResult_stopsAfterOne() throws Exception { pushToGcs(DEPOSIT_3_HOST); RdeHostReader reader = getReader(0, 1); reader.next(); assertThrows(NoSuchElementException.class, reader::next); } /** Skips already-processed records after rehydration */ @Test public void testZeroOffsetOneResult_skipsOneAfterRehydration() throws Exception { pushToGcs(DEPOSIT_3_HOST); RdeHostReader reader = getReader(0, 1); reader.next(); reader.endSlice(); reader = cloneReader(reader); reader.beginSlice(); // reader will not advance any further assertThrows(NoSuchElementException.class, reader::next); } /** Reads three hosts */ @Test public void testZeroOffsetThreeResult_readsThree() throws Exception { pushToGcs(DEPOSIT_3_HOST); RdeHostReader reader = getReader(0, 3); checkHost(reader.next(), "ns1.example1.test", "Hns1_example1_test-TEST"); checkHost(reader.next(), "ns1.example2.test", "Hns1_example2_test-TEST"); checkHost(reader.next(), "ns1.example3.test", "Hns1_example3_test-TEST"); } /** Stops reading at 3 maxResults */ @Test public void testZeroOffsetThreeResult_stopsAtThree() throws Exception { pushToGcs(DEPOSIT_4_HOST); RdeHostReader reader = getReader(0, 3); for (int i = 0; i < 3; i++) { reader.next(); } assertThrows(NoSuchElementException.class, reader::next); } /** Reads one host from file then stops at end of file */ @Test public void testZeroOffsetThreeResult_endOfFile() throws Exception { pushToGcs(DEPOSIT_1_HOST); RdeHostReader reader = getReader(0, 3); reader.next(); assertThrows(NoSuchElementException.class, reader::next); } /** Skips three hosts with offset of three */ @Test public void testThreeOffsetOneResult_skipsThree() throws Exception { pushToGcs(DEPOSIT_4_HOST); RdeHostReader reader = getReader(3, 1); checkHost(reader.next(), "ns1.example4.test", "Hns1_example4_test-TEST"); } /** Skips four hosts after advancing once at three offset, then rehydrating */ @Test public void testThreeOffsetTwoResult_skipsFourAfterRehydration() throws Exception { pushToGcs(DEPOSIT_10_HOST); RdeHostReader reader = getReader(3, 2); reader.next(); reader.endSlice(); reader = cloneReader(reader); reader.beginSlice(); checkHost(reader.next(), "ns1.example5.test", "Hns1_example5_test-TEST"); } /** Reads three at zero offset three results with rehydration in the middle */ @Test public void testZeroOffsetThreeResult_readsThreeWithRehydration() throws Exception { pushToGcs(DEPOSIT_4_HOST); RdeHostReader reader = getReader(0, 3); checkHost(reader.next(), "ns1.example1.test", "Hns1_example1_test-TEST"); reader.endSlice(); reader = cloneReader(reader); reader.beginSlice(); checkHost(reader.next(), "ns1.example2.test", "Hns1_example2_test-TEST"); checkHost(reader.next(), "ns1.example3.test", "Hns1_example3_test-TEST"); } /** Stops reading at three with zero offset three results with rehydration in the middle */ @Test public void testZeroOffsetThreeResult_stopsAtThreeWithRehydration() throws Exception { pushToGcs(DEPOSIT_4_HOST); RdeHostReader reader = getReader(0, 3); reader.next(); reader.endSlice(); reader = cloneReader(reader); reader.beginSlice(); reader.next(); reader.next(); assertThrows(NoSuchElementException.class, reader::next); } private void pushToGcs(ByteSource source) throws IOException { try (OutputStream outStream = new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize()) .openOutputStream(new GcsFilename(IMPORT_BUCKET_NAME, IMPORT_FILE_NAME)); InputStream inStream = source.openStream()) { ByteStreams.copy(inStream, outStream); } } /** Creates a deep copy of the {@link RdeHostReader} */ private RdeHostReader cloneReader( RdeHostReader reader) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout); oout.writeObject(reader); ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStream oin = new ObjectInputStream(bin); return (RdeHostReader) oin.readObject(); } /** Verifies that domain name and ROID match expected values */ private void checkHost( JaxbFragment fragment, String domainName, String repoId) { assertThat(fragment).isNotNull(); XjcRdeHost host = fragment.getInstance().getValue(); assertThat(host.getName()).isEqualTo(domainName); assertThat(host.getRoid()).isEqualTo(repoId); } /** Gets a new {@link RdeHostReader} with specified offset and maxResults */ private RdeHostReader getReader(int offset, int maxResults) throws Exception { RdeHostReader reader = new RdeHostReader(IMPORT_BUCKET_NAME, IMPORT_FILE_NAME, offset, maxResults); reader.beginSlice(); return reader; } }