btlqql opened a new pull request, #4711:
URL: https://github.com/apache/rocketmq-dashboard/pull/4711

   <!-- Base branch: `rocketmq-studio`, the RocketMQ Studio trunk. -->
   
   ### Related issue
   
   - No open issue covers this endpoint; the closest is #4557 
(test-notification failures surface as a generic 500), whose fix #4561 covers 
the notification-delivery test path in `NotificationOutboxService`.
   - This PR covers `NativeAlertRuleTestService`, which serves the alert-rule 
test button of both the cluster and the business rule pages.
   
   ### Brief Description
   
   `NativeAlertRuleTestService.test` filters the collected samples with 
`rule.getMetric().equals(sample.metricKey())` (line 72), but a rule without a 
metric reaches that line: `NativeAlertMetricCatalogService.validate` returns 
early for a null metric, and `NativeAlertRulePolicy.validate` treats a blank 
metric as "not a native metric" and returns without an error. `POST 
/api/alert-rules/test` (and its cluster twin) with a body such as 
`{"name":"rule","instanceId":"local","operator":">","threshold":10}` therefore 
answered with an unhandled `NullPointerException` - an HTTP 500 - instead of 
telling the caller which field is missing.
   
   The test run now rejects a rule without a metric up front with `400 metric 
is required`, before any samples are collected for it. That is the same 
request-error channel the surrounding validation uses (`AlertRulePolicy` for 
the scope fields, `AlertMetricCatalogService` for the metric support check), so 
no HTTP contract is changed for valid requests, and the two controllers keep 
passing a non-null rule (`requireRule` / `requireAlertRule`).
   
   ### How Did You Test This Change?
   
   Test written first and run against the unmodified tree (`red`, base commit 
`7ce9a682`):
   
   ```
   $ cd server && mvn -B -ntp test -Dtest=NativeAlertRuleTestServiceTest
   [ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
   [ERROR]   
NativeAlertRuleTestServiceTest.rejectsARuleWithoutMetricInsteadOfFailingWithANullPointerExceptionTest:141
   Expecting actual throwable to be an instance of:
     org.apache.rocketmq.studio.common.exception.BusinessException
   but was:
     java.lang.NullPointerException: Cannot invoke "String.equals(Object)" 
because the return value of
     "org.apache.rocketmq.studio.ops.alert.AlertRuleVO.getMetric()" is null
       at 
org.apache.rocketmq.studio.ops.alert.NativeAlertRuleTestService.lambda$test$1(NativeAlertRuleTestService.java:72)
   [INFO] BUILD FAILURE
   ```
   
   After the fix (`green`), together with the policy, catalog, scope-matcher 
and both rule controllers:
   
   ```
   $ cd server && mvn -B -ntp test 
-Dtest=NativeAlertRuleTestServiceTest,NativeAlertRulePolicyTest,NativeAlertMetricCatalogServiceTest,NativeAlertRuleScopeMatcherTest,AlertRuleControllerTest,ClusterAlertRuleControllerTest
   [INFO] You have 0 Checkstyle violations.
   [INFO] Tests run: 50, Failures: 0, Errors: 0, Skipped: 0
   [INFO] BUILD SUCCESS
   ```
   
   Test coverage:
   
   - `rejectsARuleWithoutMetricInsteadOfFailingWithANullPointerExceptionTest` - 
a rule with an instance, an operator and a threshold but no metric, tested 
against a collector that returns one matching sample (so the failing filter is 
actually evaluated), must raise `BusinessException` with code 400 and the 
message `metric is required`.
   - The other four cases of the class stay green, including the one that 
asserts a healthy collector still answers when another collector throws.
   
   ### Checklist
   
   - [x] One coherent change; unrelated modifications are not bundled in
   - [x] Commit subject follows Conventional Commits (`fix:`)
   - [x] Tests added or updated for non-trivial changes, test methods named 
`...Test`
   - [x] New UI text has both Chinese and English entries under `web/src/i18n/` 
- not applicable, no UI text added
   - [x] Architecture constraints stay green (`mvn test` runs the ArchUnit 
checks)
   - [x] New source files carry the ASF license header - no new source files
   - [x] Documentation touched where behaviour changed - not applicable, only 
the error path of a rejected request changed


-- 
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]

Reply via email to