gudelian941 commented on code in PR #3377:
URL:
https://github.com/apache/servicecomb-java-chassis/pull/3377#discussion_r991748666
##########
governance/src/main/java/org/apache/servicecomb/governance/marker/RequestProcessor.java:
##########
@@ -103,4 +115,68 @@ private boolean operatorMatch(String str, RawOperator
rawOperator) {
}
return true;
}
+
+ private boolean customMatch(GovernanceRequest request, Matcher matcher) {
+ if (matcher.getCustomMatcher() == null) {
+ return true;
+ }
+ String customMatcherHandlerName =
matcher.getCustomMatcher().getCustomMatcherHandler();
+ String customMatcherParameters =
matcher.getCustomMatcher().getCustomMatcherParameters();
+
+ if (StringUtils.isEmpty(customMatcherHandlerName) ||
StringUtils.isEmpty(customMatcherParameters)) {
+ return true;
+ }
+
+ CustomMatch customMatcherHandler =
generateHandler(customMatcherHandlerName);
+ return customMatcherHandler.matchRequest(request, customMatcherParameters);
+ }
+
+ private CustomMatch getBeanByHandlerName(String customMatcherHandler) {
+ Object extractObject = null;
+ if (applicationContext.containsBean(customMatcherHandler)) {
+ extractObject = applicationContext.getBean(customMatcherHandler);
+ if (!(extractObject instanceof CustomMatch)) {
+ LOGGER.error("{} {}", customMatcherHandler,
CustomMatch.errorMessageForNotImplements);
+ throw new RuntimeException(customMatcherHandler +
CustomMatch.errorMessageForNotImplements);
+ }
+ return (CustomMatch)extractObject;
+ }
+ return null;
+ }
+
+
+ public CustomMatch generateHandler(String customMatcherHandler) {
+ CustomMatch extractObject = getBeanByHandlerName(customMatcherHandler);
+ if (extractObject != null) {
+ return extractObject;
+ }
+
Review Comment:
done
##########
governance/src/test/java/org/apache/servicecomb/governance/CustomMatchTest.java:
##########
@@ -0,0 +1,82 @@
+package org.apache.servicecomb.governance;
Review Comment:
done
##########
governance/src/test/java/org/apache/servicecomb/governance/CustomMatchTest.java:
##########
@@ -0,0 +1,82 @@
+package org.apache.servicecomb.governance;
+
+import org.apache.servicecomb.governance.marker.CustomMatcher;
+import org.apache.servicecomb.governance.marker.GovernanceRequest;
+import org.apache.servicecomb.governance.marker.Matcher;
+import org.apache.servicecomb.governance.marker.RequestProcessor;
+import
org.apache.servicecomb.governance.mockclasses.service.MockConfigurationForCustomMatcher;
+import org.apache.servicecomb.governance.service.MatchersService;
+import org.apache.servicecomb.governance.utils.CustomMatch;
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ContextConfiguration;
+
+@SpringBootTest
+@ContextConfiguration(classes = {GovernanceConfiguration.class,
MockConfiguration.class, MockConfigurationForCustomMatcher.class})
+public class CustomMatchTest {
+
+ private RequestProcessor requestProcessor;
+
+ @Autowired
+ public void setRequestProcessor(RequestProcessor requestProcessor) {
+ this.requestProcessor = requestProcessor;
+ }
+
+ private Matcher generateMatcher(String customMatcherHandler, String
customMatcherParameters) {
+ CustomMatcher customMatcher = new CustomMatcher();
+ customMatcher.setCustomMatcherHandler(customMatcherHandler);
+ customMatcher.setCustomMatcherParameters(customMatcherParameters);
+ Matcher matcher = new Matcher();
+ matcher.setCustomMatcher(customMatcher);
+ return matcher;
+ }
+
+ @Test
+ public void test_should_pass_when_value_class_empty() {
+ GovernanceRequest request = new GovernanceRequest();
+ Matcher mockMatcher = generateMatcher("", "");
+ Assert.assertTrue(this.requestProcessor.match(request,mockMatcher));
+ mockMatcher = generateMatcher("", "bill");
+ Assert.assertTrue(this.requestProcessor.match(request,mockMatcher));
+ mockMatcher = generateMatcher("classWithAnnotation", "");
+ Assert.assertTrue(this.requestProcessor.match(request,mockMatcher));
+ }
+
+ @Test
+ public void test_should_pass_when_value_class_match() {
+ GovernanceRequest request = new GovernanceRequest();
+ Matcher mockMatcher = generateMatcher("classWithAnnotation", "bill");
+ Assert.assertTrue(this.requestProcessor.match(request,mockMatcher));
+ }
+
+ @Test
+ public void test_should_throw_exception_when_class_not_found() {
+ GovernanceRequest request = new GovernanceRequest();
+ try {
+ Matcher mockMatcher =
generateMatcher("classWithAnnotationNotFound", "bill");
+ this.requestProcessor.match(request,mockMatcher);
+ } catch (Exception e) {
+ Assert.assertTrue(e.getCause() instanceof ClassNotFoundException);
+ }
+ }
+
+ @Test
+ public void test_should_pass_when_multiple_value() {
+ GovernanceRequest request = new GovernanceRequest();
+ Matcher mockMatcher = generateMatcher("classWithAnnotation",
"bill,bill2");
+ Assert.assertTrue(this.requestProcessor.match(request,mockMatcher));
+ }
+
+ @Test
+ public void test_should_throw_exception_when_not_implements_interface() {
+ GovernanceRequest request = new GovernanceRequest();
+ try {
+ Matcher mockMatcher = generateMatcher("classNotImplments",
"bill,bill2");
+ this.requestProcessor.match(request,mockMatcher);
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]