Skip to content

Commit

Permalink
Merge pull request #1690 from dedis/fe2-matteo-tests-stucked
Browse files Browse the repository at this point in the history
Fe2 CI got stuck
  • Loading branch information
matteosz authored Oct 3, 2023
2 parents 8438331 + dc93e57 commit 7141b2e
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.dedis.popstellar.di.AppDatabaseModuleHelper;
import com.github.dedis.popstellar.model.objects.*;
import com.github.dedis.popstellar.model.objects.digitalcash.*;
import com.github.dedis.popstellar.model.objects.security.*;
import com.github.dedis.popstellar.repository.database.AppDatabase;
import com.github.dedis.popstellar.repository.database.digitalcash.HashDao;
import com.github.dedis.popstellar.repository.database.digitalcash.TransactionDao;
import com.github.dedis.popstellar.testutils.Base64DataUtils;
import com.github.dedis.popstellar.utility.error.keys.NoRollCallException;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.security.GeneralSecurityException;
import java.util.*;
import java.util.stream.Collectors;

import io.reactivex.Completable;
import io.reactivex.Single;
import io.reactivex.observers.TestObserver;

import static com.github.dedis.popstellar.testutils.Base64DataUtils.generatePublicKey;
import static com.github.dedis.popstellar.testutils.ObservableUtils.assertCurrentValueIs;
import static java.util.Collections.emptyList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class DigitalCashRepositoryTest {
Expand All @@ -40,17 +48,28 @@ public class DigitalCashRepositoryTest {
private static DigitalCashRepository repo;

private static final Application APPLICATION = ApplicationProvider.getApplicationContext();
private static AppDatabase appDatabase;
@Mock private static AppDatabase appDatabase;
@Mock private static TransactionDao transactionDao;
@Mock private static HashDao hashDao;

@Rule(order = 0)
public final MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void initializeRepo() {
appDatabase = AppDatabaseModuleHelper.getAppDatabase(APPLICATION);
when(appDatabase.transactionDao()).thenReturn(transactionDao);
when(appDatabase.hashDao()).thenReturn(hashDao);
repo = new DigitalCashRepository(appDatabase, APPLICATION);
}

@After
public void tearDown() {
appDatabase.close();
// Mock the DAOs
when(hashDao.getDictionaryByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(hashDao.deleteByLaoId(anyString())).thenReturn(Completable.complete());
when(hashDao.insertAll(anyList())).thenReturn(Completable.complete());
when(transactionDao.getTransactionsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(transactionDao.deleteByLaoId(anyString())).thenReturn(Completable.complete());
when(transactionDao.insert(any())).thenReturn(Completable.complete());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.dedis.popstellar.di.AppDatabaseModuleHelper;
import com.github.dedis.popstellar.model.network.method.message.data.election.ElectionVersion;
import com.github.dedis.popstellar.model.objects.Election;
import com.github.dedis.popstellar.model.objects.Lao;
import com.github.dedis.popstellar.model.objects.event.EventState;
import com.github.dedis.popstellar.repository.database.AppDatabase;
import com.github.dedis.popstellar.repository.database.event.election.ElectionDao;
import com.github.dedis.popstellar.utility.error.UnknownElectionException;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.util.Collections;
import java.util.Set;

import io.reactivex.Completable;
import io.reactivex.Single;
import io.reactivex.observers.TestObserver;

import static com.github.dedis.popstellar.testutils.Base64DataUtils.generatePublicKey;
Expand All @@ -27,6 +33,9 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class ElectionRepositoryTest {
Expand All @@ -37,18 +46,21 @@ public class ElectionRepositoryTest {
.setElectionVersion(ElectionVersion.OPEN_BALLOT)
.build();
private static final Application APPLICATION = ApplicationProvider.getApplicationContext();
private static AppDatabase appDatabase;
@Mock private static AppDatabase appDatabase;
@Mock private static ElectionDao electionDao;
private static ElectionRepository repo;

@Rule(order = 0)
public final MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setup() {
appDatabase = AppDatabaseModuleHelper.getAppDatabase(APPLICATION);
when(appDatabase.electionDao()).thenReturn(electionDao);
repo = new ElectionRepository(appDatabase, APPLICATION);
}

@After
public void tearDown() {
appDatabase.close();
when(electionDao.getElectionsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(electionDao.insert(any())).thenReturn(Completable.complete());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,34 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.dedis.popstellar.di.AppDatabaseModuleHelper;
import com.github.dedis.popstellar.model.objects.Meeting;
import com.github.dedis.popstellar.repository.database.AppDatabase;
import com.github.dedis.popstellar.repository.database.event.meeting.MeetingDao;
import com.github.dedis.popstellar.utility.error.UnknownMeetingException;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.util.*;

import io.reactivex.Completable;
import io.reactivex.Single;
import io.reactivex.observers.TestObserver;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class MeetingRepositoryTest {
private static final Application APPLICATION = ApplicationProvider.getApplicationContext();
private static AppDatabase appDatabase;
@Mock private static AppDatabase appDatabase;
@Mock private static MeetingDao meetingDao;
private static MeetingRepository meetingRepository;
private static final String LAO_ID = "LAO_ID";
private static final String ID = "ID";
Expand All @@ -47,16 +56,18 @@ public class MeetingRepositoryTest {
MODIFICATION_ID,
MODIFICATION_SIGNATURES);

@Rule(order = 0)
public final MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setUp() {
appDatabase = AppDatabaseModuleHelper.getAppDatabase(APPLICATION);
when(appDatabase.meetingDao()).thenReturn(meetingDao);
meetingRepository = new MeetingRepository(appDatabase, APPLICATION);
meetingRepository.updateMeeting(LAO_ID, meeting);
}

@After
public void tearDown() {
appDatabase.close();
when(meetingDao.insert(any())).thenReturn(Completable.complete());
when(meetingDao.getMeetingsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
meetingRepository.updateMeeting(LAO_ID, meeting);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,43 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.dedis.popstellar.di.AppDatabaseModuleHelper;
import com.github.dedis.popstellar.model.objects.*;
import com.github.dedis.popstellar.model.objects.security.MessageID;
import com.github.dedis.popstellar.model.objects.security.PublicKey;
import com.github.dedis.popstellar.repository.database.AppDatabase;
import com.github.dedis.popstellar.repository.database.socialmedia.ChirpDao;
import com.github.dedis.popstellar.repository.database.socialmedia.ReactionDao;
import com.github.dedis.popstellar.utility.error.UnknownChirpException;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

import io.reactivex.Completable;
import io.reactivex.Single;
import io.reactivex.observers.TestObserver;

import static com.github.dedis.popstellar.testutils.Base64DataUtils.*;
import static com.github.dedis.popstellar.testutils.ObservableUtils.assertCurrentValueIs;
import static java.util.Collections.addAll;
import static java.util.Collections.emptySet;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class SocialMediaRepositoryTest {

private static final Application APPLICATION = ApplicationProvider.getApplicationContext();
private static AppDatabase appDatabase;
@Mock private static AppDatabase appDatabase;
@Mock private static ReactionDao reactionDao;
@Mock private static ChirpDao chirpDao;
private static final String LAO_ID = Lao.generateLaoId(generatePublicKey(), 1000, "LAO");

private static final PublicKey SENDER = generatePublicKey();
Expand All @@ -56,15 +66,19 @@ public class SocialMediaRepositoryTest {

private static SocialMediaRepository repo;

@Rule(order = 0)
public final MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setup() {
appDatabase = AppDatabaseModuleHelper.getAppDatabase(APPLICATION);
when(appDatabase.chirpDao()).thenReturn(chirpDao);
when(appDatabase.reactionDao()).thenReturn(reactionDao);
repo = new SocialMediaRepository(appDatabase, APPLICATION);
}

@After
public void tearDown() {
appDatabase.close();
when(chirpDao.insert(any())).thenReturn(Completable.complete());
when(reactionDao.insert(any())).thenReturn(Completable.complete());
when(chirpDao.getChirpsByLaoId(anyString())).thenReturn(Single.just(Collections.emptyList()));
when(reactionDao.getReactionsByChirpId(any())).thenReturn(Single.just(Collections.emptyList()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,50 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.dedis.popstellar.di.AppDatabaseModuleHelper;
import com.github.dedis.popstellar.model.network.method.message.data.election.ElectionVersion;
import com.github.dedis.popstellar.model.objects.*;
import com.github.dedis.popstellar.model.objects.event.EventState;
import com.github.dedis.popstellar.model.objects.security.MessageID;
import com.github.dedis.popstellar.model.objects.security.PublicKey;
import com.github.dedis.popstellar.repository.database.AppDatabase;
import com.github.dedis.popstellar.repository.database.witnessing.PendingEntity;
import com.github.dedis.popstellar.repository.database.digitalcash.HashDao;
import com.github.dedis.popstellar.repository.database.digitalcash.TransactionDao;
import com.github.dedis.popstellar.repository.database.event.election.ElectionDao;
import com.github.dedis.popstellar.repository.database.event.meeting.MeetingDao;
import com.github.dedis.popstellar.repository.database.event.rollcall.RollCallDao;
import com.github.dedis.popstellar.repository.database.witnessing.*;
import com.github.dedis.popstellar.utility.error.*;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.time.Instant;
import java.util.*;

import io.reactivex.Completable;
import io.reactivex.Single;

import static com.github.dedis.popstellar.testutils.Base64DataUtils.generateKeyPair;
import static com.github.dedis.popstellar.testutils.Base64DataUtils.generateMessageID;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class WitnessingRepositoryTest {

private static AppDatabase appDatabase;
@Mock private static AppDatabase appDatabase;
@Mock private static RollCallDao rollCallDao;
@Mock private static WitnessDao witnessDao;
@Mock private static WitnessingDao witnessingDao;
@Mock private static PendingDao pendingDao;
@Mock private static TransactionDao transactionDao;
@Mock private static HashDao hashDao;
@Mock private static ElectionDao electionDao;
@Mock private static MeetingDao meetingDao;
private static WitnessingRepository witnessingRepository;
private static RollCallRepository rollCallRepo;
private static ElectionRepository electionRepo;
Expand Down Expand Up @@ -67,11 +87,20 @@ public class WitnessingRepositoryTest {
"",
new ArrayList<>());

@Rule(order = 0)
public final MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setUp() {
Application application = ApplicationProvider.getApplicationContext();
appDatabase =
AppDatabaseModuleHelper.getAppDatabase(ApplicationProvider.getApplicationContext());
when(appDatabase.witnessDao()).thenReturn(witnessDao);
when(appDatabase.witnessingDao()).thenReturn(witnessingDao);
when(appDatabase.pendingDao()).thenReturn(pendingDao);
when(appDatabase.rollCallDao()).thenReturn(rollCallDao);
when(appDatabase.electionDao()).thenReturn(electionDao);
when(appDatabase.meetingDao()).thenReturn(meetingDao);
when(appDatabase.transactionDao()).thenReturn(transactionDao);
when(appDatabase.hashDao()).thenReturn(hashDao);

rollCallRepo = new RollCallRepository(appDatabase, application);
electionRepo = new ElectionRepository(appDatabase, application);
Expand All @@ -81,6 +110,43 @@ public void setUp() {
new WitnessingRepository(
appDatabase, application, rollCallRepo, electionRepo, meetingRepo, digitalCashRepo);

when(witnessDao.insertAll(anyList())).thenReturn(Completable.complete());
when(witnessDao.getWitnessesByLao(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(witnessDao.isWitness(anyString(), any())).thenReturn(0);

when(witnessingDao.insert(any())).thenReturn(Completable.complete());
when(witnessingDao.getWitnessMessagesByLao(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(witnessingDao.deleteMessagesByIds(anyString(), anySet()))
.thenReturn(Completable.complete());

when(pendingDao.insert(any())).thenReturn(Completable.complete());
when(pendingDao.getPendingObjectsFromLao(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(pendingDao.removePendingObject(any())).thenReturn(Completable.complete());

when(rollCallDao.insert(any())).thenReturn(Completable.complete());
when(rollCallDao.getRollCallsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));

when(electionDao.getElectionsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(electionDao.insert(any())).thenReturn(Completable.complete());

when(meetingDao.insert(any())).thenReturn(Completable.complete());
when(meetingDao.getMeetingsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));

when(hashDao.getDictionaryByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(hashDao.deleteByLaoId(anyString())).thenReturn(Completable.complete());
when(hashDao.insertAll(anyList())).thenReturn(Completable.complete());
when(transactionDao.getTransactionsByLaoId(anyString()))
.thenReturn(Single.just(Collections.emptyList()));
when(transactionDao.deleteByLaoId(anyString())).thenReturn(Completable.complete());
when(transactionDao.insert(any())).thenReturn(Completable.complete());

witnessingRepository.addWitnesses(LAO_ID, WITNESSES);
witnessingRepository.addWitnessMessage(LAO_ID, WITNESS_MESSAGE);
}
Expand Down

0 comments on commit 7141b2e

Please sign in to comment.