package com.reucon.commons.web.exception;

import com.reucon.commons.web.exception.model.ExceptionReport;
import java.io.CharArrayWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.containsString;

public class ExceptionReportGeneratorTest
{
    private ExceptionReportGenerator reportGenerator;
    private MockHttpServletRequest httpServletRequest;
    private Date date;
    private String exceptionId;
    private Exception exception;
    private CharArrayWriter writer;

    @Before
    public void setUp() throws Exception
    {
        this.reportGenerator = new ExceptionReportGenerator();
        this.httpServletRequest = new MockHttpServletRequest();
        
        date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2000-01-02 01:02:03");
        exception = new Exception("sample exception");
        exceptionId = "ID:123:456";
        
        writer = new CharArrayWriter();
    }

    
    @Test
    public void writeShouldNotThrow() throws Exception
    {
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
    }
    
    @Test
    public void reportContainsExceptionId() throws Exception
    {
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("Exception Id:          ID:123:456"));
    }
    
    @Test
    public void reportContainsHeader() throws Exception
    {
        httpServletRequest.addHeader("firstHeader", "first Value");
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("firstHeader: first Value"));
    }
    
    @Test
    public void reportContainsHeaders() throws Exception
    {
        httpServletRequest.addHeader("firstHeader", "first Value");
        httpServletRequest.addHeader("firstHeader", "second Value");
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("firstHeader: first Value"));
        assertThat(report, containsString("firstHeader: second Value"));
    }
    
    @Test
    public void reportContainsParameter() throws Exception
    {
        httpServletRequest.setParameter("param", "parameter value");
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("param: parameter value"));
    }
    
    @Test
    public void reportContainsParameterArray() throws Exception
    {
        httpServletRequest.setParameter("param", new String[]{"parameter value", "value 2"});
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("param: parameter value"));
        assertThat(report, containsString("param: value 2"));
    }

    @Test
    public void reportContainsAttributes() throws Exception
    {
        httpServletRequest.setAttribute("attribute 1", "value1");
        httpServletRequest.setAttribute("attribute 2", "value2");
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("attribute 1: value1"));
        assertThat(report, containsString("attribute 2: value2"));
    }
    
    @Test
    public void reportContainsSessionId() throws Exception
    {
        final MockHttpSession session = new MockHttpSession();
        httpServletRequest.setSession(session);
        
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("Session Id:"));
    }
    
    @Test
    public void reportContainsSessionAttributes() throws Exception
    {
        final MockHttpSession session = new MockHttpSession();
        session.setAttribute("sessionAttribute 1", "value1");
        session.setAttribute("sessionAttribute 2", "value2");
        
        httpServletRequest.setSession(session);
        
        reportGenerator.writeExceptionReport(writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest));
        
        final String report = writer.toString();
        
        assertThat(report, containsString("sessionAttribute 1: value1"));
        assertThat(report, containsString("sessionAttribute 2: value2"));
    }
    
}

@@ -131,16 +131,44 @@
 Writer;%0A
+import java.io.IOException;%0A
 import j
@@ -1189,49 +1189,33 @@
     
-@Test%0A    public void writeShouldNotThrow
+void writeExceptionReport
 () t
@@ -1212,32 +1212,34 @@
 Report() throws 
+IO
 Exception%0A    %7B%0A
@@ -1361,32 +1361,147 @@
 equest));%0A    %7D%0A
+    %0A    @Test%0A    public void writeShouldNotThrow() throws Exception%0A    %7B%0A        writeExceptionReport();%0A    %7D%0A%0A
     %0A    @Test%0A 
@@ -1574,32 +1574,16 @@
         
-reportGenerator.
 writeExc
@@ -1599,85 +1599,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -1905,32 +1905,16 @@
         
-reportGenerator.
 writeExc
@@ -1930,85 +1930,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -2297,32 +2297,16 @@
         
-reportGenerator.
 writeExc
@@ -2322,85 +2322,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -2696,32 +2696,16 @@
         
-reportGenerator.
 writeExc
@@ -2721,85 +2721,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -3050,32 +3050,16 @@
         
-reportGenerator.
 writeExc
@@ -3075,85 +3075,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -3496,32 +3496,16 @@
         
-reportGenerator.
 writeExc
@@ -3521,85 +3521,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -3935,32 +3935,16 @@
         
-reportGenerator.
 writeExc
@@ -3960,85 +3960,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 
@@ -4440,32 +4440,16 @@
         
-reportGenerator.
 writeExc
@@ -4465,85 +4465,8 @@
 ort(
-writer, date, new ExceptionReport(exceptionId, exception, httpServletRequest)
 );%0A 

Introduce method for test case