Extract data holder classes from SettingsImporter

This commit is contained in:
cketti 2024-04-06 15:11:32 +02:00
parent 81d7e45ab9
commit 5a4e576d79
6 changed files with 118 additions and 130 deletions

View file

@ -0,0 +1,18 @@
package com.fsck.k9.preferences
/**
* Class to list the contents of an import file/stream.
*
* @see SettingsImporter.getImportStreamContents
*/
data class ImportContents(
/**
* True, if the import file contains global settings.
*/
val globalSettings: Boolean,
/**
* The list of accounts found in the import file.
*/
val accounts: List<AccountDescription>,
)

View file

@ -0,0 +1,23 @@
package com.fsck.k9.preferences
data class ImportResults(
@JvmField val globalSettings: Boolean,
@JvmField val importedAccounts: List<AccountDescriptionPair>,
@JvmField val erroneousAccounts: List<AccountDescription>,
)
data class AccountDescriptionPair(
@JvmField val original: AccountDescription,
@JvmField val imported: AccountDescription,
@JvmField val overwritten: Boolean,
@JvmField val authorizationNeeded: Boolean,
@JvmField val incomingPasswordNeeded: Boolean,
@JvmField val outgoingPasswordNeeded: Boolean,
@JvmField val incomingServerName: String,
@JvmField val outgoingServerName: String,
)
data class AccountDescription(
@JvmField val name: String,
@JvmField val uuid: String,
)

View file

@ -0,0 +1,67 @@
package com.fsck.k9.preferences
import com.fsck.k9.mail.AuthType
internal class Imported {
@JvmField var contentVersion: Int = 0
@JvmField var globalSettings: ImportedSettings? = null
@JvmField var accounts: Map<String, ImportedAccount>? = null
}
internal class ImportedSettings {
@JvmField var settings = mutableMapOf<String, String>()
}
internal class ImportedAccount {
@JvmField var uuid: String? = null
@JvmField var name: String? = null
@JvmField var incoming: ImportedServer? = null
@JvmField var outgoing: ImportedServer? = null
@JvmField var settings: ImportedSettings? = null
@JvmField var identities: List<ImportedIdentity>? = null
@JvmField var folders: List<ImportedFolder>? = null
}
internal class ImportedServer {
@JvmField var type: String? = null
@JvmField var host: String? = null
@JvmField var port: String? = null
@JvmField var connectionSecurity: String? = null
@JvmField var authenticationType: AuthType? = null
@JvmField var username: String? = null
@JvmField var password: String? = null
@JvmField var clientCertificateAlias: String? = null
@JvmField var extras: ImportedSettings? = null
}
internal class ImportedIdentity {
@JvmField var name: String? = null
@JvmField var email: String? = null
@JvmField var description: String? = null
@JvmField var settings: ImportedSettings? = null
}
internal class ImportedFolder {
@JvmField var name: String? = null
@JvmField var settings: ImportedSettings? = null
}

View file

