Skip to content

Commit

Permalink
Update Azure extension tests to JUnit 5 (#16521)
Browse files Browse the repository at this point in the history
Changes:
- Loosely followed the steps in the migration guide at
https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4
- Updated POM to add JUnit 5 dependencies
- Updated imports to JUnit 5 packages
- Updated annotations (Lifecycle annotations like `@BeforeEach`)
- Updated exception testing (`assertThrows`)
- Updated temporary path handling (use `@TempDir` annotation)
- Various other updates (replace other `Rule` usages, make sure to use JUnit 5 assertions)
  • Loading branch information
amaechler authored Jun 4, 2024
1 parent 8f78c90 commit 6c7443c
Show file tree
Hide file tree
Showing 23 changed files with 1,141 additions and 1,048 deletions.
19 changes: 17 additions & 2 deletions extensions-core/azure-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,23 @@
</dependency>
<!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
import org.apache.druid.storage.azure.AzureUtils;
import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

public class AzureEntityTest extends EasyMockSupport
{
private static final String STORAGE_ACCOUNT_NAME = "storageAccount";
Expand All @@ -49,8 +51,6 @@ public class AzureEntityTest extends EasyMockSupport
private CloudObjectLocation location;
private AzureByteSourceFactory byteSourceFactory;
private AzureByteSource byteSource;

private AzureEntity azureEntity;
private AzureStorage azureStorage;

static {
Expand All @@ -62,7 +62,7 @@ public class AzureEntityTest extends EasyMockSupport
}
}

@Before
@BeforeEach
public void setup()
{
location = createMock(CloudObjectLocation.class);
Expand All @@ -80,13 +80,12 @@ public void test_getUri_returnsLocationUri()
EasyMock.expect(location.toUri(AzureInputSource.SCHEME)).andReturn(ENTITY_URI);
replayAll();

azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
final AzureEntity azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);

URI actualUri = azureEntity.getUri();
Assert.assertEquals(ENTITY_URI, actualUri);
assertEquals(ENTITY_URI, actualUri);

verifyAll();

}

@Test
Expand All @@ -95,20 +94,19 @@ public void test_getUri_returnsLocationUri_azureStorageScheme()
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(
final AzureEntity azureEntity = new AzureEntity(
new CloudObjectLocation(STORAGE_ACCOUNT_NAME, CONTAINER_NAME + "/" + BLOB_NAME),
azureStorage,
AzureStorageAccountInputSource.SCHEME,
byteSourceFactory
);

Assert.assertEquals(
assertEquals(
URI.create(AzureStorageAccountInputSource.SCHEME + "://" + STORAGE_ACCOUNT_NAME + "/" + CONTAINER_NAME + "/" + BLOB_NAME),
azureEntity.getUri()
);

verifyAll();

}

@Test
Expand All @@ -120,10 +118,10 @@ public void test_readFromStart_returnsExpectedStream() throws Exception
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
final AzureEntity azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);

InputStream actualInputStream = azureEntity.readFrom(0);
Assert.assertSame(INPUT_STREAM, actualInputStream);
assertSame(INPUT_STREAM, actualInputStream);
}

@Test
Expand All @@ -135,10 +133,10 @@ public void test_readFrom_returnsExpectedStream() throws Exception
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
final AzureEntity azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);

InputStream actualInputStream = azureEntity.readFrom(OFFSET);
Assert.assertSame(INPUT_STREAM, actualInputStream);
assertSame(INPUT_STREAM, actualInputStream);
}

@Test
Expand All @@ -151,7 +149,7 @@ public void test_readFrom_throwsIOException_propogatesError()
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
final AzureEntity azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
azureEntity.readFrom(OFFSET);
}
catch (IOException e) {
Expand All @@ -167,10 +165,10 @@ public void test_getPath_returnsLocationPath()
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
final AzureEntity azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
String actualPath = azureEntity.getPath();

Assert.assertEquals(BLOB_NAME, actualPath);
assertEquals(BLOB_NAME, actualPath);
verifyAll();
}

Expand All @@ -180,20 +178,18 @@ public void test_getPath_azureStorageScheme()
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(
final AzureEntity azureEntity = new AzureEntity(
new CloudObjectLocation(STORAGE_ACCOUNT_NAME, CONTAINER_NAME + "/" + BLOB_NAME),
azureStorage,
AzureStorageAccountInputSource.SCHEME,
byteSourceFactory
);

Assert.assertEquals(
CONTAINER_NAME + "/" + BLOB_NAME,
azureEntity.getPath()
);
assertEquals(CONTAINER_NAME + "/" + BLOB_NAME, azureEntity.getPath());

verifyAll();
}

