-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Antoine MAZEAS <[email protected]>
- Loading branch information
1 parent
60c6534
commit cd7cf52
Showing
8 changed files
with
197 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,49 @@ | ||
package io.openbas.utils; | ||
|
||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.function.Function; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
public class ZipUtils { | ||
// not this only works | ||
public static String getZipEntryAsString(byte[] zipBlob, String entryName) throws IOException { | ||
public static <T> T getZipEntry(byte[] zipBlob, String entryName, Function<InputStream, T> readFunc) throws IOException { | ||
try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipBlob))) { | ||
ZipEntry entry; | ||
while ((entry = zis.getNextEntry()) != null) { | ||
if (entry.getName().equals(entryName)) { | ||
StringBuilder sb = new StringBuilder(); | ||
byte[] buffer = new byte[1024]; | ||
int read = 0; | ||
while ((read = zis.read(buffer, 0, 1024)) >= 0) { | ||
sb.append(new String(buffer, 0, read)); | ||
} | ||
// force exit of test since we have found the correct entry | ||
return sb.toString(); | ||
return readFunc.apply(zis); | ||
} | ||
} | ||
// no zip entry corresponding to expected json | ||
throw new IOException("Zip entry '%s' not found".formatted(entryName)); | ||
} | ||
} | ||
|
||
public static String streamToString(InputStream is) { | ||
StringBuilder sb = new StringBuilder(); | ||
byte[] buffer = new byte[1024]; | ||
int read = 0; | ||
while (true) { | ||
try { | ||
if (!((read = is.read(buffer, 0, 1024)) >= 0)) break; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
sb.append(new String(buffer, 0, read)); | ||
} | ||
// force exit of test since we have found the correct entry | ||
return sb.toString(); | ||
} | ||
|
||
public static byte[] streamToBytes(InputStream is) { | ||
try { | ||
return is.readAllBytes(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
openbas-api/src/test/java/io/openbas/utils/fixtures/DocumentFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
package io.openbas.utils.fixtures; | ||
|
||
import io.openbas.database.model.Document; | ||
import io.openbas.utils.fixtures.files.BaseFile; | ||
|
||
public class DocumentFixture { | ||
|
||
public static final String DOCUMENT_NAME = "A document"; | ||
public static final String TXT_DOCUMENT_NAME = "My text document"; | ||
|
||
public static Document getDocumentJpeg() { | ||
Document document = new Document(); | ||
document.setName(DOCUMENT_NAME); | ||
document.setType("image/jpeg"); | ||
return document; | ||
} | ||
|
||
public static Document getDocumentTxt(BaseFile<String> plainTextFile) { | ||
Document document = new Document(); | ||
document.setName(TXT_DOCUMENT_NAME); | ||
document.setType(plainTextFile.getMimeType()); | ||
document.setTarget(plainTextFile.getFileName()); | ||
return document; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
openbas-api/src/test/java/io/openbas/utils/fixtures/FileFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.openbas.utils.fixtures; | ||
|
||
|
||
import io.openbas.utils.fixtures.files.PlainTextFile; | ||
|
||
|
||
public class FileFixture { | ||
public static String DEFAULT_PLAIN_TEXT_CONTENT = "default plain text content"; | ||
public static String DEFAULT_PLAIN_TEXT_FILENAME = "plain_text.txt"; | ||
|
||
public static PlainTextFile getPlainTextFileContent() { | ||
return new PlainTextFile(DEFAULT_PLAIN_TEXT_CONTENT, DEFAULT_PLAIN_TEXT_FILENAME); | ||
} | ||
} |
Oops, something went wrong.