Skip to content

Commit 496516f

Browse files
JiriOndrusekjamesnetherton
authored andcommitted
Variables - global vs local setVariable test (apache#5654)
1 parent d1f970d commit 496516f

File tree

7 files changed

+509
-0
lines changed

7 files changed

+509
-0
lines changed

integration-test-groups/foundation/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<module>timer</module>
5858
<module>transformer</module>
5959
<module>type-converter</module>
60+
<module>variables</module>
6061
</modules>
6162

6263
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 io.quarkus.runtime.annotations.RegisterForReflection;
20+
import jakarta.enterprise.context.ApplicationScoped;
21+
import jakarta.enterprise.inject.Alternative;
22+
import jakarta.inject.Named;
23+
import org.apache.camel.Variable;
24+
import org.apache.camel.spi.VariableRepository;
25+
import org.apache.camel.support.service.ServiceSupport;
26+
27+
public class VariablesProducers {
28+
29+
//repository is created as alternative to be disabled by default
30+
//QuarkusTestProfile is used to enable the bean for the appropriate tests.
31+
@Named("global-variable-repository")
32+
@Alternative
33+
@ApplicationScoped
34+
public static class MyGlobalRepo extends ServiceSupport implements VariableRepository {
35+
36+
private Object value;
37+
38+
@Override
39+
public String getId() {
40+
return "myGlobal";
41+
}
42+
43+
@Override
44+
public Object getVariable(String name) {
45+
if (value != null) {
46+
return "!" + value + "!";
47+
}
48+
return null;
49+
}
50+
51+
@Override
52+
public void setVariable(String name, Object value) {
53+
this.value = value;
54+
}
55+
56+
@Override
57+
public Object removeVariable(String name) {
58+
return null;
59+
}
60+
}
61+
62+
@Named("my-bean")
63+
@ApplicationScoped
64+
@RegisterForReflection(methods = true)
65+
public static class MyBean {
66+
public boolean matches(@Variable("location") String location) {
67+
return "Medford".equals(location);
68+
}
69+
}
70+
71+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 org.apache.camel.builder.RouteBuilder;
20+
21+
public class VariablesRoutes extends RouteBuilder {
22+
23+
public static String VARIABLE_NAME = "foo";
24+
public static String VARIABLE_VALUE = "bar";
25+
26+
@Override
27+
public void configure() throws Exception {
28+
transformer().withDefaults();
29+
30+
from("direct:setLocalVariableStart")
31+
.setVariable(VARIABLE_NAME)
32+
.constant(VARIABLE_VALUE)
33+
.to("mock:setLocalVariableEnd");
34+
35+
from("direct:setGlobalVariableStart")
36+
.setVariable("global:" + VARIABLE_NAME)
37+
.constant(VARIABLE_VALUE)
38+
.to("mock:setGlobalVariableEnd");
39+
40+
from("direct:removeLocalVariableStart")
41+
.setVariable(VARIABLE_NAME)
42+
.constant(VARIABLE_VALUE)
43+
.to("mock:removeLocalVariableMid")
44+
.removeVariable(VARIABLE_NAME)
45+
.to("mock:removeLocalVariableEnd");
46+
47+
from("direct:removeGlobalVariableStart")
48+
.setVariable("global:" + VARIABLE_NAME)
49+
.constant(VARIABLE_VALUE)
50+
.to("mock:removeGlobalVariableMid")
51+
.removeVariable("global:" + VARIABLE_NAME);
52+
53+
from("direct:setGlobalCustomStart")
54+
.id("customGlobalRepository")
55+
.autoStartup(false)
56+
.setVariable("global:" + VARIABLE_NAME)
57+
.constant(VARIABLE_VALUE)
58+
.to("mock:setGlobalCustomEnd");
59+
60+
from("direct:convertStart")
61+
.setVariable(VARIABLE_NAME)
62+
.constant(11)
63+
.convertVariableTo(VARIABLE_NAME, Double.class)
64+
.to("mock:convertEnd");
65+
66+
from("direct:filterStart")
67+
.setVariable("location", header("city"))
68+
.filter().method("my-bean", "matches")
69+
.to("mock:filterEnd");
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.Collections;
20+
import java.util.Set;
21+
22+
import io.quarkus.test.junit.QuarkusTest;
23+
import io.quarkus.test.junit.QuarkusTestProfile;
24+
import io.quarkus.test.junit.TestProfile;
25+
import io.restassured.RestAssured;
26+
import io.restassured.http.ContentType;
27+
import org.hamcrest.Matchers;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* Test is executed in JVM mode only because the profile enabling customVariableRepository can not be enabled for
32+
* this class in the native (and stay disabled for the other test class).
33+
* I think that it is not necessary to create a module for this test only, because it uses code tested by other tests
34+
* and
35+
* there is no reason to believe, that it fails in native.
36+
*/
37+
@QuarkusTest
38+
@TestProfile(CustomRepositoryVariablesTest.Profile.class)
39+
class CustomRepositoryVariablesTest {
40+
41+
@Test
42+
public void testCustomGlobalRepository() {
43+
RestAssured.given()
44+
.contentType(ContentType.TEXT)
45+
.post("/variables/customGlobalRepository")
46+
.then()
47+
.statusCode(200)
48+
.body(Matchers.is("null,!" + VariablesRoutes.VARIABLE_VALUE + "!"));
49+
}
50+
51+
public static class Profile implements QuarkusTestProfile {
52+
53+
@Override
54+
public Set<Class<?>> getEnabledAlternatives() {
55+
return Collections.singleton(VariablesProducers.MyGlobalRepo.class);
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)