1+ package checkmarx .ast .eclipse .plugin .tests .unit .views .filters ;
2+
3+ import com .checkmarx .eclipse .views .filters .ActionFilters ;
4+ import com .checkmarx .eclipse .enums .ActionName ;
5+ import com .checkmarx .eclipse .enums .PluginListenerType ;
6+ import com .checkmarx .eclipse .enums .Severity ;
7+ import com .checkmarx .eclipse .views .DataProvider ;
8+ import com .checkmarx .eclipse .views .PluginListenerDefinition ;
9+ import com .google .common .eventbus .EventBus ;
10+ import org .eclipse .jface .action .Action ;
11+ import org .eclipse .jface .resource .ImageDescriptor ;
12+ import org .junit .jupiter .api .BeforeEach ;
13+ import org .junit .jupiter .api .Test ;
14+ import org .mockito .ArgumentCaptor ;
15+ import org .mockito .MockedStatic ;
16+ import org .mockito .Mockito ;
17+
18+ import java .util .List ;
19+
20+ import static org .junit .jupiter .api .Assertions .*;
21+ import static org .mockito .Mockito .*;
22+
23+ class ActionFiltersTest {
24+ private EventBus mockEventBus ;
25+ private ActionFilters actionFilters ;
26+
27+ @ BeforeEach
28+ void setUp () {
29+ mockEventBus = mock (EventBus .class );
30+ actionFilters = new ActionFilters (mockEventBus );
31+ }
32+
33+ @ Test
34+ void testCreateFilterActions_propertiesAndState () {
35+ try (MockedStatic <DataProvider > dp = Mockito .mockStatic (DataProvider .class );
36+ MockedStatic <com .checkmarx .eclipse .views .filters .FilterState > fs = Mockito .mockStatic (com .checkmarx .eclipse .views .filters .FilterState .class );
37+ MockedStatic <com .checkmarx .eclipse .Activator > activator = Mockito .mockStatic (com .checkmarx .eclipse .Activator .class )) {
38+
39+ DataProvider provider = mock (DataProvider .class );
40+ dp .when (DataProvider ::getInstance ).thenReturn (provider );
41+ when (provider .containsResults ()).thenReturn (true );
42+ fs .when (() -> com .checkmarx .eclipse .views .filters .FilterState .isSeverityEnabled (anyString ())).thenReturn (true );
43+ activator .when (() -> com .checkmarx .eclipse .Activator .getImageDescriptor (anyString ())).thenReturn (mock (ImageDescriptor .class ));
44+
45+ List <Action > actions = actionFilters .createFilterActions ();
46+ assertEquals (5 , actions .size ());
47+ assertEquals (ActionName .CRITICAL .name (), actions .get (0 ).getId ());
48+ assertEquals (ActionName .HIGH .name (), actions .get (1 ).getId ());
49+ assertEquals (ActionName .MEDIUM .name (), actions .get (2 ).getId ());
50+ assertEquals (ActionName .LOW .name (), actions .get (3 ).getId ());
51+ assertEquals (ActionName .INFO .name (), actions .get (4 ).getId ());
52+ for (Action action : actions ) {
53+ assertTrue (action .isEnabled ());
54+ assertTrue (action .isChecked ());
55+ assertNotNull (action .getToolTipText ());
56+ assertNotNull (action .getImageDescriptor ());
57+ }
58+ }
59+ }
60+
61+ @ Test
62+ void testCreateFilterActions_runActionPostsEvent () {
63+ try (MockedStatic <DataProvider > dp = Mockito .mockStatic (DataProvider .class );
64+ MockedStatic <com .checkmarx .eclipse .views .filters .FilterState > fs = Mockito .mockStatic (com .checkmarx .eclipse .views .filters .FilterState .class );
65+ MockedStatic <com .checkmarx .eclipse .Activator > activator = Mockito .mockStatic (com .checkmarx .eclipse .Activator .class );
66+ MockedStatic <PluginListenerDefinition > pld = Mockito .mockStatic (PluginListenerDefinition .class , Mockito .CALLS_REAL_METHODS )) {
67+
68+ DataProvider provider = mock (DataProvider .class );
69+ dp .when (DataProvider ::getInstance ).thenReturn (provider );
70+ when (provider .containsResults ()).thenReturn (true );
71+ fs .when (() -> com .checkmarx .eclipse .views .filters .FilterState .isSeverityEnabled (anyString ())).thenReturn (true );
72+ fs .when (() -> com .checkmarx .eclipse .views .filters .FilterState .setState (any (Severity .class ))).thenReturn (null );
73+ activator .when (() -> com .checkmarx .eclipse .Activator .getImageDescriptor (anyString ())).thenReturn (mock (ImageDescriptor .class ));
74+ when (provider .sortResults ()).thenReturn (mock (List .class ));
75+
76+ List <Action > actions = actionFilters .createFilterActions ();
77+ Action criticalAction = actions .get (0 );
78+ criticalAction .run ();
79+ ArgumentCaptor <PluginListenerDefinition > captor = ArgumentCaptor .forClass (PluginListenerDefinition .class );
80+ verify (mockEventBus , atLeastOnce ()).post (captor .capture ());
81+ PluginListenerDefinition event = captor .getValue ();
82+ assertEquals (PluginListenerType .FILTER_CHANGED , event .getListenerType ());
83+ }
84+ }
85+
86+ @ Test
87+ void testCreateFilterActions_disabledUnchecked () {
88+ try (MockedStatic <DataProvider > dp = Mockito .mockStatic (DataProvider .class );
89+ MockedStatic <com .checkmarx .eclipse .views .filters .FilterState > fs = Mockito .mockStatic (com .checkmarx .eclipse .views .filters .FilterState .class );
90+ MockedStatic <com .checkmarx .eclipse .Activator > activator = Mockito .mockStatic (com .checkmarx .eclipse .Activator .class )) {
91+
92+ DataProvider provider = mock (DataProvider .class );
93+ dp .when (DataProvider ::getInstance ).thenReturn (provider );
94+ when (provider .containsResults ()).thenReturn (false );
95+ fs .when (() -> com .checkmarx .eclipse .views .filters .FilterState .isSeverityEnabled (anyString ())).thenReturn (false );
96+ activator .when (() -> com .checkmarx .eclipse .Activator .getImageDescriptor (anyString ())).thenReturn (mock (ImageDescriptor .class ));
97+
98+ List <Action > actions = actionFilters .createFilterActions ();
99+ for (Action action : actions ) {
100+ assertFalse (action .isEnabled ());
101+ assertFalse (action .isChecked ());
102+ }
103+ }
104+ }
105+ }
0 commit comments