|
17 | 17 | package io.cdap.plugin.gcp.gcs; |
18 | 18 |
|
19 | 19 | import com.google.cloud.storage.BlobId; |
| 20 | +import com.google.cloud.storage.BucketInfo; |
| 21 | +import com.google.cloud.storage.Storage; |
| 22 | +import com.google.cloud.storage.StorageException; |
| 23 | +import org.junit.After; |
20 | 24 | import org.junit.Assert; |
| 25 | +import org.junit.Before; |
21 | 26 | import org.junit.Test; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.MockitoAnnotations; |
| 29 | +import org.slf4j.Logger; |
| 30 | + |
| 31 | +import java.io.ByteArrayOutputStream; |
| 32 | +import java.io.PrintStream; |
| 33 | + |
| 34 | +import static org.mockito.Mockito.any; |
| 35 | +import static org.mockito.Mockito.times; |
| 36 | +import static org.mockito.Mockito.verify; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
22 | 39 |
|
23 | 40 | /** |
24 | 41 | * Tests for storage client |
25 | 42 | */ |
26 | 43 | public class StorageClientTest { |
27 | 44 |
|
| 45 | + @Mock |
| 46 | + private Storage storage; |
| 47 | + |
| 48 | + private StorageClient storageClient; |
| 49 | + |
| 50 | + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 51 | + |
| 52 | + private final PrintStream originalOut = System.out; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setUp() { |
| 56 | + MockitoAnnotations.initMocks(this); |
| 57 | + storageClient = new StorageClient(storage); |
| 58 | + System.setOut(new PrintStream(outContent)); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void restoreStreams() { |
| 63 | + System.setOut(originalOut); |
| 64 | + } |
| 65 | + |
28 | 66 | @Test |
29 | 67 | public void testAppend() { |
30 | 68 | Assert.assertEquals("a/b/c", StorageClient.append("a/", "/b/c")); |
@@ -67,4 +105,43 @@ public void testNonExistingDestinationEndingSlash() { |
67 | 105 | Assert.assertEquals(BlobId.of("b0", "subdir/dir2/a/b/c"), |
68 | 106 | StorageClient.resolve("dir1/dir2", "dir1/dir2/a/b/c", GCSPath.from("b0/subdir/"), false)); |
69 | 107 | } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testCreateBucketIfNotExists() { |
| 111 | + // Test successful bucket creation |
| 112 | + GCSPath path = GCSPath.from("my-bucket"); |
| 113 | + storageClient.createBucketIfNotExists(path, "us", null); |
| 114 | + // No exception is thrown and method storage.create() is invoked once |
| 115 | + verify(storage, times(1)).create(any(BucketInfo.class)); |
| 116 | + |
| 117 | + // Test bucket already exists |
| 118 | + GCSPath existingPath = GCSPath.from("existing-bucket"); |
| 119 | + |
| 120 | + when(storage.create(any(BucketInfo.class))).thenThrow(new StorageException(409, "Conflict")); |
| 121 | + |
| 122 | + storageClient.createBucketIfNotExists(existingPath, "existing-location", null); |
| 123 | + // The exception thrown should be caught and warning log should be printed |
| 124 | + Assert.assertTrue(outContent.toString().contains("Getting 409 Conflict")); |
| 125 | + // The method storage.create() is invoked 2 times in total |
| 126 | + verify(storage, times(2)).create(any(BucketInfo.class)); |
| 127 | + |
| 128 | + // Test bucket creation failure |
| 129 | + GCSPath failurePath = GCSPath.from("failed-bucket"); |
| 130 | + |
| 131 | + when(storage.create(any(BucketInfo.class))).thenThrow(new StorageException(500, "Internal Server Error")); |
| 132 | + |
| 133 | + try { |
| 134 | + storageClient.createBucketIfNotExists(failurePath, "failed-location", null); |
| 135 | + } catch (Exception e) { |
| 136 | + // Verify that RuntimeException is caught |
| 137 | + if (!(e instanceof RuntimeException)) { |
| 138 | + Assert.fail(String.format("Test for detecting bucket creation failure did not succeed. " + |
| 139 | + "Unexpected Exception caught: %s", e)); |
| 140 | + } |
| 141 | + // The method storage.create() is invoked 3 times in total |
| 142 | + verify(storage, times(3)).create(any(BucketInfo.class)); |
| 143 | + return; |
| 144 | + } |
| 145 | + Assert.fail("Test for detecting bucket creation failure did not succeed. No exception caught"); |
| 146 | + } |
70 | 147 | } |
0 commit comments