Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.0 Friendly handshake #573

Merged
merged 9 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ public void ShouldReturnSameCollected()
((IMetadataCollector) collector).Collected.Should().BeSameAs(collector.Collected);
}

internal static string TestServerAgent = "Neo4j/3.5.2";

internal static KeyValuePair<string, object> TestMetadata =>
new KeyValuePair<string, object>(Key, "Neo4j/3.5.2");
new KeyValuePair<string, object>(Key, TestServerAgent);

internal static ServerVersion TestMetadataCollected => ServerVersion.From("Neo4j/3.5.2");
internal static ServerVersion TestMetadataCollected => ServerVersion.From(TestServerAgent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Neo4j.Driver.Internal.Connector;
using Neo4j.Driver.Internal.MessageHandling.Metadata;
using Neo4j.Driver.Internal.Util;
using Xunit;
using Neo4j.Driver.Internal.Result;
using Neo4j.Driver.Internal.Protocol;

namespace Neo4j.Driver.Internal.MessageHandling.V4
{
Expand All @@ -44,20 +40,7 @@ public void ShouldThrowIfConnectionIsNull()
}

[Fact]
public void ShouldUpdateVersionAndConnectionId()
{
var tracker = new Mock<IConnection>();
var handler = new HelloResponseHandler(tracker.Object, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

tracker.Verify(x => x.UpdateVersion(new ServerVersion(MajorVersion, MinorVersion, 0, null)), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}

[Fact]
public void ShouldThrowIfInitializedWithEalierBoltProtocolVesrion()
public void ShouldThrowIfInitializedWithEarlierBoltProtocolVersion()
{
var tracker = new Mock<IConnection>();
var exc = Xunit.Record.Exception(() => new HelloResponseHandler(tracker.Object, new Protocol.BoltProtocolVersion(3, 0)));
Expand All @@ -73,21 +56,18 @@ public void ShouldThrowIfInitializedWithNullVersion()
exc.Should().BeOfType<ArgumentNullException>();
}


[Fact]
public async Task ShouldUseProtocolVersionWhenPassedMetadata()
public void ShouldUpdateVersionAndConnectionId()
{
var mockClient = new Mock<ISocketClient>();
mockClient.Setup(x => x.ConnectAsync(null)).Returns(Task.FromResult(new Mock<IBoltProtocol>().Object));

var conn = new SocketConnection(mockClient.Object, AuthTokens.None, ConnectionSettings.DefaultUserAgent, new Mock<ILogger>().Object, new ServerInfo(new Uri("http://neo4j.com")));
await conn.InitAsync();

var responseHandler = new HelloResponseHandler(conn, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));
var tracker = new Mock<IConnection>();
var handler = new HelloResponseHandler(tracker.Object, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

responseHandler.OnSuccess(new[] {ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());
handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

//Ensure it is using the protocol version and not the version sent in the metadata
conn.Server.Agent.Should().ContainAll("Neo4j/" + MajorVersion + "." + MinorVersion + ".0");
tracker.Verify(x => x.UpdateVersion(ServerVersionCollectorTests.TestMetadataCollected), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ShouldUpdateVersionAndConnectionId()
handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

tracker.Verify(x => x.UpdateVersion(new ServerVersion(MajorVersion, MinorVersion, 0, null)), Times.Once);
tracker.Verify(x => x.UpdateVersion(ServerVersionCollectorTests.TestMetadataCollected), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}

Expand All @@ -71,22 +71,5 @@ public void ShouldThrowIfInitializedWithNullVersion()

exc.Should().BeOfType<ArgumentNullException>();
}

[Fact]
public async Task ShouldUseProtocolVersionWhenPassedMetadata()
{
var mockClient = new Mock<ISocketClient>();
mockClient.Setup(x => x.ConnectAsync(null)).Returns(Task.FromResult(new Mock<IBoltProtocol>().Object));

var conn = new SocketConnection(mockClient.Object, AuthTokens.None, ConnectionSettings.DefaultUserAgent, new Mock<ILogger>().Object, new ServerInfo(new Uri("http://neo4j.com")));
await conn.InitAsync();

var responseHandler = new HelloResponseHandler(conn, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

responseHandler.OnSuccess(new[] { ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata }.ToDictionary());

//Ensure it is using the protocol version and not the version sent in the metadata
conn.Server.Agent.Should().ContainAll("Neo4j/"+ MajorVersion + "." + MinorVersion + ".0");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ShouldUpdateVersionAndConnectionId()
handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

tracker.Verify(x => x.UpdateVersion(new ServerVersion(MajorVersion, MinorVersion, 0, null)), Times.Once);
tracker.Verify(x => x.UpdateVersion(ServerVersionCollectorTests.TestMetadataCollected), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}

Expand All @@ -71,22 +71,5 @@ public void ShouldThrowIfInitializedWithNullVersion()

exc.Should().BeOfType<ArgumentNullException>();
}

[Fact]
public async Task ShouldUseProtocolVersionWhenPassedMetadata()
{
var mockClient = new Mock<ISocketClient>();
mockClient.Setup(x => x.ConnectAsync(null)).Returns(Task.FromResult(new Mock<IBoltProtocol>().Object));

var conn = new SocketConnection(mockClient.Object, AuthTokens.None, ConnectionSettings.DefaultUserAgent, new Mock<ILogger>().Object, new ServerInfo(new Uri("http://neo4j.com")));
await conn.InitAsync();

var responseHandler = new HelloResponseHandler(conn, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

responseHandler.OnSuccess(new[] { ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata }.ToDictionary());

//Ensure it is using the protocol version and not the version sent in the metadata
conn.Server.Agent.Should().ContainAll("Neo4j/" + MajorVersion + "." + MinorVersion + ".0");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ShouldUpdateVersionAndConnectionId()
handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

tracker.Verify(x => x.UpdateVersion(new ServerVersion(MajorVersion, MinorVersion, 0, null)), Times.Once);
tracker.Verify(x => x.UpdateVersion(ServerVersionCollectorTests.TestMetadataCollected), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}

Expand All @@ -71,22 +71,5 @@ public void ShouldThrowIfInitializedWithNullVersion()

exc.Should().BeOfType<ArgumentNullException>();
}

[Fact]
public async Task ShouldUseProtocolVersionWhenPassedMetadata()
{
var mockClient = new Mock<ISocketClient>();
mockClient.Setup(x => x.ConnectAsync(null)).Returns(Task.FromResult(new Mock<IBoltProtocol>().Object));

var conn = new SocketConnection(mockClient.Object, AuthTokens.None, ConnectionSettings.DefaultUserAgent, new Mock<ILogger>().Object, new ServerInfo(new Uri("http://neo4j.com")));
await conn.InitAsync();

var responseHandler = new HelloResponseHandler(conn, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

responseHandler.OnSuccess(new[] { ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata }.ToDictionary());

//Ensure it is using the protocol version and not the version sent in the metadata
conn.Server.Agent.Should().ContainAll("Neo4j/" + MajorVersion + "." + MinorVersion + ".0");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ShouldUpdateVersionAndConnectionId()
handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

tracker.Verify(x => x.UpdateVersion(new ServerVersion(MajorVersion, MinorVersion, 0, null)), Times.Once);
tracker.Verify(x => x.UpdateVersion(ServerVersionCollectorTests.TestMetadataCollected), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}

Expand All @@ -71,22 +71,5 @@ public void ShouldThrowIfInitializedWithNullVersion()

exc.Should().BeOfType<ArgumentNullException>();
}

[Fact]
public async Task ShouldUseProtocolVersionWhenPassedMetadata()
{
var mockClient = new Mock<ISocketClient>();
mockClient.Setup(x => x.ConnectAsync(null)).Returns(Task.FromResult(new Mock<IBoltProtocol>().Object));

var conn = new SocketConnection(mockClient.Object, AuthTokens.None, ConnectionSettings.DefaultUserAgent, new Mock<ILogger>().Object, new ServerInfo(new Uri("http://neo4j.com")));
await conn.InitAsync();

var responseHandler = new HelloResponseHandler(conn, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

responseHandler.OnSuccess(new[] { ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata }.ToDictionary());

//Ensure it is using the protocol version and not the version sent in the metadata
conn.Server.Agent.Should().ContainAll("Neo4j/" + MajorVersion + "." + MinorVersion + ".0");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) "Neo4j"
// Neo4j Sweden AB [http://neo4j.com]
//
// This file is part of Neo4j.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Neo4j.Driver.Internal.Connector;
using Neo4j.Driver.Internal.MessageHandling.Metadata;
using Neo4j.Driver.Internal.Util;
using Xunit;
using Neo4j.Driver.Internal.Result;
using Neo4j.Driver.Internal.Protocol;

namespace Neo4j.Driver.Internal.MessageHandling.V5_0
{
public class HelloResponseHandlerTests
{
const int MajorVersion = 5;
const int MinorVersion = 0;
[Fact]
public void ShouldThrowIfConnectionIsNull()
{
var exc = Xunit.Record.Exception(() => new HelloResponseHandler(null, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion)));

exc.Should().BeOfType<ArgumentNullException>().Which
.ParamName.Should().Be("connection");
}

[Fact]
public void ShouldUpdateVersionAndConnectionId()
{
var tracker = new Mock<IConnection>();
var handler = new HelloResponseHandler(tracker.Object, new Protocol.BoltProtocolVersion(MajorVersion, MinorVersion));

handler.OnSuccess(new[]
{ServerVersionCollectorTests.TestMetadata, ConnectionIdCollectorTests.TestMetadata}.ToDictionary());

tracker.Verify(x => x.UpdateVersion(ServerVersionCollectorTests.TestMetadataCollected), Times.Once);
tracker.Verify(x => x.UpdateId(ConnectionIdCollectorTests.TestMetadataCollected), Times.Once);
}

[Fact]
public void ShouldThrowIfInitializedWithEalierBoltProtocolVersion()
{
var tracker = new Mock<IConnection>();
var exc = Xunit.Record.Exception(() => new HelloResponseHandler(tracker.Object, new Protocol.BoltProtocolVersion(3, 0)));

exc.Should().BeOfType<ArgumentOutOfRangeException>();
}

[Fact]
public void ShouldThrowIfInitializedWithNullVersion()
{
var exc = Xunit.Record.Exception(() => new HelloResponseHandler(null, null));

exc.Should().BeOfType<ArgumentNullException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void ShouldCreateBoltProtocolV3()
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolV3.Version);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V3_0);
boltProtocol.Should().BeOfType<BoltProtocolV3>();
}

Expand All @@ -45,7 +45,7 @@ public void ShouldCreateBoltProtocolV4()
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolV4_0.Version);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V4_0);
boltProtocol.Should().BeOfType<BoltProtocolV4_0>();
}

Expand All @@ -55,7 +55,7 @@ public void ShouldCreateBoltProtocolV4_1()
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolV4_1.Version, new Dictionary<string, string>());
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V4_1, new Dictionary<string, string>());
boltProtocol.Should().BeOfType<BoltProtocolV4_1>();
}

Expand All @@ -65,7 +65,7 @@ public void ShouldCreateBoltProtocolV4_2()
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolV4_2.Version, new Dictionary<string, string>());
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V4_2, new Dictionary<string, string>());
boltProtocol.Should().BeOfType<BoltProtocolV4_2>();
}

