/**
 * 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.example.reportincident;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.camel.CamelContext;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.BeforeClass;
import org.junit.Test;
import org.jvnet.mock_javamail.Mailbox;

import static org.junit.Assert.assertEquals;

/**
 * Unit test of our routes
 */
public class ReportIncidentRoutesTest {

    // should be the same address as we have in our route
    private static final String URL = "http://localhost:{{port}}/camel-example-reportincident/webservices/incident";

    protected CamelContext camel;

    @BeforeClass
    public static void setupFreePort() throws Exception {
        // find a free port number from 9100 onwards, and write that in the custom.properties file
        // which we will use for the unit tests, to avoid port number in use problems
        int port = AvailablePortFinder.getNextAvailable(9100);
        String s = "port=" + port;
        File custom = new File("target/custom.properties");
        FileOutputStream fos = new FileOutputStream(custom);
        fos.write(s.getBytes());
        fos.close();
    }

    protected void startCamel() throws Exception {
        camel = new DefaultCamelContext();

        // add properties component
        camel.addComponent("properties", new PropertiesComponent("classpath:incident.properties,file:target/custom.properties"));

        ReportIncidentRoutes routes = new ReportIncidentRoutes();
        routes.setUsingServletTransport(false);
        camel.addRoutes(routes);
        camel.start();
    }
    
    protected void stopCamel() throws Exception {
        camel.stop();
    }

    protected static ReportIncidentEndpoint createCXFClient(String url) {
        // we use CXF to create a client for us as its easier than JAXWS and works
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(ReportIncidentEndpoint.class);
        factory.setAddress(url);
        return (ReportIncidentEndpoint) factory.create();
    }

    @Test
    public void testReportIncident() throws Exception {
        // start camel
        startCamel();

        runTest();

        // stop camel
        stopCamel();
    }
    
    protected void runTest() throws Exception {
        // assert mailbox is empty before starting
        Mailbox inbox = Mailbox.get("incident@mycompany.com");
        inbox.clear();
        assertEquals("Should not have mails", 0, inbox.size());

        // create input parameter
        InputReportIncident input = new InputReportIncident();
        input.setIncidentId("123");
        input.setIncidentDate("2008-08-18");
        input.setGivenName("Claus");
        input.setFamilyName("Ibsen");
        input.setSummary("Bla");
        input.setDetails("Bla bla");
        input.setEmail("davsclaus@apache.org");
        input.setPhone("0045 2962 7576");

        // create the webservice client and send the request
        String url = camel.resolvePropertyPlaceholders(URL);
        ReportIncidentEndpoint client = createCXFClient(url);
        OutputReportIncident out = client.reportIncident(input);

        // assert we got a OK back
        assertEquals("0", out.getCode());

        // let some time pass to allow Camel to pickup the file and send it as an email
        Thread.sleep(3000);

        // assert mail box
        assertEquals("Should have got 1 mail", 1, inbox.size());
    }
}

@@ -932,32 +932,78 @@
 l.CamelContext;%0A
+import org.apache.camel.builder.RouteBuilder;%0A
 import org.apach
@@ -1080,32 +1080,32 @@
 mel.
-impl.DefaultCamelContext
+test.AvailablePortFinder
 ;%0Aim
@@ -1131,35 +1131,39 @@
 el.test.
-AvailablePortFinder
+junit4.CamelTestSupport
 ;%0Aimport
@@ -1305,54 +1305,8 @@
 x;%0A%0A
-import static org.junit.Assert.assertEquals;%0A%0A
 /**%0A
@@ -1374,16 +1374,41 @@
 tesTest 
+extends CamelTestSupport 
 %7B%0A%0A    /
@@ -1582,43 +1582,8 @@
 %22;%0A%0A
-    protected CamelContext camel;%0A%0A
     
@@ -1694,17 +1694,17 @@
 r from 9
-1
+2
 00 onwar
@@ -2126,134 +2126,141 @@
     
-protected void startCamel() throws Exception %7B%0A        camel = new DefaultCamelContext();%0A%0A        // add properties component
+@Override%0A    protected CamelContext createCamelContext() throws Exception %7B%0A        CamelContext camel = super.createCamelContext();
 %0A   
@@ -2382,24 +2382,133 @@
 perties%22));%0A
+        return camel;%0A    %7D%0A%0A    @Override%0A    protected RouteBuilder createRouteBuilder() throws Exception %7B
 %0A        Rep
@@ -2622,137 +2622,21 @@
     
-camel.addRoutes(routes);%0A        camel.start();%0A    %7D%0A    %0A    protected void stopCamel() throws Exception %7B%0A        camel.stop()
+return routes
 ;%0A  
@@ -3097,176 +3097,8 @@
 n %7B%0A
-        // start camel%0A        startCamel();%0A%0A        runTest();%0A%0A        // stop camel%0A        stopCamel();%0A    %7D%0A    %0A    protected void runTest() throws Exception %7B%0A
     
@@ -3792,20 +3792,22 @@
  url = c
-amel
+ontext
 .resolve

Use dynamic port numbers for testing examples.