From 906eb7b21140e82519e66e1e4c727cdd54381d0a Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Sat, 16 Dec 2023 17:49:14 +0700 Subject: [PATCH] fix(server): fix contest clarification widget for contestants (#563) --- .../api/ContestWebApiIntegrationTests.java | 56 +++++++++++++++++++ .../contest/web/ContestWebConfigFetcher.java | 29 +++++----- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/judgels-backends/judgels-server-app/src/integTest/java/judgels/uriel/api/ContestWebApiIntegrationTests.java b/judgels-backends/judgels-server-app/src/integTest/java/judgels/uriel/api/ContestWebApiIntegrationTests.java index 7ea5002f0..f7272eb6d 100644 --- a/judgels-backends/judgels-server-app/src/integTest/java/judgels/uriel/api/ContestWebApiIntegrationTests.java +++ b/judgels-backends/judgels-server-app/src/integTest/java/judgels/uriel/api/ContestWebApiIntegrationTests.java @@ -16,24 +16,32 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; +import judgels.uriel.ContestClarificationClient; import judgels.uriel.ContestWebClient; import judgels.uriel.api.contest.Contest; +import judgels.uriel.api.contest.clarification.ContestClarification; +import judgels.uriel.api.contest.clarification.ContestClarificationAnswerData; +import judgels.uriel.api.contest.clarification.ContestClarificationData; +import judgels.uriel.api.contest.clarification.ContestClarificationStatus; import judgels.uriel.api.contest.module.ContestModuleType; import judgels.uriel.api.contest.role.ContestRole; import judgels.uriel.api.contest.supervisor.SupervisorManagementPermission; import judgels.uriel.api.contest.web.ContestState; import judgels.uriel.api.contest.web.ContestTab; +import judgels.uriel.api.contest.web.ContestWebConfig; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class ContestWebApiIntegrationTests extends BaseUrielApiIntegrationTests { private final ContestWebClient webClient = createClient(ContestWebClient.class); + private final ContestClarificationClient clarificationClient = createClient(ContestClarificationClient.class); private Contest contest; @BeforeEach void before() { contest = buildContestWithRoles() + .contestants(CONTESTANT, CONTESTANT_A, CONTESTANT_B) .supervisorWithManagementPermissions(SUPERVISOR_A, SupervisorManagementPermission.ALL) .supervisors(SUPERVISOR_B) .modules(ContestModuleType.REGISTRATION) @@ -244,4 +252,52 @@ void get_web_config__state__virtual() { assertThat(webClient.getWebConfig(contestantToken, contest.getJid()).getState()) .isEqualTo(ContestState.FINISHED); } + + @Test + void get_web_config__clarifications() { + enableModule(contest, ContestModuleType.CLARIFICATION); + beginContest(contest); + + ContestWebConfig config = webClient.getWebConfig(contestantAToken, contest.getJid()); + assertThat(config.getClarificationStatus()).isEqualTo(ContestClarificationStatus.ANSWERED); + assertThat(config.getClarificationCount()).isEqualTo(0); + + config = webClient.getWebConfig(contestantBToken, contest.getJid()); + assertThat(config.getClarificationStatus()).isEqualTo(ContestClarificationStatus.ANSWERED); + assertThat(config.getClarificationCount()).isEqualTo(0); + + config = webClient.getWebConfig(supervisorAToken, contest.getJid()); + assertThat(config.getClarificationStatus()).isEqualTo(ContestClarificationStatus.ASKED); + assertThat(config.getClarificationCount()).isEqualTo(0); + + ContestClarificationData data = new ContestClarificationData.Builder() + .topicJid(contest.getJid()) + .title("title") + .question("question") + .build(); + + ContestClarification c1 = clarificationClient.createClarification(contestantAToken, contest.getJid(), data); + clarificationClient.createClarification(contestantAToken, contest.getJid(), data); + clarificationClient.createClarification(contestantBToken, contest.getJid(), data); + + assertThat(webClient.getWebConfig(contestantAToken, contest.getJid()).getClarificationCount()) + .isEqualTo(0); + assertThat(webClient.getWebConfig(contestantBToken, contest.getJid()).getClarificationCount()) + .isEqualTo(0); + assertThat(webClient.getWebConfig(supervisorAToken, contest.getJid()).getClarificationCount()) + .isEqualTo(3); + + ContestClarificationAnswerData answerData = new ContestClarificationAnswerData.Builder() + .answer("answer") + .build(); + + clarificationClient.answerClarification(managerToken, contest.getJid(), c1.getJid(), answerData); + + assertThat(webClient.getWebConfig(contestantAToken, contest.getJid()).getClarificationCount()) + .isEqualTo(1); + assertThat(webClient.getWebConfig(contestantBToken, contest.getJid()).getClarificationCount()) + .isEqualTo(0); + assertThat(webClient.getWebConfig(supervisorAToken, contest.getJid()).getClarificationCount()) + .isEqualTo(2); + } } diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/web/ContestWebConfigFetcher.java b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/web/ContestWebConfigFetcher.java index 136d22d0b..62f082df6 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/web/ContestWebConfigFetcher.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/uriel/contest/web/ContestWebConfigFetcher.java @@ -157,21 +157,24 @@ public ContestWebConfig fetchConfig(String userJid, Contest contest) { } } - ContestClarificationStatus clarificationStatus; - if (clarificationRoleChecker.canSupervise(userJid, contest)) { - clarificationStatus = ContestClarificationStatus.ASKED; - } else { - clarificationStatus = ContestClarificationStatus.ANSWERED; - } - int clarificationCount = 0; - - // TODO(fushar): remove this try block and fix the test so that it doesn't use mock for clarificationDao + ContestClarificationStatus clarificationStatus = ContestClarificationStatus.ASKED; try { - clarificationDao - .selectByContestJid(contestJid) - .whereStatusIs(clarificationStatus.name()) - .count(); + // TODO(fushar): remove this try block and fix the test so that it doesn't use mock for clarificationDao + if (clarificationRoleChecker.canManage(userJid, contest)) { + clarificationStatus = ContestClarificationStatus.ASKED; + clarificationCount = clarificationDao + .selectByContestJid(contestJid) + .whereStatusIs(clarificationStatus.name()) + .count(); + } else { + clarificationStatus = ContestClarificationStatus.ANSWERED; + clarificationCount = clarificationDao + .selectByContestJid(contestJid) + .whereUserIsAsker(userJid) + .whereStatusIs(clarificationStatus.name()) + .count(); + } } catch (NullPointerException e) { // skip; for test only }