|
18 | 18 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 | 19 | */
|
20 | 20 |
|
| 21 | +using System.ComponentModel; |
21 | 22 | using System.IO;
|
22 | 23 | using System.IO.Abstractions;
|
23 | 24 | using SonarLint.VisualStudio.ConnectedMode.Binding;
|
@@ -338,6 +339,32 @@ public void TryAdd_WritingThrowsException_DoesNotUpdateConnectionAndWritesLog()
|
338 | 339 | logger.Received(1).WriteLine($"Failed updating the {ServerConnectionsRepository.ConnectionsFileName}: {exceptionMsg}");
|
339 | 340 | }
|
340 | 341 |
|
| 342 | + [TestMethod] |
| 343 | + public void TryAdd_DoesNotAddConnection_DoesNotInvokeConnectionChangedEvent() |
| 344 | + { |
| 345 | + MockReadingFile(new ServerConnectionsListJsonModel()); |
| 346 | + jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(false); |
| 347 | + var eventHandler = Substitute.For<EventHandler>(); |
| 348 | + testSubject.ConnectionChanged += eventHandler; |
| 349 | + |
| 350 | + testSubject.TryAdd(sonarCloudServerConnection); |
| 351 | + |
| 352 | + eventHandler.DidNotReceive().Invoke(testSubject, Arg.Any<EventArgs>()); |
| 353 | + } |
| 354 | + |
| 355 | + [TestMethod] |
| 356 | + public void TryAdd_AddsConnection_InvokesConnectionChangedEvent() |
| 357 | + { |
| 358 | + MockReadingFile(new ServerConnectionsListJsonModel()); |
| 359 | + jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(true); |
| 360 | + var eventHandler = Substitute.For<EventHandler>(); |
| 361 | + testSubject.ConnectionChanged += eventHandler; |
| 362 | + |
| 363 | + testSubject.TryAdd(sonarCloudServerConnection); |
| 364 | + |
| 365 | + eventHandler.Received().Invoke(testSubject, Arg.Any<EventArgs>()); |
| 366 | + } |
| 367 | + |
341 | 368 | [TestMethod]
|
342 | 369 | public void TryDelete_FileCouldNotBeRead_ReturnsFalse()
|
343 | 370 | {
|
@@ -439,6 +466,32 @@ public void TryDelete_WritingThrowsException_DoesNotUpdateConnectionAndWritesLog
|
439 | 466 | logger.Received(1).WriteLine($"Failed updating the {ServerConnectionsRepository.ConnectionsFileName}: {exceptionMsg}");
|
440 | 467 | }
|
441 | 468 |
|
| 469 | + [TestMethod] |
| 470 | + public void TryDelete_DoesNotDeleteConnection_DoesNotInvokeConnectionChangedEvent() |
| 471 | + { |
| 472 | + var sonarQube = MockFileWithOneSonarQubeConnection(); |
| 473 | + jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(false); |
| 474 | + var eventHandler = Substitute.For<EventHandler>(); |
| 475 | + testSubject.ConnectionChanged += eventHandler; |
| 476 | + |
| 477 | + testSubject.TryDelete(sonarQube.Id); |
| 478 | + |
| 479 | + eventHandler.DidNotReceive().Invoke(testSubject, Arg.Any<EventArgs>()); |
| 480 | + } |
| 481 | + |
| 482 | + [TestMethod] |
| 483 | + public void TryDelete_DeletesConnection_InvokesConnectionChangedEvent() |
| 484 | + { |
| 485 | + var sonarQube = MockFileWithOneSonarQubeConnection(); |
| 486 | + jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(true); |
| 487 | + var eventHandler = Substitute.For<EventHandler>(); |
| 488 | + testSubject.ConnectionChanged += eventHandler; |
| 489 | + |
| 490 | + testSubject.TryDelete(sonarQube.Id); |
| 491 | + |
| 492 | + eventHandler.Received(1).Invoke(testSubject, Arg.Any<EventArgs>()); |
| 493 | + } |
| 494 | + |
442 | 495 | [TestMethod]
|
443 | 496 | public void TryUpdateSettingsById_FileCouldNotBeRead_ReturnsFalse()
|
444 | 497 | {
|
@@ -505,6 +558,32 @@ public void TryUpdateSettingsById_WritingThrowsException_DoesNotUpdateConnection
|
505 | 558 | logger.Received(1).WriteLine($"Failed updating the {ServerConnectionsRepository.ConnectionsFileName}: {exceptionMsg}");
|
506 | 559 | }
|
507 | 560 |
|
| 561 | + [TestMethod] |
| 562 | + public void TryUpdateSettingsById_DoesNotUpdateConnection_DoesNotInvokeConnectionChangedEvent() |
| 563 | + { |
| 564 | + var sonarQube = MockFileWithOneSonarQubeConnection(); |
| 565 | + jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(false); |
| 566 | + var eventHandler = Substitute.For<EventHandler>(); |
| 567 | + testSubject.ConnectionChanged += eventHandler; |
| 568 | + |
| 569 | + testSubject.TryDelete(sonarQube.Id); |
| 570 | + |
| 571 | + eventHandler.DidNotReceive().Invoke(testSubject, Arg.Any<EventArgs>()); |
| 572 | + } |
| 573 | + |
| 574 | + [TestMethod] |
| 575 | + public void TryUpdateSettingsById_UpdatesConnection_InvokesConnectionChangedEvent() |
| 576 | + { |
| 577 | + var sonarQube = MockFileWithOneSonarQubeConnection(); |
| 578 | + jsonFileHandler.TryWriteToFile(Arg.Any<string>(), Arg.Any<ServerConnectionsListJsonModel>()).Returns(true); |
| 579 | + var eventHandler = Substitute.For<EventHandler>(); |
| 580 | + testSubject.ConnectionChanged += eventHandler; |
| 581 | + |
| 582 | + testSubject.TryUpdateSettingsById(sonarQube.Id, new ServerConnectionSettings(true)); |
| 583 | + |
| 584 | + eventHandler.Received(1).Invoke(testSubject, Arg.Any<EventArgs>()); |
| 585 | + } |
| 586 | + |
508 | 587 | [TestMethod]
|
509 | 588 | public void TryUpdateCredentialsById_ConnectionDoesNotExist_DoesNotUpdateCredentials()
|
510 | 589 | {
|
@@ -540,6 +619,30 @@ public void TryUpdateCredentialsById_SonarQubeConnectionExists_UpdatesCredential
|
540 | 619 | credentialsLoader.Received(1).Save(newCredentials, sonarQube.ServerUri);
|
541 | 620 | }
|
542 | 621 |
|
| 622 | + [TestMethod] |
| 623 | + public void TryUpdateCredentialsById_DoesNotUpdateCredentials_DoesNotInvokeConnectionChangedEvent() |
| 624 | + { |
| 625 | + MockReadingFile(new ServerConnectionsListJsonModel()); |
| 626 | + var eventHandler = Substitute.For<EventHandler<ServerConnectionUpdatedEventArgs>>(); |
| 627 | + testSubject.CredentialsChanged += eventHandler; |
| 628 | + |
| 629 | + testSubject.TryUpdateCredentialsById("non-existingConn", Substitute.For<ICredentials>()); |
| 630 | + |
| 631 | + eventHandler.DidNotReceive().Invoke(testSubject, Arg.Any<ServerConnectionUpdatedEventArgs>()); |
| 632 | + } |
| 633 | + |
| 634 | + [TestMethod] |
| 635 | + public void TryUpdateCredentialsById_UpdatesCredentials_InvokesConnectionChangedEvent() |
| 636 | + { |
| 637 | + var sonarQube = MockFileWithOneSonarQubeConnection(); |
| 638 | + var eventHandler = Substitute.For<EventHandler<ServerConnectionUpdatedEventArgs>>(); |
| 639 | + testSubject.CredentialsChanged += eventHandler; |
| 640 | + |
| 641 | + testSubject.TryUpdateCredentialsById(sonarQube.Id, Substitute.For<ICredentials>()); |
| 642 | + |
| 643 | + eventHandler.Received(1).Invoke(testSubject, Arg.Is<ServerConnectionUpdatedEventArgs>(args => args.ServerConnection == sonarQube)); |
| 644 | + } |
| 645 | + |
543 | 646 | [TestMethod]
|
544 | 647 | [DataRow(true)]
|
545 | 648 | [DataRow(false)]
|
|
0 commit comments