Search in sources :

Example 36 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ResultListenerIntegrationTest method assertParticipantScoreStructure.

private void assertParticipantScoreStructure(ParticipantScore participantScore, Long expectedExerciseId, Long expectedParticipantId, Long expectedLastResultId, Double expectedLastScore, Long expectedLastRatedResultId, Double expectedLastRatedScore, Double expectedLastPoints, Double expectedLastRatedPoints) {
    assertThat(participantScore.getExercise().getId()).isEqualTo(expectedExerciseId);
    if (participantScore.getClass().equals(StudentScore.class)) {
        StudentScore studentScore = (StudentScore) participantScore;
        assertThat(studentScore.getUser().getId()).isEqualTo(expectedParticipantId);
    } else {
        TeamScore teamScore = (TeamScore) participantScore;
        assertThat(teamScore.getTeam().getId()).isEqualTo(expectedParticipantId);
    }
    if (expectedLastResultId == null) {
        assertThat(participantScore.getLastResult()).isNull();
    } else {
        assertThat(participantScore.getLastResult().getId()).isEqualTo(expectedLastResultId);
    }
    assertThat(participantScore.getLastScore()).isEqualTo(expectedLastScore);
    assertThat(participantScore.getLastPoints()).isEqualTo(expectedLastPoints);
    if (expectedLastRatedResultId == null) {
        assertThat(participantScore.getLastRatedResult()).isNull();
    } else {
        assertThat(participantScore.getLastRatedResult().getId()).isEqualTo(expectedLastRatedResultId);
    }
    assertThat(participantScore.getLastRatedScore()).isEqualTo(expectedLastRatedScore);
    assertThat(participantScore.getLastRatedPoints()).isEqualTo(expectedLastRatedPoints);
}
Also used : StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 37 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ResultListenerIntegrationTest method saveRatedResult_then_saveAnotherUnratedResult_ShouldUpdateOriginalStudentScore.

