Search in sources :

Example 16 with Participation

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

the class ResultResource method createResult.

/**
 * POST  /results : Create a new manual result.
 *
 * @param result the result to create
 * @return the ResponseEntity with status 201 (Created) and with body the new result, or with status 400 (Bad Request) if the result has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/results")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<Result> createResult(@RequestBody Result result) throws URISyntaxException {
    log.debug("REST request to save Result : {}", result);
    Participation participation = result.getParticipation();
    Course course = participation.getExercise().getCourse();
    User user = userService.getUserWithGroupsAndAuthorities();
    if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
    if (result.getId() != null) {
        throw new BadRequestAlertException("A new result cannot already have an ID.", ENTITY_NAME, "idexists");
    } else if (result.getResultString() == null) {
        throw new BadRequestAlertException("Result string is required.", ENTITY_NAME, "resultStringNull");
    } else if (result.getScore() == null) {
        throw new BadRequestAlertException("Score is required.", ENTITY_NAME, "scoreNull");
    } else if (result.getScore() != 100 && result.isSuccessful()) {
        throw new BadRequestAlertException("Only result with score 100% can be successful.", ENTITY_NAME, "scoreAndSuccessfulNotMatching");
    } else if (!result.getFeedbacks().isEmpty() && result.getFeedbacks().stream().filter(feedback -> feedback.getText() == null).count() != 0) {
        throw new BadRequestAlertException("In case feedback is present, feedback text and detail text are mandatory.", ENTITY_NAME, "feedbackTextOrDetailTextNull");
    }
    if (!result.getFeedbacks().isEmpty()) {
        result.setHasFeedback(true);
    }
    Result savedResult = resultRepository.save(result);
    result.getFeedbacks().forEach(feedback -> {
        feedback.setResult(savedResult);
        feedbackService.save(feedback);
    });
    ltiService.ifPresent(ltiService -> ltiService.onNewBuildResult(savedResult.getParticipation()));
    return ResponseEntity.created(new URI("/api/results/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) de.tum.in.www1.artemis.service(de.tum.in.www1.artemis.service) java.util(java.util) Logger(org.slf4j.Logger) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Timed(com.codahale.metrics.annotation.Timed) HttpStatus(org.springframework.http.HttpStatus) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) URI(java.net.URI) Authentication(org.springframework.security.core.Authentication) ResultRepository(de.tum.in.www1.artemis.repository.ResultRepository) HeaderUtil(de.tum.in.www1.artemis.web.rest.util.HeaderUtil) Transactional(org.springframework.transaction.annotation.Transactional) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with Participation

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

the class QuizSubmissionService method submitForPractice.

/**
 * Submit the given submission for practice
 *
 * @param quizSubmission the submission to submit
 * @param quizExercise the exercise to submit in
 * @param participation the participation where the result should be saved
 * @return the result entity
 */
@Transactional
public Result submitForPractice(QuizSubmission quizSubmission, QuizExercise quizExercise, Participation participation) {
    // update submission properties
    quizSubmission.setSubmitted(true);
    quizSubmission.setType(SubmissionType.MANUAL);
    // calculate scores
    quizSubmission.calculateAndUpdateScores(quizExercise);
    // create and save result
    Result result = new Result().participation(participation).submission(quizSubmission);
    result.setRated(false);
    result.setCompletionDate(ZonedDateTime.now());
    // calculate score and update result accordingly
    result.evaluateSubmission();
    // save result
    resultRepository.save(result);
    // replace proxy with submission, because of Lazy-fetching
    result.setSubmission(quizSubmission);
    // add result to statistics
    QuizScheduleService.addResultToStatistic(quizExercise.getId(), result);
    return result;
}
Also used : Result(de.tum.in.www1.artemis.domain.Result) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with Participation

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

the class GitServiceIntTest method testCommitAndPush.

@Test
public void testCommitAndPush() throws IOException, GitAPIException {
    Participation participation = new Participation();
    participation.setRepositoryUrl(remoteTestRepo);
    Repository repo = gitService.getOrCheckoutRepository(participation);
    Ref oldHead = repo.findRef("HEAD");
    gitService.commitAndPush(repo, "test commit");
    Ref head = repo.findRef("HEAD");
    assertThat(head).isNotEqualTo(oldHead);
    RevWalk walk = new RevWalk(repo);
    RevCommit commit = walk.parseCommit(head.getObjectId());
    assertThat(commit.getFullMessage()).isEqualTo("test commit");
    // get remote ref
    Git git = new Git(repo);
    Collection<Ref> refs = git.lsRemote().setHeads(true).call();
    Ref remoteHead = refs.iterator().next();
    assertThat(head.getObjectId()).isEqualTo(remoteHead.getObjectId());
    gitService.deleteLocalRepository(repo);
}
Also used : Participation(de.tum.in.www1.artemis.domain.Participation) Repository(de.tum.in.www1.artemis.domain.Repository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with Participation

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

the class GitServiceIntTest method testGetOrCheckoutRepositoryForNewRepo.

@Test
public void testGetOrCheckoutRepositoryForNewRepo() throws IOException, GitAPIException {
    Participation participation = new Participation();
    participation.setRepositoryUrl(remoteTestRepo);
    Repository repo = gitService.getOrCheckoutRepository(participation);
    assertThat(repo.getBranch()).isEqualTo("master");
    assertThat(repo.getDirectory()).exists();
    gitService.deleteLocalRepository(repo);
}
Also used : Participation(de.tum.in.www1.artemis.domain.Participation) Repository(de.tum.in.www1.artemis.domain.Repository) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 20 with Participation

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

the class GitServiceIntTest method testPull.

@Test
public void testPull() throws IOException, GitAPIException {
    Participation participation = new Participation();
    participation.setRepositoryUrl(remoteTestRepo);
    Repository repo = gitService.getOrCheckoutRepository(participation);
    Ref oldHead = repo.findRef("HEAD");
    // commit
    File tempDir = Files.createTempDir();
    Git git = Git.cloneRepository().setURI(remoteTestRepo).setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).setDirectory(tempDir).call();
    git.commit().setMessage("a commit").setAllowEmpty(true).call();
    git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).call();
    // pull
    PullResult pullResult = gitService.pull(repo);
    Ref newHead = repo.findRef("HEAD");
    assertThat(oldHead).isNotEqualTo(newHead);
    RevWalk walk = new RevWalk(repo);
    RevCommit commit = walk.parseCommit(newHead.getObjectId());
    assertThat(commit.getFullMessage()).isEqualTo("a commit");
    FileUtils.deleteDirectory(tempDir);
}
Also used : Participation(de.tum.in.www1.artemis.domain.Participation) Repository(de.tum.in.www1.artemis.domain.Repository) Ref(org.eclipse.jgit.lib.Ref) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) PullResult(org.eclipse.jgit.api.PullResult) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Participation (de.tum.in.www1.artemis.domain.Participation)14 Test (org.junit.Test)14 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)14 Repository (de.tum.in.www1.artemis.domain.Repository)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Result (de.tum.in.www1.artemis.domain.Result)5 Timed (com.codahale.metrics.annotation.Timed)4 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)4 URI (java.net.URI)4 ResponseEntity (org.springframework.http.ResponseEntity)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)3 de.tum.in.www1.artemis.service (de.tum.in.www1.artemis.service)3 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)3 URISyntaxException (java.net.URISyntaxException)3 URL (java.net.URL)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 HttpStatus (org.springframework.http.HttpStatus)3 Authentication (org.springframework.security.core.Authentication)3