Allow spaces and apostrophes in dimensions
Change-Id: I0f6a00496e65eeadb946849e68107321310a1a11
This commit is contained in:
parent
de87b63c9b
commit
41457907ab
|
@ -82,8 +82,26 @@ class AlarmSubExpressionListener extends AlarmExpressionBaseListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enterDimension(AlarmExpressionParser.DimensionContext ctx) {
|
public void enterDimension(AlarmExpressionParser.DimensionContext ctx) {
|
||||||
String dimensionName = ctx.getChild(0).getText();
|
StringBuilder dimensionName = new StringBuilder();
|
||||||
if (dimensions.put(dimensionName, ctx.getChild(2).getText()) != null)
|
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 "
|
throw new IllegalArgumentException("More than one value was given for dimension "
|
||||||
+ dimensionName);
|
+ dimensionName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,7 @@ dimensionList
|
||||||
;
|
;
|
||||||
|
|
||||||
dimension
|
dimension
|
||||||
: txt '=' txt
|
: (txt)+ '=' (txt)+
|
||||||
;
|
;
|
||||||
|
|
||||||
keyword
|
keyword
|
||||||
|
@ -210,7 +210,7 @@ DECIMAL
|
||||||
;
|
;
|
||||||
|
|
||||||
TXT
|
TXT
|
||||||
: ~('\'' | '}' | '{' | '&' | '|' | '>' | '<' | '=' | ',' | ')' | '(' | ' ' | '"' )+
|
: ~('}' | '{' | '&' | '|' | '>' | '<' | '=' | ',' | ')' | '(' | ' ' | '"' )+
|
||||||
;
|
;
|
||||||
|
|
||||||
STRING
|
STRING
|
||||||
|
|
|
@ -24,11 +24,12 @@ import java.util.List;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import monasca.common.model.metric.MetricDefinition;
|
import monasca.common.model.metric.MetricDefinition;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public class AlarmExpressionTest {
|
public class AlarmExpressionTest {
|
||||||
private final String restrictedChars = "(){}&|<>='\",";
|
private final String restrictedChars = "(){}&|<>=\",";
|
||||||
|
|
||||||
public void shouldParseExpression() {
|
public void shouldParseExpression() {
|
||||||
AlarmExpression expr = new AlarmExpression(
|
AlarmExpression expr = new AlarmExpression(
|
||||||
|
@ -251,6 +252,26 @@ public class AlarmExpressionTest {
|
||||||
assertEquals(alarm1.getMetricDefinition(), expected1);
|
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() {
|
public void shouldFailWithRestrictedChars() {
|
||||||
String[] expressions = {"%cmetric{foo=bar,metric_name=mem} > 4",
|
String[] expressions = {"%cmetric{foo=bar,metric_name=mem} > 4",
|
||||||
"me%ctric{foo=bar,metric_name=mem} > 4",
|
"me%ctric{foo=bar,metric_name=mem} > 4",
|
||||||
|
|
Loading…
Reference in New Issue