1111import com .azure .storage .queue .QueueServiceClientBuilder ;
1212import org .junit .AfterClass ;
1313import org .junit .BeforeClass ;
14- import org .junit .Rule ;
1514import org .junit .Test ;
1615import org .testcontainers .utility .DockerImageName ;
16+ import org .testcontainers .utility .MountableFile ;
1717
18- import java .io .File ;
19- import java .net .URL ;
20- import java .util .Optional ;
2118import java .util .Properties ;
2219
2320import static org .assertj .core .api .Assertions .assertThat ;
2421
2522public class AzuriteContainerTest {
2623
27- private static final File PFX_STORE_FILE = getResourceFile ("/keystore.pfx" );
28-
29- private static final File PEM_CERT_FILE = getResourceFile ("/certificate.pem" );
30-
31- private static final File PEM_KEY_FILE = getResourceFile ("/key.pem" );
32-
3324 private static final String PASSWORD = "changeit" ;
3425
3526 private static final String LOOPBACK_IP = "127.0.0.1" ;
@@ -39,7 +30,10 @@ public class AzuriteContainerTest {
3930 @ BeforeClass
4031 public static void captureOriginalSystemProperties () {
4132 originalSystemProperties = (Properties ) System .getProperties ().clone ();
42- System .setProperty ("javax.net.ssl.trustStore" , PFX_STORE_FILE .getAbsolutePath ());
33+ System .setProperty (
34+ "javax.net.ssl.trustStore" ,
35+ MountableFile .forClasspathResource ("/keystore.pfx" ).getFilesystemPath ()
36+ );
4337 System .setProperty ("javax.net.ssl.trustStorePassword" , PASSWORD );
4438 System .setProperty ("javax.net.ssl.trustStoreType" , "PKCS12" );
4539 }
@@ -49,119 +43,167 @@ public static void restoreOriginalSystemProperties() {
4943 System .setProperties (originalSystemProperties );
5044 }
5145
52- @ Rule
53- // emulatorContainer {
54- public AzuriteContainer emulator = new AzuriteContainer (
55- DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite" )
56- );
57-
58- // }
59-
60- @ Rule
61- public AzuriteContainer pfxEmulator = new AzuriteContainer (
62- DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite" )
63- )
64- .withSsl (PFX_STORE_FILE , PASSWORD )
65- .withHost (LOOPBACK_IP );
66-
67- @ Rule
68- public AzuriteContainer pemEmulator = new AzuriteContainer (
69- DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite" )
70- )
71- .withSsl (PEM_CERT_FILE , PEM_KEY_FILE )
72- .withHost (LOOPBACK_IP );
73-
7446 @ Test
7547 public void testWithBlobServiceClient () {
76- // getConnectionString {
77- final String connectionString = emulator .getDefaultConnectionString ();
78- // }
79- testBlob (connectionString );
48+ try (
49+ // emulatorContainer {
50+ AzuriteContainer emulator = new AzuriteContainer (
51+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
52+ );
53+ // }
54+ ) {
55+ emulator .start ();
56+ testBlob (emulator );
57+ }
8058 }
8159
8260 @ Test
8361 public void testWithQueueServiceClient () {
84- final String connectionString = emulator .getDefaultConnectionString ();
85- testQueue (connectionString );
62+ try (
63+ AzuriteContainer emulator = new AzuriteContainer (
64+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
65+ );
66+ ) {
67+ emulator .start ();
68+ testQueue (emulator );
69+ }
8670 }
8771
8872 @ Test
8973 public void testWithTableServiceClient () {
90- final String connectionString = emulator .getDefaultConnectionString ();
91- testTable (connectionString );
74+ try (
75+ AzuriteContainer emulator = new AzuriteContainer (
76+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
77+ );
78+ ) {
79+ emulator .start ();
80+ testTable (emulator );
81+ }
9282 }
9383
9484 @ Test
9585 public void testWithBlobServiceClientWithSslUsingPfx () {
96- final String connectionString = pfxEmulator .getDefaultConnectionString ();
97- testBlob (connectionString );
86+ try (
87+ AzuriteContainer emulator = new AzuriteContainer (
88+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
89+ )
90+ .withSsl (MountableFile .forClasspathResource ("/keystore.pfx" ), PASSWORD )
91+ .withHost (LOOPBACK_IP );
92+ ) {
93+ emulator .start ();
94+ testBlob (emulator );
95+ }
9896 }
9997
10098 @ Test
10199 public void testWithQueueServiceClientWithSslUsingPfx () {
102- final String connectionString = pfxEmulator .getDefaultConnectionString ();
103- testQueue (connectionString );
100+ try (
101+ AzuriteContainer emulator = new AzuriteContainer (
102+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
103+ )
104+ .withSsl (MountableFile .forClasspathResource ("/keystore.pfx" ), PASSWORD )
105+ .withHost (LOOPBACK_IP );
106+ ) {
107+ emulator .start ();
108+ testQueue (emulator );
109+ }
104110 }
105111
106112 @ Test
107113 public void testWithTableServiceClientWithSslUsingPfx () {
108- final String connectionString = pfxEmulator .getDefaultConnectionString ();
109- testTable (connectionString );
114+ try (
115+ AzuriteContainer emulator = new AzuriteContainer (
116+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
117+ )
118+ .withSsl (MountableFile .forClasspathResource ("/keystore.pfx" ), PASSWORD )
119+ .withHost (LOOPBACK_IP );
120+ ) {
121+ emulator .start ();
122+ testTable (emulator );
123+ }
110124 }
111125
112126 @ Test
113127 public void testWithBlobServiceClientWithSslUsingPem () {
114- final String connectionString = pemEmulator .getDefaultConnectionString ();
115- testBlob (connectionString );
128+ try (
129+ AzuriteContainer emulator = new AzuriteContainer (
130+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
131+ )
132+ .withSsl (
133+ MountableFile .forClasspathResource ("/certificate.pem" ),
134+ MountableFile .forClasspathResource ("/key.pem" )
135+ )
136+ .withHost (LOOPBACK_IP );
137+ ) {
138+ emulator .start ();
139+ testBlob (emulator );
140+ }
116141 }
117142
118143 @ Test
119144 public void testWithQueueServiceClientWithSslUsingPem () {
120- final String connectionString = pemEmulator .getDefaultConnectionString ();
121- testQueue (connectionString );
145+ try (
146+ AzuriteContainer emulator = new AzuriteContainer (
147+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
148+ )
149+ .withSsl (
150+ MountableFile .forClasspathResource ("/certificate.pem" ),
151+ MountableFile .forClasspathResource ("/key.pem" )
152+ )
153+ .withHost (LOOPBACK_IP );
154+ ) {
155+ emulator .start ();
156+ testQueue (emulator );
157+ }
122158 }
123159
124160 @ Test
125161 public void testWithTableServiceClientWithSslUsingPem () {
126- final String connectionString = pemEmulator .getDefaultConnectionString ();
127- testTable (connectionString );
162+ try (
163+ AzuriteContainer emulator = new AzuriteContainer (
164+ DockerImageName .parse ("mcr.microsoft.com/azure-storage/azurite:3.33.0" )
165+ )
166+ .withSsl (
167+ MountableFile .forClasspathResource ("/certificate.pem" ),
168+ MountableFile .forClasspathResource ("/key.pem" )
169+ )
170+ .withHost (LOOPBACK_IP );
171+ ) {
172+ emulator .start ();
173+ testTable (emulator );
174+ }
128175 }
129176
130- private void testBlob (final String connectionString ) {
177+ private void testBlob (AzuriteContainer container ) {
131178 // createBlobClient {
132- final BlobServiceClient blobServiceClient = new BlobServiceClientBuilder ()
133- .connectionString (connectionString )
179+ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder ()
180+ .connectionString (container . getDefaultConnectionString () )
134181 .buildClient ();
135182 // }
136- final BlobContainerClient containerClient = blobServiceClient .createBlobContainer ("test-container" );
183+ BlobContainerClient containerClient = blobServiceClient .createBlobContainer ("test-container" );
137184
138185 assertThat (containerClient .exists ()).isTrue ();
139186 }
140187
141- private void testQueue (final String connectionString ) {
188+ private void testQueue (AzuriteContainer container ) {
142189 // createQueueClient {
143- final QueueServiceClient queueServiceClient = new QueueServiceClientBuilder ()
144- .connectionString (connectionString )
190+ QueueServiceClient queueServiceClient = new QueueServiceClientBuilder ()
191+ .connectionString (container . getDefaultConnectionString () )
145192 .buildClient ();
146193 // }
147- final QueueClient queueClient = queueServiceClient .createQueue ("test-queue" );
194+ QueueClient queueClient = queueServiceClient .createQueue ("test-queue" );
148195
149196 assertThat (queueClient .getQueueUrl ()).isNotNull ();
150197 }
151198
152- private void testTable (final String connectionString ) {
199+ private void testTable (AzuriteContainer container ) {
153200 // createTableClient {
154- final TableServiceClient tableServiceClient = new TableServiceClientBuilder ()
155- .connectionString (connectionString )
201+ TableServiceClient tableServiceClient = new TableServiceClientBuilder ()
202+ .connectionString (container . getDefaultConnectionString () )
156203 .buildClient ();
157204 // }
158- final TableClient tableClient = tableServiceClient .createTable ("testtable" );
205+ TableClient tableClient = tableServiceClient .createTable ("testtable" );
159206
160207 assertThat (tableClient .getTableEndpoint ()).isNotNull ();
161208 }
162-
163- private static File getResourceFile (final String resourceName ) {
164- final URL resource = AzuriteContainerTest .class .getResource (resourceName );
165- return Optional .ofNullable (resource ).map (URL ::getFile ).map (File ::new ).orElse (null );
166- }
167209}
0 commit comments