|
| 1 | +/* |
| 2 | + * ****************************************************************************** |
| 3 | + * Copyright 2014-2015 Spectra Logic Corporation. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | + * specific language governing permissions and limitations under the License. |
| 13 | + * **************************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +package com.spectralogic.ds3client.integration; |
| 17 | + |
| 18 | +import com.spectralogic.ds3client.Ds3Client; |
| 19 | +import com.spectralogic.ds3client.helpers.Ds3ClientHelpers; |
| 20 | +import com.spectralogic.ds3client.helpers.LazyObjectIterable; |
| 21 | +import com.spectralogic.ds3client.integration.test.helpers.TempStorageIds; |
| 22 | +import com.spectralogic.ds3client.integration.test.helpers.TempStorageUtil; |
| 23 | +import com.spectralogic.ds3client.models.ChecksumType; |
| 24 | +import com.spectralogic.ds3client.models.Contents; |
| 25 | +import com.spectralogic.ds3client.serializer.XmlProcessingException; |
| 26 | +import org.junit.AfterClass; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.URISyntaxException; |
| 34 | +import java.security.SignatureException; |
| 35 | +import java.util.Iterator; |
| 36 | +import java.util.UUID; |
| 37 | + |
| 38 | +import static com.spectralogic.ds3client.integration.Util.deleteAllContents; |
| 39 | +import static com.spectralogic.ds3client.integration.Util.loadBookTestData; |
| 40 | +import static org.hamcrest.CoreMatchers.is; |
| 41 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 42 | +import static org.junit.Assert.assertFalse; |
| 43 | +import static org.junit.Assert.assertThat; |
| 44 | +import static org.junit.Assert.assertTrue; |
| 45 | + |
| 46 | +public class LazyIterator_Test { |
| 47 | + private static final Logger LOG = LoggerFactory.getLogger(LazyIterator_Test.class); |
| 48 | + |
| 49 | + private static final Ds3Client CLIENT = Util.fromEnv(); |
| 50 | + private static final Ds3ClientHelpers HELPERS = Ds3ClientHelpers.wrap(CLIENT); |
| 51 | + private static final String TEST_ENV_NAME = "lazy_iterator_test"; |
| 52 | + private static TempStorageIds envStorageIds; |
| 53 | + private static UUID envDataPolicyId; |
| 54 | + private static final int retries = 5; |
| 55 | + |
| 56 | + @BeforeClass |
| 57 | + public static void startup() throws IOException, SignatureException { |
| 58 | + LOG.info("Starting test Setup..."); |
| 59 | + envDataPolicyId = TempStorageUtil.setupDataPolicy(TEST_ENV_NAME, false, ChecksumType.Type.MD5, CLIENT); |
| 60 | + envStorageIds = TempStorageUtil.setup(TEST_ENV_NAME, envDataPolicyId, CLIENT); |
| 61 | + LOG.info("Finished test Setup..."); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterClass |
| 65 | + public static void teardown() throws IOException, SignatureException { |
| 66 | + LOG.info("Starting test teardown..."); |
| 67 | + TempStorageUtil.teardown(TEST_ENV_NAME, envStorageIds, CLIENT); |
| 68 | + CLIENT.close(); |
| 69 | + LOG.info("Finished test teardown..."); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void emptyTest() throws IOException, SignatureException { |
| 74 | + HELPERS.ensureBucketExists(TEST_ENV_NAME, envDataPolicyId); |
| 75 | + try { |
| 76 | + final String prefix = ""; |
| 77 | + final String nextMarker = null; |
| 78 | + final int maxKeys = 100; |
| 79 | + |
| 80 | + |
| 81 | + final LazyObjectIterable iterable = new LazyObjectIterable(CLIENT, TEST_ENV_NAME, prefix, nextMarker, maxKeys, retries); |
| 82 | + final Iterator<Contents> iterator = iterable.iterator(); |
| 83 | + |
| 84 | + assertFalse(iterator.hasNext()); |
| 85 | + |
| 86 | + } finally { |
| 87 | + deleteAllContents(CLIENT, TEST_ENV_NAME); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void singlePageTest() throws IOException, XmlProcessingException, SignatureException, URISyntaxException { |
| 93 | + HELPERS.ensureBucketExists(TEST_ENV_NAME, envDataPolicyId); |
| 94 | + loadBookTestData(CLIENT, TEST_ENV_NAME); |
| 95 | + try { |
| 96 | + final String prefix = ""; |
| 97 | + final String nextMarker = null; |
| 98 | + final int maxKeys = 100; |
| 99 | + |
| 100 | + final LazyObjectIterable iterable = new LazyObjectIterable(CLIENT, TEST_ENV_NAME, prefix, nextMarker, maxKeys, retries); |
| 101 | + final Iterator<Contents> iterator = iterable.iterator(); |
| 102 | + |
| 103 | + assertTrue(iterator.hasNext()); |
| 104 | + assertThat(iterator.next(), is(notNullValue())); |
| 105 | + assertTrue(iterator.hasNext()); |
| 106 | + assertThat(iterator.next(), is(notNullValue())); |
| 107 | + assertTrue(iterator.hasNext()); |
| 108 | + assertThat(iterator.next(), is(notNullValue())); |
| 109 | + assertTrue(iterator.hasNext()); |
| 110 | + assertThat(iterator.next(), is(notNullValue())); |
| 111 | + assertFalse(iterator.hasNext()); |
| 112 | + } finally { |
| 113 | + deleteAllContents(CLIENT, TEST_ENV_NAME); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void multiPageTest() throws IOException, URISyntaxException, XmlProcessingException { |
| 119 | + HELPERS.ensureBucketExists(TEST_ENV_NAME, envDataPolicyId); |
| 120 | + loadBookTestData(CLIENT, TEST_ENV_NAME); |
| 121 | + try { |
| 122 | + final String prefix = ""; |
| 123 | + final String nextMarker = null; |
| 124 | + final int maxKeys = 2; |
| 125 | + |
| 126 | + final LazyObjectIterable iterable = new LazyObjectIterable(CLIENT, TEST_ENV_NAME, prefix, nextMarker, maxKeys, retries); |
| 127 | + final Iterator<Contents> iterator = iterable.iterator(); |
| 128 | + |
| 129 | + assertTrue(iterator.hasNext()); |
| 130 | + assertThat(iterator.next(), is(notNullValue())); |
| 131 | + assertTrue(iterator.hasNext()); |
| 132 | + assertThat(iterator.next(), is(notNullValue())); |
| 133 | + assertTrue(iterator.hasNext()); |
| 134 | + assertThat(iterator.next(), is(notNullValue())); |
| 135 | + assertTrue(iterator.hasNext()); |
| 136 | + assertThat(iterator.next(), is(notNullValue())); |
| 137 | + assertFalse(iterator.hasNext()); |
| 138 | + } finally { |
| 139 | + deleteAllContents(CLIENT, TEST_ENV_NAME); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments