1+ package checkmarx .ast .eclipse .plugin .tests .unit .utils ;
2+
3+ import static org .junit .jupiter .api .Assertions .*;
4+ import static org .mockito .Mockito .*;
5+
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+
9+ import org .eclipse .jface .action .Action ;
10+ import org .eclipse .jface .viewers .ComboViewer ;
11+ import org .eclipse .jface .viewers .TreeViewer ;
12+ import org .eclipse .swt .widgets .Combo ;
13+ import org .junit .jupiter .api .Test ;
14+ import org .mockito .MockedStatic ;
15+ import org .mockito .Mockito ;
16+
17+ import com .checkmarx .eclipse .properties .Preferences ;
18+ import com .checkmarx .eclipse .utils .PluginUtils ;
19+ import com .checkmarx .eclipse .views .DataProvider ;
20+ import com .checkmarx .eclipse .views .DisplayModel ;
21+ import com .checkmarx .eclipse .views .filters .FilterState ;
22+
23+ public class PluginUtilsTest {
24+
25+ @ Test
26+ void testConvertStringTimeStampValid () {
27+ String input = "2024-01-01T10:00:00Z" ;
28+
29+ String result = PluginUtils .convertStringTimeStamp (input );
30+
31+ assertNotNull (result );
32+ assertTrue (result .contains ("2024" ));
33+ }
34+
35+ @ Test
36+ void testConvertStringTimeStampInvalid () {
37+ String input = "invalid-date" ;
38+
39+ String result = PluginUtils .convertStringTimeStamp (input );
40+
41+ assertEquals (input , result );
42+ }
43+
44+ @ Test
45+ void testValidateScanIdFormatValid () {
46+ String scanId = "d61aaad4-38fb-406c-bf54-b9b8475f81a5" ;
47+
48+ boolean result = PluginUtils .validateScanIdFormat (scanId );
49+
50+ assertTrue (result );
51+ }
52+
53+ @ Test
54+ void testValidateScanIdFormatInvalid () {
55+ String scanId = "invalid-scan-id" ;
56+
57+ boolean result = PluginUtils .validateScanIdFormat (scanId );
58+
59+ assertFalse (result );
60+ }
61+
62+ @ Test
63+ void testEnableComboViewer () {
64+ ComboViewer viewer = mock (ComboViewer .class );
65+ Combo combo = mock (Combo .class );
66+
67+ when (viewer .getCombo ()).thenReturn (combo );
68+
69+ PluginUtils .enableComboViewer (viewer , true );
70+
71+ verify (combo ).setEnabled (true );
72+ }
73+
74+ @ Test
75+ void testSetTextForComboViewer () {
76+ ComboViewer viewer = mock (ComboViewer .class );
77+ Combo combo = mock (Combo .class );
78+
79+ when (viewer .getCombo ()).thenReturn (combo );
80+
81+ PluginUtils .setTextForComboViewer (viewer , "TestText" );
82+
83+ verify (combo ).setText ("TestText" );
84+ verify (combo ).update ();
85+ }
86+
87+ @ Test
88+ void testUpdateFiltersEnabledAndCheckedState () {
89+
90+ Action action = mock (Action .class );
91+ when (action .getId ()).thenReturn ("LOW" );
92+
93+ List <Action > actions = new ArrayList <>();
94+ actions .add (action );
95+
96+ try (MockedStatic <DataProvider > dp = Mockito .mockStatic (DataProvider .class );
97+ MockedStatic <FilterState > fs = Mockito .mockStatic (FilterState .class )) {
98+
99+ DataProvider provider = mock (DataProvider .class );
100+ dp .when (DataProvider ::getInstance ).thenReturn (provider );
101+ when (provider .containsResults ()).thenReturn (true );
102+
103+ fs .when (() -> FilterState .isSeverityEnabled ("LOW" )).thenReturn (true );
104+
105+ PluginUtils .updateFiltersEnabledAndCheckedState (actions );
106+
107+ verify (action ).setEnabled (true );
108+ verify (action ).setChecked (true );
109+ }
110+ }
111+
112+ @ Test
113+ void testMessageCreation () {
114+ DisplayModel model = PluginUtils .message ("Hello" );
115+
116+ assertNotNull (model );
117+ }
118+
119+ @ Test
120+ void testShowMessage () {
121+
122+ DisplayModel root = new DisplayModel .DisplayModelBuilder ("root" ).build ();
123+ TreeViewer viewer = mock (TreeViewer .class );
124+
125+ PluginUtils .showMessage (root , viewer , "Test message" );
126+
127+ assertEquals (1 , root .children .size ());
128+ verify (viewer ).refresh ();
129+ }
130+
131+ @ Test
132+ void testClearMessage () {
133+
134+ DisplayModel root = new DisplayModel .DisplayModelBuilder ("root" ).build ();
135+ root .children .add (new DisplayModel .DisplayModelBuilder ("child" ).build ());
136+
137+ TreeViewer viewer = mock (TreeViewer .class );
138+
139+ PluginUtils .clearMessage (root , viewer );
140+
141+ assertEquals (0 , root .children .size ());
142+ verify (viewer ).refresh ();
143+ }
144+
145+ @ Test
146+ void testAreCredentialsDefinedTrue () {
147+
148+ try (MockedStatic <Preferences > prefs = Mockito .mockStatic (Preferences .class )) {
149+
150+ prefs .when (Preferences ::getApiKey ).thenReturn ("apikey" );
151+
152+ boolean result = PluginUtils .areCredentialsDefined ();
153+
154+ assertTrue (result );
155+ }
156+ }
157+
158+ @ Test
159+ void testAreCredentialsDefinedFalse () {
160+
161+ try (MockedStatic <Preferences > prefs = Mockito .mockStatic (Preferences .class )) {
162+
163+ prefs .when (Preferences ::getApiKey ).thenReturn ("" );
164+
165+ boolean result = PluginUtils .areCredentialsDefined ();
166+
167+ assertFalse (result );
168+ }
169+ }
170+ }
0 commit comments