|
25 | 25 | import static org.junit.Assert.assertArrayEquals; |
26 | 26 | import static org.junit.Assert.assertEquals; |
27 | 27 | import static org.junit.Assert.assertFalse; |
| 28 | +import static org.junit.Assert.assertNull; |
28 | 29 | import static org.junit.Assert.assertSame; |
29 | 30 | import static org.junit.Assert.assertTrue; |
30 | 31 |
|
| 32 | +import com.google.api.client.util.Lists; |
31 | 33 | import com.google.gcloud.storage.Storage.CopyRequest; |
32 | 34 | import org.easymock.Capture; |
33 | 35 | import org.junit.After; |
34 | 36 | import org.junit.Before; |
35 | 37 | import org.junit.Test; |
36 | 38 | import java.net.URL; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
37 | 41 |
|
38 | 42 | public class BlobTest { |
39 | 43 |
|
40 | 44 | private static final BlobInfo BLOB_INFO = BlobInfo.of("b", "n"); |
| 45 | + private static final BlobInfo[] BLOB_INFO_ARRAY = {BlobInfo.of("b1", "n1"), |
| 46 | + BlobInfo.of("b2", "n2"), BlobInfo.of("b3", "n3")}; |
41 | 47 |
|
42 | 48 | private Storage storage; |
43 | 49 | private Blob blob; |
@@ -159,4 +165,129 @@ public void testSignUrl() throws Exception { |
159 | 165 | replay(storage); |
160 | 166 | assertEquals(url, blob.signUrl(100)); |
161 | 167 | } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testGetNone() throws Exception { |
| 171 | + replay(storage); |
| 172 | + assertTrue(Blob.get(storage).isEmpty()); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testGetOne() throws Exception { |
| 177 | + expect(storage.get(BLOB_INFO.bucket(), BLOB_INFO.name())).andReturn(BLOB_INFO); |
| 178 | + replay(storage); |
| 179 | + List<Blob> result = Blob.get(storage, BLOB_INFO); |
| 180 | + assertEquals(1, result.size()); |
| 181 | + assertEquals(BLOB_INFO, result.get(0).info()); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testGetSome() throws Exception { |
| 186 | + List<BlobInfo> blobInfoList = Arrays.asList(BLOB_INFO_ARRAY); |
| 187 | + expect(storage.get(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1], |
| 188 | + Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList); |
| 189 | + replay(storage); |
| 190 | + List<Blob> result = Blob.get(storage, BLOB_INFO_ARRAY); |
| 191 | + assertEquals(blobInfoList.size(), result.size()); |
| 192 | + for (int i = 0; i < blobInfoList.size(); i++) { |
| 193 | + assertEquals(blobInfoList.get(i), result.get(i).info()); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testGetSomeNull() throws Exception { |
| 199 | + List<BlobInfo> blobInfoList = Arrays.asList(BLOB_INFO_ARRAY[0], null, BLOB_INFO_ARRAY[2]); |
| 200 | + expect(storage.get(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1], |
| 201 | + Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList); |
| 202 | + replay(storage); |
| 203 | + List<Blob> result = Blob.get(storage, BLOB_INFO_ARRAY); |
| 204 | + assertEquals(blobInfoList.size(), result.size()); |
| 205 | + for (int i = 0; i < blobInfoList.size(); i++) { |
| 206 | + if (blobInfoList.get(i) != null) { |
| 207 | + assertEquals(blobInfoList.get(i), result.get(i).info()); |
| 208 | + } else { |
| 209 | + assertNull(result.get(i)); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void testUpdateNone() throws Exception { |
| 216 | + replay(storage); |
| 217 | + assertTrue(Blob.update(storage).isEmpty()); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testUpdateOne() throws Exception { |
| 222 | + BlobInfo updatedBlob = BLOB_INFO.toBuilder().contentType("content").build(); |
| 223 | + expect(storage.update(BLOB_INFO)).andReturn(updatedBlob); |
| 224 | + replay(storage); |
| 225 | + List<Blob> result = Blob.update(storage, BLOB_INFO); |
| 226 | + assertEquals(1, result.size()); |
| 227 | + assertEquals(updatedBlob, result.get(0).info()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void testUpdateSome() throws Exception { |
| 232 | + List<BlobInfo> blobInfoList = Lists.newArrayListWithCapacity(BLOB_INFO_ARRAY.length); |
| 233 | + for (BlobInfo info : BLOB_INFO_ARRAY) { |
| 234 | + blobInfoList.add(info.toBuilder().contentType("content").build()); |
| 235 | + } |
| 236 | + expect(storage.update(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1], |
| 237 | + Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList); |
| 238 | + replay(storage); |
| 239 | + List<Blob> result = Blob.update(storage, BLOB_INFO_ARRAY); |
| 240 | + assertEquals(blobInfoList.size(), result.size()); |
| 241 | + for (int i = 0; i < blobInfoList.size(); i++) { |
| 242 | + assertEquals(blobInfoList.get(i), result.get(i).info()); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void testUpdateSomeNull() throws Exception { |
| 248 | + List<BlobInfo> blobInfoList = Arrays.asList( |
| 249 | + BLOB_INFO_ARRAY[0].toBuilder().contentType("content").build(), null, |
| 250 | + BLOB_INFO_ARRAY[2].toBuilder().contentType("content").build()); |
| 251 | + expect(storage.update(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1], |
| 252 | + Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))).andReturn(blobInfoList); |
| 253 | + replay(storage); |
| 254 | + List<Blob> result = Blob.update(storage, BLOB_INFO_ARRAY); |
| 255 | + assertEquals(blobInfoList.size(), result.size()); |
| 256 | + for (int i = 0; i < blobInfoList.size(); i++) { |
| 257 | + if (blobInfoList.get(i) != null) { |
| 258 | + assertEquals(blobInfoList.get(i), result.get(i).info()); |
| 259 | + } else { |
| 260 | + assertNull(result.get(i)); |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + public void testDeleteNone() throws Exception { |
| 267 | + replay(storage); |
| 268 | + assertTrue(Blob.delete(storage).isEmpty()); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void testDeleteOne() throws Exception { |
| 273 | + expect(storage.delete(BLOB_INFO.bucket(), BLOB_INFO.name())).andReturn(true); |
| 274 | + replay(storage); |
| 275 | + List<Boolean> result = Blob.delete(storage, BLOB_INFO); |
| 276 | + assertEquals(1, result.size()); |
| 277 | + assertTrue(result.get(0)); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + public void testDeleteSome() throws Exception { |
| 282 | + List<Boolean> deleleResultList = Arrays.asList(true, true, true); |
| 283 | + expect(storage.delete(BLOB_INFO_ARRAY[0], BLOB_INFO_ARRAY[1], |
| 284 | + Arrays.copyOfRange(BLOB_INFO_ARRAY, 2, BLOB_INFO_ARRAY.length))) |
| 285 | + .andReturn(deleleResultList); |
| 286 | + replay(storage); |
| 287 | + List<Boolean> result = Blob.delete(storage, BLOB_INFO_ARRAY); |
| 288 | + assertEquals(deleleResultList.size(), result.size()); |
| 289 | + for (int i = 0; i < deleleResultList.size(); i++) { |
| 290 | + assertEquals(deleleResultList.get(i), result.get(i)); |
| 291 | + } |
| 292 | + } |
162 | 293 | } |
0 commit comments