@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@ValueSource(booleans = { true, false })
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void saveRatedResult_then_saveAnotherUnratedResult_ShouldUpdateOriginalStudentScore(boolean isTeamTest) {
    ParticipantScore originalStudentScore = setupTestScenarioWithOneResultSaved(true, isTeamTest);
    Result originalResult = originalStudentScore.getLastResult();
    // creating a new rated result should trigger the entity listener and update the student score BUT only the not rated part
    Result newResult = createNewResult(isTeamTest, false);
    verifyStructureOfParticipantScoreInDatabase(isTeamTest, newResult.getId(), newResult.getScore(), originalResult.getId(), originalResult.getScore());
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 38 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project Artemis by ls1intum.

the class ScoreService method removeOrUpdateAssociatedParticipantScore.

/**
 * Either updates or removes an existing participant score when a result is removed
 * The annotation "@Transactional" is ok because it means that this method does not support run in an outer transactional context, instead the outer transaction is paused
 *
 * @param resultToBeDeleted result that will be removes
 */
// ok (see JavaDoc)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void removeOrUpdateAssociatedParticipantScore(Result resultToBeDeleted) {
    // In this method we use custom @Query methods that will fail if no authentication is available, therefore
    // we check this here and set a dummy authentication if none is available (this is the case in a scheduled service or
    // websocket)
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        SecurityUtils.setAuthorizationObject();
    }
    Optional<ParticipantScore> associatedParticipantScoreOptional;
    if (resultToBeDeleted.isRated() != null && resultToBeDeleted.isRated()) {
        associatedParticipantScoreOptional = participantScoreRepository.findParticipantScoreByLastRatedResult(resultToBeDeleted);
    } else {
        associatedParticipantScoreOptional = participantScoreRepository.findParticipantScoresByLastResult(resultToBeDeleted);
    }
    if (associatedParticipantScoreOptional.isEmpty()) {
        return;
    }
    // There is a participant score connected to the result that will be deleted
    ParticipantScore associatedParticipantScore = associatedParticipantScoreOptional.get();
    Exercise exercise = associatedParticipantScore.getExercise();
    String originalParticipantScoreStructure = associatedParticipantScore.toString();
    // There are two possibilities now:
    // A: Another result exists for the exercise and the student / team -> update participant score with the newest one
    // B: No other result exists for the exercise and the student / team -> remove participant score
    tryToFindNewLastResult(resultToBeDeleted, associatedParticipantScore, exercise);
    if (associatedParticipantScore.getLastResult() == null && associatedParticipantScore.getLastRatedResult() == null) {
        participantScoreRepository.deleteById(associatedParticipantScore.getId());
        logger.info("Deleted an existing participant score: " + originalParticipantScoreStructure);
    } else {
        ParticipantScore updatedParticipantScore = participantScoreRepository.saveAndFlush(associatedParticipantScore);
        logger.info("Updated an existing participant score. Was: " + originalParticipantScoreStructure + ". Is: " + updatedParticipantScore);
    }
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) Authentication(org.springframework.security.core.Authentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 39 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project Artemis by ls1intum.

the class ResultListenerIntegrationTest method saveRatedResult_then_saveAnotherUnratedResult_then_removeUnratedResult_ShouldUpdateOriginalStudentScore.

@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@ValueSource(booleans = { true, false })
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void saveRatedResult_then_saveAnotherUnratedResult_then_removeUnratedResult_ShouldUpdateOriginalStudentScore(boolean isTeamTest) {
    ParticipantScore originalParticipantScore = setupTestScenarioWithOneResultSaved(true, isTeamTest);
    Result originalResult = originalParticipantScore.getLastResult();
    // creating a new rated result should trigger the entity listener and update the student score BUT only the not rated part
    Result newResult = createNewResult(isTeamTest, false);
    verifyStructureOfParticipantScoreInDatabase(isTeamTest, newResult.getId(), newResult.getScore(), originalResult.getId(), originalResult.getScore());
    resultRepository.deleteById(newResult.getId());
    List<Result> savedResults = resultRepository.findAll();
    assertThat(savedResults).hasSize(1);
    verifyStructureOfParticipantScoreInDatabase(isTeamTest, originalResult.getId(), originalResult.getScore(), originalResult.getId(), originalResult.getScore());
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 40 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project Artemis by ls1intum.

the class ResultListenerIntegrationTest method saveUnratedResult_then_changeScoreOfResult_ShouldUpdateOriginalStudentScore.

@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@ValueSource(booleans = { true, false })
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void saveUnratedResult_then_changeScoreOfResult_ShouldUpdateOriginalStudentScore(boolean isTeamTest) {
    ParticipantScore originalParticipantScore = setupTestScenarioWithOneResultSaved(false, isTeamTest);
    Result originalResult = originalParticipantScore.getLastResult();
    // update the associated student score should trigger the entity listener and update the student score
    originalResult.setScore(0D);
    Result updatedResult = resultRepository.saveAndFlush(originalResult);
    verifyStructureOfParticipantScoreInDatabase(isTeamTest, updatedResult.getId(), updatedResult.getScore(), null, null);
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ParticipantScore (de.tum.in.www1.artemis.domain.scores.ParticipantScore)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 ValueSource (org.junit.jupiter.params.provider.ValueSource)28 WithMockUser (org.springframework.security.test.context.support.WithMockUser)28 StudentScore (de.tum.in.www1.artemis.domain.scores.StudentScore)14 TeamScore (de.tum.in.www1.artemis.domain.scores.TeamScore)10 Participant (de.tum.in.www1.artemis.domain.participation.Participant)4 Transactional (org.springframework.transaction.annotation.Transactional)4 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)2 ExerciseScoresAggregatedInformation (de.tum.in.www1.artemis.web.rest.dto.ExerciseScoresAggregatedInformation)2 ExerciseScoresDTO (de.tum.in.www1.artemis.web.rest.dto.ExerciseScoresDTO)2 Authentication (org.springframework.security.core.Authentication)2