From fef9aac74a50e7bae8f531906d260aee0ed414ce Mon Sep 17 00:00:00 2001 From: Alexander von Trostorff Date: Thu, 6 Sep 2018 19:18:43 +0200 Subject: [PATCH] Fixes #249 --- .../faforever/api/data/VotingElideTest.java | 95 +++++++++++++++++++ .../api/data/domain/VotingChoice.java | 2 +- .../VotingSubjectRevealWinnerValidator.java | 5 +- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/inttest/java/com/faforever/api/data/VotingElideTest.java b/src/inttest/java/com/faforever/api/data/VotingElideTest.java index 173c501f9..c34725db9 100644 --- a/src/inttest/java/com/faforever/api/data/VotingElideTest.java +++ b/src/inttest/java/com/faforever/api/data/VotingElideTest.java @@ -12,6 +12,8 @@ import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.Sql.ExecutionPhase; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; @@ -49,6 +51,84 @@ public class VotingElideTest extends AbstractIntegrationTest { " }\n" + "}"; + /* + {"data": + {"type":"votingSubject", + "attributes":{ + "subjectKey":"bla", + "numberOfVotes":0, + "topicUrl":"test", + "beginOfVoteTime":"2018-09-09T11:00:00Z", + "endOfVoteTime":"2018-09-09T11:00:00Z", + "minGamesToVote":0, + "descriptionKey":"test", + "revealWinner":false}, + "relationships":{ + "votingQuestions":{ + "data":[] + } + } + } + } + */ + private static final String CREATE_VOTING_SUBJECT_REVEAL_WINNER_FALSE = "{\"data\":\n" + + " {\"type\":\"votingSubject\",\n" + + " \"attributes\":{\n" + + " \"subjectKey\":\"bla\",\n" + + " \"numberOfVotes\":0,\n" + + " \"topicUrl\":\"test\",\n" + + " \"beginOfVoteTime\":\"2018-09-09T11:00:00Z\",\n" + + " \"endOfVoteTime\":\"2018-09-09T11:00:00Z\",\n" + + " \"minGamesToVote\":0,\n" + + " \"descriptionKey\":\"test\",\n" + + " \"revealWinner\":false},\n" + + " \"relationships\":{\n" + + " \"votingQuestions\":{\n" + + " \"data\":[]\n" + + " }\n" + + " }\n" + + " }\n" + + " }"; + + /* +{"data": + {"type":"votingSubject", + "attributes":{ + "subjectKey":"bla", + "numberOfVotes":0, + "topicUrl":"test", + "beginOfVoteTime":"2018-09-09T11:00:00Z", + "endOfVoteTime":"2018-09-09T11:00:00Z", + "minGamesToVote":0, + "descriptionKey":"test", + "revealWinner":true}, + "relationships":{ + "votingQuestions":{ + "data":[] + } + } + } +} +*/ + private static final String CREATE_VOTING_SUBJECT_REVEAL_WINNER_TRUE = "{\"data\":\n" + + " {\"type\":\"votingSubject\",\n" + + " \"attributes\":{\n" + + " \"subjectKey\":\"bla\",\n" + + " \"numberOfVotes\":0,\n" + + " \"topicUrl\":\"test\",\n" + + " \"beginOfVoteTime\":\"2018-09-09T11:00:00Z\",\n" + + " \"endOfVoteTime\":\"{end-time}\",\n" + + " \"minGamesToVote\":0,\n" + + " \"descriptionKey\":\"test\",\n" + + " \"revealWinner\":true},\n" + + " \"relationships\":{\n" + + " \"votingQuestions\":{\n" + + " \"data\":[]\n" + + " }\n" + + " }\n" + + " }\n" + + " }"; + /* { "data": { @@ -222,4 +302,19 @@ public void postVoteOnEndedSubject() throws Exception { mockMvc.perform(post("/voting/vote").contentType(MediaType.APPLICATION_JSON).content(POST_VOTE_SUBJECT2).with(getOAuthToken(OAuthScope._VOTE))) .andExpect(status().is(422)); } + + @Test + @WithUserDetails(AUTH_MODERATOR) + public void postVotingSubject() throws Exception { + mockMvc.perform(post("/data/votingSubject").contentType(MediaType.APPLICATION_JSON).content(CREATE_VOTING_SUBJECT_REVEAL_WINNER_FALSE)) + .andExpect(status().is(201)); + } + + @Test + @WithUserDetails(AUTH_MODERATOR) + public void postVotingSubjectWithRevealWinnerTrueButVoteNotEnded() throws Exception { + String votingSubject = CREATE_VOTING_SUBJECT_REVEAL_WINNER_TRUE.replaceAll("\\{end-time}", OffsetDateTime.now().plusYears(1).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); + mockMvc.perform(post("/data/votingSubject").contentType(MediaType.APPLICATION_JSON).content(votingSubject)) + .andExpect(status().is(400)); + } } diff --git a/src/main/java/com/faforever/api/data/domain/VotingChoice.java b/src/main/java/com/faforever/api/data/domain/VotingChoice.java index aa96a8a96..2c60ddf1c 100644 --- a/src/main/java/com/faforever/api/data/domain/VotingChoice.java +++ b/src/main/java/com/faforever/api/data/domain/VotingChoice.java @@ -82,7 +82,7 @@ public Integer getOrdinal() { @Transient @ComputedAttribute - public int getNumberOfAnswers() { + public Integer getNumberOfAnswers() { return numberOfAnswers; } diff --git a/src/main/java/com/faforever/api/data/validation/VotingSubjectRevealWinnerValidator.java b/src/main/java/com/faforever/api/data/validation/VotingSubjectRevealWinnerValidator.java index 3a07a2e23..5be1e65ee 100644 --- a/src/main/java/com/faforever/api/data/validation/VotingSubjectRevealWinnerValidator.java +++ b/src/main/java/com/faforever/api/data/validation/VotingSubjectRevealWinnerValidator.java @@ -6,6 +6,9 @@ import javax.validation.ConstraintValidatorContext; import java.time.OffsetDateTime; +/** + * Avoids that a VotingSubject's result is revealed before the vote on the subject ended + */ public class VotingSubjectRevealWinnerValidator implements ConstraintValidator { @Override @@ -15,6 +18,6 @@ public void initialize(VotingSubjectRevealWinnerCheck constraintAnnotation) { @Override public boolean isValid(VotingSubject votingSubject, ConstraintValidatorContext context) { - return votingSubject.getRevealWinner() && votingSubject.getEndOfVoteTime().isBefore(OffsetDateTime.now()); + return votingSubject.getRevealWinner() != Boolean.TRUE || votingSubject.getEndOfVoteTime().isBefore(OffsetDateTime.now()); } }