Mercury-IM/data/src/test/java/org/mercury_im/messenger/data/repository/AccountRepositoryTest.java

131 lines
3.9 KiB
Java
Raw Normal View History

package org.mercury_im.messenger.data.repository;
import org.junit.Test;
2019-12-09 00:28:21 +01:00
import org.mercury_im.messenger.data.di.DaggerInMemoryDatabaseComponent;
import org.mercury_im.messenger.data.di.InMemoryDatabaseComponent;
import org.mercury_im.messenger.entity.Account;
2020-01-04 22:56:34 +01:00
import java.util.NoSuchElementException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
2019-12-01 19:56:13 +01:00
import javax.inject.Inject;
2019-11-29 00:43:41 +01:00
2019-12-20 10:41:55 +01:00
import io.reactivex.disposables.CompositeDisposable;
import io.requery.Persistable;
import io.requery.reactivex.ReactiveEntityStore;
public class AccountRepositoryTest {
private static final Logger LOGGER = Logger.getLogger(AccountRepositoryTest.class.getName());
2019-12-01 19:56:13 +01:00
@Inject
ReactiveEntityStore<Persistable> dataStore;
2019-12-01 19:56:13 +01:00
@Inject
XmppAccountRepository accountRepository;
2019-12-01 19:56:13 +01:00
@Inject
2019-11-29 00:43:41 +01:00
XmppDirectChatRepository directChatRepository;
2019-12-01 19:56:13 +01:00
@Inject
XmppPeerRepository contactRepository;
2019-11-29 00:43:41 +01:00
2019-12-08 22:49:42 +01:00
@Inject
XmppMessageRepository messageRepository;
2019-12-01 19:56:13 +01:00
@Inject
public AccountRepositoryTest() {
2019-12-09 00:28:21 +01:00
InMemoryDatabaseComponent testComponent = DaggerInMemoryDatabaseComponent.builder()
.build();
2019-12-01 19:56:13 +01:00
testComponent.inject(this);
}
@Test
2019-12-20 10:41:55 +01:00
public void test() throws InterruptedException {
CompositeDisposable d = new CompositeDisposable();
d.add(accountRepository.observeAccounts()
.distinct(Account::getId)
.subscribe(a -> System.out.println("Observe: " + a.getAddress())));
Thread.sleep(100);
Account a1 = new IAccount();
a1.setAddress("a1@example.com");
2019-12-21 05:34:19 +01:00
a1.setPassword("a1a1a1");
2019-12-20 10:41:55 +01:00
a1.setEnabled(true);
d.add(accountRepository.insertAccount(a1).subscribe());
Thread.sleep(100);
Account a2 = new IAccount();
a2.setAddress("a2@example.com");
2019-12-21 05:34:19 +01:00
a2.setPassword("a2a2a2");
2019-12-20 10:41:55 +01:00
a2.setEnabled(false);
d.add(accountRepository.insertAccount(a2).subscribe());
Thread.sleep(100);
Account a3 = new IAccount();
a3.setAddress("a3@example.com");
2019-12-21 05:34:19 +01:00
a3.setPassword("a3a3a3");
2019-12-20 10:41:55 +01:00
a3.setEnabled(false);
d.add(accountRepository.insertAccount(a3).subscribe());
Thread.sleep(100);
a1.setAddress("a11@example.org");
d.add(accountRepository.updateAccount(a1).subscribe());
Thread.sleep(100);
}
@Test
public void observeDeletionTest() throws InterruptedException {
CompositeDisposable d = new CompositeDisposable();
UUID uuid = UUID.randomUUID();
d.add(accountRepository.observeAccount(uuid)
.subscribe(optAccount -> {
LOGGER.log(Level.INFO, "Changed");
if (!optAccount.isPresent()) {
LOGGER.log(Level.INFO, "Changed: Account " + uuid.toString() + " is not present.");
} else {
LOGGER.log(Level.INFO, "Changed: Account " + uuid.toString() + ": " + optAccount.getItem().toString());
}
}));
Account account = new IAccount(uuid);
account.setEnabled(true);
account.setAddress("hello@world");
account.setPassword("wooooooh");
d.add(accountRepository.insertAccount(account)
.subscribe(insert -> LOGGER.log(Level.INFO, "Inserted."),
error -> LOGGER.log(Level.SEVERE, "Inserted", error)));
Thread.sleep(100);
d.add(accountRepository.deleteAccount(uuid)
.subscribe(() -> LOGGER.log(Level.INFO, "Deleted")));
Thread.sleep(1000);
d.dispose();
}
2020-01-04 22:56:34 +01:00
@Test(expected = NoSuchElementException.class)
public void updateMissingEntityFails() {
Account missingAccount = new IAccount();
missingAccount.setAddress("this@account.is.missing");
missingAccount.setPassword("inTheDatabase");
accountRepository.updateAccount(missingAccount)
.blockingGet();
}
}