<commit_before>package com.reporting;

import com.reporting.db.ReportDAO;
import com.reporting.reports.CargoReport;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import se.citerus.dddsample.reporting.api.Handling;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/context.xml", "/context-cxf.xml", "/context-test-setup.xml"})
public class ReportServiceTest {

  @Autowired
  ReportDAO reportDAO;
  private XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

  @Test
  public void cargoReport() throws Exception {
    JSONObject json = readJSON("/cargo/ABC.json");
    JSONObject cargoReport = json.getJSONObject("cargoReport");

    JSONObject cargo = cargoReport.getJSONObject("cargo");
    
    assertEquals("ABC", cargo.get("trackingId"));
    assertEquals("Hongkong", cargo.get("receivedIn"));
    assertEquals("Stockholm", cargo.get("finalDestination"));
    assertEquals("6/15/09 12:00 PM", cargo.get("arrivalDeadline"));
    assertEquals("6/12/09 6:30 PM", cargo.get("eta"));
    assertEquals("Onboard voyage", cargo.get("currentStatus"));
    assertEquals("V0100", cargo.get("currentVoyage"));
    //assertEquals("Tokyo", cargo.get("currentLocation"));
    assertEquals("6/8/09 2:23 PM", cargo.get("lastUpdatedOn"));

    JSONArray handlings = cargoReport.getJSONArray("handlings");
    assertEquals(4, handlings.length());

    verifyHandling(handlings.getJSONObject(0), "Receive", "Hongkong", null);
    verifyHandling(handlings.getJSONObject(1), "Load", "Hongkong", "V0100");
    verifyHandling(handlings.getJSONObject(2), "Unload", "Long Beach", "V0100");
    verifyHandling(handlings.getJSONObject(3), "Load", "Long Beach", "V0200");
  }

  @Test
  public void cargoPDFReport() throws Exception {
    String pdf = readPDF("/cargo/ABC.pdf");
    assertTrue(pdf.length() > 0);
  }

  @Test(expected = FileNotFoundException.class)
  public void cargoNotFound() throws Exception {
    readJSON("/cargo/NOSUCH.json");
  }

