subscription: Port subscription import-export to workers from refactor

Please see https://github.com/TeamNewPipe/NewPipe/pull/11759/ for the original change

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta 2026-03-15 20:51:29 +08:00
parent 0d65733e53
commit 6fa97e17f5
20 changed files with 569 additions and 1016 deletions

View file

@ -5,11 +5,11 @@ import static org.junit.Assert.fail;
import org.junit.Test;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.extractor.subscription.SubscriptionItem;
import org.schabi.newpipe.local.subscription.workers.ImportExportJsonHelper;
import org.schabi.newpipe.local.subscription.workers.SubscriptionItem;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
@ -23,26 +23,22 @@ public class ImportExportJsonHelperTest {
final String emptySource =
"{\"app_version\":\"0.11.6\",\"app_version_int\": 47,\"subscriptions\":[]}";
final List<SubscriptionItem> items = ImportExportJsonHelper.readFrom(
new ByteArrayInputStream(emptySource.getBytes(StandardCharsets.UTF_8)), null);
final var items = ImportExportJsonHelper.readFrom(
new ByteArrayInputStream(emptySource.getBytes(StandardCharsets.UTF_8)));
assertTrue(items.isEmpty());
}
@Test
public void testInvalidSource() {
final List<String> invalidList = Arrays.asList(
"{}",
"",
null,
"gibberish");
final var invalidList = Arrays.asList("{}", "", null, "gibberish");
for (final String invalidContent : invalidList) {
try {
if (invalidContent != null) {
final byte[] bytes = invalidContent.getBytes(StandardCharsets.UTF_8);
ImportExportJsonHelper.readFrom((new ByteArrayInputStream(bytes)), null);
ImportExportJsonHelper.readFrom(new ByteArrayInputStream(bytes));
} else {
ImportExportJsonHelper.readFrom(null, null);
ImportExportJsonHelper.readFrom(null);
}
fail("didn't throw exception");
@ -58,38 +54,24 @@ public class ImportExportJsonHelperTest {
@Test
public void ultimateTest() throws Exception {
// Read from file
final List<SubscriptionItem> itemsFromFile = readFromFile();
final var itemsFromFile = readFromFile();
// Test writing to an output
final String jsonOut = testWriteTo(itemsFromFile);
// Read again
final List<SubscriptionItem> itemsSecondRead = readFromWriteTo(jsonOut);
final var itemsSecondRead = readFromWriteTo(jsonOut);
// Check if both lists have the exact same items
if (itemsFromFile.size() != itemsSecondRead.size()) {
if (!itemsFromFile.equals(itemsSecondRead)) {
fail("The list of items were different from each other");
}
for (int i = 0; i < itemsFromFile.size(); i++) {
final SubscriptionItem item1 = itemsFromFile.get(i);
final SubscriptionItem item2 = itemsSecondRead.get(i);
final boolean equals = item1.getServiceId() == item2.getServiceId()
&& item1.getUrl().equals(item2.getUrl())
&& item1.getName().equals(item2.getName());
if (!equals) {
fail("The list of items were different from each other");
}
}
}
private List<SubscriptionItem> readFromFile() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
"import_export_test.json");
final List<SubscriptionItem> itemsFromFile = ImportExportJsonHelper.readFrom(
inputStream, null);
final var inputStream = getClass().getClassLoader()
.getResourceAsStream("import_export_test.json");
final var itemsFromFile = ImportExportJsonHelper.readFrom(inputStream);
if (itemsFromFile.isEmpty()) {
fail("ImportExportJsonHelper.readFrom(input) returned a null or empty list");
@ -98,10 +80,10 @@ public class ImportExportJsonHelperTest {
return itemsFromFile;
}
private String testWriteTo(final List<SubscriptionItem> itemsFromFile) throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
ImportExportJsonHelper.writeTo(itemsFromFile, out, null);
final String jsonOut = out.toString("UTF-8");
private String testWriteTo(final List<SubscriptionItem> itemsFromFile) {
final var out = new ByteArrayOutputStream();
ImportExportJsonHelper.writeTo(itemsFromFile, out);
final String jsonOut = out.toString(StandardCharsets.UTF_8);
if (jsonOut.isEmpty()) {
fail("JSON returned by writeTo was empty");
@ -111,10 +93,8 @@ public class ImportExportJsonHelperTest {
}
private List<SubscriptionItem> readFromWriteTo(final String jsonOut) throws Exception {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(
jsonOut.getBytes(StandardCharsets.UTF_8));
final List<SubscriptionItem> secondReadItems = ImportExportJsonHelper.readFrom(
inputStream, null);
final var inputStream = new ByteArrayInputStream(jsonOut.getBytes(StandardCharsets.UTF_8));
final var secondReadItems = ImportExportJsonHelper.readFrom(inputStream);
if (secondReadItems.isEmpty()) {
fail("second call to readFrom returned an empty list");