1616 * See the License for the specific language governing permissions and
1717 * limitations under the License.
1818 */
19- var DummyChannel = require ( '../../lib/v1/internal/ch-dummy.js' ) ;
20- var connect = require ( "../../lib/v1/internal/connector.js" ) . connect ;
2119
22- describe ( 'connector' , function ( ) {
20+ import * as DummyChannel from '../../src/v1/internal/ch-dummy' ;
21+ import { connect , Connection } from '../../src/v1/internal/connector' ;
22+ import { Packer } from '../../src/v1/internal/packstream' ;
23+ import { Chunker } from '../../src/v1/internal/chunking' ;
24+ import { alloc } from '../../src/v1/internal/buf' ;
25+ import { Neo4jError } from '../../src/v1/error' ;
2326
24- it ( 'should read/write basic messages' , function ( done ) {
27+ describe ( 'connector' , ( ) => {
28+
29+ it ( 'should read/write basic messages' , done => {
2530 // Given
26- var conn = connect ( "bolt://localhost" )
31+ const conn = connect ( "bolt://localhost" ) ;
2732
2833 // When
29- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
30- onCompleted : function ( msg ) {
31- expect ( msg ) . not . toBeNull ( ) ;
34+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
35+ onCompleted : msg => {
36+ expect ( msg ) . not . toBeNull ( ) ;
3237 conn . close ( ) ;
3338 done ( ) ;
3439 } ,
35- onError : function ( err ) {
36- console . log ( err ) ;
37- }
40+ onError : console . log
3841 } ) ;
3942 conn . sync ( ) ;
4043
4144 } ) ;
42- it ( 'should retrieve stream' , function ( done ) {
45+
46+ it ( 'should retrieve stream' , done => {
4347 // Given
44- var conn = connect ( "bolt://localhost" )
48+ const conn = connect ( "bolt://localhost" ) ;
4549
4650 // When
47- var records = [ ] ;
48- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
49- conn . run ( "RETURN 1.0" , { } ) ;
50- conn . pullAll ( {
51- onNext : function ( record ) {
52- records . push ( record ) ;
51+ const records = [ ] ;
52+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
53+ conn . run ( "RETURN 1.0" , { } ) ;
54+ conn . pullAll ( {
55+ onNext : record => {
56+ records . push ( record ) ;
5357 } ,
54- onCompleted : function ( tail ) {
55- expect ( records [ 0 ] [ 0 ] ) . toBe ( 1 ) ;
58+ onCompleted : ( ) => {
59+ expect ( records [ 0 ] [ 0 ] ) . toBe ( 1 ) ;
5660 conn . close ( ) ;
5761 done ( ) ;
5862 }
5963 } ) ;
6064 conn . sync ( ) ;
6165 } ) ;
6266
63- it ( 'should use DummyChannel to read what gets written' , function ( done ) {
67+ it ( 'should use DummyChannel to read what gets written' , done => {
6468 // Given
65- var observer = DummyChannel . observer ;
66- var conn = connect ( "bolt://localhost" , { channel :DummyChannel . channel } ) ;
69+ const observer = DummyChannel . observer ;
70+ const conn = connect ( "bolt://localhost" , { channel : DummyChannel . channel } ) ;
6771
6872 // When
69- var records = [ ] ;
70- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
71- conn . run ( "RETURN 1" , { } ) ;
73+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } ) ;
74+ conn . run ( "RETURN 1" , { } ) ;
7275 conn . sync ( ) ;
73- expect ( observer . instance . toHex ( ) ) . toBe ( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 41 b2 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 a3 86 73 63 68 65 6d 65 85 62 61 73 69 63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72 65 64 65 6e 74 69 61 6c 73 85 6e 65 6f 34 6a 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' ) ;
76+ expect ( observer . instance . toHex ( ) ) . toBe ( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 41 b2 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 a3 86 73 63 68 65 6d 65 85 62 61 73 69 63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72 65 64 65 6e 74 69 61 6c 73 85 6e 65 6f 34 6a 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' ) ;
7477 done ( ) ;
7578 } ) ;
7679
77- it ( 'should provide error message when connecting to http-port' , function ( done ) {
80+ it ( 'should provide error message when connecting to http-port' , done => {
7881 // Given
79- var conn = connect ( "bolt://localhost:7474" , { encrypted :false } ) ;
82+ const conn = connect ( "bolt://localhost:7474" , { encrypted : false } ) ;
8083
8184 // When
82- conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
83- onCompleted : function ( msg ) {
85+ conn . initialize ( "mydriver/0.0.0" , { scheme : "basic" , principal : "neo4j" , credentials : "neo4j" } , {
86+ onCompleted : msg => {
8487 } ,
85- onError : function ( err ) {
88+ onError : err => {
8689 //only node gets the pretty error message
87- if ( require ( '../../lib/v1/internal/ch-node.js' ) . available ) {
90+ if ( require ( '../../lib/v1/internal/ch-node.js' ) . available ) {
8891 expect ( err . message ) . toBe ( "Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
8992 "(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)" ) ;
9093 }
@@ -95,4 +98,48 @@ describe('connector', function() {
9598
9699 } ) ;
97100
101+ it ( 'should convert failure messages to errors' , done => {
102+ const channel = new DummyChannel . channel ;
103+ const connection = new Connection ( channel , 'bolt://localhost' ) ;
104+
105+ const errorCode = 'Neo.ClientError.Schema.ConstraintValidationFailed' ;
106+ const errorMessage = 'Node 0 already exists with label User and property "email"=[john@doe.com]' ;
107+
108+ connection . _queueObserver ( {
109+ onError : error => {
110+ expectNeo4jError ( error , errorCode , errorMessage ) ;
111+ done ( ) ;
112+ }
113+ } ) ;
114+
115+ channel . onmessage ( packedHandshakeMessage ( ) ) ;
116+ channel . onmessage ( packedFailureMessage ( errorCode , errorMessage ) ) ;
117+ } ) ;
118+
119+ function packedHandshakeMessage ( ) {
120+ const result = alloc ( 4 ) ;
121+ result . putInt32 ( 0 , 1 ) ;
122+ result . reset ( ) ;
123+ return result ;
124+ }
125+
126+ function packedFailureMessage ( code , message ) {
127+ const channel = new DummyChannel . channel ;
128+ const chunker = new Chunker ( channel ) ;
129+ const packer = new Packer ( chunker ) ;
130+ packer . packStruct ( 0x7F , [ packer . packable ( { code : code , message : message } ) ] ) ;
131+ chunker . messageBoundary ( ) ;
132+ chunker . flush ( ) ;
133+ const data = channel . toBuffer ( ) ;
134+ const result = alloc ( data . length ) ;
135+ result . putBytes ( 0 , data ) ;
136+ return result ;
137+ }
138+
139+ function expectNeo4jError ( error , expectedCode , expectedMessage ) {
140+ expect ( ( ) => {
141+ throw error ;
142+ } ) . toThrow ( new Neo4jError ( expectedMessage , expectedCode ) ) ;
143+ }
144+
98145} ) ;
0 commit comments