  @Test
  public void voyageReportWithCargos() throws Exception {
    JSONObject json = readJSON("/voyage/V0100.json");
    JSONObject voyageReport = json.getJSONObject("voyageReport");

    JSONObject voyage = voyageReport.getJSONObject("voyage");

    assertEquals("V0100", voyage.get("voyageNumber"));
    assertEquals("Honolulu", voyage.get("nextStop"));
    assertEquals("6/10/09 4:25 AM", voyage.get("etaNextStop"));
    assertEquals("In port", voyage.get("currentStatus"));
    assertEquals(1400, voyage.get("delayedByMinutes"));
    assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));

    JSONObject cargo = voyageReport.getJSONObject("onboardCargos");
    assertEquals("ABC", cargo.get("trackingId"));
    assertEquals("Stockholm", cargo.get("finalDestination"));
  }

  @Test
  public void voyageReport() throws Exception {
    JSONObject json = readJSON("/voyage/V0200.json");
    JSONObject voyageReport = json.getJSONObject("voyageReport");

    JSONObject voyage = voyageReport.getJSONObject("voyage");

    assertEquals("V0200", voyage.get("voyageNumber"));
    assertEquals("Seattle", voyage.get("nextStop"));
    assertEquals("6/7/09 12:45 PM", voyage.get("etaNextStop"));
    assertEquals("In transit", voyage.get("currentStatus"));
    assertEquals(0, voyage.get("delayedByMinutes"));
    assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
    assertFalse(voyageReport.has("onboardCargos"));
  }

  @Test(expected = FileNotFoundException.class)
  public void voyageNotFound() throws Exception {
    readJSON("/voyage/NOSUCH.json");
  }

  @Test
  public void voyagePDFReport() throws Exception {
    String pdf = readPDF("/voyage/V0200.pdf");
    assertTrue(pdf.length() > 0);
  }

  @Test
  public void reportCargo() throws Exception {
    HttpURLConnection con = openXMLPutConnection("/cargo");
    XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(con.getOutputStream());
    writer.writeStartDocument();
    writer.writeStartElement("cargoDetails");

    addElement(writer, "trackingId", "FGH456");
    addElement(writer, "receivedIn", "HONGKONG");<commit_msg>Test fix due to changes in sample data<commit_after>package com.reporting;

import com.reporting.db.ReportDAO;
import com.reporting.reports.CargoReport;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import se.citerus.dddsample.reporting.api.Handling;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/context.xml", "/context-cxf.xml", "/context-test-setup.xml"})
public class ReportServiceTest {

  @Autowired
  ReportDAO reportDAO;
  private XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

  @Test
  public void cargoReport() throws Exception {
    JSONObject json = readJSON("/cargo/ABC.json");
    JSONObject cargoReport = json.getJSONObject("cargoReport");

    JSONObject cargo = cargoReport.getJSONObject("cargo");
    
    assertEquals("ABC", cargo.get("trackingId"));
    assertEquals("Hongkong", cargo.get("receivedIn"));
    assertEquals("Stockholm", cargo.get("finalDestination"));
    assertEquals("6/15/09 12:00 PM", cargo.get("arrivalDeadline"));
    assertEquals("6/12/09 6:30 PM", cargo.get("eta"));
    assertEquals("Onboard voyage", cargo.get("currentStatus"));
    assertEquals("V0100", cargo.get("currentVoyage"));
    //assertEquals("Tokyo", cargo.get("currentLocation"));
    assertEquals("6/8/09 2:23 PM", cargo.get("lastUpdatedOn"));

    JSONArray handlings = cargoReport.getJSONArray("handlings");
    assertEquals(4, handlings.length());

    verifyHandling(handlings.getJSONObject(0), "Receive", "Hongkong", null);
    verifyHandling(handlings.getJSONObject(1), "Load", "Hongkong", "V0100");
    verifyHandling(handlings.getJSONObject(2), "Unload", "Long Beach", "V0100");
    verifyHandling(handlings.getJSONObject(3), "Load", "Long Beach", "V0200");
  }

  @Test
  public void cargoPDFReport() throws Exception {
    String pdf = readPDF("/cargo/ABC.pdf");
    assertTrue(pdf.length() > 0);
  }

  @Test(expected = FileNotFoundException.class)
  public void cargoNotFound() throws Exception {
    readJSON("/cargo/NOSUCH.json");
  }

  @Test
  public void voyageReportWithCargos() throws Exception {
    JSONObject json = readJSON("/voyage/V0100.json");
    JSONObject voyageReport = json.getJSONObject("voyageReport");

    JSONObject voyage = voyageReport.getJSONObject("voyage");

    assertEquals("V0100", voyage.get("voyageNumber"));
    assertEquals("Honolulu", voyage.get("nextStop"));
    assertEquals("6/10/09 4:25 AM", voyage.get("etaNextStop"));
    assertEquals("In port", voyage.get("currentStatus"));
    assertEquals(1400, voyage.get("delayedByMinutes"));
    assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));

    JSONObject cargo = voyageReport.getJSONObject("onboardCargos");
    assertEquals("ABC", cargo.get("trackingId"));
    assertEquals("Stockholm", cargo.get("finalDestination"));
  }

  @Test
  public void voyageReport() throws Exception {
    JSONObject json = readJSON("/voyage/V0200.json");
    JSONObject voyageReport = json.getJSONObject("voyageReport");

    JSONObject voyage = voyageReport.getJSONObject("voyage");

    assertEquals("V0200", voyage.get("voyageNumber"));
    assertEquals("Seattle", voyage.get("nextStop"));
    assertEquals("6/7/09 12:45 PM", voyage.get("etaNextStop"));
    assertEquals("In transit", voyage.get("currentStatus"));
    assertEquals(0, voyage.get("delayedByMinutes"));
    assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
    assertTrue(voyageReport.has("onboardCargos"));
    // TODO verify onboard cargos content 
  }

  @Test(expected = FileNotFoundException.class)
  public void voyageNotFound() throws Exception {
    readJSON("/voyage/NOSUCH.json");
  }

  @Test
  public void voyagePDFReport() throws Exception {
    String pdf = readPDF("/voyage/V0200.pdf");
    assertTrue(pdf.length() > 0);
  }

  @Test
  public void reportCargo() throws Exception {
    HttpURLConnection con = openXMLPutConnection("/cargo");
    XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(con.getOutputStream());
    writer.writeStartDocument();
    writer.writeStartElement("cargoDetails");

    addElement(writer, "trackingId", "FGH456");
    addElement(writer, "receivedIn", "HONGKONG");
{'fraction_non_alphanumeric': 0.14348313461734433, 'fraction_numerical': 0.022011617242433508, 'mean_word_length': 5.477887788778878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280010', 'n_tokens_mistral': 3107, 'n_tokens_neox': 2912, 'n_words': 525}