From 0c213073ddb1af5e5c95c66cb65e8ad6c852a159 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Fri, 17 Nov 2023 16:02:09 -0500 Subject: [PATCH] remove whitespaces during parsing --- .../cryostat/agent/triggers/SmartTrigger.java | 6 +++--- .../cryostat/agent/triggers/TriggerParser.java | 2 +- .../agent/triggers/TriggerParserTest.java | 18 ++++++++---------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/cryostat/agent/triggers/SmartTrigger.java b/src/main/java/io/cryostat/agent/triggers/SmartTrigger.java index b8c544a3..54f21623 100644 --- a/src/main/java/io/cryostat/agent/triggers/SmartTrigger.java +++ b/src/main/java/io/cryostat/agent/triggers/SmartTrigger.java @@ -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 = ""; diff --git a/src/main/java/io/cryostat/agent/triggers/TriggerParser.java b/src/main/java/io/cryostat/agent/triggers/TriggerParser.java index 4a0379a8..3024c5c3 100644 --- a/src/main/java/io/cryostat/agent/triggers/TriggerParser.java +++ b/src/main/java/io/cryostat/agent/triggers/TriggerParser.java @@ -53,7 +53,7 @@ public List 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); diff --git a/src/test/java/io/cryostat/agent/triggers/TriggerParserTest.java b/src/test/java/io/cryostat/agent/triggers/TriggerParserTest.java index 04c6e736..fa977d76 100644 --- a/src/test/java/io/cryostat/agent/triggers/TriggerParserTest.java +++ b/src/test/java/io/cryostat/agent/triggers/TriggerParserTest.java @@ -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))); @@ -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(), @@ -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))); @@ -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(),