1+ package com .jfrog .ide .eclipse .ui .webview ;
2+
3+ import com .jfrog .ide .common .nodes .FileIssueNode ;
4+ import com .jfrog .ide .common .nodes .ScaIssueNode ;
5+ import com .jfrog .ide .common .nodes .SastIssueNode ;
6+ import com .jfrog .ide .common .nodes .subentities .ImpactPath ;
7+ import com .jfrog .ide .common .nodes .subentities .Severity ;
8+ import com .jfrog .ide .common .nodes .subentities .SourceCodeScanType ;
9+ import com .jfrog .ide .common .parse .Applicability ;
10+ import com .jfrog .ide .common .webview .*;
11+ import org .junit .Before ;
12+ import org .junit .Test ;
13+
14+ import static org .junit .Assert .assertEquals ;
15+ import static org .junit .Assert .assertNotNull ;
16+ import static org .junit .Assert .assertTrue ;
17+
18+ import java .util .ArrayList ;
19+ import java .util .List ;
20+
21+ public class WebviewObjectConverterTest {
22+
23+ private ScaIssueNode scaIssueNode ;
24+ private FileIssueNode secretIssueNode ;
25+ private SastIssueNode sastIssueNode ;
26+
27+ @ Before
28+ void setUp () {
29+ // Setup SCA test data
30+ String scaTitle = "CVE-2023-1234" ;
31+ String reason = "sca issue reason" ;
32+ Severity severity = Severity .High ;
33+ String ruleID = "CVE-2023-1234_test-component_1.0.0" ;
34+ Applicability applicability = Applicability .APPLICABLE ;
35+ List <List <ImpactPath >> impactPaths = createTestImpactPaths ();
36+ String [] fixedVersions = {"[1.0.1]" , "[1.0.2]" };
37+ String fullDescription = "Test vulnerability description" ;
38+
39+ scaIssueNode = new ScaIssueNode (scaTitle , reason , severity , ruleID , applicability , impactPaths , fixedVersions , fullDescription );
40+
41+ // setup common data
42+ String filePath = "/test/path/file.java" ;
43+ int rowStart = 10 ;
44+ int colStart = 5 ;
45+ int rowEnd = 20 ;
46+ int colEnd = 10 ;
47+ String lineSnippet = "vulnerable code line" ;
48+
49+ // setup secrets test data
50+ String secretTitle = "Secret issue" ;
51+ String secretReason = "Hard coded secrets were found" ;
52+ Severity secretSeverity = Severity .Medium ;
53+ String secretRuleId = "SECRET-RULE" ;
54+ String secretFullDescription = "Test Secret issue description" ;
55+
56+ secretIssueNode = new FileIssueNode (secretTitle , filePath , rowStart , colStart , rowEnd , colEnd , secretReason , lineSnippet , SourceCodeScanType .SECRETS , secretSeverity , secretFullDescription );
57+
58+
59+ // setup sast test data
60+ String sastTitle = "SAST Issue" ;
61+ String sastReason = "SAST issue reason" ;
62+ Severity sastSeverity = Severity .Critical ;
63+ String sastRuleId = "SAST-RULE" ;
64+ String sastFullDescription = "Test SAST issue description" ;
65+
66+ sastIssueNode = new SastIssueNode (sastTitle , filePath , rowStart , colStart , rowEnd , colEnd , sastReason , lineSnippet , null , sastSeverity , sastRuleId , sastFullDescription );
67+ }
68+
69+ @ Test
70+ void testConvertScaIssueToDepPage () {
71+ DependencyPage result = WebviewObjectConverter .convertScaIssueToDepPage (scaIssueNode );
72+
73+ assertNotNull (result );
74+ assertEquals (scaIssueNode .getComponentName (), result .getComponent ());
75+ assertEquals (scaIssueNode .getComponentVersion (), result .getVersion ());
76+ assertEquals (scaIssueNode .getSeverity ().getSeverityName (), result .getSeverity ());
77+ assertEquals (scaIssueNode .getFullDescription (), result .getSummary ());
78+ assertEquals (scaIssueNode .getFixedVersions (), result .getFixedVersion ());
79+ assertNotNull (result .getCve ());
80+ assertEquals (scaIssueNode .getTitle (), result .getCve ().getId ());
81+ }
82+
83+ @ Test
84+ void testConvertFileIssueToIssuePage () {
85+ IssuePage result = WebviewObjectConverter .convertFileIssueToIssuePage (secretIssueNode );
86+
87+ assertNotNull (result );
88+ assertEquals (secretIssueNode .getTitle (), result .getHeader ());
89+ assertEquals (secretIssueNode .getSeverity ().name (), result .getSeverity ());
90+ assertEquals (secretIssueNode .getFullDescription (), result .getDescription ());
91+ assertNotNull (result .getLocation ());
92+ assertEquals (secretIssueNode .getFilePath (), result .getLocation ().getFile ());
93+ assertEquals (secretIssueNode .getRowStart () + 1 , result .getLocation ().getStartRow ());
94+ assertEquals (secretIssueNode .getColStart () + 1 , result .getLocation ().getStartColumn ());
95+ }
96+
97+ @ Test
98+ void testConvertSastIssueToSastIssuePage () {
99+ IssuePage result = WebviewObjectConverter .convertSastIssueToSastIssuePage (sastIssueNode );
100+
101+ assertNotNull (result );
102+ assertTrue (result instanceof SastIssuePage );
103+ SastIssuePage sastResult = (SastIssuePage ) result ;
104+ assertEquals (sastIssueNode .getTitle (), sastResult .getHeader ());
105+ assertEquals (sastIssueNode .getRuleId (), sastResult .getRuleId ());
106+ assertEquals (sastIssueNode .getSeverity ().name (), sastResult .getSeverity ());
107+ assertEquals (sastIssueNode .getFullDescription (), sastResult .getDescription ());
108+ }
109+
110+ @ Test
111+ void testToImpactGraph_EmptyInput () {
112+ ImpactGraph result = WebviewObjectConverter .toImpactGraph (null );
113+
114+ assertNotNull (result );
115+ assertNotNull (result .getRoot ());
116+ assertEquals ("" , result .getRoot ().getName ());
117+ assertEquals (0 , result .getRoot ().getChildren ().length );
118+ }
119+
120+ @ Test
121+ void testToImpactGraph_SinglePath () {
122+ List <List <ImpactPath >> impactPaths = new ArrayList <>();
123+ List <ImpactPath > path = new ArrayList <>();
124+ path .add (new ImpactPath ("root" , "1.0" ));
125+ path .add (new ImpactPath ("child1" , "2.0" ));
126+ path .add (new ImpactPath ("child2" , "3.0" ));
127+ impactPaths .add (path );
128+
129+ ImpactGraph result = WebviewObjectConverter .toImpactGraph (impactPaths );
130+
131+ assertNotNull (result );
132+ assertNotNull (result .getRoot ());
133+ assertEquals ("root:1.0" , result .getRoot ().getName ());
134+ assertEquals (1 , result .getRoot ().getChildren ().length );
135+ assertEquals ("child1:2.0" , result .getRoot ().getChildren ()[0 ].getName ());
136+ assertEquals (1 , result .getRoot ().getChildren ()[0 ].getChildren ().length );
137+ assertEquals ("child2:3.0" , result .getRoot ().getChildren ()[0 ].getChildren ()[0 ].getName ());
138+ }
139+
140+ @ Test
141+ void testToImpactGraph_MultiplePaths () {
142+ List <List <ImpactPath >> impactPaths = new ArrayList <>();
143+
144+ // First path
145+ List <ImpactPath > path1 = new ArrayList <>();
146+ path1 .add (new ImpactPath ("root" , "1.0" ));
147+ path1 .add (new ImpactPath ("child1" , "2.0" ));
148+ impactPaths .add (path1 );
149+
150+ // Second path
151+ List <ImpactPath > path2 = new ArrayList <>();
152+ path2 .add (new ImpactPath ("root" , "1.0" ));
153+ path2 .add (new ImpactPath ("child2" , "3.0" ));
154+ impactPaths .add (path2 );
155+
156+ ImpactGraph result = WebviewObjectConverter .toImpactGraph (impactPaths );
157+
158+ assertNotNull (result );
159+ assertNotNull (result .getRoot ());
160+ assertEquals ("root:1.0" , result .getRoot ().getName ());
161+ assertEquals (2 , result .getRoot ().getChildren ().length );
162+
163+ // Verify both children exist
164+ boolean hasChild1 = false ;
165+ boolean hasChild2 = false ;
166+ for (ImpactGraphNode child : result .getRoot ().getChildren ()) {
167+ if (child .getName ().equals ("child1:2.0" )) hasChild1 = true ;
168+ if (child .getName ().equals ("child2:3.0" )) hasChild2 = true ;
169+ }
170+ assertTrue (hasChild1 && hasChild2 );
171+ }
172+
173+ @ Test
174+ void testToImpactGraph_ExceedsLimit () {
175+ List <List <ImpactPath >> impactPaths = new ArrayList <>();
176+ for (int i = 0 ; i < WebviewObjectConverter .IMPACT_PATHS_LIMIT + 5 ; i ++) {
177+ List <ImpactPath > path = new ArrayList <>();
178+ path .add (new ImpactPath ("root" + i , "1.0" ));
179+ path .add (new ImpactPath ("child" + i , "2.0" ));
180+ impactPaths .add (path );
181+ }
182+
183+ ImpactGraph result = WebviewObjectConverter .toImpactGraph (impactPaths );
184+
185+ assertNotNull (result );
186+ assertEquals (WebviewObjectConverter .IMPACT_PATHS_LIMIT , result .getPathsLimit ());
187+ }
188+
189+ private List <List <ImpactPath >> createTestImpactPaths () {
190+ List <List <ImpactPath >> impactPaths = new ArrayList <>();
191+ List <ImpactPath > path = new ArrayList <>();
192+ path .add (new ImpactPath ("root" , "1.0" ));
193+ path .add (new ImpactPath ("child1" , "2.0" ));
194+ path .add (new ImpactPath ("child2" , "3.0" ));
195+ impactPaths .add (path );
196+ return impactPaths ;
197+ }
198+ }
0 commit comments