diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs index 4c773fa8..b62e7bb7 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs @@ -243,7 +243,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer string uri = refreshParams.SessionId; ObjectExplorerSession session = null; - if (!sessionMap.TryGetValue(uri, out session)) + if (string.IsNullOrEmpty(uri) || !sessionMap.TryGetValue(uri, out session)) { Logger.Write(TraceEventType.Verbose, $"Cannot expand object explorer node. Couldn't find session for uri. {uri} "); await serviceHost.SendEvent(ExpandCompleteNotification.Type, new ExpandResponse diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ObjectExplorer/ObjectExplorerServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ObjectExplorer/ObjectExplorerServiceTests.cs index 3dbb3776..ea9f3853 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ObjectExplorer/ObjectExplorerServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ObjectExplorer/ObjectExplorerServiceTests.cs @@ -70,7 +70,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer VerifyErrorSent(contextMock); Assert.True(((string)errorResponse).Contains("ArgumentNullException")); } - + [Fact] public async Task CreateSessionRequestReturnsFalseOnConnectionFailure() { @@ -95,7 +95,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer // And expect error notification to be sent serviceHostMock.Verify(x => x.SendEvent(CreateSessionCompleteNotification.Type, It.IsAny()), Times.Once()); } - + [Fact] public async Task CreateSessionRequestWithMasterConnectionReturnsServerSuccessAndNodeInfo() @@ -207,6 +207,26 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer })); } + [Fact] + public async Task RefreshNodeGivenNullSessionShouldReturnEmptyList() + { + RefreshParams expandParams = new RefreshParams() + { + SessionId = null, + 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() { @@ -253,7 +273,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer public async Task FindNodesReturnsMatchingNode() { var session = await CreateSession(); - + var foundNodes = service.FindNodes(session.SessionId, "Server", null, null, null); Assert.Equal(1, foundNodes.Count); Assert.Equal("Server", foundNodes[0].NodeType); @@ -264,7 +284,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer public async Task FindNodesReturnsEmptyListForNoMatch() { var session = await CreateSession(); - + var foundNodes = service.FindNodes(session.SessionId, "Table", "testSchema", "testTable", "testDatabase"); Assert.Equal(0, foundNodes.Count); } @@ -290,7 +310,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer 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))); @@ -385,7 +405,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => result = p); await service.HandleCreateSessionRequest(connectionDetails, context); - Task task = service.CreateSessionTask; + Task task = service.CreateSessionTask; if (task != null) { await task; @@ -421,7 +441,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer // when creating a new session // then expect the create session request to return false await RunAndVerify( - test: (requestContext) => + test: (requestContext) => { return CallCreateSession(details, requestContext); }, @@ -433,7 +453,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer })); // And expect no error notification to be sent - serviceHostMock.Verify(x => x.SendEvent(ConnectionCompleteNotification.Type, + serviceHostMock.Verify(x => x.SendEvent(ConnectionCompleteNotification.Type, It.IsAny()), Times.Never()); }