Skip to content

Commit 8c368fa

Browse files
committed
Fix #4717 to expand camel-quarkus-mybatis test coverage
1 parent ac03cf6 commit 8c368fa

File tree

7 files changed

+292
-12
lines changed

7 files changed

+292
-12
lines changed

integration-tests/mybatis/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
</dependency>
3838
<dependency>
3939
<groupId>org.apache.camel.quarkus</groupId>
40-
<artifactId>camel-quarkus-mock</artifactId>
40+
<artifactId>camel-quarkus-jta</artifactId>
4141
</dependency>
4242
<dependency>
4343
<groupId>org.apache.camel.quarkus</groupId>
44-
<artifactId>camel-quarkus-jta</artifactId>
44+
<artifactId>camel-quarkus-mock</artifactId>
4545
</dependency>
4646
<dependency>
4747
<groupId>org.apache.camel.quarkus</groupId>

integration-tests/mybatis/src/main/java/org/apache/camel/quarkus/component/mybatis/it/MyBatisRoute.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,49 @@
2020
import javax.enterprise.context.ApplicationScoped;
2121

2222
import org.apache.camel.builder.RouteBuilder;
23+
import org.apache.camel.quarkus.component.mybatis.it.entity.Account;
2324

2425
@ApplicationScoped
2526
public class MyBatisRoute extends RouteBuilder {
2627
@Override
2728
public void configure() throws Exception {
2829
from("direct:selectOne")
29-
.to("mybatis:selectAccountById?statementType=SelectOne")
30-
.to("mock:result");
30+
.to("mybatis:selectAccountById?statementType=SelectOne");
31+
32+
from("direct:selectList")
33+
.to("mybatis:selectAllAccounts?statementType=SelectList");
3134

3235
from("direct:insertOne")
3336
.transacted()
3437
.to("mybatis:insertAccount?statementType=Insert")
35-
.to("mock:result");
38+
.process(exchange -> {
39+
Account account = exchange.getIn().getBody(Account.class);
40+
if (account.getFirstName().equals("Rollback")) {
41+
throw new RuntimeException("Rollback");
42+
}
43+
});
44+
45+
from("direct:insertList")
46+
.transacted()
47+
.to("mybatis:batchInsertAccount?statementType=InsertList");
3648

3749
from("direct:deleteOne")
3850
.transacted()
39-
.to("mybatis:deleteAccountById?statementType=Delete")
40-
.to("mock:result");
51+
.to("mybatis:deleteAccountById?statementType=Delete");
52+
53+
from("direct:deleteList")
54+
.transacted()
55+
.to("mybatis:batchDeleteAccountById?statementType=DeleteList");
56+
57+
from("direct:updateOne")
58+
.transacted()
59+
.to("mybatis:updateAccount?statementType=Update");
60+
61+
from("direct:updateList")
62+
.transacted()
63+
.to("mybatis:batchUpdateAccount?statementType=UpdateList");
64+
65+
from("mybatis:selectUnprocessedAccounts?onConsume=consumeAccount").routeId("mybatis-consumer").autoStartup(false)
66+
.to("mock:results");
4167
}
4268
}

integration-tests/mybatis/src/main/java/org/apache/camel/quarkus/component/mybatis/it/MybatisResource.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,33 @@
1616
*/
1717
package org.apache.camel.quarkus.component.mybatis.it;
1818

19+
import java.util.List;
20+
import java.util.Map;
21+
1922
import javax.enterprise.context.ApplicationScoped;
2023
import javax.inject.Inject;
2124
import javax.transaction.Transactional;
2225
import javax.ws.rs.Consumes;
2326
import javax.ws.rs.DELETE;
2427
import javax.ws.rs.GET;
2528
import javax.ws.rs.NotFoundException;
29+
import javax.ws.rs.PATCH;
2630
import javax.ws.rs.POST;
2731
import javax.ws.rs.Path;
2832
import javax.ws.rs.Produces;
2933
import javax.ws.rs.QueryParam;
3034
import javax.ws.rs.core.MediaType;
3135

36+
import org.apache.camel.CamelContext;
3237
import org.apache.camel.ProducerTemplate;
38+
import org.apache.camel.component.mock.MockEndpoint;
3339
import org.apache.camel.quarkus.component.mybatis.it.entity.Account;
3440

