|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amplifyframework.storage.s3 |
| 17 | + |
| 18 | +import android.content.Context |
| 19 | +import androidx.test.core.app.ApplicationProvider |
| 20 | +import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin |
| 21 | +import com.amplifyframework.storage.StorageCategory |
| 22 | +import com.amplifyframework.storage.StoragePath |
| 23 | +import com.amplifyframework.storage.options.StorageRemoveOptions |
| 24 | +import com.amplifyframework.storage.options.StorageUploadFileOptions |
| 25 | +import com.amplifyframework.storage.options.SubpathStrategy |
| 26 | +import com.amplifyframework.storage.s3.options.AWSS3StoragePagedListOptions |
| 27 | +import com.amplifyframework.storage.s3.test.R |
| 28 | +import com.amplifyframework.storage.s3.util.WorkmanagerTestUtils |
| 29 | +import com.amplifyframework.testutils.random.RandomTempFile |
| 30 | +import com.amplifyframework.testutils.sync.SynchronousAuth |
| 31 | +import com.amplifyframework.testutils.sync.SynchronousStorage |
| 32 | +import io.kotest.matchers.collections.shouldContainExactly |
| 33 | +import io.kotest.matchers.ints.shouldBeExactly |
| 34 | +import io.kotest.matchers.nulls.shouldBeNull |
| 35 | +import java.io.File |
| 36 | +import org.junit.After |
| 37 | +import org.junit.BeforeClass |
| 38 | +import org.junit.Test |
| 39 | + |
| 40 | +/** |
| 41 | + * Integration tests for using SubpathStrategy with Storage List API |
| 42 | + */ |
| 43 | +class AWSS3StorageSubPathStrategyListTest { |
| 44 | + companion object { |
| 45 | + private const val SMALL_FILE_SIZE = 100L |
| 46 | + private const val FIRST_FILE_NAME = "01" |
| 47 | + private const val SECOND_FILE_NAME = "02" |
| 48 | + private const val THIRD_FILE_NAME = "03" |
| 49 | + private const val FOURTH_FILE_NAME = "04" |
| 50 | + private const val FIFTH_FILE_NAME = "05" |
| 51 | + private const val CUSTOM_FILE_NAME = "custom" |
| 52 | + private const val FIRST_FILE_STRING_PATH = "public/photos/2023/$FIRST_FILE_NAME" |
| 53 | + private val FIRST_FILE_PATH = StoragePath.fromString(FIRST_FILE_STRING_PATH) |
| 54 | + private const val SECOND_FILE_STRING_PATH = "public/photos/2023/$SECOND_FILE_NAME" |
| 55 | + private val SECOND_FILE_PATH = StoragePath.fromString(SECOND_FILE_STRING_PATH) |
| 56 | + private const val THIRD_FILE_STRING_PATH = "public/photos/2024/$THIRD_FILE_NAME" |
| 57 | + private val THIRD_FILE_PATH = StoragePath.fromString(THIRD_FILE_STRING_PATH) |
| 58 | + private const val FOURTH_FILE_STRING_PATH = "public/photos/2024/$FOURTH_FILE_NAME" |
| 59 | + private val FOURTH_FILE_PATH = StoragePath.fromString(FOURTH_FILE_STRING_PATH) |
| 60 | + private const val FIFTH_FILE_STRING_PATH = "public/photos/$FIFTH_FILE_NAME" |
| 61 | + private val FIFTH_FILE_PATH = StoragePath.fromString(FIFTH_FILE_STRING_PATH) |
| 62 | + private const val CUSTOM_FILE_STRING_PATH = "public/photos/202$/$CUSTOM_FILE_NAME" |
| 63 | + private val CUSTOM_FILE_PATH = StoragePath.fromString(CUSTOM_FILE_STRING_PATH) |
| 64 | + |
| 65 | + lateinit var storageCategory: StorageCategory |
| 66 | + lateinit var synchronousStorage: SynchronousStorage |
| 67 | + lateinit var synchronousAuth: SynchronousAuth |
| 68 | + private lateinit var first: File |
| 69 | + private lateinit var second: File |
| 70 | + private lateinit var third: File |
| 71 | + private lateinit var fourth: File |
| 72 | + private lateinit var fifth: File |
| 73 | + private lateinit var customFile: File |
| 74 | + |
| 75 | + /** |
| 76 | + * Initialize mobile client and configure the storage. |
| 77 | + * Upload the test files ahead of time. |
| 78 | + */ |
| 79 | + @JvmStatic |
| 80 | + @BeforeClass |
| 81 | + fun setUpOnce() { |
| 82 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 83 | + WorkmanagerTestUtils.initializeWorkmanagerTestUtil(context) |
| 84 | + |
| 85 | + synchronousAuth = SynchronousAuth.delegatingToCognito(context, AWSCognitoAuthPlugin()) |
| 86 | + |
| 87 | + // Get a handle to storage |
| 88 | + storageCategory = TestStorageCategory.create(context, R.raw.amplifyconfiguration) |
| 89 | + synchronousStorage = SynchronousStorage.delegatingTo(storageCategory) |
| 90 | + |
| 91 | + // Upload test files |
| 92 | + first = RandomTempFile(FIRST_FILE_NAME, SMALL_FILE_SIZE) |
| 93 | + synchronousStorage.uploadFile(FIRST_FILE_PATH, first, StorageUploadFileOptions.defaultInstance()) |
| 94 | + second = RandomTempFile(SECOND_FILE_NAME, SMALL_FILE_SIZE) |
| 95 | + synchronousStorage.uploadFile(SECOND_FILE_PATH, second, StorageUploadFileOptions.defaultInstance()) |
| 96 | + third = RandomTempFile(THIRD_FILE_NAME, SMALL_FILE_SIZE) |
| 97 | + synchronousStorage.uploadFile(THIRD_FILE_PATH, third, StorageUploadFileOptions.defaultInstance()) |
| 98 | + fourth = RandomTempFile(FOURTH_FILE_NAME, SMALL_FILE_SIZE) |
| 99 | + synchronousStorage.uploadFile(FOURTH_FILE_PATH, fourth, StorageUploadFileOptions.defaultInstance()) |
| 100 | + fifth = RandomTempFile(FIFTH_FILE_NAME, SMALL_FILE_SIZE) |
| 101 | + synchronousStorage.uploadFile(FIFTH_FILE_PATH, fifth, StorageUploadFileOptions.defaultInstance()) |
| 102 | + |
| 103 | + customFile = RandomTempFile(CUSTOM_FILE_NAME, SMALL_FILE_SIZE) |
| 104 | + synchronousStorage.uploadFile(CUSTOM_FILE_PATH, customFile, StorageUploadFileOptions.defaultInstance()) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @After |
| 109 | + fun tearDown() { |
| 110 | + synchronousStorage.remove("photos/2023/$FIRST_FILE_NAME", StorageRemoveOptions.defaultInstance()) |
| 111 | + synchronousStorage.remove("photos/2023/$SECOND_FILE_NAME", StorageRemoveOptions.defaultInstance()) |
| 112 | + synchronousStorage.remove("photos/2024/$THIRD_FILE_NAME", StorageRemoveOptions.defaultInstance()) |
| 113 | + synchronousStorage.remove("photos/2024/$FOURTH_FILE_NAME", StorageRemoveOptions.defaultInstance()) |
| 114 | + synchronousStorage.remove("photos/$FIFTH_FILE_NAME", StorageRemoveOptions.defaultInstance()) |
| 115 | + synchronousStorage.remove("photos/$CUSTOM_FILE_NAME", StorageRemoveOptions.defaultInstance()) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun testListWithIncludeStrategyAndStoragePath() { |
| 120 | + val path = StoragePath.fromString("public/photos/") |
| 121 | + val options = AWSS3StoragePagedListOptions |
| 122 | + .builder() |
| 123 | + .setPageSize(10) |
| 124 | + .setSubpathStrategy(SubpathStrategy.Include) |
| 125 | + .build() |
| 126 | + |
| 127 | + val result = synchronousStorage.list(path, options) |
| 128 | + |
| 129 | + result.items.size shouldBeExactly(6) |
| 130 | + result.items.mapNotNull { it.path } shouldContainExactly listOf( |
| 131 | + "public/photos/05", |
| 132 | + "public/photos/202$/custom", |
| 133 | + "public/photos/2023/01", |
| 134 | + "public/photos/2023/02", |
| 135 | + "public/photos/2024/03", |
| 136 | + "public/photos/2024/04" |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + fun testListWithExcludeStrategyAndStoragePath() { |
| 142 | + val options = AWSS3StoragePagedListOptions |
| 143 | + .builder() |
| 144 | + .setPageSize(10) |
| 145 | + .setSubpathStrategy(SubpathStrategy.Exclude()) |
| 146 | + .build() |
| 147 | + |
| 148 | + var result = synchronousStorage.list(StoragePath.fromString("public/photos/"), options) |
| 149 | + |
| 150 | + result.items.size shouldBeExactly(1) |
| 151 | + result.items.mapNotNull { it.path } shouldContainExactly listOf("public/photos/05") |
| 152 | + |
| 153 | + result.excludedSubpaths.size shouldBeExactly(3) |
| 154 | + result.excludedSubpaths shouldContainExactly listOf( |
| 155 | + "public/photos/202$/", |
| 156 | + "public/photos/2023/", |
| 157 | + "public/photos/2024/" |
| 158 | + ) |
| 159 | + |
| 160 | + result = synchronousStorage.list(StoragePath.fromString("public/photos/2023/"), options) |
| 161 | + |
| 162 | + result.items.size shouldBeExactly(2) |
| 163 | + result.items.mapNotNull { it.path } shouldContainExactly listOf( |
| 164 | + "public/photos/2023/01", |
| 165 | + "public/photos/2023/02" |
| 166 | + ) |
| 167 | + |
| 168 | + result.excludedSubpaths.shouldBeNull() |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun testListWithExcludeCustomDelimiterStrategyAndStoragePath() { |
| 173 | + val options = AWSS3StoragePagedListOptions |
| 174 | + .builder() |
| 175 | + .setPageSize(10) |
| 176 | + .setSubpathStrategy(SubpathStrategy.Exclude("$")) |
| 177 | + .build() |
| 178 | + |
| 179 | + var result = synchronousStorage.list(StoragePath.fromString("public/photos/"), options) |
| 180 | + |
| 181 | + result.items.size shouldBeExactly(5) |
| 182 | + result.items.mapNotNull { it.path } shouldContainExactly listOf( |
| 183 | + "public/photos/05", |
| 184 | + "public/photos/2023/01", |
| 185 | + "public/photos/2023/02", |
| 186 | + "public/photos/2024/03", |
| 187 | + "public/photos/2024/04" |
| 188 | + ) |
| 189 | + |
| 190 | + result.excludedSubpaths.size shouldBeExactly(1) |
| 191 | + result.excludedSubpaths shouldContainExactly listOf("public/photos/202$") |
| 192 | + |
| 193 | + result = synchronousStorage.list(StoragePath.fromString("public/photos/2023/"), options) |
| 194 | + |
| 195 | + result.items.size shouldBeExactly(2) |
| 196 | + result.items.mapNotNull { it.path } shouldContainExactly listOf( |
| 197 | + "public/photos/2023/01", |
| 198 | + "public/photos/2023/02", |
| 199 | + ) |
| 200 | + } |
| 201 | +} |
0 commit comments