// Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_043: [The function shall create a Binary (Proton)
  // object from the content array.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_044: [The function shall create a data Section
  // (Proton) object from the Binary.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_045: [The function shall set the Message body to
  // the created data Section.]
  @Test
  public void createBinaryMessageSetsBodyFromContent() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final byte[] msgBody = {0x61, 0x62, 0x63};
    final Object messageId = "123";

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    Deencapsulation.setField(handler, "sender", mockSender);

    handler.createBinaryMessage(msgBody, messageId);

    new Verifications() {
      {
        mockBinary = new Binary(msgBody);
        mockSection = new Data(mockBinary);
        Message m = Deencapsulation.getField(handler, "outgoingMessage");
        m.setBody(mockSection);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_026: [The event handler shall get the Connection
  // (Proton) object from the event handler and set the host name on the connection.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_027: [The event handler shall create a Session
  // (Proton) object from the connection.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_028: [The event handler shall create a Receiver
  // and Sender (Proton) object and set the protocol tag on them to a predefined constant.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_029: [The event handler shall open the
  // Connection, Session, and Receiver objects.]
  @Test
  public void onConnectionInitCreatesSession() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String tag = "receiver";

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onConnectionInit(mockEvent);

    final String expectedHostName = String.format("%s:%d", hostName, PORT);

    new Verifications() {
      {
        mockEvent.getConnection();
        mockConnection.setHostname(expectedHostName);
        mockConnection.session();
        mockSession.receiver(tag);
        mockConnection.open();
        mockSession.open();
        mockReceiver.open();
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_014: [If the event type is DELIVERY, the event
  // handler shall close the Sender link.]
  @Test
  public void onDeliveryClosesSender() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String sendTag = "sender";

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockSender;
        mockSender.getName();
        result = sendTag;
        mockEvent.getType();
        result = Event.Type.DELIVERY;
        mockEvent.getDelivery();
        result = mockDelivery;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    Deencapsulation.setField(handler, "sender", mockSender);

    handler.onDelivery(mockEvent);

    new Verifications() {
      {
        mockSender.close();
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_022: [If the link is the Sender link and the
  // message was sent successfully over this link, the event handler shall reset the private Sender
  // (Proton) member variable and open the AmqpsIotHubConnection for sending.]
  @Test
  public void onLinkRemoteCloseResetsSenderAndOpens(
      @Mocked final CompletableFuture<Integer> mockFuture) {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String sendTag = "sender";

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockSender;
        mockSender.getName();
        result = sendTag;
        mockFuture.isCompletedExceptionally();
        result = false;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    Deencapsulation.setField(handler, "currentSentMessageFuture", mockFuture);
    handler.onLinkRemoteClose(mockEvent);

    new Verifications() {
      {
        mockEvent.getSession().sender(sendTag);
        Deencapsulation.invoke(mockIotHubConnection, "openSending");
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_016: [If the link was locally closed before the
  // current message is sent, the sent message CompletableFuture will be completed exceptionally
  // with a new HandlerException.]
  @Test
  public void onLinkLocalCloseClosesBeforeSendCompletesExceptionally(
      @Mocked final CompletableFuture<Integer> mockFuture) {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String receiveTag = "receiver";

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockReceiver;
        mockReceiver.getName();
        result = receiveTag;
        mockFuture.isDone();
        result = false;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    Deencapsulation.setField(handler, "currentSentMessageFuture", mockFuture);
    handler.onLinkLocalClose(mockEvent);

    new Verifications() {
      {
        mockFuture.completeExceptionally((HandlerException) any);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_031: [If the link is the Sender link, the event
  // handler shall create a new Target (Proton) object using the sender endpoint address member
  // variable.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_033: [If the link is the Sender link, the event
  // handler shall get the Link (Proton) object and set its target.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_034: [If the link is the Sender link, the event
  // handler shall set the SenderSettleMode to UNSETTLED.]
  @Test
  public void onLinkInitCreatesTargetAndSetsOnLinkForSender() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String sendTag = "sender";

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockSender;
        mockSender.getName();
        result = sendTag;
        new Target();
        result = mockTarget;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onLinkInit(mockEvent);

    final String expectedEndpoint = "/devices/test-deviceId/messages/events";

    new Verifications() {
      {
        mockSource.setAddress(expectedEndpoint);
        mockSender.setTarget(mockTarget);
        mockSender.setSenderSettleMode(SenderSettleMode.UNSETTLED);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_030: [If the link is the Receiver link, the event
  // handler shall create a new Source (Proton) object using the receiver endpoint address member
  // variable.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_032: [If the link is the Receiver link, the event
  // handler shall get the Link (Proton) object and set its source to the created Source (Proton)
  // object.]
  @Test
  public void onLinkInitCreatesSourceAndSetsOnLinkForReceiver() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String receiveTag = "receiver";

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockReceiver;
        mockReceiver.getName();
        result = receiveTag;
        new Source();
        result = mockSource;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onLinkInit(mockEvent);

    final String expectedEndpoint = "/devices/test-deviceId/messages/devicebound";

    new Verifications() {
      {
        mockSource.setAddress(expectedEndpoint);
        mockReceiver.setSource(mockSource);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_007: [If this link is the Receiver link, the
  // event handler shall get the Receiver and Delivery (Proton) objects from the event.]
  @Test
  public void onDeliveryGetsReceiverAndDelivery() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String receiveTag = "receiver";

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockReceiver;
        mockReceiver.getName();
        result = receiveTag;
        mockReceiver.current();
        result = mockDelivery;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    handler.onDelivery(mockEvent);

    new Verifications() {
      {
        mockEvent.getLink();
        mockReceiver.current();
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_046: [The function shall open the Sender (Proton)
  // link.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_047: [The function shall return a new
  // CompletableFuture for the sent message.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_048: [The function shall lock sending on the
  // AmqpsIotHubConnection.]
  @Test
  public void createBinaryMessageOpensSenderAndLocksSending(
      @Mocked final CompletableFuture<Integer> mockFuture) {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final byte[] msgBody = {0x61, 0x62, 0x63};
    final Object messageId = "123";

    new NonStrictExpectations() {
      {
        new CompletableFuture<Integer>();
        result = mockFuture;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    Deencapsulation.setField(handler, "sender", mockSender);

    CompletableFuture<Integer> actualFuture = handler.createBinaryMessage(msgBody, messageId);

    assertEquals(CompletableFuture.class, actualFuture.getClass());

    new Verifications() {
      {
        Deencapsulation.invoke(mockIotHubConnection, "lockSending");
        mockSender.open();
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_035: [If the Connection was remotely closed
  // abnormally, the event handler shall complete the sent message CompletableFuture with a new
  // HandlerException.]
  @Test
  public void onConnectionRemoteCloseClosesAbnormallyCompletesMessageExceptionally(
      @Mocked final CompletableFuture<Integer> mockFuture) {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";

    new NonStrictExpectations() {
      {
        mockEvent.getConnection();
        result = mockConnection;
        mockConnection.getCondition();
        result = new ErrorCondition();
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    Deencapsulation.setField(handler, "currentSentMessageFuture", mockFuture);
    handler.onConnectionRemoteClose(mockEvent);

    new Verifications() {
      {
        mockFuture.completeExceptionally((HandlerException) any);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_041: [The function shall set the ‘to’ property on
  // the Message object using the created event path.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_042: [The function shall set the ‘messageId’
  // property on the Message object if the messageId is not null.]
  @Test
  public void createBinaryMessageSetsToPropertyAndMessageIDIfNotNull() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final byte[] msgBody = {0x61, 0x62, 0x63};
    final Object messageId = "123";
    final String expectedEndpoint = "/devices/test-deviceId/messages/events";

    new NonStrictExpectations() {
      {
        new Properties();
        result = mockProperties;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    Deencapsulation.setField(handler, "sender", mockSender);
    handler.createBinaryMessage(msgBody, null);
    handler.createBinaryMessage(msgBody, messageId);

    new Verifications() {
      {
        mockProperties.setTo(expectedEndpoint);
        times = 2;
        mockProperties.setMessageId(messageId);
        times = 1;
        mockMessage.setProperties(mockProperties);
        times = 2;
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_017: [The event handler shall get the Sender
  // (Proton) object from the link.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_018: [The event handler shall encode the message
  // and copy the contents to the byte buffer.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_019: [The event handler shall set the delivery
  // tag on the Sender (Proton) object.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_020: [The event handler shall send the encoded
  // bytes.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_021: [The event handler shall advance the Sender
  // and complete the sent message CompletableFuture using the Delivery (Proton) object hash code.]
  @Test
  public void onLinkFlowFullTest(@Mocked final CompletableFuture<Integer> mockFuture) {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final byte[] msgBody = {0x61, 0x62, 0x63};
    final int tag = 0;

    new NonStrictExpectations() {
      {
        mockMessage.encode(msgBody, 0, msgBody.length);
        result = 3;
        mockEvent.getLink();
        result = mockSender;
        mockSender.getCredit();
        result = 1;
        mockSender.getUnsettled();
        result = 0;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    Deencapsulation.setField(handler, "sender", mockSender);
    Deencapsulation.setField(handler, "currentSentMessageFuture", mockFuture);
    handler.createBinaryMessage(msgBody, null);
    handler.onLinkFlow(mockEvent);

    new Verifications() {
      {
        mockEvent.getLink();
        byte[] buffer = new byte[1024];
        int length = mockMessage.encode(buffer, 0, buffer.length);
        byte[] deliveryTag = String.valueOf(tag).getBytes();
        Delivery d = mockSender.delivery(deliveryTag);
        mockSender.send((byte[]) any, 0, length);

        mockSender.advance();
        mockFuture.complete(d.hashCode());
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_037: [If the Connection and Session (Proton) are
  // not opened before a constant number of seconds, this handler will inform the
  // AmqpsIotHubConnection that the Connection was not opened in time.]
  @Test(expected = ExecutionException.class)
  public void onConnectionLocalOpenTimesOut() throws ExecutionException, InterruptedException {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    Deencapsulation.setField(
        mockIotHubConnection, "reactorReady", new CompletableFuture<Boolean>());
    handler.onConnectionLocalOpen(mockEvent);
    CompletableFuture<Boolean> future =
        Deencapsulation.getField(mockIotHubConnection, "reactorReady");

    future.get();
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_025: [The event handler shall set ANONYMOUS_PEER
  // authentication mode on the domain of the Transport.]
  @Test
  public void onConnectionBoundSetsAuthenticationModeOnDomain() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onConnectionBound(mockEvent);

    new Verifications() {
      {
        mockSslDomain.setPeerAuthentication(SslDomain.VerifyMode.ANONYMOUS_PEER);
        mockTransport.ssl(mockSslDomain);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_024: [The event handler shall set the SASL_PLAIN
  // authentication on the transport using the given user name and sas token.]
  @Test
  public void onConnectionBoundSetsProperAuthenticationUsingUserNameAndSasToken() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onConnectionBound(mockEvent);

    new Verifications() {
      {
        mockSasl = mockTransport.sasl();
        mockSasl.plain(userName, sasToken);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_009: [The event handler shall create an
  // AmqpsMessage object from the decoded buffer.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_010: [The event handler shall set the
  // AmqpsMessage Deliver (Proton) object.]
  @Test
  public void onDeliveryCreatesAndSetsAmqpsMessage() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String receiveTag = "receiver";
    final int pendingDeliverySize = 10;
    final byte[] buffer = new byte[pendingDeliverySize];

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockReceiver;
        mockReceiver.getName();
        result = receiveTag;
        mockReceiver.current();
        result = mockDelivery;
        mockDelivery.isReadable();
        result = true;
        mockDelivery.isPartial();
        result = false;
        mockDelivery.pending();
        result = pendingDeliverySize;
        new AmqpsMessage();
        result = mockAmqpsMessage;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    handler.onDelivery(mockEvent);

    new Verifications() {
      {
        new AmqpsMessage();
        mockAmqpsMessage.setDelivery(mockDelivery);
        mockAmqpsMessage.decode(buffer, 0, anyInt);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_023: [The event handler shall get the Transport
  // (Proton) object from the event.]
  @Test
  public void onConnectionBoundGetsTransport() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onConnectionBound(mockEvent);

    new Verifications() {
      {
        mockConnection = mockEvent.getConnection();
        mockConnection.getTransport();
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_040: [The function shall create a new Message
  // (Proton) object.]
  @Test
  public void createBinaryMessageCreatesNewProtonMessage() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final byte[] msgBody = {0x61, 0x62, 0x63};

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    Deencapsulation.setField(handler, "sender", mockSender);
    handler.createBinaryMessage(msgBody, null);

    new Verifications() {
      {
        mockMessage = mockProton.message();
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_036: [The event handler shall inform the
  // AmqpsIotHubConnection that the Reactor (Proton) is ready.]
  @Test
  public void onSessionRemoteOpenInformsReactorReady() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final CompletableFuture<Boolean> future = new CompletableFuture<>();

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    Deencapsulation.setField(mockIotHubConnection, "reactorReady", future);
    handler.onSessionRemoteOpen(mockEvent);

    new Verifications() {
      {
        future.complete(new Boolean(true));
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_013: [If the event type is DELIVERY, the event
  // handler shall note the remote delivery state and use it and the Delivery (Proton) hash code to
  // inform the AmqpsIotHubConnection of the message receipt.]
  @Test
  public void onDeliveryAckToConnection() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String sendTag = "sender";
    final int hash = 12345;

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockSender;
        mockSender.getName();
        result = sendTag;
        mockEvent.getType();
        result = Event.Type.DELIVERY;
        mockEvent.getDelivery();
        result = mockDelivery;
        mockDelivery.getRemoteState();
        result = Accepted.getInstance();
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    Deencapsulation.setField(handler, "sender", mockSender);

    handler.onDelivery(mockEvent);

    new Verifications() {
      {
        mockEvent.getDelivery();
        mockDelivery.getRemoteState();
        Deencapsulation.invoke(mockIotHubConnection, "acknowledge", mockDelivery.hashCode(), true);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_011: [The event handler shall give the message
  // back to its parent AmqpsIotHubConnection.]
  @Test
  public void onDeliveryGivesMessageToParentAmqpsReceiver() {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String receiveTag = "receiver";
    final int pendingDeliverySize = 10;
    final byte[] buffer = new byte[pendingDeliverySize];

    new NonStrictExpectations() {
      {
        mockEvent.getLink();
        result = mockReceiver;
        mockReceiver.getName();
        result = receiveTag;
        mockReceiver.current();
        result = mockDelivery;
        mockDelivery.isReadable();
        result = true;
        mockDelivery.isPartial();
        result = false;
        new AmqpsMessage();
        result = mockAmqpsMessage;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);

    handler.onDelivery(mockEvent);

    new Verifications() {
      {
        Deencapsulation.invoke(mockIotHubConnection, "addMessage", mockAmqpsMessage);
      }
    };
  }
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_038: [The event handler shall log the error and
  // description if there is a description accompanying the error.]
  // Tests_SRS_AMQPSIOTHUBCONNECTIONBASEHANDLER_14_039: [The event handler shall cause the
  // AmqpsIotHubConnection to fail.]
  @Test
  public void onTransportErrorLogsErrorAndCausesIotHubConnectionFail(
      @Mocked ErrorCondition mockCondition) {
    final String hostName = "test.host.name";
    final String deviceId = "test-deviceId";
    final String userName = "******";
    final String sasToken = "test-token";
    final String errorString = "Mock Error";
    final String errorDescription = "Mock Description";

    new NonStrictExpectations() {
      {
        mockEvent.getTransport();
        result = mockTransport;
        mockTransport.getCondition();
        result = mockCondition;
        mockCondition.toString();
        result = errorString;
        mockCondition.getDescription();
        result = errorDescription;
      }
    };

    AmqpsIotHubConnectionBaseHandler handler =
        new AmqpsIotHubConnectionBaseHandler(
            hostName, userName, sasToken, deviceId, mockIotHubConnection);
    handler.onTransportError(mockEvent);

    new Verifications() {
      {
        System.err.println("Error: " + errorString);
        System.err.println("Error: " + errorDescription);
        Deencapsulation.invoke(mockIotHubConnection, "fail", errorString);
      }
    };
  }