|
| 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.variables.it; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import jakarta.enterprise.context.ApplicationScoped; |
| 22 | +import jakarta.inject.Inject; |
| 23 | +import jakarta.ws.rs.POST; |
| 24 | +import jakarta.ws.rs.Path; |
| 25 | +import jakarta.ws.rs.PathParam; |
| 26 | +import jakarta.ws.rs.core.Response; |
| 27 | +import org.apache.camel.CamelContext; |
| 28 | +import org.apache.camel.Exchange; |
| 29 | +import org.apache.camel.ProducerTemplate; |
| 30 | +import org.apache.camel.component.mock.MockEndpoint; |
| 31 | + |
| 32 | +@Path("/variables") |
| 33 | +@ApplicationScoped |
| 34 | +public class VariablesResource { |
| 35 | + |
| 36 | + @Inject |
| 37 | + ProducerTemplate producerTemplate; |
| 38 | + |
| 39 | + @Inject |
| 40 | + CamelContext context; |
| 41 | + |
| 42 | + @Path("/setLocalVariable") |
| 43 | + @POST |
| 44 | + public String setLocalVariable(String body) throws Exception { |
| 45 | + MockEndpoint end = context.getEndpoint("mock:setLocalVariableEnd", MockEndpoint.class); |
| 46 | + end.expectedMessageCount(1); |
| 47 | + end.expectedVariableReceived(VariablesRoutes.VARIABLE_NAME, VariablesRoutes.VARIABLE_VALUE); |
| 48 | + |
| 49 | + producerTemplate.requestBody("direct:setLocalVariableStart", body, String.class); |
| 50 | + |
| 51 | + // make sure we got the message |
| 52 | + end.assertIsSatisfied(); |
| 53 | + |
| 54 | + // lets get the variable value |
| 55 | + List<Exchange> exchanges = end.getExchanges(); |
| 56 | + Exchange exchange = exchanges.get(0); |
| 57 | + |
| 58 | + return exchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class); |
| 59 | + } |
| 60 | + |
| 61 | + @Path("/setGlobalVariable") |
| 62 | + @POST |
| 63 | + public Response setGlobalVariable(String body) throws Exception { |
| 64 | + if (context.getVariable(VariablesRoutes.VARIABLE_NAME) != null) { |
| 65 | + return Response.status(500).entity(String.format("Variable '%s' has to be null before sending message to the rout.", |
| 66 | + VariablesRoutes.VARIABLE_VALUE)).build(); |
| 67 | + } |
| 68 | + |
| 69 | + MockEndpoint end = context.getEndpoint("mock:setGlobalVariableEnd", MockEndpoint.class); |
| 70 | + end.expectedMessageCount(1); |
| 71 | + |
| 72 | + producerTemplate.requestBody("direct:setGlobalVariableStart", body, String.class); |
| 73 | + |
| 74 | + // make sure we got the message |
| 75 | + end.assertIsSatisfied(); |
| 76 | + |
| 77 | + // lets get the variable value |
| 78 | + List<Exchange> exchanges = end.getExchanges(); |
| 79 | + Exchange exchange = exchanges.get(0); |
| 80 | + |
| 81 | + String resp = exchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class) + "," |
| 82 | + + context.getVariable(VariablesRoutes.VARIABLE_NAME, String.class); |
| 83 | + return Response.ok().entity(resp).build(); |
| 84 | + } |
| 85 | + |
| 86 | + @Path("/removeLocalVariable") |
| 87 | + @POST |
| 88 | + public String removeLocalVariable(String body) throws Exception { |
| 89 | + MockEndpoint end = context.getEndpoint("mock:removeLocalVariableEnd", MockEndpoint.class); |
| 90 | + end.expectedMessageCount(1); |
| 91 | + |
| 92 | + MockEndpoint mid = context.getEndpoint("mock:removeLocalVariableMid", MockEndpoint.class); |
| 93 | + mid.expectedMessageCount(1); |
| 94 | + |
| 95 | + producerTemplate.requestBody("direct:removeLocalVariableStart", body, String.class); |
| 96 | + |
| 97 | + // make sure we got the message |
| 98 | + end.assertIsSatisfied(); |
| 99 | + mid.assertIsSatisfied(); |
| 100 | + |
| 101 | + // lets get the variable value |
| 102 | + List<Exchange> exchanges = end.getExchanges(); |
| 103 | + Exchange endExchange = exchanges.get(0); |
| 104 | + exchanges = mid.getExchanges(); |
| 105 | + Exchange midExchange = exchanges.get(0); |
| 106 | + |
| 107 | + return midExchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class) + "," |
| 108 | + + endExchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class); |
| 109 | + } |
| 110 | + |
| 111 | + @Path("/removeGlobalVariable") |
| 112 | + @POST |
| 113 | + public String removeGlobalVariable(String body) throws Exception { |
| 114 | + MockEndpoint mid = context.getEndpoint("mock:removeGlobalVariableMid", MockEndpoint.class); |
| 115 | + mid.expectedMessageCount(1); |
| 116 | + |
| 117 | + producerTemplate.requestBody("direct:removeGlobalVariableStart", body, String.class); |
| 118 | + |
| 119 | + // make sure we got the message |
| 120 | + mid.assertIsSatisfied(); |
| 121 | + |
| 122 | + // lets get the variable value |
| 123 | + List<Exchange> exchanges = mid.getExchanges(); |
| 124 | + Exchange midExchange = exchanges.get(0); |
| 125 | + |
| 126 | + return midExchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class) + "," |
| 127 | + + context.getVariable(VariablesRoutes.VARIABLE_NAME, String.class); |
| 128 | + } |
| 129 | + |
| 130 | + @Path("/customGlobalRepository") |
| 131 | + @POST |
| 132 | + public Response customGlobalRepository(String body) throws Exception { |
| 133 | + context.getRouteController().startRoute("customGlobalRepository"); |
| 134 | + |
| 135 | + MockEndpoint end = context.getEndpoint("mock:setGlobalCustomEnd", MockEndpoint.class); |
| 136 | + end.expectedMessageCount(1); |
| 137 | + |
| 138 | + producerTemplate.requestBody("direct:setGlobalCustomStart", body, String.class); |
| 139 | + |
| 140 | + // make sure we got the message |
| 141 | + end.assertIsSatisfied(); |
| 142 | + |
| 143 | + // lets get the variable value |
| 144 | + List<Exchange> exchanges = end.getExchanges(); |
| 145 | + Exchange exchange = exchanges.get(0); |
| 146 | + |
| 147 | + String resp = exchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class) + "," |
| 148 | + + context.getVariable(VariablesRoutes.VARIABLE_NAME, String.class); |
| 149 | + return Response.ok().entity(resp).build(); |
| 150 | + } |
| 151 | + |
| 152 | + @Path("/convert") |
| 153 | + @POST |
| 154 | + public String convert(String body) throws Exception { |
| 155 | + MockEndpoint end = context.getEndpoint("mock:convertEnd", MockEndpoint.class); |
| 156 | + end.expectedMessageCount(1); |
| 157 | + |
| 158 | + producerTemplate.requestBody("direct:convertStart", body, String.class); |
| 159 | + |
| 160 | + // make sure we got the message |
| 161 | + end.assertIsSatisfied(); |
| 162 | + |
| 163 | + List<Exchange> exchanges = end.getExchanges(); |
| 164 | + Exchange exchange = exchanges.get(0); |
| 165 | + |
| 166 | + return exchange.getVariable(VariablesRoutes.VARIABLE_NAME, String.class); |
| 167 | + } |
| 168 | + |
| 169 | + @Path("/filter/{city}") |
| 170 | + @POST |
| 171 | + public Response filter(String body, @PathParam("city") String city) throws Exception { |
| 172 | + MockEndpoint end = context.getEndpoint("mock:filterEnd", MockEndpoint.class); |
| 173 | + end.expectedMessageCount(1); |
| 174 | + |
| 175 | + producerTemplate.requestBodyAndHeader("direct:filterStart", body, "city", city, String.class); |
| 176 | + |
| 177 | + try { |
| 178 | + Exchange exhange = end.assertExchangeReceived(0); |
| 179 | + return Response.ok().entity(exhange.getIn().getBody(String.class)).build(); |
| 180 | + } catch (AssertionError e) { |
| 181 | + return Response.status(204).build(); |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments