From fbb900d4ad0edeeb73e2b13316b4cd0045831b51 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 11 Feb 2023 18:42:09 +0100 Subject: [PATCH] make transformer testable note that the test will currently fail because the implemtation isnt complete --- build.gradle | 13 +++- .../android/xmpp/TransformationTest.java | 67 +++++++++++++++++++ .../android/database/dao/MessageDao.java | 4 ++ .../database/entity/MessageEntity.java | 2 + .../android/transformer/Transformer.java | 11 ++- .../xmpp/model/reactions/Reactions.java | 4 ++ .../xmpp/model/reactions/package-info.java | 2 +- .../xmpp/processor/MessageProcessor.java | 4 +- 8 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/androidTest/java/im/conversations/android/xmpp/TransformationTest.java diff --git a/build.gradle b/build.gradle index 6d646cfc1..df69918f7 100644 --- a/build.gradle +++ b/build.gradle @@ -64,6 +64,8 @@ dependencies { testImplementation 'junit:junit:4.13.2' 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 @@ -144,6 +146,9 @@ android { } } + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunnerArguments clearPackageData: 'true' + } testOptions { @@ -173,6 +178,7 @@ android { targetCompatibility JavaVersion.VERSION_11 } + flavorDimensions("mode", "distribution") productFlavors { @@ -237,9 +243,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { - shrinkResources true - minifyEnabled true - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + //useProguard false + //shrinkResources true + //minifyEnabled true + //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } diff --git a/src/androidTest/java/im/conversations/android/xmpp/TransformationTest.java b/src/androidTest/java/im/conversations/android/xmpp/TransformationTest.java new file mode 100644 index 000000000..76ff19b16 --- /dev/null +++ b/src/androidTest/java/im/conversations/android/xmpp/TransformationTest.java @@ -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)); + } +} diff --git a/src/main/java/im/conversations/android/database/dao/MessageDao.java b/src/main/java/im/conversations/android/database/dao/MessageDao.java index bc68a7b30..8020f03fd 100644 --- a/src/main/java/im/conversations/android/database/dao/MessageDao.java +++ b/src/main/java/im/conversations/android/database/dao/MessageDao.java @@ -76,6 +76,10 @@ public abstract class MessageDao { transformation.messageId); if (messageIdentifier != null) { if (messageIdentifier.isStub()) { + LOGGER.info( + "Found stub for stanzaId '{}' and messageId '{}'", + transformation.stanzaId, + transformation.messageId); // TODO create version // TODO fill up information return messageIdentifier; diff --git a/src/main/java/im/conversations/android/database/entity/MessageEntity.java b/src/main/java/im/conversations/android/database/entity/MessageEntity.java index 4adc26236..f09aaed88 100644 --- a/src/main/java/im/conversations/android/database/entity/MessageEntity.java +++ b/src/main/java/im/conversations/android/database/entity/MessageEntity.java @@ -84,6 +84,7 @@ public class MessageEntity { public static MessageEntity stubOfStanzaId(final long chatId, String stanzaId) { final var entity = new MessageEntity(); + entity.chatId = chatId; entity.stanzaIdVerified = false; entity.stanzaId = stanzaId; return entity; @@ -91,6 +92,7 @@ public class MessageEntity { public static MessageEntity stubOfMessageId(final long chatId, String messageId) { final var entity = new MessageEntity(); + entity.chatId = chatId; entity.stanzaIdVerified = false; entity.messageId = messageId; return entity; diff --git a/src/main/java/im/conversations/android/transformer/Transformer.java b/src/main/java/im/conversations/android/transformer/Transformer.java index 946a1bfdd..80ab3844b 100644 --- a/src/main/java/im/conversations/android/transformer/Transformer.java +++ b/src/main/java/im/conversations/android/transformer/Transformer.java @@ -1,6 +1,6 @@ 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.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -31,16 +31,16 @@ public class Transformer { private static final Logger LOGGER = LoggerFactory.getLogger(Transformer.class); - private final Context context; + private final ConversationsDatabase database; private final Account account; - public Transformer(final Context context, final Account account) { - this.context = context; + public Transformer(final ConversationsDatabase database, final Account account) { + Preconditions.checkArgument(account != null, "Account must not be null"); + this.database = database; this.account = account; } public boolean transform(final Transformation transformation) { - final var database = ConversationsDatabase.getInstance(context); return database.runInTransaction(() -> transform(database, transformation)); } @@ -156,7 +156,6 @@ public class Transformer { private void transformMessageState( final ChatIdentifier chat, final Transformation transformation) { - final var database = ConversationsDatabase.getInstance(context); final var displayed = transformation.getExtension(Displayed.class); if (displayed != null) { if (transformation.outgoing()) { diff --git a/src/main/java/im/conversations/android/xmpp/model/reactions/Reactions.java b/src/main/java/im/conversations/android/xmpp/model/reactions/Reactions.java index 4d71420a3..0ea2972e2 100644 --- a/src/main/java/im/conversations/android/xmpp/model/reactions/Reactions.java +++ b/src/main/java/im/conversations/android/xmpp/model/reactions/Reactions.java @@ -23,4 +23,8 @@ public class Reactions extends Extension { public String getId() { return this.getAttribute("id"); } + + public void setId(String id) { + this.setAttribute("id", id); + } } diff --git a/src/main/java/im/conversations/android/xmpp/model/reactions/package-info.java b/src/main/java/im/conversations/android/xmpp/model/reactions/package-info.java index 5254dde5c..7fa845595 100644 --- a/src/main/java/im/conversations/android/xmpp/model/reactions/package-info.java +++ b/src/main/java/im/conversations/android/xmpp/model/reactions/package-info.java @@ -2,4 +2,4 @@ package im.conversations.android.xmpp.model.reactions; import eu.siacs.conversations.xml.Namespace; -import im.conversations.android.annotation.XmlPackage; \ No newline at end of file +import im.conversations.android.annotation.XmlPackage; diff --git a/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java b/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java index dc7d658e7..db34bc1e6 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/MessageProcessor.java @@ -1,6 +1,7 @@ package im.conversations.android.xmpp.processor; import android.content.Context; +import im.conversations.android.database.ConversationsDatabase; import im.conversations.android.transformer.TransformationFactory; import im.conversations.android.transformer.Transformer; 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 boolean sendReceipts; 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); } else { sendReceipts = true;