Skip to content

Commit

Permalink
Removed Response classes, currently not needed. Cleared a few compile…
Browse files Browse the repository at this point in the history
…r warnings.
  • Loading branch information
vteague committed Apr 6, 2024
1 parent e1160f2 commit e654d21
Showing 9 changed files with 165 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -20,21 +20,10 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra

package au.org.democracydevelopers.raireservice.controller;

import au.org.democracydevelopers.raireservice.persistence.entity.Contest;
import au.org.democracydevelopers.raireservice.persistence.repository.ContestRepository;
import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.RequestValidationException;
import au.org.democracydevelopers.raireservice.response.GenerateAssertionsResponse;
import au.org.democracydevelopers.raireservice.response.GetAssertionsResponse;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -80,7 +69,7 @@ public ResponseEntity<String> serve(@RequestBody GenerateAssertionsRequest reque
request.Validate(contestRepository);
// For the moment, this is just a dummy "OK" response. Later, it will contain the winner
// as a ResponseEntity<GenerateAssertionsRequest>.
return new ResponseEntity<String>("Placeholder winner", HttpStatus.OK);
return new ResponseEntity<>("Placeholder winner", HttpStatus.OK);
}


@@ -99,7 +88,7 @@ public ResponseEntity<String> serve(@RequestBody GetAssertionsRequest request)
throws RequestValidationException {
request.Validate(contestRepository);
// For the moment, this is just a dummy "OK" response. Later, it will contain the winner.
return new ResponseEntity<String>("Placeholder assertions", HttpStatus.OK);
return new ResponseEntity<>("Placeholder assertions", HttpStatus.OK);
}

/**
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
package au.org.democracydevelopers.raireservice.request;

import au.org.democracydevelopers.raireservice.persistence.repository.ContestRepository;
import io.swagger.v3.oas.annotations.Hidden;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -67,14 +66,6 @@ public ContestRequest(String contestName, List<String> candidates) {
this.candidates = candidates;
}

/**
* Return the contest Name.
* @return the name of the contest.
*/
public String getContestName() {
return contestName;
}

/**
* Set the contest name. Used for deserialization.
* @param contestName the name of the contest.
Original file line number Diff line number Diff line change
@@ -69,7 +69,6 @@ public void setTimeLimitSeconds(float timeLimitSeconds) {
this.timeLimitSeconds = timeLimitSeconds;
}


/**
* No args constructor. Used for serialization.
*/
Original file line number Diff line number Diff line change
@@ -20,12 +20,10 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra

package au.org.democracydevelopers.raireservice.request;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -24,9 +24,7 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import static org.junit.jupiter.api.Assertions.assertTrue;

import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import com.google.gson.Gson;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import org.junit.jupiter.api.BeforeAll;
@@ -312,4 +310,125 @@ public void generateAssertionsWithWhiteSpaceCandidateNameIsAnError() {
assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(), "Bad candidate list"));
}

/**
* The generateAssertions endpoint, called with null/missing total auditable ballots, returns a
* meaningful error.
*/
@Test
public void generateAssertionsWithNullAuditableBallotsIsAnError() {
String url = baseURL + port + generateAssertionsEndpoint;

String requestAsJson =
"{\"timeLimitSeconds\":10.0,\"contestName\":\"Ballina Mayoral\",\"candidates\":[\"Alice\",\"Bob\"]}";

HttpEntity<String> request = new HttpEntity<>(requestAsJson, httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(),"Non-positive total auditable ballots"));
}

/**
* The generateAssertions endpoint, called with zero total auditable ballots, returns a meaningful error.
*/
@Test
public void generateAssertionsWithZeroAuditableBallotsIsAnError() {
String url = baseURL + port + generateAssertionsEndpoint;

GenerateAssertionsRequest generateAssertions = new GenerateAssertionsRequest(
ballina,
0,
5,
List.of("Alice", "Bob")
);

HttpEntity<String> request = new HttpEntity<>(gson.toJson(generateAssertions), httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(),"Non-positive total auditable ballots"));
}

