1+ package checkmarx .ast .eclipse .plugin .tests .unit ;
2+
3+ import com .checkmarx .eclipse .views .CheckmarxView ;
4+ import com .checkmarx .eclipse .views .DataProvider ;
5+ import com .checkmarx .eclipse .views .actions .ToolBarActions ;
6+ import com .checkmarx .eclipse .properties .Preferences ;
7+ import com .checkmarx .eclipse .utils .PluginUtils ;
8+ import com .checkmarx .ast .project .Project ;
9+ import com .checkmarx .ast .scan .Scan ;
10+ import com .checkmarx .eclipse .Activator ;
11+
12+ import org .eclipse .swt .widgets .Composite ;
13+ import org .eclipse .swt .widgets .Display ;
14+ import org .eclipse .swt .widgets .Shell ;
15+ import org .eclipse .jface .action .Action ;
16+ import org .eclipse .jface .resource .ImageDescriptor ;
17+ import org .eclipse .swt .graphics .Image ;
18+
19+ import org .eclipse .ui .PlatformUI ;
20+ import org .eclipse .ui .IWorkbench ;
21+ import org .eclipse .ui .IWorkbenchWindow ;
22+
23+ import org .eclipse .e4 .core .services .events .IEventBroker ;
24+ import java .util .Map ;
25+ import java .util .Arrays ;
26+ import java .util .Collections ;
27+ import java .util .HashMap ;
28+ import java .util .List ;
29+
30+ import org .osgi .service .event .Event ;
31+
32+ import org .junit .jupiter .api .*;
33+ import org .mockito .MockedStatic ;
34+ import org .mockito .Mockito ;
35+
36+ import java .lang .reflect .Field ;
37+ import java .lang .reflect .Method ;
38+
39+ import static org .junit .jupiter .api .Assertions .*;
40+
41+ class CheckmarxViewTest {
42+
43+ private static Display display ;
44+
45+ private static MockedStatic <Activator > activatorStaticMock ;
46+ private static MockedStatic <ImageDescriptor > imageDescriptorStaticMock ;
47+
48+ private MockedStatic <PlatformUI > platformUIMock ;
49+ private MockedStatic <PluginUtils > pluginUtilsMock ;
50+
51+ private CheckmarxView checkmarxView ;
52+ private Shell shell ;
53+ private Composite parent ;
54+
55+ @ BeforeAll
56+ static void beforeAll () {
57+
58+ display = Display .getDefault ();
59+
60+ activatorStaticMock = Mockito .mockStatic (Activator .class );
61+ imageDescriptorStaticMock = Mockito .mockStatic (ImageDescriptor .class , Mockito .CALLS_REAL_METHODS );
62+
63+ ImageDescriptor descriptor = Mockito .mock (ImageDescriptor .class );
64+ Image image = Mockito .mock (Image .class );
65+
66+ activatorStaticMock
67+ .when (() -> Activator .getImageDescriptor (Mockito .anyString ()))
68+ .thenReturn (descriptor );
69+
70+ Mockito .when (descriptor .createImage ()).thenReturn (image );
71+ }
72+
73+ @ AfterAll
74+ static void afterAll () {
75+ activatorStaticMock .close ();
76+ imageDescriptorStaticMock .close ();
77+ }
78+
79+ @ BeforeEach
80+ void setUp () throws Exception {
81+
82+ platformUIMock = Mockito .mockStatic (PlatformUI .class );
83+ pluginUtilsMock = Mockito .mockStatic (PluginUtils .class );
84+
85+ IWorkbench workbench = Mockito .mock (IWorkbench .class );
86+ IWorkbenchWindow window = Mockito .mock (IWorkbenchWindow .class );
87+
88+ display .syncExec (() -> {
89+ shell = new Shell (display );
90+ parent = new Composite (shell , 0 );
91+ });
92+
93+ Mockito .when (window .getShell ()).thenReturn (shell );
94+ Mockito .when (workbench .getActiveWorkbenchWindow ()).thenReturn (window );
95+ Mockito .when (workbench .getDisplay ()).thenReturn (display );
96+
97+ platformUIMock .when (PlatformUI ::getWorkbench ).thenReturn (workbench );
98+
99+ IEventBroker broker = Mockito .mock (IEventBroker .class );
100+ Mockito .when (broker .subscribe (Mockito .anyString (), Mockito .any ())).thenReturn (true );
101+
102+ pluginUtilsMock .when (PluginUtils ::getEventBroker ).thenReturn (broker );
103+
104+ checkmarxView = new CheckmarxView ();
105+
106+ injectDependencies ();
107+ }
108+
109+ @ AfterEach
110+ void tearDown () {
111+
112+ platformUIMock .close ();
113+ pluginUtilsMock .close ();
114+
115+ if (shell != null && !shell .isDisposed ()) {
116+ display .syncExec (() -> shell .dispose ());
117+ }
118+ }
119+
120+ @ Test
121+ void testConstructorInitializesFields () {
122+ assertNotNull (checkmarxView );
123+ }
124+
125+ @ Test
126+ void testDisposeDoesNotThrow () {
127+ assertDoesNotThrow (() -> checkmarxView .dispose ());
128+ }
129+
130+ @ Test
131+ void testSetFocusDoesNotThrow () {
132+ assertDoesNotThrow (() -> checkmarxView .setFocus ());
133+ }
134+
135+ @ Test
136+ void testHandleEventWithEmptyApiKey () throws Exception {
137+
138+ Event event = new Event ("test/topic" , new HashMap <String , Object >());
139+
140+ try (MockedStatic <Preferences > prefMock =
141+ Mockito .mockStatic (Preferences .class , Mockito .CALLS_REAL_METHODS )) {
142+
143+ prefMock .when (Preferences ::getApiKey ).thenReturn ("" );
144+
145+ assertDoesNotThrow (() ->
146+ Display .getDefault ().syncExec (() -> checkmarxView .handleEvent (event ))
147+ );
148+ }
149+ }
150+
151+ @ Test
152+ void testHandleEventWithNonEmptyApiKey () {
153+
154+ org .osgi .service .event .Event event =
155+ new org .osgi .service .event .Event ("test/topic" , new HashMap <>());
156+
157+ try (MockedStatic <Preferences > prefMock =
158+ Mockito .mockStatic (Preferences .class , Mockito .CALLS_REAL_METHODS )) {
159+
160+ prefMock .when (Preferences ::getApiKey ).thenReturn ("dummyApiKey" );
161+
162+ assertDoesNotThrow (() ->
163+ Display .getDefault ().syncExec (() -> checkmarxView .handleEvent (event ))
164+ );
165+ }
166+ }
167+
168+ @ Test
169+ void testStaticFieldsNotNull () {
170+
171+ assertNotNull (CheckmarxView .ID );
172+ assertNotNull (CheckmarxView .CHECKMARX_OPEN_SETTINGS_LOGO );
173+ assertNotNull (CheckmarxView .CRITICAL_SEVERITY );
174+ assertNotNull (CheckmarxView .HIGH_SEVERITY );
175+ assertNotNull (CheckmarxView .MEDIUM_SEVERITY );
176+ assertNotNull (CheckmarxView .LOW_SEVERITY );
177+ assertNotNull (CheckmarxView .INFO_SEVERITY );
178+ assertNotNull (CheckmarxView .USER );
179+ assertNotNull (CheckmarxView .CREATED_AT_IMAGE );
180+ assertNotNull (CheckmarxView .COMMENT );
181+ assertNotNull (CheckmarxView .STATE );
182+ assertNotNull (CheckmarxView .BFL );
183+ }
184+
185+ @ Test
186+ void testRemoveCount () throws Exception {
187+ Method method = CheckmarxView .class .getDeclaredMethod ("removeCount" , String .class );
188+ method .setAccessible (true );
189+
190+ String result = (String ) method .invoke (null , "High (5)" );
191+
192+ assertEquals ("High" , result );
193+ }
194+
195+ @ Test
196+ void testGetLatestScanFromScanList () throws Exception {
197+
198+ Scan scan1 = Mockito .mock (Scan .class );
199+ Scan scan2 = Mockito .mock (Scan .class );
200+
201+ List <Scan > scans = Arrays .asList (scan1 , scan2 );
202+
203+ Method method = CheckmarxView .class .getDeclaredMethod ("getLatestScanFromScanList" , List .class );
204+ method .setAccessible (true );
205+
206+ Scan result = (Scan ) method .invoke (checkmarxView , scans );
207+
208+ assertEquals (scan1 , result );
209+ }
210+
211+ @ Test
212+ void testGetProjectFromIdFound () throws Exception {
213+
214+ Project project = Mockito .mock (Project .class );
215+ Mockito .when (project .getId ()).thenReturn ("123" );
216+ Mockito .when (project .getName ()).thenReturn ("DemoProject" );
217+
218+ List <Project > projects = List .of (project );
219+
220+ Method method = CheckmarxView .class .getDeclaredMethod (
221+ "getProjectFromId" , List .class , String .class );
222+ method .setAccessible (true );
223+
224+ String result = (String ) method .invoke (checkmarxView , projects , "123" );
225+
226+ assertEquals ("DemoProject" , result );
227+ }
228+
229+ @ Test
230+ void testGetProjectFromIdNotFound () throws Exception {
231+
232+ Project project = Mockito .mock (Project .class );
233+ Mockito .when (project .getId ()).thenReturn ("123" );
234+
235+ List <Project > projects = List .of (project );
236+
237+ Method method = CheckmarxView .class .getDeclaredMethod (
238+ "getProjectFromId" , List .class , String .class );
239+ method .setAccessible (true );
240+
241+ String result = (String ) method .invoke (checkmarxView , projects , "999" );
242+
243+ assertEquals ("Select a project" , result );
244+ }
245+
246+ @ Test
247+ void testGetProjectFromIdEmptyList () throws Exception {
248+
249+ Method method = CheckmarxView .class .getDeclaredMethod (
250+ "getProjectFromId" , List .class , String .class );
251+ method .setAccessible (true );
252+
253+ String result = (String ) method .invoke (checkmarxView , Collections .emptyList (), "123" );
254+
255+ assertEquals ("No projects available." , result );
256+ }
257+
258+ @ Test
259+ void testFormatScanLabelNormal () throws Exception {
260+
261+ Scan scan = Mockito .mock (Scan .class );
262+
263+ Mockito .when (scan .getId ()).thenReturn ("scan123" );
264+ Mockito .when (scan .getUpdatedAt ()).thenReturn ("2024-01-01T10:00:00Z" );
265+
266+ Field latestScanField = CheckmarxView .class .getDeclaredField ("latestScanId" );
267+ latestScanField .setAccessible (true );
268+ latestScanField .set (checkmarxView , "otherScan" );
269+
270+ Method method = CheckmarxView .class .getDeclaredMethod ("formatScanLabel" , Scan .class );
271+ method .setAccessible (true );
272+
273+ String label = (String ) method .invoke (checkmarxView , scan );
274+
275+ assertTrue (label .contains ("scan123" ));
276+ }
277+
278+ @ Test
279+ void testFormatScanLabelLatest () throws Exception {
280+
281+ Scan scan = Mockito .mock (Scan .class );
282+
283+ Mockito .when (scan .getId ()).thenReturn ("scan999" );
284+ Mockito .when (scan .getUpdatedAt ()).thenReturn ("2024-01-01T10:00:00Z" );
285+
286+ Field latestScanField = CheckmarxView .class .getDeclaredField ("latestScanId" );
287+ latestScanField .setAccessible (true );
288+ latestScanField .set (checkmarxView , "scan999" );
289+
290+ Method method = CheckmarxView .class .getDeclaredMethod ("formatScanLabel" , Scan .class );
291+ method .setAccessible (true );
292+
293+ String label = (String ) method .invoke (checkmarxView , scan );
294+
295+ assertTrue (label .contains ("latest" ));
296+ }
297+
298+ @ Test
299+ void testGetProjectsSuccess () throws Exception {
300+
301+ List <Project > projects = List .of (Mockito .mock (Project .class ));
302+
303+ DataProvider provider = Mockito .mock (DataProvider .class );
304+ Mockito .when (provider .getProjects ()).thenReturn (projects );
305+
306+ try (MockedStatic <DataProvider > mocked = Mockito .mockStatic (DataProvider .class )) {
307+
308+ mocked .when (DataProvider ::getInstance ).thenReturn (provider );
309+
310+ Method method = CheckmarxView .class .getDeclaredMethod ("getProjects" );
311+ method .setAccessible (true );
312+
313+ List <Project > result = (List <Project >) method .invoke (checkmarxView );
314+
315+ assertEquals (1 , result .size ());
316+ }
317+ }
318+
319+ @ Test
320+ void testGetProjectsException () throws Exception {
321+
322+ DataProvider provider = Mockito .mock (DataProvider .class );
323+
324+ Mockito .when (provider .getProjects ()).thenThrow (new RuntimeException ("error" ));
325+
326+ try (MockedStatic <DataProvider > mocked = Mockito .mockStatic (DataProvider .class )) {
327+
328+ mocked .when (DataProvider ::getInstance ).thenReturn (provider );
329+
330+ Method method = CheckmarxView .class .getDeclaredMethod ("getProjects" );
331+ method .setAccessible (true );
332+
333+ List <Project > result = (List <Project >) method .invoke (checkmarxView );
334+
335+ assertNotNull (result );
336+ }
337+ }
338+
339+ @ Test
340+ void testUpdateStartScanButtonEnabled () throws Exception {
341+
342+ ToolBarActions toolBarActions = Mockito .mock (ToolBarActions .class );
343+ Action startAction = Mockito .mock (Action .class );
344+
345+ Mockito .when (toolBarActions .getStartScanAction ()).thenReturn (startAction );
346+
347+ Field field = CheckmarxView .class .getDeclaredField ("toolBarActions" );
348+ field .setAccessible (true );
349+ field .set (checkmarxView , toolBarActions );
350+
351+ Method method = CheckmarxView .class .getDeclaredMethod ("updateStartScanButton" , boolean .class );
352+ method .setAccessible (true );
353+
354+ method .invoke (checkmarxView , true );
355+
356+ Mockito .verify (startAction ).setEnabled (Mockito .anyBoolean ());
357+ }
358+
359+ private void injectDependencies () throws Exception {
360+
361+ ToolBarActions toolbar = Mockito .mock (ToolBarActions .class );
362+ Action action = Mockito .mock (Action .class );
363+
364+ Mockito .when (toolbar .getStartScanAction ()).thenReturn (action );
365+
366+ Field toolbarField = CheckmarxView .class .getDeclaredField ("toolBarActions" );
367+ toolbarField .setAccessible (true );
368+ toolbarField .set (checkmarxView , toolbar );
369+
370+ Field parentField = CheckmarxView .class .getDeclaredField ("parent" );
371+ parentField .setAccessible (true );
372+ parentField .set (checkmarxView , parent );
373+ }
374+ }
0 commit comments