FilterJSON extends Filter { @Override public void apply(String inputStr, InputMarker inputMarker) throws LogfeederException { Map<String, Object> jsonMap = null; try { jsonMap = LogFeederUtil.toJSONObject(inputStr); } catch (Exception e) { LOG.error(e.getLocalizedMessage()); throw new LogfeederException("Json parsing failed for inputstr = " + inputStr ,e.getCause()); } Double lineNumberD = (Double) jsonMap.get("line_number"); if (lineNumberD != null) { long lineNumber = lineNumberD.longValue(); jsonMap.put("line_number", lineNumber); } String timeStampStr = (String) jsonMap.get("logtime"); if (timeStampStr != null && !timeStampStr.isEmpty()) { String logtime = DateUtil.getDate(timeStampStr); jsonMap.put("logtime", logtime); jsonMap.put(LogFeederConstants.IN_MEMORY_TIMESTAMP, Long.parseLong(timeStampStr)); } super.apply(jsonMap, inputMarker); }  @Override void apply(String inputStr, InputMarker inputMarker);  }
@Test public void testJSONFilterCode_convertFields() throws Exception { LOG.info("testJSONFilterCode_convertFields()"); init(new HashMap<String, Object>()); mockOutputManager.write(EasyMock.capture(capture), EasyMock.anyObject(InputMarker.class)); EasyMock.expectLastCall(); EasyMock.replay(mockOutputManager); Date d = new Date(); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String dateString = sdf.format(d); filterJson.apply("{ logtime: '" + d.getTime() + "', line_number: 100 }", new InputMarker(null, null, 0)); EasyMock.verify(mockOutputManager); Map<String, Object> jsonParams = capture.getValue(); assertEquals("Incorrect decoding: log time", dateString, jsonParams.remove("logtime")); assertEquals("Incorrect decoding: in memory timestamp", d.getTime(), jsonParams.remove(LogFeederConstants.IN_MEMORY_TIMESTAMP)); assertEquals("Incorrect decoding: line number", 100l, jsonParams.remove("line_number")); assertTrue("jsonParams are not empty!", jsonParams.isEmpty()); }
@Test public void testJSONFilterCode_logTimeOnly() throws Exception { LOG.info("testJSONFilterCode_logTimeOnly()"); init(new HashMap<String, Object>()); mockOutputManager.write(EasyMock.capture(capture), EasyMock.anyObject(InputMarker.class)); EasyMock.expectLastCall(); EasyMock.replay(mockOutputManager); Date d = new Date(); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String dateString = sdf.format(d); filterJson.apply("{ logtime: '" + d.getTime() + "', some_field: 'abc' }", new InputMarker(null, null, 0)); EasyMock.verify(mockOutputManager); Map<String, Object> jsonParams = capture.getValue(); assertEquals("Incorrect decoding: log time", dateString, jsonParams.remove("logtime")); assertEquals("Incorrect decoding: in memory timestamp", d.getTime(), jsonParams.remove(LogFeederConstants.IN_MEMORY_TIMESTAMP)); assertEquals("Incorrect decoding: some field", "abc", jsonParams.remove("some_field")); assertTrue("jsonParams are not empty!", jsonParams.isEmpty()); }
@Test public void testJSONFilterCode_lineNumberOnly() throws Exception { LOG.info("testJSONFilterCode_lineNumberOnly()"); init(new HashMap<String, Object>()); mockOutputManager.write(EasyMock.capture(capture), EasyMock.anyObject(InputMarker.class)); EasyMock.expectLastCall(); EasyMock.replay(mockOutputManager); filterJson.apply("{ line_number: 100, some_field: 'abc' }", new InputMarker(null, null, 0)); EasyMock.verify(mockOutputManager); Map<String, Object> jsonParams = capture.getValue(); assertEquals("Incorrect decoding: line number", 100l, jsonParams.remove("line_number")); assertEquals("Incorrect decoding: some field", "abc", jsonParams.remove("some_field")); assertTrue("jsonParams are not empty!", jsonParams.isEmpty()); }
@Test public void testJSONFilterCode_invalidJson() throws Exception { LOG.info("testJSONFilterCode_invalidJson()"); init(new HashMap<String, Object>()); String inputStr="invalid json"; try{ filterJson.apply(inputStr,new InputMarker(null, null, 0)); fail("Expected LogfeederException was not occured"); }catch(LogfeederException logfeederException){ assertEquals("Json parsing failed for inputstr = "+inputStr, logfeederException.getLocalizedMessage()); } }