Skip to content

Commit

Permalink
Matched error handling to design doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
vteague committed Mar 14, 2024
1 parent 563c0d3 commit e6ea537
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package au.org.democracydevelopers.raireservice.controller;

import au.org.democracydevelopers.raire.RaireSolution;
import au.org.democracydevelopers.raireservice.request.ContestRequestByIDs;
import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.DirectContestRequest;
import au.org.democracydevelopers.raireservice.request.RequestByContestName;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import au.org.democracydevelopers.raireservice.response.*;
import au.org.democracydevelopers.raireservice.service.GenerateAssertionsService;
import au.org.democracydevelopers.raireservice.service.GetAssertionsService;
Expand All @@ -25,7 +25,7 @@ public class AssertionController {
private final GenerateAssertionsService generateAssertionsService;

@PostMapping(path = "/generate-assertions", produces = MediaType.APPLICATION_JSON_VALUE)
public GenerateAssertionsResponse serve(@RequestBody ContestRequestByIDs request) {
public GenerateAssertionsResponse serve(@RequestBody GenerateAssertionsRequest request) {
log.info("Received request to get assertions for contest: {}", request.getContestName());
try {
DirectContestRequest contest = generateAssertionsService.getVotesFromDatabase(request);
Expand All @@ -38,7 +38,7 @@ public GenerateAssertionsResponse serve(@RequestBody ContestRequestByIDs request


@PostMapping(path = "/get-assertions", produces = MediaType.APPLICATION_JSON_VALUE)
public GetAssertionsResponse serve(@RequestBody RequestByContestName contest) {
public GetAssertionsResponse serve(@RequestBody GetAssertionsRequest contest) {
log.info("Received request to get assertions for contest: {}", contest.getContestName());
return getAssertionsService.getAssertions(contest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean votesAreValid() {
* @param request A slightly different form of contest request, for which we want to keep the metadata
* @param votesByName Votes in colorado-rla style, as ordered lists of candidate names.
*/
public DirectContestRequest(ContestRequestByIDs request, List<String[]> votesByName) {
public DirectContestRequest(GenerateAssertionsRequest request, List<String[]> votesByName) {
candidates = request.getCandidates().toArray(new String[0]);
contestName = request.getContestName();
totalAuditableBallots = request.getTotalAuditableBallots();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContestRequestByIDs {
public class GenerateAssertionsRequest {
private String contestName;
private int totalAuditableBallots;
private Integer timeProvisionForResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RequestByContestName {
public class GetAssertionsRequest {
private String contestName;
private List<String> candidates;
private BigDecimal riskLimit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package au.org.democracydevelopers.raireservice.response;

import au.org.democracydevelopers.raireservice.repository.entity.Assertion;
import au.org.democracydevelopers.raireservice.request.ContestRequestByIDs;
import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.DirectContestRequest;
import au.org.democracydevelopers.raireservice.request.RequestByContestName;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;

import java.math.BigDecimal;
import java.util.HashMap;
Expand Down Expand Up @@ -33,13 +33,13 @@ public void AddRisks(List<Assertion> assertions) {
metadata.put("assertionRisks", risks);
}
}
public Metadata(RequestByContestName request) {
public Metadata(GetAssertionsRequest request) {
metadata.put("candidates", request.getCandidates());
metadata.put("contest", request.getContestName());
metadata.put("riskLimit", request.getRiskLimit());
}

public Metadata(ContestRequestByIDs request) {
public Metadata(GenerateAssertionsRequest request) {
metadata.put("candidates", request.getCandidates());
metadata.put("contest", request.getContestName());
metadata.put("totalAuditableBallots", request.getTotalAuditableBallots());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.stream.StreamSupport;

/**
* Everything that could go wrong in RaireService. Typically this will be returned as a thrown RaireServiceException with this as its argument.
*
* Everything that could go wrong in RaireService. Typically this will be returned as a thrown RaireServiceException
* with this as its argument.
* It is implemented as a class rather than an Exception hierarchy to facilitate detailed error serialization.
**/
@JsonSerialize(using= RaireServiceError.RaireServiceErrorSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import au.org.democracydevelopers.raireservice.repository.AssertionRepository;
import au.org.democracydevelopers.raireservice.repository.CVRRepository;
import au.org.democracydevelopers.raireservice.repository.converters.StringArrayConverter;
import au.org.democracydevelopers.raireservice.request.ContestRequestByIDs;
import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.DirectContestRequest;
import au.org.democracydevelopers.raireservice.response.*;
import lombok.RequiredArgsConstructor;
Expand All @@ -34,7 +34,7 @@ public class GenerateAssertionsService {
* the relevant CVRs.
* @return The vote data for the requested contest.
*/
public DirectContestRequest getVotesFromDatabase(ContestRequestByIDs request) throws RaireServiceException {
public DirectContestRequest getVotesFromDatabase(GenerateAssertionsRequest request) throws RaireServiceException {
StringArrayConverter conv = new StringArrayConverter();

validateContestRequestByIDs(request);
Expand Down Expand Up @@ -142,7 +142,7 @@ public GenerateAssertionsResponse storeAssertions(RaireSolution.RaireResultOrErr
* @param request
* TODO Implement all necessary checks. This will require some database interaction.
*/
private void validateContestRequestByIDs(ContestRequestByIDs request) throws RaireServiceException {
private void validateContestRequestByIDs(GenerateAssertionsRequest request) throws RaireServiceException {
if (request.getCountyAndContestIDs().isEmpty()) {
throw new RaireServiceException(new RaireServiceError.InvalidRequest("No IDs"));
} else if (request.getCandidates().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import au.org.democracydevelopers.raire.assertions.AssertionAndDifficulty;
import au.org.democracydevelopers.raireservice.repository.entity.Assertion;
import au.org.democracydevelopers.raireservice.repository.AssertionRepository;
import au.org.democracydevelopers.raireservice.request.RequestByContestName;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import au.org.democracydevelopers.raireservice.response.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -29,7 +29,7 @@ public class GetAssertionsService {
* @return a RaireSolution - the resulting collection of assertions, with metadata, or an error.
* Database interactions: retrieves assertions from the database.
*/
public GetAssertionsResponse getAssertions(RequestByContestName request) {
public GetAssertionsResponse getAssertions(GetAssertionsRequest request) {

// TODO: Validate the request, e.g. check that the contest name exists.
// Return INVALID_REQUEST error if not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import au.org.democracydevelopers.raireservice.repository.CVRRepository;
import au.org.democracydevelopers.raireservice.repository.entity.Assertion;
import au.org.democracydevelopers.raireservice.repository.entity.CVRContestInfo;
import au.org.democracydevelopers.raireservice.repository.entity.NEBAssertion;
import au.org.democracydevelopers.raireservice.repository.entity.NENAssertion;
import au.org.democracydevelopers.raireservice.request.ContestRequestByIDs;
import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.CountyAndContestID;
import au.org.democracydevelopers.raireservice.request.DirectContestRequest;
import au.org.democracydevelopers.raireservice.response.GenerateAssertionsResponse;
Expand Down Expand Up @@ -178,7 +176,7 @@ void generateTrivialAssertionFromDatabase() throws RaireServiceException {
CVRContestInfo cvr1 = new CVRContestInfo(1L,1L,1L,aliceOnly);
cvrRepository.save(cvr1);

ContestRequestByIDs request = new ContestRequestByIDs("testContestName5", 1,
GenerateAssertionsRequest request = new GenerateAssertionsRequest("testContestName5", 1,
100, List.of(aliceBeforeBob), List.of(new CountyAndContestID(1L,1L)) );

DirectContestRequest contestRequest = generateAssertionsService.getVotesFromDatabase(request);
Expand Down Expand Up @@ -212,7 +210,7 @@ void generateTrivialAssertionFromDatabase() throws RaireServiceException {
@Test
void emptyCandidatesInContestRequestThrowsException() {

ContestRequestByIDs request = new ContestRequestByIDs("testContestName5", 1,
GenerateAssertionsRequest request = new GenerateAssertionsRequest("testContestName5", 1,
100, new ArrayList<String>(), List.of(new CountyAndContestID(1L,1L)) );

Exception e = assertThrows(RaireServiceException.class, () -> {
Expand All @@ -231,7 +229,7 @@ void emtpyIDsInContestRequestThrowsException() {

List<CountyAndContestID> nothing = new ArrayList<>();

ContestRequestByIDs request = new ContestRequestByIDs("testContestName5", 1,
GenerateAssertionsRequest request = new GenerateAssertionsRequest("testContestName5", 1,
100, List.of(aliceBeforeBob), nothing);

Exception e = assertThrows(RaireServiceException.class, () -> {
Expand Down

0 comments on commit e6ea537

Please sign in to comment.