GameLogsController { @RequestMapping(value = "/logs") @ResponseBody public String gameLogs(HttpServletRequest request) { try { StringWriter writer = new StringWriter(); String loggedUser = (String) request.getAttribute(SecurityFilter.LOGGED_USER); List<String> logFiles = null; if (loggedUser != null) { File userLogDir = new File(configuration.getLogsDir(), loggedUser); String[] fileNames = userLogDir.list(new TimestampFilter()); if (fileNames != null) { logFiles = Arrays.asList(fileNames); } } if (logFiles != null) { Collections.sort(logFiles, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); } serializer.serialize(writer, new GameLogData(logFiles)); return writer.toString(); } catch (IOException e) { throw new RuntimeException(e); } } @Autowired  GameLogsController(ServiceConfiguration configuration, JsonSerializer serializer); @RequestMapping(value = "/logs") @ResponseBody String gameLogs(HttpServletRequest request); @Value("${timestamp.format}") void setTimestampFormat(String timestampFormat);  }
@Test public void shouldBeEmptyArrayWhenNotLoggedUser() { String json = controller.gameLogs(request); assertEmptyDataArray(json); }
@Test public void shouldSendGameLogEntry() { GameLogFile logFile = fixture.createLogFile("testUser", "2012-10-11 112233"); logFile.log("something", "something"); loginUser("testUser"); String content = controller.gameLogs(request); JsonPath jsonPath = from(content); List<List> dataList = jsonPath.getList("aaData"); assertEquals("2012-10-11 112233", TestUtils.getCell(dataList, 0, 0)); }
@Test public void shouldIgnoreNonLogFiles() throws IOException { loginUser("testUser"); File testUserLogsDir = new File(fixture.getConfiguration().getLogsDir(), "testUser"); testUserLogsDir.mkdirs(); FileUtils.writeStringToFile(new File(testUserLogsDir, "scores.txt"), "bla-bla"); String content = controller.gameLogs(request); assertEmptyDataArray(content); }
@Test public void shouldSendGameLogSeveralEntries() { GameLogFile logFile1 = fixture.createLogFile("testUser", "2012-10-10 112233"); GameLogFile logFile2 = fixture.createLogFile("testUser", "2012-11-11 112233"); logFile1.log("something", "something"); logFile2.log("something", "something"); loginUser("testUser"); String content = controller.gameLogs(request); JsonPath jsonPath = from(content); List<List> dataList = jsonPath.getList("aaData"); assertEquals("2012-11-11 112233", TestUtils.getCell(dataList, 0, 0)); assertEquals("2012-10-10 112233", TestUtils.getCell(dataList, 1, 0)); }
@Test public void shouldReturnEmptyListWhenLogDirNotExists(){ loginUser("testUser"); assertEmptyDataArray(controller.gameLogs(request)); }