|
| 1 | +package org.jooby.issues; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import org.jooby.rx.Rx; |
| 6 | +import org.jooby.rx.RxJdbc; |
| 7 | +import org.jooby.test.ServerFeature; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import com.github.davidmoten.rx.jdbc.Database; |
| 11 | +import com.typesafe.config.ConfigFactory; |
| 12 | +import com.typesafe.config.ConfigValueFactory; |
| 13 | + |
| 14 | +public class Issue351 extends ServerFeature { |
| 15 | + |
| 16 | + { |
| 17 | + use(ConfigFactory.empty() |
| 18 | + .withValue("db", ConfigValueFactory.fromAnyRef("mem"))); |
| 19 | + |
| 20 | + use(new RxJdbc()); |
| 21 | + |
| 22 | + onStart(r -> { |
| 23 | + Database db = r.require(Database.class); |
| 24 | + db.update("create table something (id int primary key, name varchar(100))") |
| 25 | + .execute(); |
| 26 | + |
| 27 | + db.update("insert into something (id, name) values (?, ?)") |
| 28 | + .parameters(1, "jooby") |
| 29 | + .execute(); |
| 30 | + }); |
| 31 | + |
| 32 | + get("/351/reactive", req -> |
| 33 | + req.require(Database.class) |
| 34 | + .select("select name from something where id = :id") |
| 35 | + .parameter("id", 1) |
| 36 | + .getAs(String.class) |
| 37 | + ).map(Rx.rx()); |
| 38 | + |
| 39 | + get("/351/blocking", req -> { |
| 40 | + return req.require(Database.class) |
| 41 | + .select("select name from something where id = :id") |
| 42 | + .parameter("id", 1) |
| 43 | + .getAs(String.class) |
| 44 | + .toBlocking() |
| 45 | + .single(); |
| 46 | + }); |
| 47 | + |
| 48 | + get("/db", req -> { |
| 49 | + assertEquals(req.require(Database.class), req.require(Database.class)); |
| 50 | + return "OK"; |
| 51 | + }); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void rxjdbc() throws Exception { |
| 57 | + request().get("/351/reactive") |
| 58 | + .expect("jooby"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void rxjdbcBlocking() throws Exception { |
| 63 | + request().get("/351/blocking") |
| 64 | + .expect("jooby"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void singletondb() throws Exception { |
| 69 | + request().get("/db") |
| 70 | + .expect("OK"); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments