Search in sources :

Example 1 with RegexTesterResponse

use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.

the class RegexTesterResource method doTestRegex.

private RegexTesterResponse doTestRegex(String example, String regex) {
    final Pattern pattern;
    try {
        pattern = Pattern.compile(regex, Pattern.DOTALL);
    } catch (PatternSyntaxException e) {
        throw new BadRequestException("Invalid regular expression: " + e.getMessage(), e);
    }
    final Matcher matcher = pattern.matcher(example);
    boolean matched = matcher.find();
    // Get the first matched group.
    final RegexTesterResponse.Match match;
    if (matched && matcher.groupCount() > 0) {
        match = RegexTesterResponse.Match.create(matcher.group(1), matcher.start(1), matcher.end(1));
    } else {
        match = null;
    }
    return RegexTesterResponse.create(matched, match, regex, example);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BadRequestException(javax.ws.rs.BadRequestException) RegexTesterResponse(org.graylog2.rest.models.tools.responses.RegexTesterResponse) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with RegexTesterResponse

use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.

the class RegexTesterResourceTest method regexTesterReturnsValidResponseIfRegExMatches.

@Test
public void regexTesterReturnsValidResponseIfRegExMatches() throws Exception {
    final RegexTesterResponse response = resource.regexTester("([a-z]+)", "test");
    assertThat(response.matched()).isTrue();
    assertThat(response.regex()).isEqualTo("([a-z]+)");
    assertThat(response.string()).isEqualTo("test");
    assertThat(response.match()).isEqualTo(RegexTesterResponse.Match.create("test", 0, 4));
}
Also used : RegexTesterResponse(org.graylog2.rest.models.tools.responses.RegexTesterResponse) Test(org.junit.Test)

Example 3 with RegexTesterResponse

use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.

the class RegexTesterResourceTest method testRegexReturnsValidResponseIfRegExMatches.

@Test
public void testRegexReturnsValidResponseIfRegExMatches() throws Exception {
    final RegexTesterResponse response = resource.testRegex(RegexTestRequest.create("test", "([a-z]+)"));
    assertThat(response.matched()).isTrue();
    assertThat(response.regex()).isEqualTo("([a-z]+)");
    assertThat(response.string()).isEqualTo("test");
    assertThat(response.match()).isEqualTo(RegexTesterResponse.Match.create("test", 0, 4));
}
Also used : RegexTesterResponse(org.graylog2.rest.models.tools.responses.RegexTesterResponse) Test(org.junit.Test)

Example 4 with RegexTesterResponse

use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.

the class RegexTesterResourceTest method regexTesterReturnsValidResponseIfRegExDoesNotMatch.

@Test
public void regexTesterReturnsValidResponseIfRegExDoesNotMatch() throws Exception {
    final RegexTesterResponse response = resource.regexTester("([0-9]+)", "test");
    assertThat(response.matched()).isFalse();
    assertThat(response.regex()).isEqualTo("([0-9]+)");
    assertThat(response.string()).isEqualTo("test");
    assertThat(response.match()).isNull();
}
Also used : RegexTesterResponse(org.graylog2.rest.models.tools.responses.RegexTesterResponse) Test(org.junit.Test)

Example 5 with RegexTesterResponse

use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.

the class RegexTesterResourceTest method testRegexReturnsValidResponseIfRegExDoesNotMatch.

@Test
public void testRegexReturnsValidResponseIfRegExDoesNotMatch() throws Exception {
    final RegexTesterResponse response = resource.testRegex(RegexTestRequest.create("test", "([0-9]+)"));
    assertThat(response.matched()).isFalse();
    assertThat(response.regex()).isEqualTo("([0-9]+)");
    assertThat(response.string()).isEqualTo("test");
    assertThat(response.match()).isNull();
}
Also used : RegexTesterResponse(org.graylog2.rest.models.tools.responses.RegexTesterResponse) Test(org.junit.Test)

Aggregations

RegexTesterResponse (org.graylog2.rest.models.tools.responses.RegexTesterResponse)5 Test (org.junit.Test)4 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 BadRequestException (javax.ws.rs.BadRequestException)1