Skip to content

Commit 75ec29f

Browse files
committed
chore: SP-2487 add extractFilePathFromWFP() utils function
1 parent 0ec8201 commit 75ec29f

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/main/java/com/scanoss/utils/WinnowingUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package com.scanoss.utils;
2424

25+
import org.jetbrains.annotations.NotNull;
26+
2527
/**
2628
* SCANOSS Winnowing Utils Class
2729
* <p>
@@ -47,4 +49,31 @@ public static char normalize(char c) {
4749
return 0;
4850
}
4951
}
52+
53+
/**
54+
* Extract the file path from a WFP.
55+
* The format of the input string is as follows:
56+
* file=e8585d8740d6664fda9e242a1d68b0f0,1815,demo.ts
57+
* 4=30777ca8,e9227657,81c8e7e1
58+
* 9=831bd2c5,701a2c74
59+
* This method extracts and returns the `demo.ts` part.
60+
*
61+
* @param wfpEntry the WFP entry as a string
62+
* @return the extracted file path, or null if not found
63+
*/
64+
public static String extractFilePathFromWFP(@NotNull String wfpEntry) {
65+
if (wfpEntry.isEmpty()) {
66+
return null;
67+
}
68+
for (String line : wfpEntry.split("\n")) {
69+
if (line.startsWith("file=")) {
70+
String[] parts = line.split(",");
71+
if (parts.length == 3) {
72+
return parts[2]; // The file path is the 3rd element
73+
}
74+
}
75+
}
76+
return null;
77+
}
78+
5079
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.scanoss.utils;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class WinnowingUtilsTest {
8+
9+
@Test
10+
public void testExtractFilePathFromWFP_ValidEntry_ReturnsFilePath() {
11+
String wfpEntry = "file=sha1,md5,/path/to/file\nanother=line";
12+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
13+
assertEquals("/path/to/file", result);
14+
}
15+
16+
@Test
17+
public void testExtractFilePathFromWFP_EmptyString_ReturnsNull() {
18+
String wfpEntry = "";
19+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
20+
assertNull(result);
21+
}
22+
23+
@Test
24+
public void testExtractFilePathFromWFP_NoFileLine_ReturnsNull() {
25+
String wfpEntry = "another=line\nsomething=else";
26+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
27+
assertNull(result);
28+
}
29+
30+
@Test
31+
public void testExtractFilePathFromWFP_InvalidFileLineFormat_ReturnsNull() {
32+
String wfpEntry = "file=sha1,md5\nfile=incorrect,format";
33+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
34+
assertNull(result);
35+
}
36+
37+
@Test
38+
public void testExtractFilePathFromWFP_MultipleLinesWithValidFileLine_ReturnsFilePath() {
39+
String wfpEntry = "something=else\nfile=sha1,md5,/valid/path\nanother=line";
40+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
41+
assertEquals("/valid/path", result);
42+
}
43+
44+
@Test
45+
public void testExtractFilePathFromWFP_FileLineWithExtraCommas_ReturnsNull() {
46+
String wfpEntry = "file=sha1,md5,/path,with,comma";
47+
String result = WinnowingUtils.extractFilePathFromWFP(wfpEntry);
48+
assertNull(result);
49+
}
50+
51+
@Test(expected = NullPointerException.class)
52+
public void testExtractFilePathFromWFP_NullInput_ThrowsNullPointerException() {
53+
String wfpEntry = null;
54+
WinnowingUtils.extractFilePathFromWFP(wfpEntry);
55+
}
56+
}

0 commit comments

Comments
 (0)