Improve import/export tests

This commit is contained in:
Isira Seneviratne 2024-11-28 06:34:39 +05:30
parent 173ad83e0e
commit affd64938b
2 changed files with 12 additions and 33 deletions

View File

@ -16,7 +16,7 @@ class SubscriptionData(
} }
@Serializable @Serializable
class SubscriptionItem( data class SubscriptionItem(
@SerialName("service_id") @SerialName("service_id")
val serviceId: Int, val serviceId: Int,
val url: String, val url: String,

View File

@ -9,7 +9,6 @@ import org.schabi.newpipe.local.subscription.workers.SubscriptionItem;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -23,18 +22,14 @@ public class ImportExportJsonHelperTest {
final String emptySource = final String emptySource =
"{\"app_version\":\"0.11.6\",\"app_version_int\": 47,\"subscriptions\":[]}"; "{\"app_version\":\"0.11.6\",\"app_version_int\": 47,\"subscriptions\":[]}";
final List<SubscriptionItem> items = ImportExportJsonHelper.readFrom( final var items = ImportExportJsonHelper.readFrom(
new ByteArrayInputStream(emptySource.getBytes(StandardCharsets.UTF_8))); new ByteArrayInputStream(emptySource.getBytes(StandardCharsets.UTF_8)));
assertTrue(items.isEmpty()); assertTrue(items.isEmpty());
} }
@Test @Test
public void testInvalidSource() { public void testInvalidSource() {
final List<String> invalidList = Arrays.asList( final var invalidList = Arrays.asList("{}", "", null, "gibberish");
"{}",
"",
null,
"gibberish");
for (final String invalidContent : invalidList) { for (final String invalidContent : invalidList) {
try { try {
@ -58,38 +53,24 @@ public class ImportExportJsonHelperTest {
@Test @Test
public void ultimateTest() throws Exception { public void ultimateTest() throws Exception {
// Read from file // Read from file
final List<SubscriptionItem> itemsFromFile = readFromFile(); final var itemsFromFile = readFromFile();
// Test writing to an output // Test writing to an output
final String jsonOut = testWriteTo(itemsFromFile); final String jsonOut = testWriteTo(itemsFromFile);
// Read again // Read again
final List<SubscriptionItem> itemsSecondRead = readFromWriteTo(jsonOut); final var itemsSecondRead = readFromWriteTo(jsonOut);
// Check if both lists have the exact same items // 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"); 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 { private List<SubscriptionItem> readFromFile() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream( final var inputStream = getClass().getClassLoader()
"import_export_test.json"); .getResourceAsStream("import_export_test.json");
final List<SubscriptionItem> itemsFromFile = ImportExportJsonHelper.readFrom( final var itemsFromFile = ImportExportJsonHelper.readFrom(inputStream);
inputStream);
if (itemsFromFile.isEmpty()) { if (itemsFromFile.isEmpty()) {
fail("ImportExportJsonHelper.readFrom(input) returned a null or empty list"); fail("ImportExportJsonHelper.readFrom(input) returned a null or empty list");
@ -99,7 +80,7 @@ public class ImportExportJsonHelperTest {
} }
private String testWriteTo(final List<SubscriptionItem> itemsFromFile) { private String testWriteTo(final List<SubscriptionItem> itemsFromFile) {
final ByteArrayOutputStream out = new ByteArrayOutputStream(); final var out = new ByteArrayOutputStream();
ImportExportJsonHelper.writeTo(itemsFromFile, out); ImportExportJsonHelper.writeTo(itemsFromFile, out);
final String jsonOut = out.toString(StandardCharsets.UTF_8); final String jsonOut = out.toString(StandardCharsets.UTF_8);
@ -111,10 +92,8 @@ public class ImportExportJsonHelperTest {
} }
private List<SubscriptionItem> readFromWriteTo(final String jsonOut) throws Exception { private List<SubscriptionItem> readFromWriteTo(final String jsonOut) throws Exception {
final ByteArrayInputStream inputStream = new ByteArrayInputStream( final var inputStream = new ByteArrayInputStream(jsonOut.getBytes(StandardCharsets.UTF_8));
jsonOut.getBytes(StandardCharsets.UTF_8)); final var secondReadItems = ImportExportJsonHelper.readFrom(inputStream);
final List<SubscriptionItem> secondReadItems = ImportExportJsonHelper.readFrom(
inputStream);
if (secondReadItems.isEmpty()) { if (secondReadItems.isEmpty()) {
fail("second call to readFrom returned an empty list"); fail("second call to readFrom returned an empty list");