public CheckPolicyRequestType transformEntityAlertToCheckPolicy(
      RespondingGatewaySendAlertMessageType message, String target) {

    CheckPolicyRequestType result = new CheckPolicyRequestType();
    if (message == null) {
      log.error("Request is null.");
      return result;
    }
    if (target == null || target.isEmpty()) {
      log.error("target is missing");
      return result;
    }

    if (message.getEDXLDistribution() == null) {
      log.error("missing body");
      return result;
    }
    if (message.getAssertion() == null) {
      log.error("missing assertion");
      return result;
    }
    if (message.getAssertion().getHomeCommunity() == null) {
      log.error("missing home community");
      return result;
    }

    EDXLDistribution body = message.getEDXLDistribution();
    // RequestType request = getRequestType(patDiscReq, event.getAssertion());
    RequestType request = new RequestType();
    AttributeHelper attrHelper = new AttributeHelper();

    log.debug("transformEntityAlertToCheckPolicy - adding subject");
    SubjectHelper subjHelp = new SubjectHelper();
    // SubjectType subject = subjHelp.subjectFactory(event.getAssertion().getHomeCommunity(),
    // event.getAssertion());
    SubjectType subject = new SubjectType();
    subject.setSubjectCategory(SubjectHelper.SubjectCategory);
    log.debug("transformEntityAlertToCheckPolicy - adding subject");
    request.getSubject().add(subject);

    ResourceType resource = new ResourceType();
    resource
        .getAttribute()
        .add(
            attrHelper.attributeFactory(
                Constants.HomeCommunityAttributeId,
                Constants.DataTypeString,
                message.getAssertion().getHomeCommunity().getHomeCommunityId()));

    request.getResource().add(resource);

    log.debug("transformEntityAlertToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, message.getAssertion());

    request.setAction(ActionHelper.actionFactory(ActionOutValue));
    result.setAssertion(message.getAssertion());
    result.setRequest(request);
    return result;
  }
  public CheckPolicyRequestType transformNhinAlertToCheckPolicy(
      EDXLDistribution message, AssertionType assertion) {
    CheckPolicyRequestType result = new CheckPolicyRequestType();

    RequestType request = new RequestType();

    if (assertion == null) {
      log.error("Missing Assertion");
      return result;
    }
    if (message == null) {
      log.error("Missing message");
      return result;
    }
    log.debug("transformAdminDistributionNhincToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, assertion);

    request.setAction(ActionHelper.actionFactory(ActionInValue));

    result.setAssertion(assertion);
    result.setRequest(request);

    return result;
  }
  /**
   * Transform method to create a CheckPolicyRequest object from a 201306 message
   *
   * @param request
   * @return CheckPolicyRequestType
   */
  public CheckPolicyRequestType transformPatientDiscoveryNhincToCheckPolicy(
      PatDiscReqEventType event) {
    addDebugLog(
        "Begin -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryNhincToCheckPolicy()");
    CheckPolicyRequestType checkPolicyRequest = null;

    if (event == null) {
      addErrorLog("Request is null.");
      return checkPolicyRequest;
    } else {
      checkPolicyRequest = new CheckPolicyRequestType();
    }

    PRPAIN201306UV02 patDiscReq = event.getPRPAIN201306UV02();
    RequestType request = new RequestType();

    SubjectHelper subjHelp = new SubjectHelper();
    SubjectType subject =
        subjHelp.subjectFactory(event.getSendingHomeCommunity(), event.getAssertion());
    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding subject");
    request.getSubject().add(subject);

    II qualifiedPatientIdentifier = extractPatientIdentifier(patDiscReq);
    if (qualifiedPatientIdentifier != null) {
      ResourceType resource = new ResourceType();
      AttributeHelper attrHelper = new AttributeHelper();
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientAssigningAuthorityAttributeId,
                  Constants.DataTypeString,
                  qualifiedPatientIdentifier.getRoot()));
      String sStrippedPatientId =
          PatientIdFormatUtil.parsePatientId(qualifiedPatientIdentifier.getExtension());
      log.debug(
          "transformPatientDiscoveryNhincToCheckPolicy: sStrippedPatientId = "
              + sStrippedPatientId);
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientIdAttributeId, Constants.DataTypeString, sStrippedPatientId));

      request.getResource().add(resource);
    }

    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, event.getAssertion());

    request.setAction(ActionHelper.actionFactory(ActionInValue));

    checkPolicyRequest.setRequest(request);
    checkPolicyRequest.setAssertion(event.getAssertion());
    addDebugLog(
        "End -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryNhincToCheckPolicy()");
    return checkPolicyRequest;
  }
  @Test
  public void testCheckPolicyHappy() {
    try {
      AdapterComponentPolicyEngineImpl sut =
          new AdapterComponentPolicyEngineImpl() {
            @Override
            protected Log createLogger() {
              return mockLog;
            }

            @Override
            protected AdapterPolicyEngineOrchestratorImpl getAdapterPolicyEngineOrchestratorImpl() {
              return mockAdapterPolicyEngineOrchestratorImpl;
            }

            @Override
            protected void loadAssertion(AssertionType assertion, WebServiceContext wsContext)
                throws Exception {}
          };
      context.checking(
          new Expectations() {
            {
              allowing(mockLog).debug(with(aNonNull(String.class)));
              oneOf(mockAdapterPolicyEngineOrchestratorImpl)
                  .checkPolicy(
                      with(aNonNull(CheckPolicyRequestType.class)),
                      with(aNonNull(AssertionType.class)));
            }
          });

      CheckPolicyRequestType request = new CheckPolicyRequestType();
      AssertionType assertion = new AssertionType();
      request.setAssertion(assertion);
      CheckPolicyResponseType response = sut.checkPolicy(request, mockWebServiceContext);
      assertNotNull("CheckPolicyResponseType was null", response);
    } catch (Throwable t) {
      System.out.println("Error running testCheckPolicyHappy: " + t.getMessage());
      t.printStackTrace();
      fail("Error running testCheckPolicyHappy: " + t.getMessage());
    }
  }
  /**
   * Transform method to create a CheckPolicyRequest object from a 201305 request
   *
   * @param request
   * @return CheckPolicyRequestType
   */
  public CheckPolicyRequestType transformPatientDiscoveryEntityToCheckPolicy(
      RespondingGatewayPRPAIN201305UV02RequestType event) {
    addDebugLog(
        "Begin -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryEntityToCheckPolicy()");

    CheckPolicyRequestType checkPolicyRequest = null;

    if (event == null) {
      addErrorLog("Request is null.");
      return checkPolicyRequest;
    } else {
      checkPolicyRequest = new CheckPolicyRequestType();
    }

    PRPAIN201305UV02 patDiscReq = event.getPRPAIN201305UV02();
    // RequestType request = getRequestType(patDiscReq, event.getAssertion());
    RequestType request = new RequestType();

    SubjectHelper subjHelp = new SubjectHelper();
    // SubjectType subject = subjHelp.subjectFactory(event.getAssertion().getHomeCommunity(),
    // event.getAssertion());
    SubjectType subject = new SubjectType();
    subject.setSubjectCategory(SubjectHelper.SubjectCategory);
    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding subject");
    request.getSubject().add(subject);

    II qualifiedPatientIdentifier = extractPatientIdentifier(patDiscReq);
    if (qualifiedPatientIdentifier != null) {
      ResourceType resource = new ResourceType();
      AttributeHelper attrHelper = new AttributeHelper();
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientAssigningAuthorityAttributeId,
                  Constants.DataTypeString,
                  qualifiedPatientIdentifier.getRoot()));
      String sStrippedPatientId =
          PatientIdFormatUtil.parsePatientId(qualifiedPatientIdentifier.getExtension());
      log.debug(
          "transformPatientDiscoveryNhincToCheckPolicy: sStrippedPatientId = "
              + sStrippedPatientId);
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  PatientIdAttributeId, Constants.DataTypeString, sStrippedPatientId));

      HomeCommunityType homeCommunityId = null;

      if (event != null
          && event.getPRPAIN201305UV02() != null
          && NullChecker.isNotNullish(event.getPRPAIN201305UV02().getReceiver())
          && event.getPRPAIN201305UV02().getReceiver().get(0) != null
          && event.getPRPAIN201305UV02().getReceiver().get(0).getDevice() != null
          && event.getPRPAIN201305UV02().getReceiver().get(0).getDevice().getAsAgent() != null
          && event.getPRPAIN201305UV02().getReceiver().get(0).getDevice().getAsAgent().getValue()
              != null
          && event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
              != null
          && event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
              != null
          && NullChecker.isNotNullish(
              event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
                  .getId())
          && event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
                  .getId()
                  .get(0)
              != null
          && NullChecker.isNotNullish(
              event
                  .getPRPAIN201305UV02()
                  .getReceiver()
                  .get(0)
                  .getDevice()
                  .getAsAgent()
                  .getValue()
                  .getRepresentedOrganization()
                  .getValue()
                  .getId()
                  .get(0)
                  .getRoot())) {
        homeCommunityId = new HomeCommunityType();
        homeCommunityId.setHomeCommunityId(
            event
                .getPRPAIN201305UV02()
                .getReceiver()
                .get(0)
                .getDevice()
                .getAsAgent()
                .getValue()
                .getRepresentedOrganization()
                .getValue()
                .getId()
                .get(0)
                .getRoot());
      }
      resource
          .getAttribute()
          .add(
              attrHelper.attributeFactory(
                  Constants.HomeCommunityAttributeId,
                  Constants.DataTypeString,
                  subjHelp.determineSendingHomeCommunityId(homeCommunityId, event.getAssertion())));

      request.getResource().add(resource);
    }

    log.debug("transformPatientDiscoveryNhincToCheckPolicy - adding assertion data");
    AssertionHelper assertHelp = new AssertionHelper();
    assertHelp.appendAssertionDataToRequest(request, event.getAssertion());

    request.setAction(ActionHelper.actionFactory(ActionOutValue));

    checkPolicyRequest.setRequest(request);
    checkPolicyRequest.setAssertion(event.getAssertion());

    addDebugLog(
        "End -- PatientDiscoveryPolicyTransformHelper.transformPatientDiscoveryEntityToCheckPolicy()");
    return checkPolicyRequest;
  }