Search in sources :

Example 11 with Data

use of org.thoughtcrime.securesms.jobmanager.Data in project Signal-Android by WhisperSystems.

the class JsonDataSerializerTest method deserialize_dataMatchesExpected.

@Test
public void deserialize_dataMatchesExpected() throws IOException {
    Data data = new JsonDataSerializer().deserialize(StreamUtil.readFullyAsString(ClassLoader.getSystemClassLoader().getResourceAsStream("data/data_serialized.json")));
    assertEquals("s1 value", data.getString("s1"));
    assertEquals("s2 value", data.getString("s2"));
    assertArrayEquals(new String[] { "a", "b", "c" }, data.getStringArray("s_array_1"));
    assertEquals(1, data.getInt("i1"));
    assertEquals(2, data.getInt("i2"));
    assertEquals(Integer.MAX_VALUE, data.getInt("max"));
    assertEquals(Integer.MIN_VALUE, data.getInt("min"));
    assertArrayEquals(new int[] { 1, 2, 3, Integer.MAX_VALUE, Integer.MIN_VALUE }, data.getIntegerArray("i_array_1"));
    assertEquals(10, data.getLong("l1"));
    assertEquals(20, data.getLong("l2"));
    assertEquals(Long.MAX_VALUE, data.getLong("max"));
    assertEquals(Long.MIN_VALUE, data.getLong("min"));
    assertArrayEquals(new long[] { 1, 2, 3, Long.MAX_VALUE, Long.MIN_VALUE }, data.getLongArray("l_array_1"));
    assertEquals(1.2f, data.getFloat("f1"), FloatDelta);
    assertEquals(3.4f, data.getFloat("f2"), FloatDelta);
    assertArrayEquals(new float[] { 5.6f, 7.8f }, data.getFloatArray("f_array_1"), FloatDelta);
    assertEquals(10.2, data.getDouble("d1"), FloatDelta);
    assertEquals(30.4, data.getDouble("d2"), FloatDelta);
    assertArrayEquals(new double[] { 50.6, 70.8 }, data.getDoubleArray("d_array_1"), FloatDelta);
    assertTrue(data.getBoolean("b1"));
    assertFalse(data.getBoolean("b2"));
    assertArrayEquals(new boolean[] { false, true }, data.getBooleanArray("b_array_1"));
}
Also used : Data(org.thoughtcrime.securesms.jobmanager.Data) Test(org.junit.Test)

Example 12 with Data

use of org.thoughtcrime.securesms.jobmanager.Data in project Signal-Android by WhisperSystems.

the class MarkReadReceiverTest method givenMultipleThreadsWithMultipleMessagesEach_whenIProcess_thenIProperlyGroupByThreadAndRecipient.

@Test
public void givenMultipleThreadsWithMultipleMessagesEach_whenIProcess_thenIProperlyGroupByThreadAndRecipient() {
    // GIVEN
    List<RecipientId> recipients = Stream.range(1L, 4L).map(RecipientId::from).toList();
    List<Long> threads = Stream.range(4L, 7L).toList();
    int expected = recipients.size() * threads.size() + 1;
    List<MessageDatabase.MarkedMessageInfo> infoList = Stream.of(threads).flatMap(threadId -> Stream.of(recipients).map(recipientId -> createMarkedMessageInfo(threadId, recipientId))).toList();
    List<MessageDatabase.MarkedMessageInfo> duplicatedList = Util.concatenatedList(infoList, infoList);
    // WHEN
    MarkReadReceiver.process(mockContext, duplicatedList);
    // THEN
    assertEquals("Should have 10 total jobs, including MultiDeviceReadUpdateJob", expected, jobs.size());
    Set<Pair<Long, String>> threadRecipientPairs = new HashSet<>();
    Stream.of(jobs).forEach(job -> {
        if (job instanceof MultiDeviceReadUpdateJob) {
            return;
        }
        Data data = job.serialize();
        long threadId = data.getLong("thread");
        String recipientId = data.getString("recipient");
        long[] messageIds = data.getLongArray("message_ids");
        assertEquals("Each job should contain two messages.", 2, messageIds.length);
        assertTrue("Each thread recipient pair should only exist once.", threadRecipientPairs.add(new Pair<>(threadId, recipientId)));
    });
    assertEquals("Should have 9 total combinations.", 9, threadRecipientPairs.size());
}
Also used : Context(android.content.Context) Stream(com.annimon.stream.Stream) Util(org.thoughtcrime.securesms.util.Util) PowerMockito.mockStatic(org.powermock.api.mockito.PowerMockito.mockStatic) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) RunWith(org.junit.runner.RunWith) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) Pair(org.whispersystems.libsignal.util.Pair) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Recipient(org.thoughtcrime.securesms.recipients.Recipient) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) LinkedList(java.util.LinkedList) Before(org.junit.Before) MessageId(org.thoughtcrime.securesms.database.model.MessageId) PowerMockito.when(org.powermock.api.mockito.PowerMockito.when) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.any(org.mockito.Matchers.any) List(java.util.List) PowerMockito.mock(org.powermock.api.mockito.PowerMockito.mock) MultiDeviceReadUpdateJob(org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Job(org.thoughtcrime.securesms.jobmanager.Job) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) Assert.assertEquals(org.junit.Assert.assertEquals) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Data(org.thoughtcrime.securesms.jobmanager.Data) MultiDeviceReadUpdateJob(org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob) Pair(org.whispersystems.libsignal.util.Pair) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with Data

