18
18
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
*/
20
20
21
+ using System . Windows ;
22
+ using SonarLint . VisualStudio . Core ;
21
23
using SonarLint . VisualStudio . Core . Analysis ;
22
24
using SonarLint . VisualStudio . Integration . TestInfrastructure ;
23
25
using SonarLint . VisualStudio . IssueVisualization . Models ;
@@ -33,22 +35,28 @@ namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.ReportVie
33
35
[ TestClass ]
34
36
public class HotspotsReportViewModelTest
35
37
{
38
+ private readonly LocalHotspot serverHotspot = CreateMockedHotspot ( "myFile.cs" , "serverKey" ) ;
36
39
private ILocalHotspotsStore localHotspotsStore ;
37
- private HotspotsReportViewModel testSubject ;
40
+ private IMessageBox messageBox ;
38
41
private IReviewHotspotsService reviewHotspotsService ;
42
+ private HotspotsReportViewModel testSubject ;
39
43
40
44
[ TestInitialize ]
41
45
public void TestInitialize ( )
42
46
{
43
47
localHotspotsStore = Substitute . For < ILocalHotspotsStore > ( ) ;
44
48
reviewHotspotsService = Substitute . For < IReviewHotspotsService > ( ) ;
45
- testSubject = new HotspotsReportViewModel ( localHotspotsStore , reviewHotspotsService ) ;
49
+ messageBox = Substitute . For < IMessageBox > ( ) ;
50
+ testSubject = new HotspotsReportViewModel ( localHotspotsStore , reviewHotspotsService , messageBox ) ;
46
51
}
47
52
48
53
[ TestMethod ]
49
54
public void MefCtor_CheckIsExported ( ) =>
50
55
MefTestHelpers . CheckTypeCanBeImported < HotspotsReportViewModel , IHotspotsReportViewModel > (
51
- MefTestHelpers . CreateExport < ILocalHotspotsStore > ( ) ) ;
56
+ MefTestHelpers . CreateExport < ILocalHotspotsStore > ( ) ,
57
+ MefTestHelpers . CreateExport < IReviewHotspotsService > ( ) ,
58
+ MefTestHelpers . CreateExport < IMessageBox > ( )
59
+ ) ;
52
60
53
61
[ TestMethod ]
54
62
public void MefCtor_CheckIsSingleton ( ) => MefTestHelpers . CheckIsSingletonMefComponent < HotspotsReportViewModel > ( ) ;
@@ -128,12 +136,83 @@ public async Task ShowHotspotInBrowserAsync_CallsHandler()
128
136
reviewHotspotsService . Received ( 1 ) . OpenHotspotAsync ( hotspot . Visualization . Issue . IssueServerKey ) . IgnoreAwaitForAssert ( ) ;
129
137
}
130
138
131
- private static LocalHotspot CreateMockedHotspot ( string filePath )
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 )
132
210
{
133
211
var analysisIssueVisualization = Substitute . For < IAnalysisIssueVisualization > ( ) ;
134
212
var analysisIssueBase = Substitute . For < IAnalysisIssueBase > ( ) ;
135
213
analysisIssueBase . PrimaryLocation . FilePath . Returns ( filePath ) ;
136
214
analysisIssueVisualization . Issue . Returns ( analysisIssueBase ) ;
215
+ analysisIssueVisualization . Issue . IssueServerKey . Returns ( hotspotKey ) ;
137
216
138
217
return new LocalHotspot ( analysisIssueVisualization , default , default ) ;
139
218
}
@@ -150,4 +229,12 @@ private static void VerifyExpectedHotspotGroupViewModel(GroupFileViewModel group
150
229
groupFileVm . FilteredIssues . Should ( ) . ContainSingle ( vm => ( ( HotspotViewModel ) vm ) . LocalHotspot == expectedHotspot ) ;
151
230
}
152
231
}
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 ) ;
153
240
}
0 commit comments