-
Notifications
You must be signed in to change notification settings - Fork 1k
/
BaseParserTest.java
82 lines (73 loc) · 2.9 KB
/
BaseParserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* This file is part of Graylog Pipeline Processor.
*
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog.plugins.pipelineprocessor;
import com.google.common.base.Charsets;
import org.graylog.plugins.pipelineprocessor.ast.Rule;
import org.graylog.plugins.pipelineprocessor.ast.statements.Statement;
import org.graylog.plugins.pipelineprocessor.parser.FunctionRegistry;
import org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser;
import org.graylog2.plugin.Message;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.rules.TestName;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;
public class BaseParserTest {
protected static final AtomicBoolean actionsTriggered = new AtomicBoolean(false);
protected static FunctionRegistry functionRegistry;
@org.junit.Rule
public TestName name = new TestName();
protected PipelineRuleParser parser;
@Before
public void setup() {
parser = new PipelineRuleParser(functionRegistry);
// initialize before every test!
actionsTriggered.set(false);
}
protected Message evaluateRule(Rule rule, Message message) {
final EvaluationContext context = new EvaluationContext(message);
if (rule.when().evaluateBool(context)) {
for (Statement statement : rule.then()) {
statement.evaluate(context);
}
return message;
} else {
return null;
}
}
@Nullable
protected Message evaluateRule(Rule rule) {
final Message message = new Message("hello test", "source", DateTime.now());
return evaluateRule(rule, message);
}
protected String ruleForTest() {
try {
final URL resource = this.getClass().getResource(name.getMethodName().concat(".txt"));
final Path path = Paths.get(resource.toURI());
final byte[] bytes = Files.readAllBytes(path);
return new String(bytes, Charsets.UTF_8);
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
}