-
Notifications
You must be signed in to change notification settings - Fork 1k
Increase code coverage on dynamodb-enhanced module #6700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
andreas-grafenberger
wants to merge
21
commits into
aws:master
from
anasatirbasa:437-increase_test_coverage
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8dd1add
Increase code coverage on dynamodb-enhanced module
andreas-grafenberger b278881
Merge branch 'master' into 437-increase_test_coverage
andreas-grafenberger e33be5f
Addressing comments (refactoring, removing duplicated tests).
andreas-grafenberger 1ef7ea3
Addressing comments (adding more tests for chain extension).
andreas-grafenberger 523fb64
Adding release change note.
andreas-grafenberger adadbe9
Merge branch 'master' into 437-increase_test_coverage
andreas-grafenberger 377bde1
Parametrization of annotated bean and immutable table schema tests. A…
andreas-grafenberger d0d61ed
Refactoring test class: AutoGeneratedUuidExtensionTest
andreas-grafenberger 0d124ef
Removing test class: AutoGeneratedUuidExtensionTest... will be added …
andreas-grafenberger 01a65e1
Merge remote-tracking branch 'origin/437-increase_test_coverage' into…
andreas-grafenberger ef3dc6b
Merge branch 'master' into 437-increase_test_coverage
andreas-grafenberger 11d648d
Removing unused utils class for tests.
andreas-grafenberger 55633a3
Merge branch 'master' into 437-increase_test_coverage
andreas-grafenberger d2322c6
Fixing conflict and updated tests.
andreas-grafenberger 53b6218
Merge branch 'master' into 437-increase_test_coverage
andreas-grafenberger 1f8a330
Fixing conflicts on EnhancedClientUtilsTest
andreas-grafenberger e547100
Removing duplicate test pairs.
andreas-grafenberger 87a6083
Replacing hamcrest imports with assertj.
andreas-grafenberger 84bdb63
Merge branch 'master' into 437-increase_test_coverage
andreas-grafenberger 386e270
Tests refactorization (parameterization).
andreas-grafenberger 7815adf
Merge branch 'master' into 437-increase_test_coverage
zoewangg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changes/next-release/feature-AmazonDynamoDBEnhancedClient-900697b.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Amazon DynamoDB Enhanced Client", | ||
| "contributor": "", | ||
| "description": "Increase code coverage on dynamodb-enhanced module" | ||
| } |
66 changes: 66 additions & 0 deletions
66
.../java/software/amazon/awssdk/enhanced/dynamodb/DefaultAttributeConverterProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.enhanced.dynamodb; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.event.Level; | ||
|
|
||
| public class DefaultAttributeConverterProviderTest { | ||
|
|
||
| @Test | ||
| void findConverter_whenConverterFound_logsConverterFound() { | ||
| try (LogCaptor logCaptor = new LogCaptor(DefaultAttributeConverterProvider.class, Level.DEBUG)) { | ||
| DefaultAttributeConverterProvider provider = DefaultAttributeConverterProvider.create(); | ||
| provider.converterFor(EnhancedType.of(String.class)); | ||
|
|
||
| List<LogEvent> logEvents = logCaptor.loggedEvents(); | ||
| assertThat(logEvents).hasSize(1); | ||
| assertThat(logEvents.get(0).getLevel().name()).isEqualTo(Level.DEBUG.name()); | ||
| assertThat(logEvents.get(0).getMessage().getFormattedMessage()) | ||
| .contains("Converter for EnhancedType(java.lang.String): software.amazon.awssdk.enhanced.dynamodb.internal" | ||
| + ".converter.attribute.StringAttributeConverter"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void findConverter_whenConverterNotFound_logsNoConverter() { | ||
| try (LogCaptor logCaptor = new LogCaptor(DefaultAttributeConverterProvider.class, Level.DEBUG)) { | ||
| DefaultAttributeConverterProvider provider = DefaultAttributeConverterProvider.create(); | ||
|
|
||
| assertThatThrownBy(() -> provider.converterFor(EnhancedType.of(CustomUnsupportedType.class))) | ||
| .isInstanceOf(IllegalStateException.class) | ||
| .hasMessageContaining("Converter not found for EnhancedType(software.amazon.awssdk.enhanced.dynamodb" | ||
| + ".DefaultAttributeConverterProviderTest$CustomUnsupportedType)"); | ||
| List<LogEvent> logEvents = logCaptor.loggedEvents(); | ||
| assertThat(logEvents).hasSize(1); | ||
| assertThat(logEvents.get(0).getLevel().name()).isEqualTo(Level.DEBUG.name()); | ||
| assertThat(logEvents.get(0).getMessage().getFormattedMessage()) | ||
| .contains("No converter available for EnhancedType(software.amazon.awssdk.enhanced.dynamodb" | ||
| + ".DefaultAttributeConverterProviderTest$CustomUnsupportedType)"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A custom type with no converter registered for it. | ||
| */ | ||
| private static class CustomUnsupportedType { | ||
| } | ||
| } |
181 changes: 181 additions & 0 deletions
181
...java/software/amazon/awssdk/enhanced/dynamodb/DefaultMethodsUnsupportedOperationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.enhanced.dynamodb; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.CALLS_REAL_METHODS; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.File; | ||
| import java.lang.reflect.Method; | ||
| import java.net.URL; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Consumer; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.api.DynamicTest; | ||
| import org.junit.jupiter.api.TestFactory; | ||
|
|
||
| /** | ||
| * Test class that discovers all interfaces with default methods that throw UnsupportedOperationException. Shows individual test | ||
| * scenarios and results using DynamicTest. | ||
| */ | ||
| public class DefaultMethodsUnsupportedOperationTest { | ||
|
|
||
| private static final String BASE_PACKAGE = "software.amazon.awssdk.enhanced.dynamodb"; | ||
| private static final Pattern CLASS_PATTERN = Pattern.compile(".class", Pattern.LITERAL); | ||
|
|
||
| private static final List<String> testScenarios = Collections.synchronizedList(new java.util.ArrayList<>()); | ||
|
|
||
| @TestFactory | ||
| Stream<DynamicTest> testDefaultMethodsThrowUnsupportedOperation() { | ||
| List<DynamicTest> dynamicTestList = scanPackageForClasses(BASE_PACKAGE) | ||
| .filter(Class::isInterface) | ||
| .filter(this::hasDefaultMethods) | ||
| .collect(toList()) | ||
| .stream() | ||
| .flatMap(this::createTestsForInterface) | ||
| .collect(toList()); | ||
| assertEquals(102, dynamicTestList.size()); | ||
| return dynamicTestList.stream(); | ||
| } | ||
|
|
||
| private Stream<Class<?>> scanPackageForClasses(String packageName) { | ||
| try { | ||
| ClassLoader loader = Thread.currentThread().getContextClassLoader(); | ||
| return Collections.list(loader.getResources(packageName.replace('.', '/'))) | ||
| .stream() | ||
| .map(URL::getFile) | ||
| .map(File::new) | ||
| .filter(File::exists) | ||
| .flatMap(dir -> findClassesInDirectory(dir, packageName)); | ||
| } catch (Exception e) { | ||
| return Stream.empty(); | ||
| } | ||
| } | ||
|
|
||
| private Stream<Class<?>> findClassesInDirectory(File dir, String packageName) { | ||
| return Optional.ofNullable(dir.listFiles()) | ||
| .map(Arrays::stream) | ||
| .orElseGet(Stream::empty) | ||
| .flatMap(file -> | ||
| file.isDirectory() | ||
| ? findClassesInDirectory(file, packageName + "." + file.getName()) | ||
| : loadClassFromFile(file, packageName)); | ||
| } | ||
|
|
||
| private Stream<Class<?>> loadClassFromFile(File file, String packageName) { | ||
| if (!file.getName().endsWith(".class")) { | ||
| return Stream.empty(); | ||
| } | ||
|
|
||
| String className = packageName + '.' + CLASS_PATTERN.matcher(file.getName()).replaceAll(""); | ||
| try { | ||
| return Stream.of(Class.forName(className)); | ||
| } catch (ClassNotFoundException | NoClassDefFoundError e) { | ||
| return Stream.empty(); | ||
| } | ||
| } | ||
|
|
||
| private boolean hasDefaultMethods(Class<?> interfaceClass) { | ||
| return Arrays.stream(interfaceClass.getDeclaredMethods()) | ||
| .anyMatch(Method::isDefault); | ||
| } | ||
|
|
||
| private Stream<DynamicTest> createTestsForInterface(Class<?> interfaceClass) { | ||
| return Arrays.stream(interfaceClass.getDeclaredMethods()) | ||
| .filter(Method::isDefault) | ||
| .filter(method -> throwsUnsupportedOperation(interfaceClass, method)) | ||
| .map(method -> { | ||
| String testName = String.format("%s.%s() → throws UnsupportedOperationException", | ||
| interfaceClass.getSimpleName(), | ||
| method.getName()); | ||
| testScenarios.add(testName); | ||
|
|
||
| return DynamicTest.dynamicTest(testName, () -> | ||
| testMethodThrowsUnsupportedOperation(interfaceClass, method)); | ||
| }); | ||
| } | ||
|
|
||
| private boolean throwsUnsupportedOperation(Class<?> interfaceClass, Method method) { | ||
| try { | ||
| Object mockInstance = createMockInstance(interfaceClass); | ||
| Object[] args = createArguments(method); | ||
| method.invoke(mockInstance, args); | ||
| return false; | ||
| } catch (Exception e) { | ||
| Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| return cause instanceof UnsupportedOperationException; | ||
| } | ||
| } | ||
|
|
||
| private void testMethodThrowsUnsupportedOperation(Class<?> interfaceClass, Method method) { | ||
| Object mockInstance = createMockInstance(interfaceClass); | ||
| Object[] args = createArguments(method); | ||
|
|
||
| assertThrows(UnsupportedOperationException.class, () -> { | ||
| try { | ||
| method.invoke(mockInstance, args); | ||
| } catch (Exception e) { | ||
| Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| if (cause instanceof UnsupportedOperationException) { | ||
| throw cause; | ||
| } | ||
| throw new RuntimeException(cause); | ||
| } | ||
| }, () -> String.format("Expected %s.%s() to throw UnsupportedOperationException", | ||
| interfaceClass.getSimpleName(), method.getName())); | ||
| } | ||
|
|
||
| private <T> T createMockInstance(Class<T> interfaceClass) { | ||
| T mock = mock(interfaceClass, CALLS_REAL_METHODS); | ||
| if (mock instanceof MappedTableResource) { | ||
| when(((MappedTableResource<?>) mock).tableName()).thenReturn("test-table"); | ||
| } | ||
| return mock; | ||
| } | ||
|
|
||
| private Object[] createArguments(Method method) { | ||
| return Arrays.stream(method.getParameterTypes()).map(this::createArgument).toArray(); | ||
| } | ||
|
|
||
| private Object createArgument(Class<?> paramType) { | ||
| if (paramType == String.class) { | ||
| return "test"; | ||
| } | ||
| if (paramType == Key.class) { | ||
| return Key.builder().partitionValue("test").build(); | ||
| } | ||
| if (Consumer.class.isAssignableFrom(paramType)) { | ||
| return (Consumer<?>) obj -> { | ||
| }; | ||
| } | ||
| if (paramType.isInterface()) { | ||
| return mock(paramType); | ||
| } | ||
| try { | ||
| return mock(paramType); | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems brittle; any future interface change breaks this with a confusing failure. Consider "assertThat(...).isNotEmpty()" instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree - this was the purpose, to gain more control over methods that throw UnsupportedOperation and avoid mistakes. Replacing that assertion with
assertThat(...).isNotEmpty()makes the test lose its value.