Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request phoenixnap#157 from georgkoester/issue156error_rep…
Browse files Browse the repository at this point in the history
…orting

Fix phoenixnap#156 error reporting
  • Loading branch information
stojsavljevic authored May 26, 2017
2 parents f5d5f8d + a646b67 commit 9f9dcc6
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.phoenixnap.oss.ramlapisync.raml;

import java.util.List;

/**
* @author georgkoester
*/
public class InvalidRamlError extends Error {

private static final long serialVersionUID = 770865086433357483L;
private final List<?> errors;
private final String ramlFileUrl;

public InvalidRamlError(String ramlFileUrl, List<?> errors) {
this.errors = errors;
this.ramlFileUrl = ramlFileUrl;
}

@Override
public String getMessage() {
return this.toString();
}

@Override
public String toString() {
return getClass().getSimpleName() + " on raml file " + ramlFileUrl + " {" +
"errors=" + errors +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
package com.phoenixnap.oss.ramlapisync.raml;


import com.phoenixnap.oss.ramlapisync.raml.rjp.raml08v1.RJP08V1RamlModelFactory;
import com.phoenixnap.oss.ramlapisync.raml.rjp.raml10v2.RJP10V2RamlModelFactory;
import org.raml.v2.api.RamlModelBuilder;
import org.raml.v2.api.RamlModelResult;
import org.raml.v2.api.model.common.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.phoenixnap.oss.ramlapisync.raml.rjp.raml08v1.RJP08V1RamlModelFactory;
import com.phoenixnap.oss.ramlapisync.raml.rjp.raml10v2.RJP10V2RamlModelFactory;
import java.util.List;

/**
* Factory for creating different instances of RamlModelFactory.
Expand Down Expand Up @@ -93,7 +95,37 @@ public static RamlModelFactory createRamlModelFactoryFor(String ramlURL, RamlVer
logger.info("RJP08V1RamlModelFactory Instantiated");
return new RJP08V1RamlModelFactory();
}
throw new UnsupportedRamlVersionError(RamlVersion.V08, RamlVersion.V10);

if (containsUnsupportedVersionError(ramlModelResult.getValidationResults()) || !isSupportedRamlVersionCombination(ramlVersion, ramlModelResult)) {
throw new UnsupportedRamlVersionError(RamlVersion.V08, RamlVersion.V10);
}

throw new InvalidRamlError(ramlURL, ramlModelResult.getValidationResults());
}

private static boolean containsUnsupportedVersionError(List<ValidationResult> validationResults) {
if (validationResults != null) {
for (ValidationResult result : validationResults) {
if (result.getMessage() != null && result.getMessage().contains("Unsupported version")) {
return true;
}
}
}
return false;
}

private static boolean isSupportedRamlVersionCombination(RamlVersion ramlVersion, RamlModelResult ramlModelResult) {
if (ramlVersion != null) {
if (RamlVersion.V08.equals(ramlVersion) && ramlModelResult.isVersion08()) {
return true;
}
if (RamlVersion.V10.equals(ramlVersion) && ramlModelResult.isVersion10()) {
return true;
}
return false;
} else {
return true; // add unsupported ramlModelResult.isVersionXX() here and return false
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.phoenixnap.oss.ramlapisync.raml;

import java.util.Arrays;

/**
* @author aweisser
*/
Expand All @@ -8,6 +10,6 @@ public class UnsupportedRamlVersionError extends Error {
private static final long serialVersionUID = -2773078854396182834L;

public UnsupportedRamlVersionError(RamlVersion... supportedVersions) {
super("RAML Version is not supported. Supported versions are "+supportedVersions);
super("RAML Version is not supported. Supported versions are " + Arrays.asList(supportedVersions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.phoenixnap.oss.ramlapisync.generation.rules;

import static org.junit.Assert.fail;

import com.phoenixnap.oss.ramlapisync.raml.InvalidRamlError;
import com.phoenixnap.oss.ramlapisync.raml.UnsupportedRamlVersionError;
import org.junit.Test;

/**
* @author kurtpa
* @since 0.4.2
*/
public class Issue156RulesTest extends AbstractRuleTestBase {

@Test
public void versionCheck_shouldReportUnsupportedVersion() throws Exception {
try {
RamlLoader.loadRamlFromFile("issue-156-unsupported.raml");
fail();
} catch (UnsupportedRamlVersionError urve) {
// ok
}
}

@Test
public void versionCheck_shouldReportErrorAsInvalidRamlError() throws Exception {
try {
RamlLoader.loadRamlFromFile("issue-156-normal_invalidity.raml");
fail();
} catch (InvalidRamlError ire) {
// ok
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.Iterator;

import org.junit.Test;

import com.phoenixnap.oss.ramlapisync.generation.RamlGenerator;
import com.phoenixnap.oss.ramlapisync.generation.RamlVerifier;
import com.phoenixnap.oss.ramlapisync.generation.rules.RamlLoader;
Expand All @@ -35,7 +30,7 @@
import com.phoenixnap.oss.ramlapisync.verification.checkers.ActionQueryParameterChecker;
import com.phoenixnap.oss.ramlapisync.verification.checkers.ActionResponseBodySchemaChecker;
import com.phoenixnap.oss.ramlapisync.verification.checkers.ResourceExistenceChecker;

import org.junit.Test;
import test.phoenixnap.oss.plugin.naming.testclasses.ContentTypeTestController;
import test.phoenixnap.oss.plugin.naming.testclasses.MultiContentTypeTestController;
import test.phoenixnap.oss.plugin.naming.testclasses.ParamTestController;
Expand All @@ -51,6 +46,9 @@
import test.phoenixnap.oss.plugin.naming.testclasses.VerifierTestController;
import test.phoenixnap.oss.plugin.naming.testclasses.VerifierUriParamTestController;

import java.util.Collections;
import java.util.Iterator;



/**
Expand Down Expand Up @@ -262,7 +260,7 @@ public void test_ActionQueryParameterChecker_MissingParamsNotRequiredDowngradeTo
RamlVerifier verifier = new RamlVerifier(published, computed, Collections.emptyList(), Collections.singletonList(new ActionQueryParameterChecker()), null);
assertTrue("Check that there are errors", verifier.hasErrors());
assertTrue("Check that there are warnings", verifier.hasWarnings());
assertEquals("Check that implementation should have 2 errors", 3, verifier.getErrors().size());
assertEquals("Check that implementation should have 3 errors", 3, verifier.getErrors().size());
assertEquals("Check that implementation should have 1 warning", 1, verifier.getWarnings().size());
TestHelper.verifyIssuesUnordered(verifier.getErrors(), new Issue[] {
new Issue(IssueSeverity.ERROR, IssueLocation.SOURCE, IssueType.MISSING, ActionQueryParameterChecker.QUERY_PARAMETER_MISSING, "param5 : POST /base/endpointWithURIParam/{uriParam}"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#%RAML 0.8
title: raml2springmvc_invalid_raml_response_statuscode_missing
version: "0.0.1"
baseUri: /api
/customers:
get:
responses:
description: Example.
body:
text/plain:
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#%RAML 0.5
title: raml2springmvc_unsupported_ramlversion
version: "0.0.1"
baseUri: /api
/customers:
get:
responses:
200:
description: Example.
body:
text/plain:

0 comments on commit 9f9dcc6

Please sign in to comment.