Skip to content

Commit

Permalink
Merge pull request #1344 from dedis/test-fe2-fleische-event-repo-refa…
Browse files Browse the repository at this point in the history
…ctor

Attempt to simplify the event repositories
  • Loading branch information
GabrielFleischer authored Jan 13, 2023
2 parents e4785d2 + 6eaa79c commit a539127
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.dedis.popstellar.model.objects.Channel;
import com.github.dedis.popstellar.model.objects.Election;
import com.github.dedis.popstellar.utility.error.UnknownElectionException;
import com.github.dedis.popstellar.utility.error.UnknownEventException;

import java.util.*;

Expand All @@ -22,7 +21,7 @@
* <p>Its main purpose is to store elections and publish updates
*/
@Singleton
public class ElectionRepository implements EventRepository<Election> {
public class ElectionRepository {

private final Map<String, LaoElections> electionsByLao = new HashMap<>();

Expand Down Expand Up @@ -90,26 +89,10 @@ public Observable<Election> getElectionObservable(
* @return an observable that will be updated with the set of all election's ids
*/
@NonNull
public Observable<Set<String>> getElectionsObservable(@NonNull String laoId) {
public Observable<Set<Election>> getElectionsObservable(@NonNull String laoId) {
return getLaoElections(laoId).getElectionsSubject();
}

@Override
public Observable<Election> getEventObservable(String laoId, String eventId)
throws UnknownEventException {
return getElectionObservable(laoId, eventId);
}

@Override
public Observable<Set<String>> getEventIdsObservable(String laoId) {
return getElectionsObservable(laoId);
}

@Override
public Class<Election> getType() {
return Election.class;
}

@NonNull
private synchronized LaoElections getLaoElections(String laoId) {
// Create the lao elections object if it is not present yet
Expand All @@ -119,7 +102,7 @@ private synchronized LaoElections getLaoElections(String laoId) {
private static final class LaoElections {
private final Map<String, Election> electionById = new HashMap<>();
private final Map<String, Subject<Election>> electionSubjects = new HashMap<>();
private final BehaviorSubject<Set<String>> electionsSubject =
private final BehaviorSubject<Set<Election>> electionsSubject =
BehaviorSubject.createDefault(Collections.emptySet());

public synchronized void updateElection(@NonNull Election election) {
Expand All @@ -130,7 +113,7 @@ public synchronized void updateElection(@NonNull Election election) {
//noinspection ConstantConditions
electionSubjects.get(id).onNext(election);

electionsSubject.onNext(electionById.keySet());
electionsSubject.onNext(Collections.unmodifiableSet(new HashSet<>(electionById.values())));
}

public Election getElection(@NonNull String electionId) throws UnknownElectionException {
Expand All @@ -143,7 +126,7 @@ public Election getElection(@NonNull String electionId) throws UnknownElectionEx
}
}

public Observable<Set<String>> getElectionsSubject() {
public Observable<Set<Election>> getElectionsSubject() {
return electionsSubject;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.github.dedis.popstellar.model.objects.RollCall;
import com.github.dedis.popstellar.model.objects.security.PublicKey;
import com.github.dedis.popstellar.utility.error.UnknownEventException;
import com.github.dedis.popstellar.utility.error.UnknownRollCallException;
import com.github.dedis.popstellar.utility.error.keys.NoRollCallException;

Expand All @@ -20,13 +19,16 @@
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;

import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;

/**
* This class is the repository of the roll call events
*
* <p>Its main purpose is to store roll calls and publish updates
*/
@Singleton
public class RollCallRepository implements EventRepository<RollCall> {
public class RollCallRepository {
public static final String TAG = RollCallRepository.class.getSimpleName();
private final Map<String, LaoRollCalls> rollCallsByLao = new HashMap<>();

Expand Down Expand Up @@ -66,22 +68,6 @@ public Observable<RollCall> getRollCallObservable(String laoId, String persisten
return getLaoRollCalls(laoId).getRollCallObservable(persistentId);
}

@Override
public Observable<RollCall> getEventObservable(String laoId, String eventId)
throws UnknownEventException {
return getRollCallObservable(laoId, eventId);
}

@Override
public Observable<Set<String>> getEventIdsObservable(String laoId) {
return getRollCallsObservableInLao(laoId);
}

@Override
public Class<RollCall> getType() {
return RollCall.class;
}

public RollCall getRollCallWithPersistentId(String laoId, String persistentId)
throws UnknownRollCallException {
return getLaoRollCalls(laoId).getRollCallWithPersistentId(persistentId);
Expand All @@ -97,7 +83,7 @@ public RollCall getRollCallWithId(String laoId, String rollCallId)
* @return an observable set of ids who correspond to the set of roll calls published on the given
* lao
*/
public Observable<Set<String>> getRollCallsObservableInLao(String laoId) {
public Observable<Set<RollCall>> getRollCallsObservableInLao(String laoId) {
return getLaoRollCalls(laoId).getRollCallsSubject();
}

Expand Down Expand Up @@ -134,8 +120,8 @@ private static final class LaoRollCalls {
private final Map<String, Subject<RollCall>> rollCallSubjects = new HashMap<>();

// This allows to observe the collection of roll calls as a whole
private final Subject<Set<String>> rollCallsSubject =
BehaviorSubject.createDefault(Collections.emptySet());
private final Subject<Set<RollCall>> rollCallsSubject =
BehaviorSubject.createDefault(unmodifiableSet(emptySet()));

/**
* This either updates the roll call in the repository or adds it if absent
Expand All @@ -161,7 +147,7 @@ public synchronized void update(RollCall rollCall) {
rollCallSubjects.put(persistentId, BehaviorSubject.createDefault(rollCall));
}

rollCallsSubject.onNext(this.rollCallByPersistentId.keySet());
rollCallsSubject.onNext(unmodifiableSet(new HashSet<>(this.rollCallByPersistentId.values())));
}

public RollCall getRollCallWithPersistentId(String persistentId)
Expand Down Expand Up @@ -189,8 +175,8 @@ public Observable<RollCall> getRollCallObservable(String id) throws UnknownRollC
}
}

public Observable<Set<String>> getRollCallsSubject() {
return rollCallsSubject.map(HashSet::new);
public Observable<Set<RollCall>> getRollCallsSubject() {
return rollCallsSubject;
}

public Set<PublicKey> getAllAttendees() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.github.dedis.popstellar.utility.PoPRXOperators.suppressErrors;

@HiltViewModel
public class LaoDetailViewModel extends NavigationViewModel<LaoTab>
implements QRCodeScanningViewModel {
Expand Down Expand Up @@ -684,7 +682,8 @@ public void subscribeToLao(String laoId) {
//
// Thus, we need to create the rollcall event list twice ¯\_(ツ)_/¯
this.attendedRollCalls =
createEventListObservable(rollCallRepo, laoId)
rollCallRepo
.getRollCallsObservableInLao(laoId)
.map(
rcs ->
rcs.stream()
Expand All @@ -695,8 +694,8 @@ public void subscribeToLao(String laoId) {
// Create the events set observable
this.events =
Observable.combineLatest(
createEventListObservable(rollCallRepo, laoId),
createEventListObservable(electionRepo, laoId),
rollCallRepo.getRollCallsObservableInLao(laoId),
electionRepo.getElectionsObservable(laoId),
(rcs, elecs) -> {
Set<Event> union = new HashSet<>(rcs);
union.addAll(elecs);
Expand Down Expand Up @@ -725,41 +724,6 @@ public void subscribeToLao(String laoId) {
error -> Log.d(TAG, "error updating LAO :" + error)));
}

public static <T extends Event> Observable<Set<T>> createEventListObservable(
EventRepository<T> eventRepo, String laoId) {
return eventRepo
.getEventIdsObservable(laoId)
.map(
idSet ->
idSet.stream()
.map(
id -> {
try {
return eventRepo.getEventObservable(laoId, id);
} catch (UnknownEventException e) {
// Roll calls whose ids are in that list may not be absent
throw new IllegalStateException(
"Could not fetch "
+ eventRepo.getType().getSimpleName()
+ " with id "
+ id);
}
})
.collect(Collectors.toList()))
.flatMap(
subjects ->
// If there are no subjects, returns an empty set rather than no value
subjects.isEmpty()
? Observable.just(new HashSet<T>())
: Observable.combineLatest(
subjects,
events ->
Arrays.stream(events)
.map(eventRepo.getType()::cast)
.collect(Collectors.toSet())))
.lift(suppressErrors(err -> Log.e(TAG, "Error creating rollcall list : ", err)));
}

public PoPToken getCurrentPopToken(RollCall rollCall) throws KeyException, UnknownLaoException {
return keyManager.getPoPToken(getLaoView(), rollCall);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import com.github.dedis.popstellar.SingleEvent;
import com.github.dedis.popstellar.model.network.method.message.MessageGeneral;
import com.github.dedis.popstellar.model.network.method.message.data.digitalcash.*;
import com.github.dedis.popstellar.model.objects.Channel;
import com.github.dedis.popstellar.model.objects.Wallet;
import com.github.dedis.popstellar.model.objects.*;
import com.github.dedis.popstellar.model.objects.digitalcash.TransactionObject;
import com.github.dedis.popstellar.model.objects.security.*;
import com.github.dedis.popstellar.model.objects.view.LaoView;
Expand Down Expand Up @@ -377,7 +376,7 @@ public Observable<List<TransactionObject>> getTransactionsObservable() {
}
}

public Observable<Set<String>> getRollCallsObservable() {
public Observable<Set<RollCall>> getRollCallsObservable() {
return rollCallRepo.getRollCallsObservableInLao(laoId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ protected void before() throws KeyException, GeneralSecurityException, UnknownLa
builder.setInputs(Collections.singletonList(io));
builder.setTransactionId("some id");
TransactionObject transaction = builder.build();
Set<String> rcIdSet = new HashSet<>();
rcIdSet.add(ROLL_CALL.getId());
Set<RollCall> rcIdSet = new HashSet<>();
rcIdSet.add(ROLL_CALL);

when(digitalCashRepo.getTransactions(any(), any()))
.thenReturn(Collections.singletonList(transaction));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class ElectionRepositoryTest {
@Test
public void addingElectionUpdatesIds() {
ElectionRepository repo = new ElectionRepository();
TestObserver<Set<String>> ids = repo.getElectionsObservable(LAO_ID).test();
TestObserver<Set<Election>> elections = repo.getElectionsObservable(LAO_ID).test();

assertCurrentValueIs(ids, emptySet());
assertCurrentValueIs(elections, emptySet());

repo.updateElection(ELECTION);

assertCurrentValueIs(ids, singleton(ELECTION.getId()));
assertCurrentValueIs(elections, singleton(ELECTION));
}

@Test
Expand Down

0 comments on commit a539127

Please sign in to comment.