use of org.thoughtcrime.securesms.jobmanager.Data in project Signal-Android by signalapp.

the class StorageForcePushJob method onRun.

@Override
protected void onRun() throws IOException, RetryLaterException {
    if (SignalStore.account().isLinkedDevice()) {
        Log.i(TAG, "Only the primary device can force push");
        return;
    }
    StorageKey storageServiceKey = SignalStore.storageService().getOrCreateStorageKey();
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    RecipientDatabase recipientDatabase = SignalDatabase.recipients();
    UnknownStorageIdDatabase storageIdDatabase = SignalDatabase.unknownStorageIds();
    long currentVersion = accountManager.getStorageManifestVersion();
    Map<RecipientId, StorageId> oldContactStorageIds = recipientDatabase.getContactStorageSyncIdsMap();
    long newVersion = currentVersion + 1;
    Map<RecipientId, StorageId> newContactStorageIds = generateContactStorageIds(oldContactStorageIds);
    List<SignalStorageRecord> inserts = Stream.of(oldContactStorageIds.keySet()).map(recipientDatabase::getRecordForSync).withoutNulls().map(s -> StorageSyncModels.localToRemoteRecord(s, Objects.requireNonNull(newContactStorageIds.get(s.getId())).getRaw())).toList();
    SignalStorageRecord accountRecord = StorageSyncHelper.buildAccountRecord(context, Recipient.self().fresh());
    List<StorageId> allNewStorageIds = new ArrayList<>(newContactStorageIds.values());
    inserts.add(accountRecord);
    allNewStorageIds.add(accountRecord.getId());
    SignalStorageManifest manifest = new SignalStorageManifest(newVersion, allNewStorageIds);
    StorageSyncValidations.validateForcePush(manifest, inserts, Recipient.self().fresh());
    try {
        if (newVersion > 1) {
            Log.i(TAG, String.format(Locale.ENGLISH, "Force-pushing data. Inserting %d IDs.", inserts.size()));
            if (accountManager.resetStorageRecords(storageServiceKey, manifest, inserts).isPresent()) {
                Log.w(TAG, "Hit a conflict. Trying again.");
                throw new RetryLaterException();
            }
        } else {
            Log.i(TAG, String.format(Locale.ENGLISH, "First version, normal push. Inserting %d IDs.", inserts.size()));
            if (accountManager.writeStorageRecords(storageServiceKey, manifest, inserts, Collections.emptyList()).isPresent()) {
                Log.w(TAG, "Hit a conflict. Trying again.");
                throw new RetryLaterException();
            }
        }
    } catch (InvalidKeyException e) {
        Log.w(TAG, "Hit an invalid key exception, which likely indicates a conflict.");
        throw new RetryLaterException(e);
    }
    Log.i(TAG, "Force push succeeded. Updating local manifest version to: " + newVersion);
    SignalStore.storageService().setManifest(manifest);
    recipientDatabase.applyStorageIdUpdates(newContactStorageIds);
    recipientDatabase.applyStorageIdUpdates(Collections.singletonMap(Recipient.self().getId(), accountRecord.getId()));
    storageIdDatabase.deleteAll();
}
Also used : SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) StorageSyncValidations(org.thoughtcrime.securesms.storage.StorageSyncValidations) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) StorageSyncModels(org.thoughtcrime.securesms.storage.StorageSyncModels) HashMap(java.util.HashMap) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ArrayList(java.util.ArrayList) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) StorageSyncHelper(org.thoughtcrime.securesms.storage.StorageSyncHelper) Locale(java.util.Locale) Map(java.util.Map) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) StorageId(org.whispersystems.signalservice.api.storage.StorageId) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) UnknownStorageIdDatabase(org.thoughtcrime.securesms.database.UnknownStorageIdDatabase) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) IOException(java.io.IOException) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Log(org.signal.core.util.logging.Log) List(java.util.List) Job(org.thoughtcrime.securesms.jobmanager.Job) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) Collections(java.util.Collections) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) StorageId(org.whispersystems.signalservice.api.storage.StorageId) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) UnknownStorageIdDatabase(org.thoughtcrime.securesms.database.UnknownStorageIdDatabase) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey)

Example 14 with Data

use of org.thoughtcrime.securesms.jobmanager.Data in project Signal-Android by signalapp.

