-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Improve properties merging testcases #43734
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
Merged
moarychan
merged 16 commits into
Azure:main
from
moarychan:feature/improve-properties-merge-testcases
Jan 14, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6d5b412
Improve properties merging testcases
moarychan a906735
Fixes code smell
moarychan c179e2e
Resolve comments
moarychan 58c6c59
Update log
moarychan 35dab6e
Format code
moarychan d98d781
refactor
saragluna fc58f4c
Impl comparator
moarychan 34236ee
Move package
moarychan 65472e4
Sort
moarychan 952bd7c
Fixes code smells
moarychan 9816725
Fixes code smells
moarychan aeefb10
Update
moarychan 4eff17b
Add suppression for the test classes
moarychan ea32d56
Update
moarychan d06671a
Simplify assertions
moarychan ed392f0
Improve
moarychan 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
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
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
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
82 changes: 82 additions & 0 deletions
82
...servicebus/implementation/properties/merger/PropertiesMergerUsingInjectedValuesTests.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,82 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.spring.messaging.servicebus.implementation.properties.merger; | ||
|
|
||
| import com.azure.spring.messaging.servicebus.core.properties.NamespaceProperties; | ||
| import com.azure.spring.messaging.servicebus.core.properties.ProcessorProperties; | ||
| import com.azure.spring.messaging.servicebus.core.properties.ProducerProperties; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.azure.spring.messaging.servicebus.implementation.properties.merger.util.TestPropertiesComparer.isMergedPropertiesCorrect; | ||
| import static com.azure.spring.messaging.servicebus.implementation.properties.merger.util.TestPropertiesValueInjectHelper.injectPseudoPropertyValues; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class PropertiesMergerUsingInjectedValuesTests { | ||
|
|
||
| @Test | ||
| void allParentPropertiesWillBeMergedBySenderMerger() { | ||
| // Arrange | ||
| ProducerProperties child = new ProducerProperties(); | ||
|
|
||
| NamespaceProperties parent = new NamespaceProperties(); | ||
| injectPseudoPropertyValues(parent, List.of("cloudType"), "FullyQualifiedNamespace"); | ||
|
|
||
| // Action | ||
| SenderPropertiesParentMerger merger = new SenderPropertiesParentMerger(); | ||
| ProducerProperties result = merger.merge(child, parent); | ||
|
|
||
| // Assertion | ||
| assertTrue(isMergedPropertiesCorrect(parent, child, result)); | ||
| } | ||
|
|
||
| @Test | ||
| void allChildPropertiesWillBeMergedBySenderMerger() { | ||
| // Arrange | ||
| ProducerProperties child = new ProducerProperties(); | ||
| injectPseudoPropertyValues(child, List.of("cloudType"), "FullyQualifiedNamespace"); | ||
|
|
||
| NamespaceProperties parent = new NamespaceProperties(); | ||
|
|
||
| // Action | ||
| SenderPropertiesParentMerger merger = new SenderPropertiesParentMerger(); | ||
| ProducerProperties result = merger.merge(child, parent); | ||
|
|
||
| // Assertion | ||
| assertTrue(isMergedPropertiesCorrect(child, child, result)); | ||
| } | ||
|
|
||
| @Test | ||
| void allParentPropertiesWillBeMergedByProcessorMerger() { | ||
| // Arrange | ||
| ProcessorProperties child = new ProcessorProperties(); | ||
|
|
||
| NamespaceProperties parent = new NamespaceProperties(); | ||
| injectPseudoPropertyValues(parent, List.of("cloudType"), "FullyQualifiedNamespace"); | ||
|
|
||
| // Action | ||
| ProcessorPropertiesParentMerger merger = new ProcessorPropertiesParentMerger(); | ||
| ProcessorProperties result = merger.merge(child, parent); | ||
|
|
||
| // Assertion | ||
| assertTrue(isMergedPropertiesCorrect(parent, child, result)); | ||
| } | ||
|
|
||
| @Test | ||
| void allChildPropertiesWillBeMergedByProcessorMerger() { | ||
| // Arrange | ||
| ProcessorProperties child = new ProcessorProperties(); | ||
| injectPseudoPropertyValues(child, List.of("cloudType"), "FullyQualifiedNamespace"); | ||
|
|
||
| NamespaceProperties parent = new NamespaceProperties(); | ||
|
|
||
| // Action | ||
| ProcessorPropertiesParentMerger merger = new ProcessorPropertiesParentMerger(); | ||
| ProcessorProperties result = merger.merge(child, parent); | ||
|
|
||
| // Assertion | ||
| assertTrue(isMergedPropertiesCorrect(child, child, result)); | ||
| } | ||
| } |
167 changes: 167 additions & 0 deletions
167
...ng/messaging/servicebus/implementation/properties/merger/util/TestPropertiesComparer.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,167 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.spring.messaging.servicebus.implementation.properties.merger.util; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static com.azure.spring.cloud.core.implementation.util.ClassUtils.isPrimitiveDefaultValue; | ||
| import static com.azure.spring.messaging.servicebus.implementation.properties.merger.util.TestPropertiesUtils.BUILT_IN_MEMBER_VARIABLE_NAMES; | ||
| import static com.azure.spring.messaging.servicebus.implementation.properties.merger.util.TestPropertiesUtils.IGNORED_CLASSES; | ||
|
|
||
| public class TestPropertiesComparer { | ||
|
|
||
| public static <T, S> boolean isMergedPropertiesCorrect(T parent, | ||
| S child, | ||
| S result, | ||
| String... ignoredMemberVariableNames) { | ||
| Set<String> ignored = Arrays.stream(ignoredMemberVariableNames) | ||
| .map(String::toLowerCase) | ||
| .collect(Collectors.toSet()); | ||
| AtomicInteger mismatchedCounter = new AtomicInteger(); | ||
| return isMergedPropertiesCorrect(parent, child, result, ignored, mismatchedCounter, result.getClass()); | ||
| } | ||
|
|
||
| private static <T, S> boolean isMergedPropertiesCorrect(T parent, | ||
| S child, | ||
| S result, | ||
| Set<String> ignored, | ||
| AtomicInteger counter, | ||
| Class<?> targetClass) { | ||
| if (IGNORED_CLASSES.contains(targetClass)) { | ||
| return true; | ||
| } | ||
|
|
||
| Arrays.stream(targetClass.getDeclaredMethods()) | ||
| .filter(TestPropertiesUtils::isGetter) | ||
| .forEach(getter -> checkGetter(parent, child, result, ignored, counter, getter)); | ||
| Class<?> parentClass = targetClass.getSuperclass(); | ||
| if (parentClass != null) { | ||
| isMergedPropertiesCorrect(parent, child, result, ignored, counter, parentClass); | ||
| } | ||
| return counter.get() == 0; | ||
| } | ||
|
|
||
| private static <T, S> void checkGetter(T parent, | ||
| S child, | ||
| S result, | ||
| Set<String> ignored, | ||
| AtomicInteger counter, | ||
| Method getter) { | ||
| String varName = getVariableNameByGetter(getter); | ||
|
|
||
| String varNameLowerCase = varName.toLowerCase(); | ||
| if (ignored.contains(varNameLowerCase)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| Object gotValue = getter.invoke(result); | ||
| if (gotValue == null) { | ||
| return; | ||
| } | ||
|
|
||
| Class<?> returnType = getter.getReturnType(); | ||
| if (isPrimitiveDefaultValue(returnType, gotValue)) { | ||
| System.out.println("Found the property that has a primitive default value: " | ||
| + varName + "=" + gotValue); | ||
| counter.getAndIncrement(); | ||
| return; | ||
| } | ||
|
|
||
| if (BUILT_IN_MEMBER_VARIABLE_NAMES.contains(varNameLowerCase)) { | ||
| Object builtInParent = findBuiltInNestedMemberVariable(varNameLowerCase, parent, parent.getClass()); | ||
| Object builtInChild = findBuiltInNestedMemberVariable(varNameLowerCase, child, child.getClass()); | ||
| isMergedPropertiesCorrect(builtInParent, builtInChild, gotValue, ignored, counter, gotValue.getClass()); | ||
| return; | ||
| } | ||
|
|
||
| Boolean matched = isMatchedInOriginProperties(gotValue, getter.getName(), returnType, child, child.getClass()); | ||
| if (matched == null) { | ||
| matched = isMatchedInOriginProperties(gotValue, getter.getName(), returnType, parent, parent.getClass()); | ||
| if (matched == null) { | ||
| System.out.println("Found the property that doesn't exist in child and parent " | ||
| + "properties: " + varName + "=" + gotValue); | ||
| counter.getAndIncrement(); | ||
| return; | ||
| } | ||
| } | ||
| if (!matched) { | ||
| System.out.println("Found the property that mismatch in child and parent" | ||
| + "properties: " + varName + "=" + gotValue); | ||
| counter.getAndIncrement(); | ||
| } | ||
| } catch (IllegalAccessException | InvocationTargetException e) { | ||
| throw new RuntimeException("Check getter failed: " + getter.getName(), e); | ||
| } | ||
| } | ||
|
|
||
| private static <T> Object findBuiltInNestedMemberVariable(String memberVariableName, T target, | ||
| Class<?> targetClass) { | ||
| try { | ||
| Field builtInMemberVariable = targetClass.getDeclaredField(memberVariableName); | ||
| builtInMemberVariable.setAccessible(true); | ||
| return builtInMemberVariable.get(target); | ||
| } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| Class<?> parentClass = targetClass.getSuperclass(); | ||
| if (parentClass != null) { | ||
| return findBuiltInNestedMemberVariable(memberVariableName, target, parentClass); | ||
| } | ||
| } | ||
|
|
||
| throw new RuntimeException("Not found the built-in member variable: " + memberVariableName); | ||
| } | ||
|
|
||
| private static <S> Boolean isMatchedInOriginProperties(Object usedValue, | ||
| String getterMethodName, | ||
| Class<?> returnType, | ||
| S origin, | ||
| Class<?> targetClass) { | ||
| try { | ||
| Method getter = targetClass.getDeclaredMethod(getterMethodName); | ||
| Object gotValue = getter.invoke(origin); | ||
| if (gotValue == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (isPrimitiveDefaultValue(returnType, gotValue)) { | ||
| return null; | ||
| } | ||
|
|
||
| boolean matched = false; | ||
| switch (returnType.getSimpleName()) { | ||
| case "boolean", "Boolean", "Duration", "int", | ||
| "Integer", "long", "Long", "String" -> | ||
| matched = usedValue.equals(gotValue); | ||
| case "AmqpTransportType", "CloudType", "ServiceBusEntityType", | ||
| "ServiceBusReceiveMode", "SubQueue", "RetryMode" -> | ||
| matched = usedValue == gotValue; | ||
| default -> System.out.println("Not support the getter parameter type: " + returnType.getName()); | ||
| } | ||
| return matched; | ||
| } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { | ||
| Class<?> parentClass = targetClass.getSuperclass(); | ||
| if (parentClass != null) { | ||
| return isMatchedInOriginProperties(usedValue, getterMethodName, returnType, origin, parentClass); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static String getVariableNameByGetter(Method getter) { | ||
| String varName; | ||
| String getterName = getter.getName(); | ||
| if (getterName.contains("get")) { | ||
| varName = getterName.substring("get".length()); | ||
| } else { | ||
| varName = getterName.substring("is".length()); | ||
| } | ||
| return varName; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.