// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks; using Microsoft.SqlTools.Hosting.Protocol; using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; using Microsoft.SqlTools.ServiceLayer.ObjectExplorer; using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Contracts; using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes; using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility; using Moq; using Xunit; using Microsoft.SqlTools.ServiceLayer.LanguageServices; using Microsoft.SqlServer.Management.Common; namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer { public class ObjectExplorerServiceTests : ObjectExplorerTestBase { private ObjectExplorerService service; private Mock connectionServiceMock; private Mock serviceHostMock; string fakeConnectionString = "Data Source=server;Initial Catalog=database;Integrated Security=False;User Id=user"; private static ConnectionDetails details = new ConnectionDetails() { UserName = "user", Password = "password", DatabaseName = "msdb", ServerName = "serverName" }; ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details); ConnectedBindingQueue connectedBindingQueue; public ObjectExplorerServiceTests() { connectionServiceMock = new Mock(); serviceHostMock = new Mock(); service = CreateOEService(connectionServiceMock.Object); connectionServiceMock.Setup(x => x.RegisterConnectedQueue(It.IsAny(), It.IsAny())); service.InitializeService(serviceHostMock.Object); ConnectedBindingContext connectedBindingContext = new ConnectedBindingContext(); connectedBindingContext.ServerConnection = new ServerConnection(new SqlConnection(fakeConnectionString)); connectedBindingQueue = new ConnectedBindingQueue(false); connectedBindingQueue.BindingContextMap.Add($"{details.ServerName}_{details.DatabaseName}_{details.UserName}_NULL", connectedBindingContext); service.ConnectedBindingQueue = connectedBindingQueue; } [Fact] public async Task CreateSessionRequestErrorsIfConnectionDetailsIsNull() { object errorResponse = null; var contextMock = RequestContextMocks.Create(null) .AddErrorHandling((errorMessage, errorCode) => errorResponse = errorMessage); await service.HandleCreateSessionRequest(null, contextMock.Object); VerifyErrorSent(contextMock); Assert.True(((string)errorResponse).Contains("ArgumentNullException")); } [Fact] public async Task CreateSessionRequestReturnsFalseOnConnectionFailure() { // Given the connection service fails to connect ConnectionDetails details = TestObjects.GetTestConnectionDetails(); string expectedExceptionText = "Error!!!"; connectionServiceMock.Setup(c => c.Connect(It.IsAny())) .Throws(new Exception(expectedExceptionText)); // when creating a new session // then expect the create session request to return false await RunAndVerify( test: (requestContext) => CallCreateSession(details, requestContext), verify: (actual => { Assert.NotNull(actual.SessionId); Assert.NotNull(actual); Assert.True(actual.ErrorMessage.Contains(expectedExceptionText)); })); // And expect error notification to be sent serviceHostMock.Verify(x => x.SendEvent(CreateSessionCompleteNotification.Type, It.IsAny()), Times.Once()); } [Fact] public async Task CreateSessionRequestWithMasterConnectionReturnsServerSuccessAndNodeInfo() { // Given the connection service fails to connect ConnectionDetails details = new ConnectionDetails() { UserName = "user", Password = "password", DatabaseName = "master", ServerName = "serverName" }; await CreateSessionRequestAndVerifyServerNodeHelper(details); } [Fact] public async Task CreateSessionRequestWithEmptyConnectionReturnsServerSuccessAndNodeInfo() { // Given the connection service fails to connect ConnectionDetails details = new ConnectionDetails() { UserName = "user", Password = "password", DatabaseName = "", ServerName = "serverName" }; await CreateSessionRequestAndVerifyServerNodeHelper(details); } [Fact] public async Task CreateSessionRequestWithMsdbConnectionReturnsServerSuccessAndNodeInfo() { // Given the connection service fails to connect ConnectionDetails details = new ConnectionDetails() { UserName = "user", Password = "password", DatabaseName = "msdb", ServerName = "serverName" }; await CreateSessionRequestAndVerifyServerNodeHelper(details); } [Fact] public async Task ExpandNodeGivenValidSessionShouldReturnTheNodeChildren() { await ExpandAndVerifyServerNodes(); } [Fact] public async Task RefreshNodeGivenValidSessionShouldReturnTheNodeChildren() { await RefreshAndVerifyServerNodes(); } [Fact] public async Task ExpandNodeGivenInvalidSessionShouldReturnEmptyList() { ExpandParams expandParams = new ExpandParams() { SessionId = "invalid session is", NodePath = "Any path" }; // when expanding // then expect the nodes are server children await RunAndVerify( test: (requestContext) => CallServiceExpand(expandParams, requestContext), verify: (actual => { Assert.Equal(actual.SessionId, expandParams.SessionId); Assert.Null(actual.Nodes); })); } [Fact] public async Task RefreshNodeGivenInvalidSessionShouldReturnEmptyList() { RefreshParams expandParams = new RefreshParams() { SessionId = "invalid session is", NodePath = "Any path" }; // when expanding // then expect the nodes are server children await RunAndVerify( test: (requestContext) => CallServiceRefresh(expandParams, requestContext), verify: (actual => { Assert.Equal(actual.SessionId, expandParams.SessionId); Assert.Null(actual.Nodes); })); } [Fact] public async Task CloseSessionGivenInvalidSessionShouldReturnEmptyList() { CloseSessionParams closeSessionParamsparams = new CloseSessionParams() { SessionId = "invalid session is", }; // when expanding // then expect the nodes are server children await RunAndVerify( test: (requestContext) => CallCloseSession(closeSessionParamsparams, requestContext), verify: (actual => { Assert.Equal(actual.SessionId, closeSessionParamsparams.SessionId); Assert.False(actual.Success); })); } [Fact] public async Task CloseSessionGivenValidSessionShouldCloseTheSessionAndDisconnect() { var session = await CreateSession(); CloseSessionParams closeSessionParamsparams = new CloseSessionParams() { SessionId = session.SessionId, }; // when expanding // then expect the nodes are server children await RunAndVerify( test: (requestContext) => CallCloseSession(closeSessionParamsparams, requestContext), verify: (actual => { Assert.Equal(actual.SessionId, closeSessionParamsparams.SessionId); Assert.True(actual.Success); Assert.False(service.SessionIds.Contains(session.SessionId)); })); connectionServiceMock.Verify(c => c.Disconnect(It.IsAny())); } private async Task CreateSession() { SessionCreatedParameters sessionResult = null; serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => sessionResult = p); CreateSessionResponse result = default(CreateSessionResponse); var contextMock = RequestContextMocks.Create(r => result = r).AddErrorHandling(null); connectionServiceMock.Setup(c => c.Connect(It.IsAny())) .Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details))); ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details); connectionInfo.AddConnection("Default", new SqlConnection(fakeConnectionString)); connectionServiceMock.Setup((c => c.TryFindConnection(It.IsAny(), out connectionInfo))). OutCallback((string t, out ConnectionInfo v) => v = connectionInfo) .Returns(true); connectionServiceMock.Setup(c => c.Disconnect(It.IsAny())).Returns(true); await service.HandleCreateSessionRequest(details, contextMock.Object); await service.CreateSessionTask; return sessionResult; } private async Task ExpandAndVerifyServerNodes() { var session = await CreateSession(); ExpandParams expandParams = new ExpandParams() { SessionId = session.SessionId, NodePath = session.RootNode.NodePath }; // when expanding // then expect the nodes are server children await RunAndVerify( test: (requestContext) => CallServiceExpand(expandParams, requestContext), verify: (actual => { Assert.Equal(actual.SessionId, session.SessionId); Assert.NotNull(actual.SessionId); VerifyServerNodeChildren(actual.Nodes); })); } private async Task RefreshAndVerifyServerNodes() { var session = await CreateSession(); RefreshParams expandParams = new RefreshParams() { SessionId = session.SessionId, NodePath = session.RootNode.NodePath }; // when expanding // then expect the nodes are server children await RunAndVerify( test: (requestContext) => CallServiceRefresh(expandParams, requestContext), verify: (actual => { Assert.Equal(actual.SessionId, session.SessionId); Assert.NotNull(actual.SessionId); VerifyServerNodeChildren(actual.Nodes); })); } private async Task CallServiceRefresh(RefreshParams expandParams, RequestContext requestContext) { ExpandResponse result = null; serviceHostMock.AddEventHandling(ExpandCompleteNotification.Type, (et, p) => result = p); await service.HandleRefreshRequest(expandParams, requestContext); Task task = service.ExpandTask; if (task != null) { await task; } return result; } private async Task CallServiceExpand(ExpandParams expandParams, RequestContext requestContext) { ExpandResponse result = null; serviceHostMock.AddEventHandling(ExpandCompleteNotification.Type, (et, p) => result = p); await service.HandleExpandRequest(expandParams, requestContext); Task task = service.ExpandTask; if (task != null) { await task; } return result; } private async Task CallCreateSession(ConnectionDetails connectionDetails, RequestContext context) { SessionCreatedParameters result = null; serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => result = p); await service.HandleCreateSessionRequest(connectionDetails, context); Task task = service.CreateSessionTask; if (task != null) { await task; } return result; } private async Task CallCloseSession(CloseSessionParams closeSessionParams, RequestContext context) { SessionCreatedParameters result = null; serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => result = p); await service.HandleCloseSessionRequest(closeSessionParams, context); return null; } private async Task CreateSessionRequestAndVerifyServerNodeHelper(ConnectionDetails details) { serviceHostMock.AddEventHandling(ConnectionCompleteNotification.Type, null); //SessionCreatedParameters sessionResult connectionServiceMock.Setup(c => c.Connect(It.IsAny())) .Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details))); ConnectionInfo connectionInfo = new ConnectionInfo(null, null, details); connectionInfo.AddConnection("Default", new SqlConnection(fakeConnectionString)); connectionServiceMock.Setup((c => c.TryFindConnection(It.IsAny(), out connectionInfo))). OutCallback((string t, out ConnectionInfo v) => v = connectionInfo) .Returns(true); // when creating a new session // then expect the create session request to return false await RunAndVerify( test: (requestContext) => { return CallCreateSession(details, requestContext); }, verify: (actual => { Assert.True(actual.Success); Assert.NotNull(actual.SessionId); VerifyServerNode(actual.RootNode, details); })); // And expect no error notification to be sent serviceHostMock.Verify(x => x.SendEvent(ConnectionCompleteNotification.Type, It.IsAny()), Times.Never()); } private void VerifyServerNode(NodeInfo serverNode, ConnectionDetails details) { Assert.NotNull(serverNode); Assert.Equal(NodeTypes.Server.ToString(), serverNode.NodeType); string[] pathParts = serverNode.NodePath.Split(TreeNode.PathPartSeperator); Assert.Equal(1, pathParts.Length); Assert.Equal(details.ServerName, pathParts[0]); Assert.True(serverNode.Label.Contains(details.ServerName)); Assert.False(serverNode.IsLeaf); } private void VerifyServerNodeChildren(NodeInfo[] children) { Assert.NotNull(children); Assert.True(children.Count() == 3); Assert.True(children.All((x => x.NodeType == "Folder"))); } private static ConnectionCompleteParams GetCompleteParamsForConnection(string uri, ConnectionDetails details) { return new ConnectionCompleteParams() { ConnectionId = Guid.NewGuid().ToString(), OwnerUri = uri, ConnectionSummary = new ConnectionSummary() { ServerName = details.ServerName, DatabaseName = details.DatabaseName, UserName = details.UserName }, ServerInfo = TestObjects.GetTestServerInfo() }; } } }