TaskUpload.java 2.18 KB
Newer Older
Dominik Vayhinger's avatar
Dominik Vayhinger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package de.hftstuttgart.junitlauncher.rest;

import de.hftstuttgart.junitlauncher.models.UserResult;
import de.hftstuttgart.junitlauncher.utils.JUnitTestHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.IOException;

@Component
public class TaskUpload {

    public final String backendUrl;
    private final JUnitTestHelper jUnitTestHelper;
    private static final Logger LOG = LoggerFactory.getLogger(TaskUpload.class);

    public TaskUpload(@Value("${junitlauncher.backend.url}") String backendUrl, JUnitTestHelper jUnitTestHelper) {
        this.backendUrl = backendUrl;
        this.jUnitTestHelper = jUnitTestHelper;
    }

    @PostConstruct
    public void startTest() {
        String env = System.getenv("job_id");
        if(env == null || env.equals("")) {
            throw new IllegalArgumentException("Environment variable env not set");
        }
        LOG.info("Running jobId: " + env);
        UserResult jUnitResult = null;
        try {
            jUnitResult = jUnitTestHelper.runUnitTests();
            LOG.info("jUnitResult: " + jUnitResult.getCompilationErrors() + jUnitResult.getTestResults());
        } catch (IOException | ClassNotFoundException e) {
            LOG.error("Error while compiling or testing", e);
        }
        LOG.info("Posting UserResult to backend at: " + this.backendUrl);
        ResponseEntity<Void> response = null;
        try {
            response = new RestTemplate().postForEntity(this.backendUrl + "?jobID=" + env, jUnitResult, Void.class);
        } catch (Exception e) {
            LOG.error("Error while Posting to backend: " + e.getMessage());
Kammleiter's avatar
Kammleiter committed
48
//            System.exit(0);
Dominik Vayhinger's avatar
Dominik Vayhinger committed
49
50
51
52
53
54
        }
        if (response.getStatusCode() != HttpStatus.OK) {
            LOG.error("Error while Posting to backend: " + response.toString());
        } else {
            LOG.info("Successfully tested and sent");
        }
Kammleiter's avatar
Kammleiter committed
55
//        System.exit(0);
Dominik Vayhinger's avatar
Dominik Vayhinger committed
56
57
    }
}