GrokServiceImpl implements GrokService { @Override public Path saveTemporary(String statement, String name) throws RestException { if (statement != null) { Path path = getTemporaryGrokRootPath(); hdfsService.mkdirs(path); hdfsService.write(new Path(path, name), statement.getBytes(StandardCharsets.UTF_8),null,null,null); return path; } else { throw new RestException("A grokStatement must be provided"); } } @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 saveTemporaryShouldProperlySaveFile() throws Exception { new File("./target/user1").delete(); String statement = "grok statement"; Authentication authentication = mock(Authentication.class); when(authentication.getName()).thenReturn("user1"); SecurityContextHolder.getContext().setAuthentication(authentication); when(environment.getProperty(GROK_TEMP_PATH_SPRING_PROPERTY)).thenReturn("./target"); grokService.saveTemporary(statement, "squid"); File testRoot = new File("./target/user1"); assertEquals(statement, FileUtils.readFileToString(new File(testRoot, "squid"), StandardCharsets.UTF_8)); testRoot.delete(); }
@Test public void missingGrokStatementShouldThrowRestException() { RestException e = assertThrows(RestException.class, () -> grokService.saveTemporary(null, "squid")); assertEquals("A grokStatement must be provided", e.getMessage()); }