Expand All @@ -75,7 +75,7 @@ public void ShouldCreateBoltProtocolV4_3()
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolV4_3.Version, new Dictionary<string, string>());
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V4_3, new Dictionary<string, string>());
boltProtocol.Should().BeOfType<BoltProtocolV4_3>();
}

Expand All @@ -85,12 +85,21 @@ public void ShouldCreateBoltProtocolV4_4()
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolV4_4.Version, new Dictionary<string, string>());
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V4_4, new Dictionary<string, string>());
boltProtocol.Should().BeOfType<BoltProtocolV4_4>();
}

[Fact]
public void ShouldCreateBoltProtocolV5_0()
{
var connMock = new Mock<ITcpSocketClient>();
TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);
TcpSocketClientTestSetup.CreateReadStreamMock(connMock);
var boltProtocol = BoltProtocolFactory.ForVersion(BoltProtocolVersion.V5_0, new Dictionary<string, string>());
boltProtocol.Should().BeOfType<BoltProtocolV5_0>();
}

[Theory]
[Theory]
[InlineData(0, 0, "The Neo4j server does not support any of the protocol versions supported by this client")]
[InlineData(1, 0, "Protocol error, server suggested unexpected protocol version: 1.0")]
[InlineData(2, 0, "Protocol error, server suggested unexpected protocol version: 2.0")]
Expand Down
Loading