the class MarkReadReceiverTest method givenMultipleThreadsWithMultipleMessagesEach_whenIProcess_thenIProperlyGroupByThreadAndRecipient.

@Test
public void givenMultipleThreadsWithMultipleMessagesEach_whenIProcess_thenIProperlyGroupByThreadAndRecipient() {
    // GIVEN
    List<RecipientId> recipients = Stream.range(1L, 4L).map(RecipientId::from).toList();
    List<Long> threads = Stream.range(4L, 7L).toList();
    int expected = recipients.size() * threads.size() + 1;
    List<MessageDatabase.MarkedMessageInfo> infoList = Stream.of(threads).flatMap(threadId -> Stream.of(recipients).map(recipientId -> createMarkedMessageInfo(threadId, recipientId))).toList();
    List<MessageDatabase.MarkedMessageInfo> duplicatedList = Util.concatenatedList(infoList, infoList);
    // WHEN
    MarkReadReceiver.process(mockContext, duplicatedList);
    // THEN
    assertEquals("Should have 10 total jobs, including MultiDeviceReadUpdateJob", expected, jobs.size());
    Set<Pair<Long, String>> threadRecipientPairs = new HashSet<>();
    Stream.of(jobs).forEach(job -> {
        if (job instanceof MultiDeviceReadUpdateJob) {
            return;
        }
        Data data = job.serialize();
        long threadId = data.getLong("thread");
        String recipientId = data.getString("recipient");
        long[] messageIds = data.getLongArray("message_ids");
        assertEquals("Each job should contain two messages.", 2, messageIds.length);
        assertTrue("Each thread recipient pair should only exist once.", threadRecipientPairs.add(new Pair<>(threadId, recipientId)));
    });
    assertEquals("Should have 9 total combinations.", 9, threadRecipientPairs.size());
}
Also used : Context(android.content.Context) Stream(com.annimon.stream.Stream) Util(org.thoughtcrime.securesms.util.Util) PowerMockito.mockStatic(org.powermock.api.mockito.PowerMockito.mockStatic) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) RunWith(org.junit.runner.RunWith) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) Pair(org.whispersystems.libsignal.util.Pair) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Recipient(org.thoughtcrime.securesms.recipients.Recipient) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) LinkedList(java.util.LinkedList) Before(org.junit.Before) MessageId(org.thoughtcrime.securesms.database.model.MessageId) PowerMockito.when(org.powermock.api.mockito.PowerMockito.when) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.any(org.mockito.Matchers.any) List(java.util.List) PowerMockito.mock(org.powermock.api.mockito.PowerMockito.mock) MultiDeviceReadUpdateJob(org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Job(org.thoughtcrime.securesms.jobmanager.Job) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) Assert.assertEquals(org.junit.Assert.assertEquals) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Data(org.thoughtcrime.securesms.jobmanager.Data) MultiDeviceReadUpdateJob(org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob) Pair(org.whispersystems.libsignal.util.Pair) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with Data

use of org.thoughtcrime.securesms.jobmanager.Data in project Signal-Android by signalapp.

the class RecipientIdJobMigration method migrateMultiDeviceReadUpdateJob.

@NonNull
private JobData migrateMultiDeviceReadUpdateJob(@NonNull JobData jobData) {
    try {
        String[] rawOld = jobData.getData().getStringArray("message_ids");
        String[] rawUpdated = new String[rawOld.length];
        for (int i = 0; i < rawOld.length; i++) {
            OldSerializableSyncMessageId old = JsonUtils.fromJson(rawOld[i], OldSerializableSyncMessageId.class);
            Recipient recipient = Recipient.external(application, old.sender);
            NewSerializableSyncMessageId updated = new NewSerializableSyncMessageId(recipient.getId().serialize(), old.timestamp);
            rawUpdated[i] = JsonUtils.toJson(updated);
        }
        Data updatedData = new Data.Builder().putStringArray("message_ids", rawUpdated).build();
        return jobData.withData(updatedData);
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) Data(org.thoughtcrime.securesms.jobmanager.Data) IOException(java.io.IOException) NonNull(androidx.annotation.NonNull)

Aggregations

Data (org.thoughtcrime.securesms.jobmanager.Data)42 NonNull (androidx.annotation.NonNull)34 Recipient (org.thoughtcrime.securesms.recipients.Recipient)26 IOException (java.io.IOException)10 Stream (com.annimon.stream.Stream)6 List (java.util.List)6 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)6 Job (org.thoughtcrime.securesms.jobmanager.Job)6 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)6 Context (android.content.Context)4 TimeUnit (java.util.concurrent.TimeUnit)4 Test (org.junit.Test)4 Log (org.signal.core.util.logging.Log)4 NetworkConstraint (org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)4 SignalStore (org.thoughtcrime.securesms.keyvalue.SignalStore)4 WorkerThread (androidx.annotation.WorkerThread)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 Assert.assertEquals (org.junit.Assert.assertEquals)2