@Test
  public void shouldListenForAndParseORU_R01Message() throws Exception {

    ModuleActivator activator = new PacsIntegrationActivator();
    activator.started();
    Context.getService(PacsIntegrationService.class).initializeHL7Listener();

    List<Patient> patients =
        patientService.getPatients(
            null,
            "101-6",
            Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()),
            true);
    assertThat(patients.size(), is(1)); // sanity check
    Patient patient = patients.get(0);
    List<Encounter> encounters =
        encounterService.getEncounters(
            patient,
            null,
            null,
            null,
            null,
            Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()),
            null,
            null,
            null,
            false);
    assertThat(encounters.size(), is(0)); // sanity check

    try {
      String message =
          "MSH|^~\\&|HMI|Mirebalais Hospital|RAD|REPORTS|20130228174549||ORU^R01|RTS01CE16055AAF5290|P|2.3|\r"
              + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r"
              + "PV1|1||||||||||||||||||\r"
              + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170556||||||||||||MBL^CR||||||F|||||||M123&Goodrich&Mark&&&&||||20130228170556\r"
              + "OBX|1|TX|127689^SOME_X-RAY||Clinical Indication: ||||||F\r";

      Thread.sleep(2000); // give the simple server time to start

      Socket socket = new Socket("127.0.0.1", 6665);

      PrintStream writer = new PrintStream(socket.getOutputStream());
      BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      writer.print(header);
      writer.print(message);
      writer.print(trailer + "\r");
      writer.flush();

      Thread.sleep(2000);

      // confirm that report encounter has been created and has obs (we more thoroughly test the
      // handler in the ORU_R01 handler and Radiology Service (in emr module) tests)
      encounters =
          encounterService.getEncounters(
              patient,
              null,
              null,
              null,
              null,
              Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()),
              null,
              null,
              null,
              false);
      assertThat(encounters.size(), is(1));
      assertThat(encounters.get(0).getObs().size(), is(4));

      // confirm that the proper ack is sent out
      String response = reader.readLine();
      assertThat(response, containsString("|ACK|"));
    } finally {
      activator.stopped();
    }
  }
  @Test
  public void shouldNotImportORM_001MessageWithDuplicateOrderNumber() throws Exception {

    ModuleActivator activator = new PacsIntegrationActivator();
    activator.started();
    Context.getService(PacsIntegrationService.class).initializeHL7Listener();

    List<Patient> patients =
        patientService.getPatients(
            null,
            "101-6",
            Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()),
            true);
    assertThat(patients.size(), is(1)); // sanity check
    Patient patient = patients.get(0);
    List<Encounter> encounters =
        encounterService.getEncounters(
            patient,
            null,
            null,
            null,
            null,
            Collections.singletonList(radiologyProperties.getRadiologyStudyEncounterType()),
            null,
            null,
            null,
            false);
    assertThat(encounters.size(), is(0)); // sanity check

    try {

      String message =
          "MSH|^~\\&|HMI||RAD|REPORTS|20130228174643||ORM^O01|RTS01CE16057B105AC0|P|2.3|\r"
              + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r"
              + "ORC|\r"
              + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170350||||||||||||MBL^CR||||||P|||||||&Goodrich&Mark&&&&||||20130228170350\r"
              + "OBX|1|RP|||||||||F\r"
              + "OBX|2|TX|EventType^EventType|1|REVIEWED\r"
              + "OBX|3|CN|Technologist^Technologist|1|1435^Duck^Donald\r"
              + "OBX|4|TX|ExamRoom^ExamRoom|1|100AcreWoods\r"
              + "OBX|5|TS|StartDateTime^StartDateTime|1|20111009215317\r"
              + "OBX|6|TS|StopDateTime^StopDateTime|1|20111009215817\r"
              + "OBX|7|TX|ImagesAvailable^ImagesAvailable|1|1\r"
              + "ZDS|2.16.840.1.113883.3.234.1.3.101.1.2.1013.2011.15607503.2^HMI^Application^DICOM\r";

      Thread.sleep(2000); // give the simple server time to start

      Socket socket = new Socket("127.0.0.1", 6665);

      PrintStream writer = new PrintStream(socket.getOutputStream());

      // send the message twice
      for (int i = 0; i < 2; i++) {
        writer.print(header);
        writer.print(message);
        writer.print(trailer + "\r");
        writer.flush();
      }

      Thread.sleep(2000);

      // confirm that only one encounter has been created
      encounters =
          encounterService.getEncounters(
              patient,
              null,
              null,
              null,
              null,
              Collections.singletonList(radiologyProperties.getRadiologyStudyEncounterType()),
              null,
              null,
              null,
              false);
      assertThat(encounters.size(), is(1));

    } finally {
      activator.stopped();
    }
  }
  @Test
  public void shouldCreateTwoReportsIfBodyDifferent() throws Exception {

    ModuleActivator activator = new PacsIntegrationActivator();
    activator.started();
    Context.getService(PacsIntegrationService.class).initializeHL7Listener();

    List<Patient> patients =
        patientService.getPatients(
            null,
            "101-6",
            Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()),
            true);
    assertThat(patients.size(), is(1)); // sanity check
    Patient patient = patients.get(0);
    List<Encounter> encounters =
        encounterService.getEncounters(
            patient,
            null,
            null,
            null,
            null,
            Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()),
            null,
            null,
            null,
            false);
    assertThat(encounters.size(), is(0)); // sanity check

    try {
      String message1 =
          "MSH|^~\\&|HMI|Mirebalais Hospital|RAD|REPORTS|20130228174549||ORU^R01|RTS01CE16055AAF5290|P|2.3|\r"
              + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r"
              + "PV1|1||||||||||||||||||\r"
              + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170556||||||||||||MBL^CR||||||F|||||||Test&Goodrich&Mark&&&&||||20130228170556\r"
              + "OBX|1|TX|127689^SOME_X-RAY||Clinical Indication: ||||||F\r";

      String message2 =
          "MSH|^~\\&|HMI|Mirebalais Hospital|RAD|REPORTS|20130228174549||ORU^R01|RTS01CE16055AAF5290|P|2.3|\r"
              + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r"
              + "PV1|1||||||||||||||||||\r"
              + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170556||||||||||||MBL^CR||||||F|||||||Test&Goodrich&Mark&&&&||||20130228170556\r"
              + "OBX|1|TX|127689^SOME_X-RAY||Another Clinical Indication: ||||||F\r";

      Thread.sleep(2000); // give the simple server time to start

      Socket socket = new Socket("127.0.0.1", 6665);

      PrintStream writer = new PrintStream(socket.getOutputStream());

      writer.print(header);
      writer.print(message1);
      writer.print(trailer + "\r");
      writer.flush();

      writer.print(header);
      writer.print(message2);
      writer.print(trailer + "\r");
      writer.flush();

      Thread.sleep(2000);

      encounters =
          encounterService.getEncounters(
              patient,
              null,
              null,
              null,
              null,
              Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()),
              null,
              null,
              null,
              false);
      assertThat(encounters.size(), is(2));

    } finally {
      activator.stopped();
    }
  }
  @Test
  public void shouldListenForAndParseORM_001Message() throws Exception {

    ModuleActivator activator = new PacsIntegrationActivator();
    activator.started();
    Context.getService(PacsIntegrationService.class).initializeHL7Listener();

    List<Patient> patients =
        patientService.getPatients(
            null,
            "101-6",
            Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()),
            true);
    assertThat(patients.size(), is(1)); // sanity check
    Patient patient = patients.get(0);
    List<Encounter> encounters =
        encounterService.getEncounters(
            patient,
            null,
            null,
            null,
            null,
            Collections.singletonList(radiologyProperties.getRadiologyStudyEncounterType()),
            null,
            null,
            null,
            false);
    assertThat(encounters.size(), is(0)); // sanity check

    try {

      String message =
          "MSH|^~\\&|HMI||RAD|REPORTS|20130228174643||ORM^O01|RTS01CE16057B105AC0|P|2.3|\r"
              + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r"
              + "ORC|\r"
              + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170350||||||||||||MBL^CR||||||P|||||||&Goodrich&Mark&&&&^||||20130228170350\r"
              + "OBX|1|RP|||||||||F\r"
              + "OBX|2|TX|EventType^EventType|1|REVIEWED\r"
              + "OBX|3|CN|Technologist^Technologist|1|1435^Duck^Donald\r"
              + "OBX|4|TX|ExamRoom^ExamRoom|1|100AcreWoods\r"
              + "OBX|5|TS|StartDateTime^StartDateTime|1|20111009215317\r"
              + "OBX|6|TS|StopDateTime^StopDateTime|1|20111009215817\r"
              + "OBX|7|TX|ImagesAvailable^ImagesAvailable|1|1\r"
              + "ZDS|2.16.840.1.113883.3.234.1.3.101.1.2.1013.2011.15607503.2^HMI^Application^DICOM\r";

      Thread.sleep(2000); // give the simple server time to start

      Socket socket = new Socket("127.0.0.1", 6665);

      PrintStream writer = new PrintStream(socket.getOutputStream());
      BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      writer.print(header);
      writer.print(message);
      writer.print(trailer + "\r");
      writer.flush();

      Thread.sleep(2000);

      // confirm that study encounter has been created and has obs (we more thoroughly test the
      // handler in the ORU_R01 handler and Radiology Service (in emr module) tests)
      encounters =
          encounterService.getEncounters(
              patient,
              null,
              null,
              null,
              null,
              Collections.singletonList(radiologyProperties.getRadiologyStudyEncounterType()),
              null,
              null,
              null,
              false);
      assertThat(encounters.size(), is(1));
      assertThat(encounters.get(0).getObs().size(), is(3));

      // confirm that the proper ack is sent out
      String response = reader.readLine();
      assertThat(response, containsString("|ACK|"));
    } finally {
      activator.stopped();
    }
  }