forked from timyates/mod-jdbc-persistor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rxtests: Clean up imports Added MapMany Rx test Initial commit of an Rx based test Added extra info to mod.json for the module repo Added pom name Fixed JS require statements Up to beta5 and newer c3p0
- Loading branch information
Showing
5 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{ | ||
"main": "com.bloidonia.vertx.mods.JdbcProcessor", | ||
"multi-threaded": true, | ||
"worker": true | ||
"worker": true, | ||
|
||
"description":"JDBC persistor module for Vert.x", | ||
"licenses": ["The Apache Software License Version 2.0"], | ||
"author": "Tim Yates", | ||
"keywords": ["jdbc", "database", "databases", "persistence", "json", "sql"], | ||
"homepage": "https://github.com/timyates/mod-jdbc-persistor" | ||
} |
162 changes: 162 additions & 0 deletions
162
src/test/java/com/bloidonia/vertx/mods/integration/JavaBasedRxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package com.bloidonia.vertx.mods.integration ; | ||
|
||
import io.vertx.rxcore.java.eventbus.RxEventBus ; | ||
import io.vertx.rxcore.java.eventbus.RxMessage ; | ||
|
||
import java.util.ArrayList ; | ||
|
||
import org.junit.Test ; | ||
|
||
import org.vertx.java.core.AsyncResult ; | ||
import org.vertx.java.core.AsyncResultHandler ; | ||
import org.vertx.java.core.json.JsonObject ; | ||
import org.vertx.java.core.json.JsonArray ; | ||
import org.vertx.testtools.TestVerticle ; | ||
|
||
import rx.Observable ; | ||
import rx.util.functions.Action1 ; | ||
import rx.util.functions.Func1 ; | ||
|
||
import static org.vertx.testtools.VertxAssert.* ; | ||
|
||
public class JavaBasedRxTest extends TestVerticle { | ||
|
||
private RxEventBus rxEventBus ; | ||
|
||
private JsonObject createMsg, insertMsg, selectMsg, dropMsg ; | ||
|
||
private void appReady() { | ||
super.start() ; | ||
} | ||
|
||
public void start() { | ||
rxEventBus = new RxEventBus( vertx.eventBus() ) ; | ||
JsonObject config = new JsonObject() {{ | ||
putString( "address", "test.persistor" ) ; | ||
putString( "url", "jdbc:hsqldb:mem:testdb?shutdown=true" ) ; | ||
}} ; | ||
|
||
container.deployModule(System.getProperty("vertx.modulename"), config, 1, new AsyncResultHandler<String>() { | ||
@Override | ||
public void handle(AsyncResult<String> event1) { | ||
createMsg = new JsonObject() {{ | ||
putString( "action", "execute" ) ; | ||
putString( "stmt", "CREATE TABLE IF NOT EXISTS test ( id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL, name VARCHAR(80), age INTEGER, CONSTRAINT testid PRIMARY KEY ( id ) )" ) ; | ||
}} ; | ||
insertMsg = new JsonObject() {{ | ||
putString( "action", "insert" ) ; | ||
putString( "stmt", "INSERT INTO test ( name, age ) VALUES ( ?, ? )" ) ; | ||
putArray( "values", new JsonArray( new ArrayList() {{ | ||
add( new ArrayList() {{ add( "tim" ) ; add( 65 ) ; }} ) ; | ||
add( new ArrayList() {{ add( "dave" ) ; add( 29 ) ; }} ) ; | ||
add( new ArrayList() {{ add( "mike" ) ; add( 42 ) ; }} ) ; | ||
}} ) ) ; | ||
}} ; | ||
selectMsg = new JsonObject() {{ | ||
putString( "action", "select" ) ; | ||
putString( "stmt", "SELECT * FROM test ORDER BY age ASC" ) ; | ||
}} ; | ||
dropMsg = new JsonObject() {{ | ||
putString( "action", "execute" ) ; | ||
putString( "stmt", "DROP TABLE test" ) ; | ||
}} ; | ||
appReady() ; | ||
} | ||
} ) ; | ||
} | ||
|
||
@Test | ||
public void testCreateInsertSelectAndDropMapMany() { | ||
rxEventBus.send( "test.persistor", createMsg ) | ||
.mapMany( new Func1<RxMessage<JsonObject>, Observable<RxMessage<JsonObject>>>() { | ||
@Override | ||
public Observable<RxMessage<JsonObject>> call(RxMessage<JsonObject> message) { | ||
assertEquals( message.body().getString( "status" ), "ok" ) ; | ||
return rxEventBus.send( "test.persistor", insertMsg ) ; | ||
} | ||
} ) | ||
.mapMany( new Func1<RxMessage<JsonObject>, Observable<RxMessage<JsonObject>>>() { | ||
@Override | ||
public Observable<RxMessage<JsonObject>> call(RxMessage<JsonObject> message) { | ||
assertEquals( message.body().getString( "status" ), "ok" ) ; | ||
JsonArray result = message.body().getArray( "result" ) ; | ||
assertEquals( ((JsonObject)result.get( 0 )).getNumber( "ID" ), 1 ) ; | ||
assertEquals( ((JsonObject)result.get( 1 )).getNumber( "ID" ), 2 ) ; | ||
assertEquals( ((JsonObject)result.get( 2 )).getNumber( "ID" ), 3 ) ; | ||
return rxEventBus.send( "test.persistor", selectMsg ) ; | ||
} | ||
} ) | ||
.mapMany( new Func1<RxMessage<JsonObject>, Observable<RxMessage<JsonObject>>>() { | ||
@Override | ||
public Observable<RxMessage<JsonObject>> call(RxMessage<JsonObject> message) { | ||
assertEquals( message.body().getString( "status" ), "ok" ) ; | ||
JsonArray result = message.body().getArray( "result" ) ; | ||
assertEquals( ((JsonObject)result.get( 0 )).getNumber( "AGE" ), 29 ) ; | ||
assertEquals( ((JsonObject)result.get( 1 )).getNumber( "AGE" ), 42 ) ; | ||
assertEquals( ((JsonObject)result.get( 2 )).getNumber( "AGE" ), 65 ) ; | ||
return rxEventBus.send( "test.persistor", dropMsg ) ; | ||
} | ||
} ) | ||
.subscribe( new Action1<RxMessage<JsonObject>>() { | ||
@Override | ||
public void call( RxMessage<JsonObject> message ) { | ||
assertEquals( message.body().getString( "status" ), "ok" ) ; | ||
testComplete() ; | ||
} | ||
} ) ; | ||
} | ||
|
||
@Test | ||
public void testCreateInsertSelectAndDrop() { | ||
// Last subscription in the chain; drop the table | ||
final Action1<RxMessage<JsonObject>> dropSubscription = new Action1<RxMessage<JsonObject>>() { | ||
@Override | ||
public void call( RxMessage<JsonObject> message ) { | ||
assertEquals( message.body().getString( "status" ), "ok" ) ; | ||
testComplete() ; | ||
} | ||
} ; | ||
|
||
// Thirdly we select the data from the table | ||
final Action1<RxMessage<JsonObject>> selectSubscription = new Action1<RxMessage<JsonObject>>() { | ||
@Override | ||
public void call( RxMessage<JsonObject> message ) { | ||
JsonArray result = message.body().getArray( "result" ) ; | ||
assertEquals( ((JsonObject)result.get( 0 )).getNumber( "AGE" ), 29 ) ; | ||
assertEquals( ((JsonObject)result.get( 1 )).getNumber( "AGE" ), 42 ) ; | ||
assertEquals( ((JsonObject)result.get( 2 )).getNumber( "AGE" ), 65 ) ; | ||
rxEventBus.send( "test.persistor", dropMsg ).subscribe( dropSubscription ) ; | ||
} | ||
} ; | ||
|
||
// Secondly we insert some data | ||
final Action1<RxMessage<JsonObject>> insertSubscription = new Action1<RxMessage<JsonObject>>() { | ||
@Override | ||
public void call( RxMessage<JsonObject> message ) { | ||
JsonArray result = message.body().getArray( "result" ) ; | ||
assertEquals( ((JsonObject)result.get( 0 )).getNumber( "ID" ), 1 ) ; | ||
assertEquals( ((JsonObject)result.get( 1 )).getNumber( "ID" ), 2 ) ; | ||
assertEquals( ((JsonObject)result.get( 2 )).getNumber( "ID" ), 3 ) ; | ||
rxEventBus.send( "test.persistor", selectMsg ).subscribe( selectSubscription ) ; | ||
} | ||
} ; | ||
|
||
// Initially, we create the table | ||
final Action1<RxMessage<JsonObject>> createSubscription = new Action1<RxMessage<JsonObject>>() { | ||
@Override | ||
public void call( RxMessage<JsonObject> message ) { | ||
assertEquals( message.body().getString( "status" ), "ok" ) ; | ||
rxEventBus.send( "test.persistor", insertMsg ).subscribe( insertSubscription ) ; | ||
} | ||
} ; | ||
|
||
// Kick off the chain, and subscribe | ||
final JsonObject createMsg = new JsonObject() {{ | ||
putString( "action", "execute" ) ; | ||
putString( "stmt", "CREATE TABLE IF NOT EXISTS test ( id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL, name VARCHAR(80), age INTEGER, CONSTRAINT testid PRIMARY KEY ( id ) )" ) ; | ||
}} ; | ||
|
||
final Observable<RxMessage<JsonObject>> create = rxEventBus.send( "test.persistor", createMsg ) ; | ||
create.subscribe( createSubscription ) ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters