/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.qpid.jms.test.testpeer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import org.apache.qpid.proton.amqp.Binary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class HeaderHandlerImpl implements HeaderHandler
{
    private static final Logger LOGGER = LoggerFactory.getLogger(HeaderHandlerImpl.class.getName());

    private final byte[] _expectedHeader;
    private final byte[] _response;
    private AmqpPeerRunnable _onCompletion;

    HeaderHandlerImpl(byte[] expectedHeader, byte[] response)
    {
       this(expectedHeader, response, null);
    }

    public HeaderHandlerImpl(byte[] header, byte[] response, AmqpPeerRunnable onCompletion)
    {
        _expectedHeader = header;
        _response = response;
        _onCompletion = onCompletion;
    }

    @Override
    public void header(byte[] header, TestAmqpPeer peer) throws AssertionError
    {
        LOGGER.debug("About to check received header {}", new Binary(header));

        assertThat("Header should match", header, equalTo(_expectedHeader));
        peer.sendHeader(_response);
        if(_onCompletion != null)
        {
            _onCompletion.run();
        }
    }

    @Override
    public String toString()
    {
        return "HeaderHandlerImpl [_expectedHeader=" + new Binary(_expectedHeader) + "]";
    }

    @Override
    public AmqpPeerRunnable getOnCompletionAction()
    {
        return _onCompletion;
    }

    @Override
    public Handler onCompletion(AmqpPeerRunnable onCompletion)
    {
        _onCompletion = onCompletion;
        return this;
    }
}
@@ -1823,24 +1823,50 @@
 (header));%0A%0A
+        try%0A        %7B%0A    
         asse
@@ -1930,16 +1930,231 @@
 ader));%0A
+        %7D%0A        catch(AssertionError ae)%0A        %7B%0A            LOGGER.error(%22Failure when verifying header%22, ae);%0A            peer.assertionFailed(ae);%0A        %7D%0A%0A        LOGGER.debug(%22Sending header response.%22);%0A
         
@@ -2177,24 +2177,25 @@
 _response);%0A
+%0A
         if(_
@@ -2218,32 +2218,32 @@
 null)%0A        %7B%0A
-
             _onC
@@ -2265,24 +2265,125 @@
 ;%0A        %7D%0A
+        else%0A        %7B%0A            LOGGER.debug(%22No onCompletion action, doing nothing.%22);%0A        %7D%0A
     %7D%0A%0A    @

update header handler to treat assertion errors in line with frame handlers