package com.pronoia.camel.builder.mllp;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class MllpToFileRouteBuilderTest extends CamelTestSupport {
  static final String TEST_MESSAGE =
      "MSH|^~\\&|ADT|EPIC|JCAPS|CC|20150107161440|RISTECH|ADT^A08|28919|D|2.3^^|||||||" + '\r' +
          "EVN|A08|20150107161440||REG_UPDATE_SEND_VISIT_MESSAGES_ON_PATIENT_CHANGES|RISTECH^RADIOLOGY^TECHNOLOGIST^^^^^^UCLA^^^^^RRMC||" + '\r' +
          "PID|1|2100355^^^MRN^MRN|2100355^^^MRN^MRN||MDCLS9^MC9||19700109|F||U|111 HOVER STREET^^LOS ANGELES^CA^90032^USA^P^^LOS ANGELE|LOS ANGELE|(310)725-6952^P^PH^^^310^7256952||ENGLISH|U||60000013647|565-33-2222|||U||||||||N||" + '\r' +
          "PD1|||UCLA HEALTH SYSTEM^^10|10002116^ADAMS^JOHN^D^^^^^EPIC^^^^PROVID||||||||||||||" + '\r' +
          "NK1|1|DOE^MC9^^|OTH|^^^^^USA|(310)888-9999^^^^^310^8889999|(310)999-2222^^^^^310^9992222|Emergency Contact 1|||||||||||||||||||||||||||" + '\r' +
          "PV1|1|OUTPATIENT|RR CT^^^1000^^^^^^^DEPID|EL|||017511^TOBIAS^JONATHAN^^^^^^EPIC^^^^PROVID|017511^TOBIAS^JONATHAN^^^^^^EPIC^^^^PROVID||||||CLR|||||60000013647|SELF|||||||||||||||||||||HOV_CONF|^^^1000^^^^^^^||20150107161438||||||||||" + '\r' +
          "PV2||||||||20150107161438||||CT BRAIN W WO CONTRAST||||||||||N|||||||||||||||||||||||||||" + '\r' +
          "ZPV||||||||||||20150107161438|||||||||" + '\r' +
          "AL1|1||33361^NO KNOWN ALLERGIES^^NOTCOMPUTRITION^NO KNOWN ALLERGIES^EXTELG||||||" + '\r' +
          "DG1|1|DX|784.0^Headache^DX|Headache||VISIT" + '\r' +
          "GT1|1|1000235129|MDCLS9^MC9^^||111 HOVER STREET^^LOS ANGELES^CA^90032^USA^^^LOS ANGELE|(310)725-6952^^^^^310^7256952||19700109|F|P/F|SLF|565-33-2222|||||^^^^^USA|||UNKNOWN|||||||||||||||||||||||||||||" + '\r' +
          "UB2||||||||" + '\r' +
          '\r' + '\n';
  String mllpDestinationHost = "0.0.0.0";
  int mllpDestinationPort = AvailablePortFinder.getNextAvailable();
  @EndpointInject(uri = "mock://file:target/out")
  MockEndpoint target;

  @Override
  public String isMockEndpoints() {
    return "file://*";
  }

  @Override
  protected CamelContext createCamelContext() throws Exception {
    DefaultCamelContext context = (DefaultCamelContext) super.createCamelContext();

    context.setUseMDCLogging(true);
    context.setName(this.getClass().getSimpleName());

    return context;
  }

  @Override
  protected RouteBuilder createRouteBuilder() {
    MllpToFileRouteBuilder builder = new MllpToFileRouteBuilder();

    builder.setMllpDestinationHost(mllpDestinationHost);
    builder.setMllpDestinationPort(mllpDestinationPort);

    builder.setFileTargetDirectory("target/out");

    return builder;
  }

  @Test
  public void testRouteBuilder() throws Exception {
    int messageDelay = 1;
    int messageCount = 1000;

    target.setAssertPeriod(1000);
    target.setExpectedMessageCount(messageCount);

    for (int i = 1; i <= messageCount; ++i) {
      template.sendBody(String.format("mllp://%s:%d", mllpDestinationHost, mllpDestinationPort), TEST_MESSAGE);
      Thread.sleep(messageDelay);
    }

    assertMockEndpointsSatisfied();
  }
}