3541
@Path("/mybatis")
3642
@ApplicationScoped
3743
public class MybatisResource {
44+
@Inject
45+
CamelContext context;
3846

3947
@Inject
4048
ProducerTemplate template;
@@ -50,6 +58,13 @@ public Account selectOne(@QueryParam("id") Integer id) {
5058
return account;
5159
}
5260

61+
@Path("/selectList")
62+
@GET
63+
@Produces(MediaType.APPLICATION_JSON)
64+
public List selectList() {
65+
return template.requestBody("direct:selectList", null, List.class);
66+
}
67+
5368
@Path("/insertOne")
5469
@POST
5570
@Consumes(MediaType.APPLICATION_JSON)
@@ -60,6 +75,16 @@ public Integer insertOne(Account account) {
6075
return getCounts();
6176
}
6277

78+
@Path("/insertList")
79+
@POST
80+
@Consumes(MediaType.APPLICATION_JSON)
81+
@Produces(MediaType.TEXT_PLAIN)
82+
@Transactional
83+
public Integer insertList(List<Account> accounts) {
84+
template.sendBody("direct:insertList", accounts);
85+
return getCounts();
86+
}
87+
6388
@Path("/deleteOne")
6489
@DELETE
6590
@Produces(MediaType.TEXT_PLAIN)
@@ -69,7 +94,49 @@ public Integer deleteOne(@QueryParam("id") Integer id) {
6994
return getCounts();
7095
}
7196

97+
@Path("/deleteList")
98+
@DELETE
99+
@Produces(MediaType.TEXT_PLAIN)
100+
@Transactional
101+
public Integer deleteList(List<Integer> ids) {
102+
template.sendBody("direct:deleteList", ids);
103+
return getCounts();
104+
}
105+
106+
@Path("/updateOne")
107+
@PATCH
108+
@Consumes(MediaType.APPLICATION_JSON)
109+
@Produces(MediaType.TEXT_PLAIN)
110+
@Transactional
111+
public Integer updateOne(Account account) {
112+
template.sendBody("direct:updateOne", account);
113+
return getCounts();
114+
}
115+
116+
@Path("/updateList")
117+
@PATCH
118+
@Consumes(MediaType.APPLICATION_JSON)
119+
@Produces(MediaType.TEXT_PLAIN)
120+
@Transactional
121+
public Integer updateList(Map<String, Object> params) {
122+
template.sendBody("direct:updateList", params);
123+
return getCounts();
124+
}
125+
72126
private Integer getCounts() {
73127
return template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class);
74128
}
129+
130+
@Path("/consumer")
131+
@GET
132+
@Produces(MediaType.APPLICATION_JSON)
133+
public List consumer() throws Exception {
134+
MockEndpoint results = context.getEndpoint("mock:results", MockEndpoint.class);
135+
results.expectedMessageCount(2);
136+
137+
context.getRouteController().startRoute("mybatis-consumer");
138+
MockEndpoint.assertIsSatisfied(context);
139+
140+
return template.requestBody("mybatis:selectProcessedAccounts?statementType=SelectList", null, List.class);
141+
}
75142
}

integration-tests/mybatis/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ quarkus.datasource.username=username-default
2020
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:default
2121
quarkus.datasource.jdbc.transactions=xa
2222

23+
quarkus.transaction-manager.enable-recovery=true
24+
2325
quarkus.mybatis.environment=development
2426
quarkus.mybatis.xmlconfig.enable=true
2527
quarkus.mybatis.xmlconfig.path=SqlMapConfig.xml

integration-tests/mybatis/src/main/resources/insert.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ CREATE TABLE ACCOUNT (
2121
ACC_ID INTEGER,
2222
ACC_FIRST_NAME VARCHAR(255),
2323
ACC_LAST_NAME VARCHAR(255),
24-
ACC_EMAIL VARCHAR(255)
24+
ACC_EMAIL VARCHAR(255),
25+
PROCESSED BOOLEAN DEFAULT false
2526
);
2627

27-
INSERT INTO ACCOUNT VALUES (123, 'James', 'Strachan', '[email protected]');
28-
INSERT INTO ACCOUNT VALUES (456, 'Claus', 'Ibsen', '[email protected]');
28+
INSERT INTO ACCOUNT VALUES (123, 'James', 'Strachan', '[email protected]', false);
29+
INSERT INTO ACCOUNT VALUES (456, 'Claus', 'Ibsen', '[email protected]', false);
2930

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
18+
package org.apache.camel.quarkus.component.mybatis.it;
19+
20+
import io.quarkus.test.common.QuarkusTestResource;
21+
import io.quarkus.test.h2.H2DatabaseTestResource;
22+
import io.quarkus.test.junit.QuarkusTest;
23+
import io.restassured.RestAssured;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.hamcrest.Matchers.equalTo;
27+
28+
@QuarkusTest
29+
@QuarkusTestResource(H2DatabaseTestResource.class)
30+
public class MyBatisConsumerTest {
31+
32+
@Test
33+
public void testSelectList() {
34+
RestAssured.get("/mybatis/consumer")
35+
.then()
36+
.statusCode(200)
37+
.body("size()", equalTo(2))
38+
.body("[0].id", equalTo(123))
39+
.body("[0].firstName", equalTo("James"))
40+
.body("[0].lastName", equalTo("Strachan"))
41+
.body("[0].emailAddress", equalTo("[email protected]"))
42+
.body("[1].id", equalTo(456))
43+
.body("[1].firstName", equalTo("Claus"))
44+
.body("[1].lastName", equalTo("Ibsen"))
45+
.body("[1].emailAddress", equalTo("[email protected]"));
46+
}
47+
}

0 commit comments

Comments
 (0)