|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.quarkus.main; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.io.Writer; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Properties; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import io.quarkus.test.QuarkusDevModeTest; |
| 27 | +import io.restassured.RestAssured; |
| 28 | +import io.restassured.response.Response; |
| 29 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 30 | +import org.jboss.shrinkwrap.api.asset.Asset; |
| 31 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 32 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.awaitility.Awaitility.await; |
| 38 | + |
| 39 | +class CamelMainRoutesIncludePatternWithSupportedFileExtensionsDevModeTest { |
| 40 | + static final String ROUTE_ID_BASE = "route"; |
| 41 | + static final String XML_ROUTE_ID = "xml-" + ROUTE_ID_BASE; |
| 42 | + static final String CAMEL_XML_ROUTE_ID = "camel-" + XML_ROUTE_ID; |
| 43 | + static final String YAML_ROUTE_ID = "yaml-" + ROUTE_ID_BASE; |
| 44 | + static final String CAMEL_YAML_ROUTE_ID = "camel-" + YAML_ROUTE_ID; |
| 45 | + |
| 46 | + @RegisterExtension |
| 47 | + static final QuarkusDevModeTest TEST = new QuarkusDevModeTest() |
| 48 | + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) |
| 49 | + .addClass(CamelSupportResource.class) |
| 50 | + .addAsResource(routeXml(XML_ROUTE_ID), "camel/routes.xml") |
| 51 | + .addAsResource(routeXml(CAMEL_XML_ROUTE_ID), "camel/routes.camel.xml") |
| 52 | + .addAsResource(routeYaml(YAML_ROUTE_ID), "camel/routes.yaml") |
| 53 | + .addAsResource(routeYaml(CAMEL_YAML_ROUTE_ID), "camel/routes.camel.yaml") |
| 54 | + .addAsResource(applicationProperties(), "application.properties")); |
| 55 | + |
| 56 | + public static Asset routeXml(String routeId) { |
| 57 | + String xml = """ |
| 58 | + <?xml version="1.0" encoding="UTF-8"?> |
| 59 | + <routes> |
| 60 | + <route id="%s"> |
| 61 | + <from uri="direct:%s"/> |
| 62 | + <setBody><constant>Hello World</constant></setBody> |
| 63 | + </route> |
| 64 | + </routes>""".formatted(routeId, routeId); |
| 65 | + return new StringAsset(xml); |
| 66 | + } |
| 67 | + |
| 68 | + public static Asset routeYaml(String routeId) { |
| 69 | + String yaml = """ |
| 70 | + - route: |
| 71 | + id: "%s" |
| 72 | + from: |
| 73 | + uri: "direct:%s" |
| 74 | + steps: |
| 75 | + - setBody: |
| 76 | + constant: "Hello World" |
| 77 | + """.formatted(routeId, routeId); |
| 78 | + return new StringAsset(yaml); |
| 79 | + } |
| 80 | + |
| 81 | + public static Asset applicationProperties() { |
| 82 | + Writer writer = new StringWriter(); |
| 83 | + |
| 84 | + Properties props = new Properties(); |
| 85 | + props.setProperty("quarkus.banner.enabled", "false"); |
| 86 | + |
| 87 | + try { |
| 88 | + props.store(writer, ""); |
| 89 | + } catch (IOException e) { |
| 90 | + throw new RuntimeException(e); |
| 91 | + } |
| 92 | + |
| 93 | + return new StringAsset(writer.toString()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void routesDiscoveryWithSupportedFileExtensions() { |
| 98 | + await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { |
| 99 | + Response res = RestAssured.when().get("/test/describe").thenReturn(); |
| 100 | + |
| 101 | + assertThat(res.statusCode()).isEqualTo(200); |
| 102 | + assertThat(res.body().jsonPath().getList("routes", String.class)).containsOnly(XML_ROUTE_ID, CAMEL_XML_ROUTE_ID, |
| 103 | + YAML_ROUTE_ID, CAMEL_YAML_ROUTE_ID); |
| 104 | + }); |
| 105 | + |
| 106 | + Map<String, String> fileToRouteIdMappings = Map.of("camel/routes.xml", XML_ROUTE_ID, |
| 107 | + "camel/routes.camel.xml", CAMEL_XML_ROUTE_ID, |
| 108 | + "camel/routes.yaml", YAML_ROUTE_ID, |
| 109 | + "camel/routes.camel.yaml", CAMEL_YAML_ROUTE_ID); |
| 110 | + |
| 111 | + fileToRouteIdMappings.forEach((path, routeId) -> { |
| 112 | + TEST.modifyResourceFile(path, route -> route.replaceAll(routeId, routeId + "-updated")); |
| 113 | + |
| 114 | + await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { |
| 115 | + Response res = RestAssured.when().get("/test/describe").thenReturn(); |
| 116 | + |
| 117 | + assertThat(res.statusCode()).isEqualTo(200); |
| 118 | + assertThat(res.body().jsonPath().getList("routes", String.class)).contains(routeId + "-updated"); |
| 119 | + }); |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
0 commit comments