Merge "Allow spaces and apostrophes in dimensions"
This commit is contained in:
commit
cb06432fef
@ -82,8 +82,26 @@ class AlarmSubExpressionListener extends AlarmExpressionBaseListener {
|
||||
|
||||
@Override
|
||||
public void enterDimension(AlarmExpressionParser.DimensionContext ctx) {
|
||||
String dimensionName = ctx.getChild(0).getText();
|
||||
if (dimensions.put(dimensionName, ctx.getChild(2).getText()) != null)
|
||||
StringBuilder dimensionName = new StringBuilder();
|
||||
dimensionName.append(ctx.getChild(0).getText());
|
||||
int i = 1;
|
||||
while (!ctx.getChild(i).getText().equals("=")) {
|
||||
dimensionName.append(' ');
|
||||
dimensionName.append(ctx.getChild(i).getText());
|
||||
i++;
|
||||
}
|
||||
// move past the '=' token
|
||||
i++;
|
||||
|
||||
StringBuilder dimensionValue = new StringBuilder();
|
||||
dimensionValue.append(ctx.getChild(i).getText());
|
||||
i++;
|
||||
while (i < ctx.getChildCount()) {
|
||||
dimensionValue.append(' ');
|
||||
dimensionValue.append(ctx.getChild(i).getText());
|
||||
i++;
|
||||
}
|
||||
if (dimensions.put(dimensionName.toString(), dimensionValue.toString()) != null)
|
||||
throw new IllegalArgumentException("More than one value was given for dimension "
|
||||
+ dimensionName);
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ dimensionList
|
||||
;
|
||||
|
||||
dimension
|
||||
: txt '=' txt
|
||||
: (txt)+ '=' (txt)+
|
||||
;
|
||||
|
||||
keyword
|
||||
@ -210,7 +210,7 @@ DECIMAL
|
||||
;
|
||||
|
||||
TXT
|
||||
: ~('\'' | '}' | '{' | '&' | '|' | '>' | '<' | '=' | ',' | ')' | '(' | ' ' | '"' )+
|
||||
: ~('}' | '{' | '&' | '|' | '>' | '<' | '=' | ',' | ')' | '(' | ' ' | '"' )+
|
||||
;
|
||||
|
||||
STRING
|
||||
|
@ -24,11 +24,12 @@ import java.util.List;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import monasca.common.model.metric.MetricDefinition;
|
||||
|
||||
@Test
|
||||
public class AlarmExpressionTest {
|
||||
private final String restrictedChars = "(){}&|<>='\",";
|
||||
private final String restrictedChars = "(){}&|<>=\",";
|
||||
|
||||
public void shouldParseExpression() {
|
||||
AlarmExpression expr = new AlarmExpression(
|
||||
@ -251,6 +252,26 @@ public class AlarmExpressionTest {
|
||||
assertEquals(alarm1.getMetricDefinition(), expected1);
|
||||
}
|
||||
|
||||
public void shouldParseDimensionsWithSpaces() {
|
||||
AlarmExpression[] expr_list = {
|
||||
new AlarmExpression("test_metric{this_is_a_test=this is a test} > 10"),
|
||||
new AlarmExpression("test_metric{this is also a test = this_is_also_a_test} > 10")
|
||||
};
|
||||
MetricDefinition[] expected_list = {
|
||||
new MetricDefinition("test_metric", ImmutableMap.<String,String>builder()
|
||||
.put("this_is_a_test", "this is a test")
|
||||
.build()),
|
||||
new MetricDefinition("test_metric", ImmutableMap.<String,String>builder()
|
||||
.put("this is also a test", "this_is_also_a_test")
|
||||
.build())
|
||||
};
|
||||
|
||||
for(int i = 0; i < expr_list.length; i++) {
|
||||
AlarmSubExpression expr = expr_list[i].getSubExpressions().get(0);
|
||||
assertEquals(expr.getMetricDefinition(), expected_list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void shouldFailWithRestrictedChars() {
|
||||
String[] expressions = {"%cmetric{foo=bar,metric_name=mem} > 4",
|
||||
"me%ctric{foo=bar,metric_name=mem} > 4",
|
||||
|
Loading…
x
Reference in New Issue
Block a user