make transformer testable

note that the test will currently fail because the implemtation isnt complete
This commit is contained in:
Daniel Gultsch 2023-02-11 18:42:09 +01:00
parent 6c24cb12dd
commit fbb900d4ad
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
8 changed files with 96 additions and 11 deletions

View file

@ -64,6 +64,8 @@ dependencies {
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.9' testImplementation 'org.robolectric:robolectric:4.9'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test:runner:1.5.2'
// legacy dependencies. Ideally everything below should be carefully reviewed and eventually moved up // legacy dependencies. Ideally everything below should be carefully reviewed and eventually moved up
@ -144,6 +146,9 @@ android {
} }
} }
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
} }
testOptions { testOptions {
@ -173,6 +178,7 @@ android {
targetCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11
} }
flavorDimensions("mode", "distribution") flavorDimensions("mode", "distribution")
productFlavors { productFlavors {
@ -237,9 +243,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
debug { debug {
shrinkResources true //useProguard false
minifyEnabled true //shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //minifyEnabled true
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
} }

View file

@ -0,0 +1,67 @@
package im.conversations.android.xmpp;
import android.content.Context;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import eu.siacs.conversations.xmpp.Jid;
import im.conversations.android.IDs;
import im.conversations.android.database.ConversationsDatabase;
import im.conversations.android.database.entity.AccountEntity;
import im.conversations.android.transformer.Transformation;
import im.conversations.android.transformer.Transformer;
import im.conversations.android.xmpp.model.jabber.Body;
import im.conversations.android.xmpp.model.reactions.Reaction;
import im.conversations.android.xmpp.model.reactions.Reactions;
import im.conversations.android.xmpp.model.stanza.Message;
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class TransformationTest {
private static final Jid ACCOUNT = Jid.of("user@example.com");
private static final Jid REMOTE = Jid.of("juliet@example.com");
private Transformer transformer;
@Before
public void setupTransformer() throws ExecutionException, InterruptedException {
Context context = ApplicationProvider.getApplicationContext();
final var database =
Room.inMemoryDatabaseBuilder(context, ConversationsDatabase.class).build();
final var account = new AccountEntity();
account.address = ACCOUNT;
account.enabled = true;
account.randomSeed = IDs.seed();
final long id = database.accountDao().insert(account);
this.transformer =
new Transformer(database, database.accountDao().getEnabledAccount(id).get());
}
@Test
public void reactionBeforeOriginal() {
final var reactionMessage = new Message();
reactionMessage.setId("2");
reactionMessage.setTo(ACCOUNT);
reactionMessage.setFrom(REMOTE.withResource("junit"));
final var reactions = reactionMessage.addExtension(new Reactions());
reactions.setId("1");
final var reaction = reactions.addExtension(new Reaction());
reaction.setContent("Y");
this.transformer.transform(
Transformation.of(reactionMessage, Instant.now(), REMOTE, "stanza-b", null));
final var originalMessage = new Message();
originalMessage.setId("1");
originalMessage.setTo(REMOTE);
originalMessage.setFrom(ACCOUNT.withResource("junit"));
final var body = originalMessage.addExtension(new Body());
body.setContent("Hi Juliet. How are you?");
this.transformer.transform(
Transformation.of(originalMessage, Instant.now(), REMOTE, "stanza-a", null));
}
}

View file

@ -76,6 +76,10 @@ public abstract class MessageDao {
transformation.messageId); transformation.messageId);
if (messageIdentifier != null) { if (messageIdentifier != null) {
if (messageIdentifier.isStub()) { if (messageIdentifier.isStub()) {
LOGGER.info(
"Found stub for stanzaId '{}' and messageId '{}'",
transformation.stanzaId,
transformation.messageId);
// TODO create version // TODO create version
// TODO fill up information // TODO fill up information
return messageIdentifier; return messageIdentifier;

View file

@ -84,6 +84,7 @@ public class MessageEntity {
public static MessageEntity stubOfStanzaId(final long chatId, String stanzaId) { public static MessageEntity stubOfStanzaId(final long chatId, String stanzaId) {
final var entity = new MessageEntity(); final var entity = new MessageEntity();
entity.chatId = chatId;
entity.stanzaIdVerified = false; entity.stanzaIdVerified = false;
entity.stanzaId = stanzaId; entity.stanzaId = stanzaId;
return entity; return entity;
@ -91,6 +92,7 @@ public class MessageEntity {
public static MessageEntity stubOfMessageId(final long chatId, String messageId) { public static MessageEntity stubOfMessageId(final long chatId, String messageId) {
final var entity = new MessageEntity(); final var entity = new MessageEntity();
entity.chatId = chatId;
entity.stanzaIdVerified = false; entity.stanzaIdVerified = false;
entity.messageId = messageId; entity.messageId = messageId;
return entity; return entity;

View file

@ -1,6 +1,6 @@
package im.conversations.android.transformer; package im.conversations.android.transformer;
import android.content.Context; import com.google.common.base.Preconditions;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -31,16 +31,16 @@ public class Transformer {
private static final Logger LOGGER = LoggerFactory.getLogger(Transformer.class); private static final Logger LOGGER = LoggerFactory.getLogger(Transformer.class);
private final Context context; private final ConversationsDatabase database;
private final Account account; private final Account account;
public Transformer(final Context context, final Account account) { public Transformer(final ConversationsDatabase database, final Account account) {
this.context = context; Preconditions.checkArgument(account != null, "Account must not be null");
this.database = database;
this.account = account; this.account = account;
} }
public boolean transform(final Transformation transformation) { public boolean transform(final Transformation transformation) {
final var database = ConversationsDatabase.getInstance(context);
return database.runInTransaction(() -> transform(database, transformation)); return database.runInTransaction(() -> transform(database, transformation));
} }
@ -156,7 +156,6 @@ public class Transformer {
private void transformMessageState( private void transformMessageState(
final ChatIdentifier chat, final Transformation transformation) { final ChatIdentifier chat, final Transformation transformation) {
final var database = ConversationsDatabase.getInstance(context);
final var displayed = transformation.getExtension(Displayed.class); final var displayed = transformation.getExtension(Displayed.class);
if (displayed != null) { if (displayed != null) {
if (transformation.outgoing()) { if (transformation.outgoing()) {

View file

@ -23,4 +23,8 @@ public class Reactions extends Extension {
public String getId() { public String getId() {
return this.getAttribute("id"); return this.getAttribute("id");
} }
public void setId(String id) {
this.setAttribute("id", id);
}
} }

View file

@ -2,4 +2,4 @@
package im.conversations.android.xmpp.model.reactions; package im.conversations.android.xmpp.model.reactions;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import im.conversations.android.annotation.XmlPackage; import im.conversations.android.annotation.XmlPackage;

View file

@ -1,6 +1,7 @@
package im.conversations.android.xmpp.processor; package im.conversations.android.xmpp.processor;
import android.content.Context; import android.content.Context;
import im.conversations.android.database.ConversationsDatabase;
import im.conversations.android.transformer.TransformationFactory; import im.conversations.android.transformer.TransformationFactory;
import im.conversations.android.transformer.Transformer; import im.conversations.android.transformer.Transformer;
import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.XmppConnection;
@ -70,7 +71,8 @@ public class MessageProcessor extends XmppConnection.Delegate implements Consume
final var transformation = transformationFactory.create(message, stanzaId); final var transformation = transformationFactory.create(message, stanzaId);
final boolean sendReceipts; final boolean sendReceipts;
if (transformation.isAnythingToTransform()) { if (transformation.isAnythingToTransform()) {
final var transformer = new Transformer(context, getAccount()); final var database = ConversationsDatabase.getInstance(context);
final var transformer = new Transformer(database, getAccount());
sendReceipts = transformer.transform(transformation); sendReceipts = transformer.transform(transformation);
} else { } else {
sendReceipts = true; sendReceipts = true;