@ -40,76 +40,6 @@ import static java.util.Collections.unmodifiableMap;
public class SettingsImporter {
/**
* Class to list the contents of an import file/stream.
*
* @see SettingsImporter#getImportStreamContents(InputStream)
*/
public static class ImportContents {
/**
* True, if the import file contains global settings.
*/
public final boolean globalSettings;
/**
* The list of accounts found in the import file. Never {@code null}.
*/
public final List<AccountDescription> accounts;
private ImportContents(boolean globalSettings, List<AccountDescription> accounts) {
this.globalSettings = globalSettings;
this.accounts = accounts;
}
}
public static class AccountDescription {
public final String name;
public final String uuid;
private AccountDescription(String name, String uuid) {
this.name = name;
this.uuid = uuid;
}
}
public static class AccountDescriptionPair {
public final AccountDescription original;
public final AccountDescription imported;
public final boolean overwritten;
public final boolean authorizationNeeded;
public final boolean incomingPasswordNeeded;
public final boolean outgoingPasswordNeeded;
public final String incomingServerName;
public final String outgoingServerName;
private AccountDescriptionPair(AccountDescription original, AccountDescription imported,
boolean overwritten, boolean authorizationNeeded, boolean incomingPasswordNeeded,
boolean outgoingPasswordNeeded, String incomingServerName, String outgoingServerName) {
this.original = original;
this.imported = imported;
this.overwritten = overwritten;
this.authorizationNeeded = authorizationNeeded;
this.incomingPasswordNeeded = incomingPasswordNeeded;
this.outgoingPasswordNeeded = outgoingPasswordNeeded;
this.incomingServerName = incomingServerName;
this.outgoingServerName = outgoingServerName;
}
}
public static class ImportResults {
public final boolean globalSettings;
public final List<AccountDescriptionPair> importedAccounts;
public final List<AccountDescription> erroneousAccounts;
private ImportResults(boolean globalSettings, List<AccountDescriptionPair> importedAccounts,
List<AccountDescription> erroneousAccounts) {
this.globalSettings = globalSettings;
this.importedAccounts = importedAccounts;
this.erroneousAccounts = erroneousAccounts;
}
}
/**
* Parses an import {@link InputStream} and returns information on whether it contains global
* settings and/or account settings. For all account configurations found, the name of the
@ -1080,52 +1010,4 @@ public class SettingsImporter {
return ConnectionSecurity.SSL_TLS_REQUIRED;
}
}
@VisibleForTesting
static class Imported {
public int contentVersion;
public ImportedSettings globalSettings;
public Map<String, ImportedAccount> accounts;
}
private static class ImportedSettings {
public Map<String, String> settings = new HashMap<>();
}
@VisibleForTesting
static class ImportedAccount {
public String uuid;
public String name;
public ImportedServer incoming;
public ImportedServer outgoing;
public ImportedSettings settings;
public List<ImportedIdentity> identities;
public List<ImportedFolder> folders;
}
@VisibleForTesting
static class ImportedServer {
public String type;
public String host;
public String port;
public String connectionSecurity;
public AuthType authenticationType;
public String username;
public String password;
public String clientCertificateAlias;
public ImportedSettings extras;
}
@VisibleForTesting
static class ImportedIdentity {
public String name;
public String email;
public String description;
public ImportedSettings settings;
}
private static class ImportedFolder {
public String name;
public ImportedSettings settings;
}
}

View file

@ -12,19 +12,13 @@ import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isInstanceOf
import assertk.assertions.isNotNull
import assertk.assertions.isTrue
import assertk.assertions.key
import assertk.assertions.prop
import com.fsck.k9.K9RobolectricTest
import com.fsck.k9.Preferences
import com.fsck.k9.mail.AuthType
import com.fsck.k9.preferences.SettingsImporter.AccountDescription
import com.fsck.k9.preferences.SettingsImporter.AccountDescriptionPair
import com.fsck.k9.preferences.SettingsImporter.ImportContents
import com.fsck.k9.preferences.SettingsImporter.ImportResults
import com.fsck.k9.preferences.SettingsImporter.ImportedAccount
import com.fsck.k9.preferences.SettingsImporter.ImportedIdentity
import com.fsck.k9.preferences.SettingsImporter.ImportedServer
import java.util.UUID
import org.junit.Before
import org.junit.Test
@ -130,7 +124,7 @@ class SettingsImporterTest : K9RobolectricTest() {
val results = SettingsImporter.parseSettings(inputStream, true, accountUuids, true)
assertThat(results.accounts).all {
assertThat(results.accounts).isNotNull().all {
hasSize(1)
key(accountUuid).all {
prop(ImportedAccount::uuid).isEqualTo(accountUuid)
@ -161,11 +155,12 @@ class SettingsImporterTest : K9RobolectricTest() {
val results = SettingsImporter.parseSettings(inputStream, true, accountUuids, true)
assertThat(results.accounts).all {
assertThat(results.accounts).isNotNull().all {
hasSize(1)
key(accountUuid).all {
prop(ImportedAccount::uuid).isEqualTo(accountUuid)
prop(ImportedAccount::identities).extracting(ImportedIdentity::email).containsExactly("user@gmail.com")
prop(ImportedAccount::identities).isNotNull()
.extracting(ImportedIdentity::email).containsExactly("user@gmail.com")
}
}
}
@ -191,8 +186,10 @@ class SettingsImporterTest : K9RobolectricTest() {
val results = SettingsImporter.parseSettings(inputStream, true, accountUuids, false)
assertThat(results.accounts)
.isNotNull()
.key(accountUuid)
.prop(ImportedAccount::incoming)
.isNotNull()
.prop(ImportedServer::authenticationType)
.isEqualTo(AuthType.CRAM_MD5)
}

View file

@ -12,8 +12,9 @@ import androidx.lifecycle.viewModelScope
import app.k9mail.feature.settings.import.SettingsImportExternalContract.AccountActivator
import com.fsck.k9.helper.SingleLiveEvent
import com.fsck.k9.helper.measureRealtimeMillisWithResult
import com.fsck.k9.preferences.ImportContents
import com.fsck.k9.preferences.ImportResults
import com.fsck.k9.preferences.SettingsImporter
import com.fsck.k9.preferences.SettingsImporter.ImportResults
import com.fsck.k9.ui.base.bundle.getEnum
import com.fsck.k9.ui.base.bundle.putEnum
import kotlinx.coroutines.Dispatchers
@ -380,7 +381,7 @@ internal class SettingsImportViewModel(
}
}
private fun readSettings(contentUri: Uri): SettingsImporter.ImportContents {
private fun readSettings(contentUri: Uri): ImportContents {
return context.contentResolver.openInputStream(contentUri).use { inputStream ->
SettingsImporter.getImportStreamContents(inputStream)
}