GrokServiceImpl implements GrokService { @Override public GrokValidation validateGrokStatement(GrokValidation grokValidation) throws RestException { Map<String, Object> results; try { if (grokValidation.getPatternLabel() == null) { throw new RestException("Pattern label is required"); } if (Strings.isEmpty(grokValidation.getStatement())) { throw new RestException("Grok statement is required"); } Grok grok = new Grok(); grok.addPatternFromReader(new InputStreamReader(getClass().getResourceAsStream( "/patterns/common"), StandardCharsets.UTF_8)); grok.addPatternFromReader(new StringReader(grokValidation.getStatement())); String grokPattern = "%{" + grokValidation.getPatternLabel() + "}"; grok.compile(grokPattern); Match gm = grok.match(grokValidation.getSampleData()); gm.captures(); results = gm.toMap(); results.remove(grokValidation.getPatternLabel()); } catch (Exception e) { throw new RestException(e); } grokValidation.setResults(results); return grokValidation; } @Autowired  GrokServiceImpl(Environment environment, Grok commonGrok, HdfsService hdfsService); @Override Map<String, String> getCommonGrokPatterns(); @Override GrokValidation validateGrokStatement(GrokValidation grokValidation); @Override Path saveTemporary(String statement, String name); void deleteTemporary(); String getStatementFromClasspath(String path);  }
@Test public void validateGrokStatementShouldThrowExceptionWithNullStringAsPatternLabel() { GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData("asdf asdf"); grokValidation.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); RestException e = assertThrows(RestException.class, () -> grokService.validateGrokStatement(grokValidation)); assertEquals("Pattern label is required", e.getMessage()); }
@Test public void validateGrokStatementShouldThrowExceptionWithEmptyStringAsStatement() { GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData("asdf asdf"); grokValidation.setPatternLabel("LABEL"); grokValidation.setStatement(""); RestException e = assertThrows(RestException.class, () -> grokService.validateGrokStatement(grokValidation)); assertEquals("Grok statement is required", e.getMessage()); }
@Test public void validateGrokStatementShouldThrowExceptionWithNullStringAsStatement() { GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData("asdf asdf"); grokValidation.setPatternLabel("LABEL"); grokValidation.setStatement(null); RestException e = assertThrows(RestException.class, () -> grokService.validateGrokStatement(grokValidation)); assertEquals("Grok statement is required", e.getMessage()); }
@Test public void validateGrokStatementShouldProperlyMatchSampleDataAgainstGivenStatement() throws Exception { final GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData("asdf asdf"); grokValidation.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); grokValidation.setPatternLabel("LABEL"); GrokValidation expected = new GrokValidation(); expected.setResults(new HashMap<String, Object>() {{ put("word1", "asdf"); put("word2", "asdf"); }}); expected.setSampleData("asdf asdf"); expected.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); expected.setPatternLabel("LABEL"); GrokValidation actual = grokService.validateGrokStatement(grokValidation); assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); }
@Test public void validateGrokStatementShouldProperlyMatchNothingAgainstEmptyString() throws Exception { final GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData(""); grokValidation.setPatternLabel("LABEL"); grokValidation.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); GrokValidation expected = new GrokValidation(); expected.setResults(new HashMap<>()); expected.setSampleData(""); expected.setPatternLabel("LABEL"); expected.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); assertEquals(expected, grokService.validateGrokStatement(grokValidation)); }
@Test public void validateGrokStatementShouldProperlyMatchNothingAgainstNullString() throws Exception { final GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData(null); grokValidation.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); grokValidation.setPatternLabel("LABEL"); GrokValidation expected = new GrokValidation(); expected.setResults(new HashMap<>()); expected.setSampleData(null); expected.setStatement("LABEL %{WORD:word1} %{WORD:word2}"); expected.setPatternLabel("LABEL"); assertEquals(expected, grokService.validateGrokStatement(grokValidation)); }
@Test public void invalidGrokStatementShouldThrowRestException() { GrokValidation grokValidation = new GrokValidation(); grokValidation.setResults(new HashMap<>()); grokValidation.setSampleData(null); grokValidation.setStatement("LABEL %{WORD:word1} %{WORD:word2"); assertThrows(RestException.class, () -> grokService.validateGrokStatement(grokValidation)); }