Skip to content

Commit

Permalink
Merge pull request #116 from cxcorp/fix-validation-parsing
Browse files Browse the repository at this point in the history
Fix parsing of validation case errors
  • Loading branch information
nygrenh authored Dec 1, 2016
2 parents 784fe8d + 63255a3 commit 4265d51
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>fi.helsinki.cs.tmc</groupId>
<artifactId>core</artifactId>
<version>0.9.2-SNAPSHOT</version>
<version>0.9.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tmc-core</name>
<url>http://testmycode.net</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package fi.helsinki.cs.tmc.core.communication.serialization;

import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
import fi.helsinki.cs.tmc.core.domain.submission.ValidationErrorImpl;
import fi.helsinki.cs.tmc.langs.abstraction.ValidationError;
import fi.helsinki.cs.tmc.stylerunner.validation.CheckstyleResult;
import fi.helsinki.cs.tmc.testrunner.CaughtException;
import fi.helsinki.cs.tmc.testrunner.StackTraceSerializer;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -21,6 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -49,6 +52,16 @@ SubmissionResult.Status.class, new StatusDeserializer())
StackTraceElement.class, new StackTraceSerializer())
.registerTypeAdapter(
ImmutableList.class, new ImmutableListJsonDeserializer())
.registerTypeAdapter(
/* Needed because ValidationResultImpl stores filenames in
* Map<File, List<ValidationError>, but Gson doesn't know
* how to deserialize a string into a File */
File.class, new FileDeserializer())
.registerTypeAdapter(
/* Needed because ValidationResultImpl stores errors in
* abstract ValidationErrors which obviously don't have a
* default constructor for Gson to use */
ValidationError.class, new ValidationErrorInstanceCreator())
.create();

SubmissionResult result = gson.fromJson(json, SubmissionResult.class);
Expand Down Expand Up @@ -91,7 +104,26 @@ public SubmissionResult.Status deserialize(
}
}

private class ImmutableListJsonDeserializer implements JsonDeserializer<ImmutableList<?>> {
private static class ValidationErrorInstanceCreator
implements InstanceCreator<ValidationError> {
@Override
public ValidationError createInstance(Type type) {
return new ValidationErrorImpl();
}
}

private static class FileDeserializer implements JsonDeserializer<File> {
@Override
public File deserialize(
JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
String filePath = jsonElement.getAsString();
return new File(filePath);
}
}

private static class ImmutableListJsonDeserializer
implements JsonDeserializer<ImmutableList<?>> {
@Override
public ImmutableList<?> deserialize(
JsonElement json, Type type, JsonDeserializationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public SubmissionResult() {
@SerializedName("solution_url")
private String solutionUrl;

private ValidationResultImpl validations;

private String valgrind;

private boolean reviewed;
Expand Down Expand Up @@ -105,14 +103,6 @@ public void setError(String error) {
this.error = error;
}

public ValidationResultImpl getValidations() {
return validations;
}

public void setValidations(ValidationResultImpl validations) {
this.validations = validations;
}

public List<TestResult> getTestCases() {
return testCases;
}
Expand Down Expand Up @@ -322,8 +312,6 @@ public String toString() {
+ feedbackAnswerUrl
+ ", solutionUrl="
+ solutionUrl
+ ", validations="
+ validations
+ ", \n valgrind="
+ valgrind
+ ", reviewed="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
import fi.helsinki.cs.tmc.core.communication.serialization.SubmissionResultParser;
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
import fi.helsinki.cs.tmc.core.utils.TestUtils;
import fi.helsinki.cs.tmc.langs.abstraction.Strategy;
import fi.helsinki.cs.tmc.langs.abstraction.ValidationError;
import fi.helsinki.cs.tmc.langs.abstraction.ValidationResult;
import fi.helsinki.cs.tmc.langs.domain.TestResult;
import fi.helsinki.cs.tmc.testrunner.TestCase;

import com.google.common.collect.ImmutableList;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.net.URI;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class SubmissionResultParserTest {


private SubmissionResultParser parser;

@Before
Expand All @@ -39,7 +37,7 @@ public void parsesNonLangsTestResult() throws Exception {
TestResult test = tests.get(0);
assertThat(test.getException()).isInstanceOf(ImmutableList.class);
assertThat(test.getException().size()).isEqualTo(27);
for (String str : test.getException()){
for (String str : test.getException()) {
assertThat(str).isInstanceOf(String.class);
}
}
Expand All @@ -54,8 +52,30 @@ public void parsesLangsTestResult() throws Exception {
TestResult test = tests.get(0);
assertThat(test.getDetailedMessage()).isInstanceOf(ImmutableList.class);
assertThat(test.getDetailedMessage().size()).isEqualTo(26);
for (String str : test.getDetailedMessage()){
for (String str : test.getDetailedMessage()) {
assertThat(str).isInstanceOf(String.class);
}
}

@Test
public void parsesTestResultsWithFailedCheckstyle() throws IOException {
String json = TestUtils.readJsonFile(this.getClass(), "checkstyleFailed.json");
SubmissionResult submissionResult = parser.parseFromJson(json);
assertThat(submissionResult).isNotNull();

ValidationResult result = submissionResult.getValidationResult();
assertThat(result.getStrategy()).isEqualTo(Strategy.FAIL);

Map<File, List<ValidationError>> filesErrors = result.getValidationErrors();
assertThat(filesErrors).hasSize(1);

List<ValidationError> errors = filesErrors.get(new File("error/errorMessages.java"));
assertThat(errors).hasSize(73);

for (ValidationError error : errors) {
assertThat(error).isNotNull();
assertThat(error.getMessage()).isNotNull();
assertThat(error.getSourceName()).isNotNull();
}
}
}
4 changes: 2 additions & 2 deletions src/test/resources/json/checkstyleFailed.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
"strategy": "FAIL",
"validationErrors":
{
"eRror/errorMessages.java":
"error/errorMessages.java":
[
{
"column": 0,
Expand Down Expand Up @@ -725,4 +725,4 @@
"requests_review": false,
"submitted_at": "2015-06-04T13:34:58.155+03:00"

}
}

0 comments on commit 4265d51

Please sign in to comment.