karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject karaf-decanter git commit: [KARAF-4305] Fix timestamp generation
Date Tue, 02 Feb 2016 16:18:00 GMT
Repository: karaf-decanter
Updated Branches:
  refs/heads/master 355aa8337 -> 70eda3b16


[KARAF-4305] Fix timestamp generation


Project: http://git-wip-us.apache.org/repos/asf/karaf-decanter/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-decanter/commit/70eda3b1
Tree: http://git-wip-us.apache.org/repos/asf/karaf-decanter/tree/70eda3b1
Diff: http://git-wip-us.apache.org/repos/asf/karaf-decanter/diff/70eda3b1

Branch: refs/heads/master
Commit: 70eda3b168eb1976dc664aba98a769221edd4c11
Parents: 355aa83
Author: Christian Schneider <chris@die-schneider.net>
Authored: Tue Feb 2 17:17:48 2016 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Tue Feb 2 17:17:48 2016 +0100

----------------------------------------------------------------------
 .../marshaller/json/JsonMarshaller.java         | 11 ++++++++--
 .../marshaller/json/TestJsonMarshaller.java     | 21 ++++++++++++++++++--
 2 files changed, 28 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/70eda3b1/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
----------------------------------------------------------------------
diff --git a/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
b/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
index bbf9e4b..ac82184 100644
--- a/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
+++ b/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
@@ -25,6 +25,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.TimeZone;
 
 import javax.json.Json;
 import javax.json.JsonArray;
@@ -36,9 +37,15 @@ import javax.json.JsonWriter;
 import org.apache.karaf.decanter.api.marshaller.Marshaller;
 import org.apache.karaf.decanter.api.marshaller.Unmarshaller;
 import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
 
 public class JsonMarshaller implements Marshaller, Unmarshaller {
-    private final SimpleDateFormat tsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSS'Z'");
+    private final SimpleDateFormat tsFormat;
+    
+    public JsonMarshaller() {
+        tsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSSX");
+        tsFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+    }
 
     @Override
     public void marshal(Object obj, OutputStream out) {
@@ -74,7 +81,7 @@ public class JsonMarshaller implements Marshaller, Unmarshaller {
     }
 
     private void addTimestamp(Event event, JsonObjectBuilder json) {
-        Long ts = (Long)event.getProperty("timestamp");
+        Long ts = (Long)event.getProperty(EventConstants.TIMESTAMP);
         Date date = ts != null ? new Date(ts) : new Date();
         json.add("@timestamp", tsFormat.format(date));
     }

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/70eda3b1/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
----------------------------------------------------------------------
diff --git a/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
b/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
index a567142..3891717 100644
--- a/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
+++ b/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
@@ -16,22 +16,39 @@
  */
 package org.apache.karaf.decanter.marshaller.json;
 
+import java.io.StringReader;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+
 import org.apache.karaf.decanter.api.marshaller.Marshaller;
+import org.junit.Assert;
 import org.junit.Test;
 import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
 
 public class TestJsonMarshaller {
+    private static final long EXPECTED_TIMESTAMP = 1454428780634L;
+    private static final String EXPECTED_TOPIC = "testTopic";
 
    @Test
    public void testMarshal() throws Exception {
        Marshaller marshaller = new JsonMarshaller();
        Map<String, Object> map = new HashMap<>();
-       map.put("a", "b");
+       map.put(EventConstants.TIMESTAMP, EXPECTED_TIMESTAMP);
        map.put("c", "d");
-       String jsonSt = marshaller.marshal(new Event("testTopic", map));
+       String jsonSt = marshaller.marshal(new Event(EXPECTED_TOPIC, map));
+       System.out.println(jsonSt);
+       JsonReader reader = Json.createReader(new StringReader(jsonSt));
+       JsonObject jsonO = reader.readObject();
+       Assert.assertEquals("Timestamp string", "2016-02-02T15:59:40,634Z",jsonO.getString("@timestamp"));
+       long ts = jsonO.getJsonNumber(EventConstants.TIMESTAMP).longValue();
+       Assert.assertEquals("timestamp long", EXPECTED_TIMESTAMP, ts);
+       Assert.assertEquals("Topic", EXPECTED_TOPIC, jsonO.getString(EventConstants.EVENT_TOPIC));
+
    }
 
 }


Mime
View raw message