package com.bugsnag;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;

import static org.junit.Assert.*;

public class NotificationTest {

    private JsonNode rootNode;

    @Before
    public void setUp() throws Throwable {
        ObjectMapper mapper = new ObjectMapper();
        Configuration config = new Configuration("api-key");
        Report report = new Report(config, new RuntimeException());
        report.setSession(new Session("123", new Date()));
        Notification notification = new Notification(config, report);
        String json = mapper.writeValueAsString(notification);
        rootNode = mapper.readTree(json);
    }

    @Test
    public void testJsonSerialisation() throws Throwable {
        assertNotNull(rootNode);
        assertNotNull(rootNode.get("apiKey").asText());
        assertNotNull(rootNode.get("notifier"));

        JsonNode events = rootNode.get("events");
        assertNotNull(events);

        JsonNode event = events.get(0);
        assertNotNull(event);

        // check contains a session
        JsonNode session = event.get("session");
        assertNotNull(session);

        assertNotNull(session.get("id").asText());
        assertNotNull(session.get("startedAt").asText());

        JsonNode handledState = session.get("events");
        assertNotNull(handledState);
        assertNotNull(handledState.get("handled"));
        assertNotNull(handledState.get("unhandled"));
    }

}
@@ -119,33 +119,8 @@
 er;%0A
-import org.junit.Assert;%0A
 impo
@@ -164,16 +164,44 @@
 .Test;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -300,90 +300,74 @@
 ate 
-JsonNode rootNode;%0A%0A    @Before%0A    public void setUp() throws Throwable %7B%0A       
+Report report;%0A    private Configuration config;%0A    private final
  Obj
@@ -409,29 +409,71 @@
 ();%0A
+%0A
     
-    Configuration
+@Before%0A    public void setUp() throws Throwable %7B%0A       
  con
@@ -516,23 +516,16 @@
         
-Report 
 report =
@@ -577,62 +577,123 @@
     
+%7D%0A%0A
     
-report.setSession(new Session(%22123%22, new Date()));
+private JsonNode generateJson(ObjectMapper mapper, Configuration config, Report report) throws IOException %7B
 %0A   
@@ -835,17 +835,13 @@
    r
-ootNode =
+eturn
  map
@@ -897,18 +897,21 @@
 oid test
-Js
+Sessi
 onSerial
@@ -951,139 +951,59 @@
     
-assertNotNull(rootNode);%0A        assertNotNull(rootNode.get(%22apiKey%22).asText());%0A        assertNotNull(rootNode.get(%22notifier%22
+report.setSession(new Session(%22123%22, new Date()
 ));%0A
-%0A
     
@@ -1019,140 +1019,93 @@
 ode 
-events = rootNode.get(%22events%22);%0A        assertNotNull(events);%0A%0A        JsonNode event = events.get(0);%0A        assertNotNull(event
+rootNode = generateJson(mapper, config, report);%0A        validateErrorReport(rootNode
 );%0A%0A
@@ -1167,21 +1167,45 @@
 ssion = 
-event
+rootNode.get(%22events%22).get(0)
 .get(%22se
@@ -1245,17 +1245,16 @@
 ssion);%0A
-%0A
         
@@ -1560,9 +1560,691 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testWithoutSessionSerialisation() throws Throwable %7B%0A        report.setSession(new Session(%22123%22, new Date()));%0A        JsonNode rootNode = generateJson(mapper, config, report);%0A        validateErrorReport(rootNode);%0A        assertNull(rootNode.get(%22events%22).get(%22session%22));%0A    %7D%0A%0A    private void validateErrorReport(JsonNode rootNode) throws Throwable %7B%0A        assertNotNull(rootNode);%0A        assertNotNull(rootNode.get(%22apiKey%22).asText());%0A        assertNotNull(rootNode.get(%22notifier%22));%0A%0A        JsonNode events = rootNode.get(%22events%22);%0A        assertNotNull(events);%0A%0A        JsonNode event = events.get(0);%0A        assertNotNull(event);%0A    %7D%0A%0A
 %7D

add test to check session serialisation ignored when null