Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#32] Incorrent validation marker location #35

Merged
merged 2 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.reprezen.swagedit.editor.SwaggerDocument
import com.reprezen.swagedit.validation.Validator
import org.junit.Test

import static org.junit.Assert.assertEquals
import static org.junit.Assert.*
import static org.hamcrest.core.IsCollectionContaining.*

/**
* Tests as documentation for #9 - User-friendly validation messages
Expand All @@ -16,15 +17,6 @@ class ValidationMessageTest {
val validator = new Validator
val document = new SwaggerDocument

def assertModelHasValidationError(String expectedMessage, String modelText) {
document.set(modelText)
val errors = validator.validate(document)
assertEquals(1, errors.size)

val error = errors.get(0)
assertEquals(expectedMessage, error.message)
}

@Test
def testMessage_additionalItems_notAllowed() {
// previous message 'instance type (integer) does not match any allowed primitive type (allowed: ["array"])'
Expand All @@ -45,7 +37,11 @@ class ValidationMessageTest {
description: OK
'''

assertModelHasValidationError(expected, content)
document.set(content)
val errors = validator.validate(document)

assertEquals(1, errors.size)
assertEquals(expected, errors.get(0).message)
}

@Test
Expand All @@ -65,7 +61,11 @@ class ValidationMessageTest {
responses: 2
'''

assertModelHasValidationError(expected, content)
document.set(content)
val errors = validator.validate(document)

assertEquals(1, errors.size)
assertEquals(expected, errors.get(0).message)
}

@Test
Expand All @@ -89,18 +89,16 @@ class ValidationMessageTest {
description: OK
'''

assertModelHasValidationError(expected, content)
document.set(content)
val errors = validator.validate(document)

assertEquals(1, errors.size)
assertEquals(expected, errors.get(0).message)
}

@Test
def testMessage_oneOf_fail() {
// previous message 'instance failed to match exactly one schema (matched 0 out of 2)'
val expected =
'''
value of type integer is not allowed, value should be of type string
object has properties "description" which are not allowed
object has missing required properties "$ref"
'''

val content = '''
swagger: '2.0'
Expand All @@ -116,7 +114,14 @@ class ValidationMessageTest {
description: 200
'''

assertModelHasValidationError(expected, content)
document.set(content)
val errors = validator.validate(document)

assertThat(errors.map[message], hasItems(
"value of type integer is not allowed, value should be of type string",
"object has properties \"description\" which are not allowed",
"object has missing required properties \"$ref\""
))
}

@Test
Expand All @@ -139,7 +144,10 @@ class ValidationMessageTest {
description: OK
'''

assertModelHasValidationError(expected, content)
document.set(content)
val errors = validator.validate(document)

assertThat(errors.map[message], hasItems(expected))
}

@Test
Expand All @@ -157,7 +165,11 @@ class ValidationMessageTest {
description: OK
'''

assertModelHasValidationError(expected, content)
document.set(content)
val errors = validator.validate(document)

assertEquals(1, errors.size)
assertEquals(expected, errors.get(0).message)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.eclipse.core.resources.IMarker
import org.junit.Test

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue

class ValidatorTest {

Expand Down Expand Up @@ -149,4 +150,31 @@ class ValidatorTest {
assertEquals(5, error.getLine())
}

@Test
def void shouldReturnCorrectErrorPositionOnPathWithHierarchy() throws IOException {
// invalid property schema
val content = '''
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/foo/{bar}:
get:
responses:
'200':
description: OK
schema:
'''

document.set(content)
val errors = validator.validate(document)

assertEquals(5, errors.size())

errors.forEach[
assertTrue(it.line == 10 || it.line == 11)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use a range: (10..11).contains(it.line)

assertEquals(IMarker.SEVERITY_ERROR, it.level)
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dadacoalition.yedit.YEditLog;
import org.dadacoalition.yedit.editor.YEdit;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void doSaveAs() {
protected void checkErrors() {
final IFile file = ((IFileEditorInput) getEditorInput()).getFile();
final IDocument document = getDocumentProvider().getDocument(getEditorInput());
final List<SwaggerError> errors = validator.validate((SwaggerDocument) document);
final Set<SwaggerError> errors = validator.validate((SwaggerDocument) document);

for (SwaggerError error : errors) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.reprezen.swagedit.validation;

import java.util.Iterator;
import java.util.Objects;

import org.eclipse.core.resources.IMarker;
import org.yaml.snakeyaml.parser.ParserException;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerException;
import com.github.fge.jsonschema.core.report.LogLevel;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.google.common.base.Joiner;
import com.reprezen.swagedit.Messages;

Expand Down Expand Up @@ -38,8 +36,8 @@ public static SwaggerError create(JsonMappingException exception) {
return new SwaggerError(IMarker.SEVERITY_ERROR, exception.getMessage(), line);
}

public static SwaggerError create(ProcessingMessage error, int line) {
return new SwaggerError(getLevel(error), rewriteError(error.asJson()), line);
public static SwaggerError create(JsonNode error, int line) {
return new SwaggerError(getLevel(error), rewriteError(error), line);
}

public static SwaggerError create(ParserException e) {
Expand All @@ -64,8 +62,6 @@ private static String rewriteError(JsonNode error) {
return rewriteTypeError(error);
case "enum":
return rewriteEnumError(error);
case "oneOf":
return rewriteOneOfError(error);
case "additionalProperties":
return rewriteAdditionalProperties(error);
case "required":
Expand All @@ -87,26 +83,6 @@ private static String rewriteAdditionalProperties(JsonNode error) {
return String.format(Messages.error_additional_properties_not_allowed, Joiner.on(", ").join(unwanted));
}

private static String rewriteOneOfError(JsonNode node) {
final JsonNode reports = node.get("reports");
String result = "";

for (Iterator<String> it = reports.fieldNames(); it.hasNext();) {
final String key = it.next();
final JsonNode value = reports.get(key);

if (value.isObject()) {
result += rewriteError(value) + "\n";
} else if (value.isArray()) {
for (JsonNode one: value) {
result += rewriteError(one) + "\n";
}
}
}

return result;
}

private static String rewriteTypeError(JsonNode error) {
final JsonNode found = error.get("found");
final JsonNode expected = error.get("expected");
Expand All @@ -129,18 +105,18 @@ private static String rewriteEnumError(JsonNode error) {
return String.format(Messages.error_notInEnum, value.asText(), enumString);
}

protected static int getLevel(ProcessingMessage message) {
if (message == null) {
protected static int getLevel(JsonNode message) {
if (message == null || !message.has("level")) {
return IMarker.SEVERITY_INFO;
}

final LogLevel level = message.getLogLevel();
final String level = message.get("level").asText();

switch (level) {
case ERROR:
case FATAL:
case "error":
case "fatal":
return IMarker.SEVERITY_ERROR;
case WARNING:
case "warning":
return IMarker.SEVERITY_WARNING;
default:
return IMarker.SEVERITY_INFO;
Expand All @@ -164,4 +140,19 @@ public String toString() {
return "{ (level=" + getLevel() + ") " + getMessage() + " at line " + getLine() + " }";
}

@Override
public boolean equals(Object obj) {
if (obj instanceof SwaggerError) {
return Objects.equals(level, ((SwaggerError) obj).level) &&
Objects.equals(line, ((SwaggerError) obj).line) &&
Objects.equals(message, ((SwaggerError) obj).message);
}

return super.equals(obj);
}

@Override
public int hashCode() {
return message.hashCode();
}
}
Loading