Prevent ArgumentNullException in refresh request (#963)

This commit is contained in:
Soheil Alizadeh
2020-06-15 10:54:31 +04:30
committed by GitHub
parent 28e479f239
commit 31659e1126
2 changed files with 29 additions and 9 deletions

View File

@@ -243,7 +243,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
string uri = refreshParams.SessionId; string uri = refreshParams.SessionId;
ObjectExplorerSession session = null; 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} "); Logger.Write(TraceEventType.Verbose, $"Cannot expand object explorer node. Couldn't find session for uri. {uri} ");
await serviceHost.SendEvent(ExpandCompleteNotification.Type, new ExpandResponse await serviceHost.SendEvent(ExpandCompleteNotification.Type, new ExpandResponse

View File

@@ -70,7 +70,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
VerifyErrorSent(contextMock); VerifyErrorSent(contextMock);
Assert.True(((string)errorResponse).Contains("ArgumentNullException")); Assert.True(((string)errorResponse).Contains("ArgumentNullException"));
} }
[Fact] [Fact]
public async Task CreateSessionRequestReturnsFalseOnConnectionFailure() public async Task CreateSessionRequestReturnsFalseOnConnectionFailure()
{ {
@@ -95,7 +95,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
// And expect error notification to be sent // And expect error notification to be sent
serviceHostMock.Verify(x => x.SendEvent(CreateSessionCompleteNotification.Type, It.IsAny<SessionCreatedParameters>()), Times.Once()); serviceHostMock.Verify(x => x.SendEvent(CreateSessionCompleteNotification.Type, It.IsAny<SessionCreatedParameters>()), Times.Once());
} }
[Fact] [Fact]
public async Task CreateSessionRequestWithMasterConnectionReturnsServerSuccessAndNodeInfo() 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<bool, ExpandResponse>(
test: (requestContext) => CallServiceRefresh(expandParams, requestContext),
verify: (actual =>
{
Assert.Equal(actual.SessionId, expandParams.SessionId);
Assert.Null(actual.Nodes);
}));
}
[Fact] [Fact]
public async Task CloseSessionGivenInvalidSessionShouldReturnEmptyList() public async Task CloseSessionGivenInvalidSessionShouldReturnEmptyList()
{ {
@@ -253,7 +273,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public async Task FindNodesReturnsMatchingNode() public async Task FindNodesReturnsMatchingNode()
{ {
var session = await CreateSession(); var session = await CreateSession();
var foundNodes = service.FindNodes(session.SessionId, "Server", null, null, null); var foundNodes = service.FindNodes(session.SessionId, "Server", null, null, null);
Assert.Equal(1, foundNodes.Count); Assert.Equal(1, foundNodes.Count);
Assert.Equal("Server", foundNodes[0].NodeType); Assert.Equal("Server", foundNodes[0].NodeType);
@@ -264,7 +284,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
public async Task FindNodesReturnsEmptyListForNoMatch() public async Task FindNodesReturnsEmptyListForNoMatch()
{ {
var session = await CreateSession(); var session = await CreateSession();
var foundNodes = service.FindNodes(session.SessionId, "Table", "testSchema", "testTable", "testDatabase"); var foundNodes = service.FindNodes(session.SessionId, "Table", "testSchema", "testTable", "testDatabase");
Assert.Equal(0, foundNodes.Count); Assert.Equal(0, foundNodes.Count);
} }
@@ -290,7 +310,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => sessionResult = p); serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => sessionResult = p);
CreateSessionResponse result = default(CreateSessionResponse); CreateSessionResponse result = default(CreateSessionResponse);
var contextMock = RequestContextMocks.Create<CreateSessionResponse>(r => result = r).AddErrorHandling(null); var contextMock = RequestContextMocks.Create<CreateSessionResponse>(r => result = r).AddErrorHandling(null);
connectionServiceMock.Setup(c => c.Connect(It.IsAny<ConnectParams>())) connectionServiceMock.Setup(c => c.Connect(It.IsAny<ConnectParams>()))
.Returns((ConnectParams connectParams) => Task.FromResult(GetCompleteParamsForConnection(connectParams.OwnerUri, details))); .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); serviceHostMock.AddEventHandling(CreateSessionCompleteNotification.Type, (et, p) => result = p);
await service.HandleCreateSessionRequest(connectionDetails, context); await service.HandleCreateSessionRequest(connectionDetails, context);
Task task = service.CreateSessionTask; Task task = service.CreateSessionTask;
if (task != null) if (task != null)
{ {
await task; await task;
@@ -421,7 +441,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
// when creating a new session // when creating a new session
// then expect the create session request to return false // then expect the create session request to return false
await RunAndVerify<CreateSessionResponse, SessionCreatedParameters>( await RunAndVerify<CreateSessionResponse, SessionCreatedParameters>(
test: (requestContext) => test: (requestContext) =>
{ {
return CallCreateSession(details, requestContext); return CallCreateSession(details, requestContext);
}, },
@@ -433,7 +453,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
})); }));
// And expect no error notification to be sent // And expect no error notification to be sent
serviceHostMock.Verify(x => x.SendEvent(ConnectionCompleteNotification.Type, serviceHostMock.Verify(x => x.SendEvent(ConnectionCompleteNotification.Type,
It.IsAny<ConnectionCompleteParams>()), Times.Never()); It.IsAny<ConnectionCompleteParams>()), Times.Never());
} }