@Test
public void test_getRetryCondition_returnsExpectedRetryCondition()
{
Expand All @@ -202,8 +198,8 @@ public void test_getRetryCondition_returnsExpectedRetryCondition()
EasyMock.expect(byteSourceFactory.create(CONTAINER_NAME, BLOB_NAME, azureStorage)).andReturn(byteSource);
replayAll();

azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
final AzureEntity azureEntity = new AzureEntity(location, azureStorage, AzureInputSource.SCHEME, byteSourceFactory);
Predicate<Throwable> actualRetryCondition = azureEntity.getRetryCondition();
Assert.assertSame(AzureUtils.AZURE_RETRY, actualRetryCondition);
assertSame(AzureUtils.AZURE_RETRY, actualRetryCondition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,44 @@
import org.apache.druid.storage.azure.AzureStorage;
import org.apache.druid.storage.azure.AzureStorageDruidModule;
import org.easymock.EasyMockSupport;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class AzureInputSourceSerdeTest extends EasyMockSupport
{
private static final String JSON_WITH_URIS = "{\n"
+ " \"type\": \"azure\",\n"
+ " \"uris\": [\"azure://datacontainer2/wikipedia.json\"]\n"
+ "}";

private static final String JSON_WITH_PREFIXES = "{\n"
+ " \"type\": \"azure\",\n"
+ " \"prefixes\": [\"azure://datacontainer2\"]\n"
+ "}";

private static final String JSON_WITH_OBJECTS = "{\n"
+ " \"type\": \"azure\",\n"
+ " \"objects\": [\n"
+ " { \"bucket\": \"container1\", \"path\": \"bar/file1.json\"},\n"
+ " { \"bucket\": \"conatiner2\", \"path\": \"foo/file2.json\"}\n"
+ " ]\n"
+ " }";
private static final String JSON_WITH_URIS =
"{\n"
+ " \"type\": \"azure\",\n"
+ " \"uris\": [\"azure://datacontainer2/wikipedia.json\"]\n"
+ "}";

private static final String JSON_WITH_PREFIXES =
"{\n"
+ " \"type\": \"azure\",\n"
+ " \"prefixes\": [\"azure://datacontainer2\"]\n"
+ "}";

private static final String JSON_WITH_OBJECTS =
"{\n"
+ " \"type\": \"azure\",\n"
+ " \"objects\": [\n"
+ " { \"bucket\": \"container1\", \"path\": \"bar/file1.json\"},\n"
+ " { \"bucket\": \"conatiner2\", \"path\": \"foo/file2.json\"}\n"
+ " ]\n"
+ "}";

private static final String JSON_WITH_URIS_AND_SYSFIELDS =
"{\n"
+ " \"type\": \"azure\",\n"
+ " \"uris\": [\"azure://datacontainer2/wikipedia.json\"],\n"
+ " \"systemFields\": [\"__file_uri\"]\n"
+ " \"type\": \"azure\",\n"
+ " \"uris\": [\"azure://datacontainer2/wikipedia.json\"],\n"
+ " \"systemFields\": [\"__file_uri\"]\n"
+ "}";

private static final List<URI> EXPECTED_URIS;
Expand All @@ -77,7 +82,6 @@ public class AzureInputSourceSerdeTest extends EasyMockSupport
private AzureInputDataConfig inputDataConfig;
private AzureAccountConfig accountConfig;


static {
try {
EXPECTED_URIS = ImmutableList.of(new URI("azure://datacontainer2/wikipedia.json"));
Expand All @@ -92,7 +96,7 @@ public class AzureInputSourceSerdeTest extends EasyMockSupport
}
}

@Before
@BeforeEach
public void setup()
{
azureStorage = createMock(AzureStorage.class);
Expand All @@ -117,7 +121,6 @@ public void test_uriSerde_constructsProperAzureInputSource() throws Exception
objectMapper.writeValueAsBytes(inputSource),
AzureInputSource.class);
verifyInputSourceWithUris(roundTripInputSource);

}

@Test
Expand All @@ -129,12 +132,12 @@ public void test_uriAndSystemFieldsSerde_constructsProperAzureInputSource() thro
objectMapper.setInjectableValues(injectableValues);

final AzureInputSource inputSource = objectMapper.readValue(JSON_WITH_URIS_AND_SYSFIELDS, AzureInputSource.class);
Assert.assertEquals(Collections.singleton(SystemField.URI), inputSource.getConfiguredSystemFields());
assertEquals(Collections.singleton(SystemField.URI), inputSource.getConfiguredSystemFields());

final AzureInputSource roundTripInputSource = objectMapper.readValue(
objectMapper.writeValueAsBytes(inputSource),
AzureInputSource.class);
Assert.assertEquals(Collections.singleton(SystemField.URI), roundTripInputSource.getConfiguredSystemFields());
assertEquals(Collections.singleton(SystemField.URI), roundTripInputSource.getConfiguredSystemFields());
}

@Test
Expand All @@ -153,7 +156,6 @@ public void test_prefixSerde_constructsProperAzureInputSource() throws Exception
objectMapper.writeValueAsBytes(inputSource),
AzureInputSource.class);
verifyInputSourceWithPrefixes(roundTripInputSource);

}

@Test
Expand Down Expand Up @@ -186,24 +188,22 @@ private InjectableValues.Std initInjectableValues()

private static void verifyInputSourceWithUris(final AzureInputSource inputSource)
{

Assert.assertEquals(EXPECTED_URIS, inputSource.getUris());
Assert.assertNull(inputSource.getPrefixes());
Assert.assertNull(inputSource.getObjects());
assertEquals(EXPECTED_URIS, inputSource.getUris());
assertNull(inputSource.getPrefixes());
assertNull(inputSource.getObjects());
}

private static void verifyInputSourceWithPrefixes(final AzureInputSource inputSource)
{

Assert.assertNull(inputSource.getUris());
Assert.assertEquals(EXPECTED_PREFIXES, inputSource.getPrefixes());
Assert.assertNull(inputSource.getObjects());
assertNull(inputSource.getUris());
assertEquals(EXPECTED_PREFIXES, inputSource.getPrefixes());
assertNull(inputSource.getObjects());
}

private static void verifyInputSourceWithObjects(final AzureInputSource inputSource)
{
Assert.assertNull(inputSource.getUris());
Assert.assertNull(inputSource.getPrefixes());
Assert.assertEquals(EXPECTED_CLOUD_OBJECTS, inputSource.getObjects());
assertNull(inputSource.getUris());
assertNull(inputSource.getPrefixes());
assertEquals(EXPECTED_CLOUD_OBJECTS, inputSource.getObjects());
}
}
Loading

0 comments on commit 6c7443c

Please sign in to comment.