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

import org.apache.camel.impl.DefaultCamelContext;
import org.junit.jupiter.api.Test;

import static org.apache.camel.util.CollectionHelper.mapOf;
import static org.assertj.core.api.Assertions.assertThat;

public class RouteTemplateErrorHandlerTest {
    @Test
    public void testDefineRouteTemplate() throws Exception {
        try (DefaultCamelContext context = new DefaultCamelContext()) {
            context.getRegistry().bind("myErrorHandler", new NoErrorHandlerBuilder());
            context.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    errorHandler("myErrorHandler");

                    routeTemplate("myTemplate")
                            .templateParameter("foo")
                            .templateParameter("bar")
                            .from("direct:{{foo}}")
                            .to("mock:{{bar}}");

                    from("direct:_r")
                            .to("mock:_r");
                }
            });

            assertThat(context.getRouteDefinitions()).first().satisfies(d -> {
                assertThat(d.getErrorHandlerFactory()).isInstanceOfSatisfying(ErrorHandlerBuilderRef.class, r -> {
                    assertThat(r.getRef()).isEqualTo("myErrorHandler");
                });
            });

            assertThat(context.getRouteTemplateDefinitions()).first().satisfies(d -> {
                assertThat(d.route().getErrorHandlerFactory()).isInstanceOfSatisfying(ErrorHandlerBuilderRef.class, r -> {
                    assertThat(r.getRef()).isEqualTo("myErrorHandler");
                });
            });

            context.addRouteFromTemplate("myTemplate", "", mapOf("", ""));
        }
    }
}

@@ -831,16 +831,49 @@
 ilder;%0A%0A
+import org.apache.camel.Channel;%0A
 import o
@@ -929,32 +929,203 @@
 org.
-junit.jupiter.api.Test;%0A
+apache.camel.impl.engine.DefaultRoute;%0Aimport org.apache.camel.processor.errorhandler.NoErrorHandler;%0Aimport org.junit.jupiter.api.Test;%0A%0Aimport static org.apache.camel.TestSupport.unwrapChannel;
 %0Aimp
@@ -2258,33 +2258,33 @@
 ilderRef.class, 
-r
+h
  -%3E %7B%0A          
@@ -2296,33 +2296,33 @@
      assertThat(
-r
+h
 .getRef()).isEqu
@@ -2585,17 +2585,17 @@
 .class, 
-r
+h
  -%3E %7B%0A  
@@ -2623,17 +2623,17 @@
 ertThat(
-r
+h
 .getRef(
@@ -2734,32 +2734,40 @@
 uteFromTemplate(
+%22myId%22, 
 %22myTemplate%22, %22%22
@@ -2767,20 +2767,16 @@
 te%22,
- %22%22,
  mapOf(%22
 %22, %22
@@ -2775,14 +2775,457 @@
 Of(%22
+foo
 %22, %22
-%22)
+f%22, %22bar%22, %22b%22));%0A            context.start();%0A%0A            assertThat(context.getRoutes()).allSatisfy(r -%3E %7B%0A                assertThat(r).isInstanceOfSatisfying(DefaultRoute.class, dr -%3E %7B%0A                    Channel channel = unwrapChannel(r.getProcessor());%0A                    assertThat(channel).isNotNull();%0A                    assertThat(channel.getErrorHandler()).isInstanceOf(NoErrorHandler.class);%0A                %7D);%0A            %7D
 );%0A 

Fix failing test