Skip to content

Commit

Permalink
remove whitespaces during parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Nov 27, 2023
1 parent da53b4d commit 0c21307
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/cryostat/agent/triggers/SmartTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public SmartTrigger(String expression, String templateName) {
this.state = TriggerState.NEW;
Matcher m = DEFINITION_PATTERN.matcher(expression);
if (m.matches()) {
triggerCondition = m.group(1).strip();
durationConstraint = m.group(2).replaceAll("'", "\"").strip();
triggerCondition = m.group(1).replaceAll("\\s", "");
durationConstraint = m.group(2).replaceAll("'", "\"").replaceAll("\\s", "");
/* Duration.parse requires timestamps in ISO8601 Duration format */
targetDuration = Duration.parse("PT" + m.group(3).strip());
targetDuration = Duration.parse("PT" + m.group(3).replaceAll("\\s", ""));
} else {
triggerCondition = expression;
durationConstraint = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<SmartTrigger> parse(String[] args) {

String[] expressions = triggerDefinitions.split(",");
for (String s : expressions) {
s = s.strip();
s = s.replaceAll("\\s", "");
Matcher m = EXPRESSION_PATTERN.matcher(s);
if (m.matches()) {
String constraintString = m.group(1);
Expand Down
18 changes: 8 additions & 10 deletions src/test/java/io/cryostat/agent/triggers/TriggerParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ void testSingleComplexTriggerWithWhitespace() {

MatcherAssert.assertThat(
trigger.getExpression(),
Matchers.equalTo("ProcessCpuLoad > 0.2 ; TargetDuration > duration(\"30s\")"));
Matchers.equalTo("ProcessCpuLoad>0.2;TargetDuration>duration(\"30s\")"));
MatcherAssert.assertThat(trigger.getRecordingTemplateName(), Matchers.equalTo("profile"));
MatcherAssert.assertThat(
trigger.getDurationConstraint(),
Matchers.equalTo("TargetDuration > duration(\"30s\")"));
Matchers.equalTo("TargetDuration>duration(\"30s\")"));
MatcherAssert.assertThat(
trigger.getTriggerCondition(), Matchers.equalTo("ProcessCpuLoad > 0.2"));
trigger.getTriggerCondition(), Matchers.equalTo("ProcessCpuLoad>0.2"));
MatcherAssert.assertThat(trigger.getState(), Matchers.equalTo(TriggerState.NEW));
MatcherAssert.assertThat(
trigger.getTargetDuration(), Matchers.equalTo(Duration.ofSeconds(30)));
Expand All @@ -143,7 +143,7 @@ void testMultipleComplexTriggerWithWhitespace() {
SmartTrigger trigger1 = out.get(0);
MatcherAssert.assertThat(
trigger1.getExpression(),
Matchers.equalTo("ProcessCpuLoad>0.2 ; TargetDuration>duration(\"30s\")"));
Matchers.equalTo("ProcessCpuLoad>0.2;TargetDuration>duration(\"30s\")"));
MatcherAssert.assertThat(trigger1.getRecordingTemplateName(), Matchers.equalTo("profile"));
MatcherAssert.assertThat(
trigger1.getDurationConstraint(),
Expand All @@ -159,17 +159,15 @@ void testMultipleComplexTriggerWithWhitespace() {
MatcherAssert.assertThat(
trigger2.getExpression(),
Matchers.equalTo(
"(HeapMemoryUsagePercent > 50 && NonHeapMemoryUsage > 1) || SystemCpuLoad >"
+ " 4 ; TargetDuration > duration(\"2m\")"));
"(HeapMemoryUsagePercent>50&&NonHeapMemoryUsage>1)||SystemCpuLoad>4;TargetDuration>duration(\"2m\")"));
MatcherAssert.assertThat(trigger2.getRecordingTemplateName(), Matchers.equalTo("default"));
MatcherAssert.assertThat(
trigger2.getDurationConstraint(),
Matchers.equalTo("TargetDuration > duration(\"2m\")"));
Matchers.equalTo("TargetDuration>duration(\"2m\")"));
MatcherAssert.assertThat(
trigger2.getTriggerCondition(),
Matchers.equalTo(
"(HeapMemoryUsagePercent > 50 && NonHeapMemoryUsage > 1) || SystemCpuLoad >"
+ " 4"));
"(HeapMemoryUsagePercent>50&&NonHeapMemoryUsage>1)||SystemCpuLoad>4"));
MatcherAssert.assertThat(trigger2.getState(), Matchers.equalTo(TriggerState.NEW));
MatcherAssert.assertThat(
trigger2.getTargetDuration(), Matchers.equalTo(Duration.ofMinutes(2)));
Expand All @@ -194,7 +192,7 @@ void testMultipleComplexTriggerWithWhitespaceWhenOnlyOneTemplateValid() {
SmartTrigger trigger1 = out.get(0);
MatcherAssert.assertThat(
trigger1.getExpression(),
Matchers.equalTo("ProcessCpuLoad>0.2 ; TargetDuration>duration(\"30s\")"));
Matchers.equalTo("ProcessCpuLoad>0.2;TargetDuration>duration(\"30s\")"));
MatcherAssert.assertThat(trigger1.getRecordingTemplateName(), Matchers.equalTo("profile"));
MatcherAssert.assertThat(
trigger1.getDurationConstraint(),
Expand Down

0 comments on commit 0c21307

Please sign in to comment.