@Before
 public void init() throws Exception {
   this.factory = new EventFactory(mockPrincipalProvider);
   this.principal = TestUtil.createOwnerPrincipal();
   eventFilter = new EventFilter(new CandlepinCommonTestConfig());
   when(mockPrincipalProvider.get()).thenReturn(this.principal);
   when(mockSessionFactory.createTransactedSession()).thenReturn(mockClientSession);
   when(mockClientSession.createProducer(anyString())).thenReturn(mockClientProducer);
   when(mockClientSession.createMessage(anyBoolean())).thenReturn(mockClientMessage);
   when(mockClientMessage.getBodyBuffer()).thenReturn(HornetQBuffers.fixedBuffer(2000));
   when(mockSessionFactory.getServerLocator()).thenReturn(mockLocator);
   this.mapper = spy(new ObjectMapper());
   this.eventSinkImpl = createEventSink(mockSessionFactory);
   o = new Owner("test owner");
 }
    public boolean deliver() throws Exception {
      lockDelivery.readLock().lock();
      try {
        if (largeMessage == null) {
          return true;
        }

        if (availableCredits != null && availableCredits.get() <= 0) {
          if (HornetQServerLogger.LOGGER.isTraceEnabled()) {
            HornetQServerLogger.LOGGER.trace(
                this
                    + "::FlowControl::delivery largeMessage interrupting as there are no more credits, available="
                    + availableCredits);
          }

          return false;
        }

        if (!sentInitialPacket) {
          context = largeMessage.getBodyEncoder();

          sizePendingLargeMessage = context.getLargeBodySize();

          context.open();

          sentInitialPacket = true;

          int packetSize =
              callback.sendLargeMessage(
                  largeMessage, id, context.getLargeBodySize(), ref.getDeliveryCount());

          if (availableCredits != null) {
            availableCredits.addAndGet(-packetSize);

            if (HornetQServerLogger.LOGGER.isTraceEnabled()) {
              HornetQServerLogger.LOGGER.trace(
                  this
                      + "::FlowControl::"
                      + " deliver initialpackage with "
                      + packetSize
                      + " delivered, available now = "
                      + availableCredits);
            }
          }

          // Execute the rest of the large message on a different thread so as not to tie up the
          // delivery thread
          // for too long

          resumeLargeMessage();

          return false;
        } else {
          if (availableCredits != null && availableCredits.get() <= 0) {
            if (ServerConsumerImpl.isTrace) {
              HornetQServerLogger.LOGGER.trace(
                  this
                      + "::FlowControl::deliverLargeMessage Leaving loop of send LargeMessage because of credits, available="
                      + availableCredits);
            }

            return false;
          }

          int localChunkLen = 0;

          localChunkLen =
              (int)
                  Math.min(
                      sizePendingLargeMessage - positionPendingLargeMessage, minLargeMessageSize);

          HornetQBuffer bodyBuffer = HornetQBuffers.fixedBuffer(localChunkLen);

          context.encode(bodyBuffer, localChunkLen);

          byte[] body = bodyBuffer.toByteBuffer().array();

          int packetSize =
              callback.sendLargeMessageContinuation(
                  id,
                  body,
                  positionPendingLargeMessage + localChunkLen < sizePendingLargeMessage,
                  false);

          int chunkLen = body.length;

          if (availableCredits != null) {
            availableCredits.addAndGet(-packetSize);

            if (HornetQServerLogger.LOGGER.isTraceEnabled()) {
              HornetQServerLogger.LOGGER.trace(
                  this
                      + "::FlowControl::largeMessage deliver continuation, packetSize="
                      + packetSize
                      + " available now="
                      + availableCredits);
            }
          }

          positionPendingLargeMessage += chunkLen;

          if (positionPendingLargeMessage < sizePendingLargeMessage) {
            resumeLargeMessage();

            return false;
          }
        }

        if (ServerConsumerImpl.isTrace) {
          HornetQServerLogger.LOGGER.trace("Finished deliverLargeMessage");
        }

        finish();

        return true;
      } finally {
        lockDelivery.readLock().unlock();
      }
    }