/**
* The generateAssertions endpoint, called with negative total auditable ballots, returns a meaningful error.
*/
@Test
public void generateAssertionsWithNegativeAuditableBallotsIsAnError() {
String url = baseURL + port + generateAssertionsEndpoint;

GenerateAssertionsRequest generateAssertions = new GenerateAssertionsRequest(
ballina,
-10,
5,
List.of("Alice", "Bob")
);

HttpEntity<String> request = new HttpEntity<>(gson.toJson(generateAssertions), httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(),"Non-positive total auditable ballots"));
}



/**
* The generateAssertions endpoint, called with null/missing time limit, returns a meaningful error.
*/
@Test
public void generateAssertionsWithNullTimeLimitIsAnError() {
String url = baseURL + port + generateAssertionsEndpoint;

String requestAsJson =
"{\"totalAuditableBallots\":100,\"contestName\":\"Ballina Mayoral\",\"candidates\":[\"Alice\",\"Bob\"]}";

HttpEntity<String> request = new HttpEntity<>(requestAsJson, httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(),"Non-positive time limit"));
}

/**
* The generateAssertions endpoint, called with zero time limit, returns a meaningful error.
*/
@Test
public void generateAssertionsWithZeroTimeLimitIsAnError() {
String url = baseURL + port + generateAssertionsEndpoint;

GenerateAssertionsRequest generateAssertions = new GenerateAssertionsRequest(
ballina,
100,
0,
List.of("Alice", "Bob")
);

HttpEntity<String> request = new HttpEntity<>(gson.toJson(generateAssertions), httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(),"Non-positive time limit"));
}

/**
* The generateAssertions endpoint, called with negative time limit, returns a meaningful error.
*/
@Test
public void generateAssertionsWithNegativeTimeLimitIsAnError() {
String url = baseURL + port + generateAssertionsEndpoint;

GenerateAssertionsRequest generateAssertions = new GenerateAssertionsRequest(
ballina,
100,
-5,
List.of("Alice", "Bob")
);

HttpEntity<String> request = new HttpEntity<>(gson.toJson(generateAssertions), httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(),"Non-positive time limit"));
}
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import static au.org.democracydevelopers.raireservice.util.StringUtils.containsIgnoreCase;
import static org.junit.jupiter.api.Assertions.assertTrue;

import au.org.democracydevelopers.raireservice.request.GenerateAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import com.google.gson.Gson;
import java.math.BigDecimal;
@@ -304,4 +303,45 @@ public void getAssertionsWithWhitespaceCandidateNameIsAnError() {
assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(), "Bad candidate list"));
}
}

/**
* The getAssertions endpoint, called with a null risk limit, returns a meaningful error.
*/
@Test
public void getAssertionsWithNullRiskLimitIsAnError() {
String url = baseURL + port + getAssertionsEndpoint;

GetAssertionsRequest getAssertions = new GetAssertionsRequest(
ballina,
List.of("Alice", "Bob"),
null
);

HttpEntity<String> request = new HttpEntity<>(gson.toJson(getAssertions), httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(), "Null or negative risk limit"));
}

/**
* The getAssertions endpoint, called with a negative risk limit, returns a meaningful error.
* (Note that a value >=1 is vacuously met but not invalid.)
*/
@Test
public void getAssertionsWithNegativeRiskLimitIsAnError() {
String url = baseURL + port + getAssertionsEndpoint;

GetAssertionsRequest getAssertions = new GetAssertionsRequest(
ballina,
List.of("Alice", "Bob"),
BigDecimal.valueOf(-0.03)
);

HttpEntity<String> request = new HttpEntity<>(gson.toJson(getAssertions), httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

assertTrue(response.getStatusCode().is4xxClientError());
assertTrue(containsIgnoreCase(response.getBody(), "Null or negative risk limit"));
}
}

0 comments on commit e654d21

Please sign in to comment.