/*
 * 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.camel.attachment;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Assert;
import org.junit.Test;

public class BeanMethodWithExchangeTest extends CamelTestSupport {
    
    @Test
    public void testBeanWithAnnotationAndExchangeTest() throws Exception {
        Exchange result = template.request("direct:start1", new Processor() {

            public void process(Exchange exchange) throws Exception {
                AttachmentMessage m = exchange.getIn(AttachmentMessage.class);
                m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/attachment/BeanMethodWithExchangeTest.java")));
            }
            
        });
        
        Assert.assertTrue(result.getMessage(AttachmentMessage.class).getAttachmentObjects().containsKey("attachment2"));
        Assert.assertTrue(result.getMessage(AttachmentMessage.class).getAttachments().containsKey("attachment1"));
        assertEquals("attachmentValue1", result.getMessage(AttachmentMessage.class).getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1"));
        Assert.assertFalse(result.getMessage(AttachmentMessage.class).getAttachments().containsKey("attachment"));
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from("direct:start1").process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        // remove the old attachment
                        exchange.getMessage(AttachmentMessage.class).removeAttachment("attachment");
                        // and add 2 new attachments
                        Attachment att = new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/attachment/BeanMethodWithExchangeTest.java"));
                        att.addHeader("attachmentHeader1", "attachmentValue1");
                        exchange.getMessage(AttachmentMessage.class).addAttachmentObject("attachment1", att);
                        exchange.getMessage(AttachmentMessage.class).addAttachment("attachment2", new DataHandler(new FileDataSource("src/test/org/apache/camel/support/attachments/BeanMethodWithExchangeTest.java")));
                    }
                });
            }
        };
    }

}
