1818 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919 */
2020
21+ using System . Windows ;
22+ using SonarLint . VisualStudio . Core ;
2123using SonarLint . VisualStudio . Core . Analysis ;
24+ using SonarLint . VisualStudio . Integration . TestInfrastructure ;
2225using SonarLint . VisualStudio . IssueVisualization . Models ;
2326using SonarLint . VisualStudio . IssueVisualization . Security . Hotspots ;
27+ using SonarLint . VisualStudio . IssueVisualization . Security . Hotspots . ReviewHotspot ;
2428using SonarLint . VisualStudio . IssueVisualization . Security . IssuesStore ;
2529using SonarLint . VisualStudio . IssueVisualization . Security . ReportView ;
2630using SonarLint . VisualStudio . IssueVisualization . Security . ReportView . Hotspots ;
@@ -31,20 +35,28 @@ namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.ReportVie
3135[ TestClass ]
3236public class HotspotsReportViewModelTest
3337{
38+ private readonly LocalHotspot serverHotspot = CreateMockedHotspot ( "myFile.cs" , "serverKey" ) ;
3439 private ILocalHotspotsStore localHotspotsStore ;
40+ private IMessageBox messageBox ;
41+ private IReviewHotspotsService reviewHotspotsService ;
3542 private HotspotsReportViewModel testSubject ;
3643
3744 [ TestInitialize ]
3845 public void TestInitialize ( )
3946 {
4047 localHotspotsStore = Substitute . For < ILocalHotspotsStore > ( ) ;
41- testSubject = new HotspotsReportViewModel ( localHotspotsStore ) ;
48+ reviewHotspotsService = Substitute . For < IReviewHotspotsService > ( ) ;
49+ messageBox = Substitute . For < IMessageBox > ( ) ;
50+ testSubject = new HotspotsReportViewModel ( localHotspotsStore , reviewHotspotsService , messageBox ) ;
4251 }
4352
4453 [ TestMethod ]
4554 public void MefCtor_CheckIsExported ( ) =>
4655 MefTestHelpers . CheckTypeCanBeImported < HotspotsReportViewModel , IHotspotsReportViewModel > (
47- MefTestHelpers . CreateExport < ILocalHotspotsStore > ( ) ) ;
56+ MefTestHelpers . CreateExport < ILocalHotspotsStore > ( ) ,
57+ MefTestHelpers . CreateExport < IReviewHotspotsService > ( ) ,
58+ MefTestHelpers . CreateExport < IMessageBox > ( )
59+ ) ;
4860
4961 [ TestMethod ]
5062 public void MefCtor_CheckIsSingleton ( ) => MefTestHelpers . CheckIsSingletonMefComponent < HotspotsReportViewModel > ( ) ;
@@ -114,12 +126,93 @@ public void HotspotsChanged_RaisedOnStoreIssuesChanged()
114126 raised . Should ( ) . BeTrue ( ) ;
115127 }
116128
117- private static LocalHotspot CreateMockedHotspot ( string filePath )
129+ [ TestMethod ]
130+ public async Task ShowHotspotInBrowserAsync_CallsHandler ( )
131+ {
132+ var hotspot = CreateMockedHotspot ( "myFile.cs" ) ;
133+
134+ await testSubject . ShowHotspotInBrowserAsync ( hotspot ) ;
135+
136+ reviewHotspotsService . Received ( 1 ) . OpenHotspotAsync ( hotspot . Visualization . Issue . IssueServerKey ) . IgnoreAwaitForAssert ( ) ;
137+ }
138+
139+ [ TestMethod ]
140+ public async Task GetAllowedStatusesAsync_ChangeStatusPermitted_ReturnsListOfAllowedStatuses ( )
141+ {
142+ var allowedStatuses = new List < HotspotStatus > { HotspotStatus . Fixed , HotspotStatus . ToReview } ;
143+ MockChangeStatusPermitted ( serverHotspot . Visualization . Issue . IssueServerKey , allowedStatuses ) ;
144+
145+ var result = await testSubject . GetAllowedStatusesAsync ( new HotspotViewModel ( serverHotspot ) ) ;
146+
147+ result . Should ( ) . BeEquivalentTo ( allowedStatuses ) ;
148+ messageBox . DidNotReceive ( ) . Show ( Arg . Any < string > ( ) , Arg . Any < string > ( ) , Arg . Any < MessageBoxButton > ( ) , Arg . Any < MessageBoxImage > ( ) ) ;
149+ }
150+
151+ [ TestMethod ]
152+ public async Task GetAllowedStatusesAsync_ChangeStatusNotPermitted_ShowsMessageBoxAndReturnsNull ( )
153+ {
154+ var reason = "Not permitted" ;
155+ MockChangeStatusNotPermitted ( serverHotspot . Visualization . Issue . IssueServerKey , reason ) ;
156+
157+ var result = await testSubject . GetAllowedStatusesAsync ( new HotspotViewModel ( serverHotspot ) ) ;
158+
159+ result . Should ( ) . BeNull ( ) ;
160+ messageBox . Received ( 1 ) . Show ( Arg . Is < string > ( x => x == string . Format ( Resources . ReviewHotspotWindow_CheckReviewPermittedFailureMessage , reason ) ) ,
161+ Arg . Is < string > ( x => x == Resources . ReviewHotspotWindow_FailureTitle ) , MessageBoxButton . OK , MessageBoxImage . Error ) ;
162+ }
163+
164+ [ TestMethod ]
165+ public async Task GetAllowedStatusesAsync_NoStatusSelected_ShowsMessageBoxAndReturnsNull ( )
166+ {
167+ var result = await testSubject . GetAllowedStatusesAsync ( null ) ;
168+
169+ result . Should ( ) . BeNull ( ) ;
170+ messageBox . Received ( 1 ) . Show (
171+ Arg . Is < string > ( x => x == string . Format ( Resources . ReviewHotspotWindow_CheckReviewPermittedFailureMessage , Resources . ReviewHotspotWindow_NoStatusSelectedFailureMessage ) ) ,
172+ Arg . Is < string > ( x => x == Resources . ReviewHotspotWindow_FailureTitle ) , MessageBoxButton . OK , MessageBoxImage . Error ) ;
173+ }
174+
175+ [ TestMethod ]
176+ [ DataRow ( HotspotStatus . Fixed ) ]
177+ [ DataRow ( HotspotStatus . ToReview ) ]
178+ [ DataRow ( HotspotStatus . Acknowledged ) ]
179+ [ DataRow ( HotspotStatus . Safe ) ]
180+ public async Task ChangeHotspotStatusAsync_Succeeds_ReturnsTrue ( HotspotStatus newStatus )
181+ {
182+ var hotspotViewModel = new HotspotViewModel ( serverHotspot ) ;
183+ MockReviewHotspot ( serverHotspot . Visualization . Issue . IssueServerKey , newStatus , true ) ;
184+
185+ var result = await testSubject . ChangeHotspotStatusAsync ( hotspotViewModel , newStatus ) ;
186+
187+ result . Should ( ) . BeTrue ( ) ;
188+ reviewHotspotsService . Received ( 1 ) . ReviewHotspotAsync ( serverHotspot . Visualization . Issue . IssueServerKey , newStatus ) . IgnoreAwaitForAssert ( ) ;
189+ messageBox . DidNotReceive ( ) . Show ( Arg . Any < string > ( ) , Arg . Any < string > ( ) , Arg . Any < MessageBoxButton > ( ) , Arg . Any < MessageBoxImage > ( ) ) ;
190+ }
191+
192+ [ TestMethod ]
193+ [ DataRow ( HotspotStatus . Fixed ) ]
194+ [ DataRow ( HotspotStatus . ToReview ) ]
195+ [ DataRow ( HotspotStatus . Acknowledged ) ]
196+ [ DataRow ( HotspotStatus . Safe ) ]
197+ public async Task ChangeHotspotStatusAsync_Fails_ShowsMessageBox ( HotspotStatus newStatus )
198+ {
199+ var hotspotViewModel = new HotspotViewModel ( serverHotspot ) ;
200+ MockReviewHotspot ( serverHotspot . Visualization . Issue . IssueServerKey , newStatus , false ) ;
201+
202+ var result = await testSubject . ChangeHotspotStatusAsync ( hotspotViewModel , newStatus ) ;
203+
204+ result . Should ( ) . BeFalse ( ) ;
205+ messageBox . Received ( 1 ) . Show ( Arg . Is < string > ( x => x == Resources . ReviewHotspotWindow_ReviewFailureMessage ) , Arg . Is < string > ( x => x == Resources . ReviewHotspotWindow_FailureTitle ) ,
206+ MessageBoxButton . OK , MessageBoxImage . Error ) ;
207+ }
208+
209+ private static LocalHotspot CreateMockedHotspot ( string filePath , string hotspotKey = null )
118210 {
119211 var analysisIssueVisualization = Substitute . For < IAnalysisIssueVisualization > ( ) ;
120212 var analysisIssueBase = Substitute . For < IAnalysisIssueBase > ( ) ;
121213 analysisIssueBase . PrimaryLocation . FilePath . Returns ( filePath ) ;
122214 analysisIssueVisualization . Issue . Returns ( analysisIssueBase ) ;
215+ analysisIssueVisualization . Issue . IssueServerKey . Returns ( hotspotKey ) ;
123216
124217 return new LocalHotspot ( analysisIssueVisualization , default , default ) ;
125218 }
@@ -136,4 +229,12 @@ private static void VerifyExpectedHotspotGroupViewModel(GroupFileViewModel group
136229 groupFileVm . FilteredIssues . Should ( ) . ContainSingle ( vm => ( ( HotspotViewModel ) vm ) . LocalHotspot == expectedHotspot ) ;
137230 }
138231 }
232+
233+ private void MockChangeStatusPermitted ( string hotspotKey , List < HotspotStatus > allowedStatuses ) =>
234+ reviewHotspotsService . CheckReviewHotspotPermittedAsync ( hotspotKey ) . Returns ( new ReviewHotspotPermittedArgs ( allowedStatuses ) ) ;
235+
236+ private void MockChangeStatusNotPermitted ( string hotspotKey , string reason ) =>
237+ reviewHotspotsService . CheckReviewHotspotPermittedAsync ( hotspotKey ) . Returns ( new ReviewHotspotNotPermittedArgs ( reason ) ) ;
238+
239+ private void MockReviewHotspot ( string hotspotKey , HotspotStatus newStatus , bool succeeded ) => reviewHotspotsService . ReviewHotspotAsync ( hotspotKey , newStatus ) . Returns ( succeeded ) ;
139240}
0 commit comments