下面列出了怎么用javax.validation.UnexpectedTypeException的API类实例代码及写法,或者点击链接到github查看源代码。
@Bean
@Scope(value = "prototype")
public AbstractApiDocService abstractApiDocService(String content) {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
ObjectNode objectNode = mapper.readValue(content, ObjectNode.class);
JsonNode openApiNode = objectNode.get("openapi");
if (openApiNode != null) {
return new ApiDocV3Service(gatewayClient);
} else {
JsonNode swaggerNode = objectNode.get("swagger");
if (swaggerNode != null) {
return new ApiDocV2Service(gatewayClient);
}
}
} catch (IOException e) {
log.debug("Could not convert response body to a Swagger/OpenAPI object.", e);
throw new UnexpectedTypeException("Response is not a Swagger or OpenAPI type object.");
}
return null;
}
/**
* Does transformation API documentation
*
* @param serviceId the unique service id
* @param apiDocInfo the API doc and additional information about transformation
* @return the transformed API documentation relative to Gateway
* @throws ApiDocTransformationException if could not convert Swagger/OpenAPI to JSON
* @throws UnexpectedTypeException if response is not a Swagger/OpenAPI type object
*/
public String transformApiDoc(String serviceId, ApiDocInfo apiDocInfo) {
//maybe null check of apidocinfo
AbstractApiDocService abstractApiDocService = beanApiDocFactory.apply(apiDocInfo.getApiDocContent());
if (abstractApiDocService == null) {
throw new UnexpectedTypeException("Response is not a Swagger or OpenAPI type object.");
}
return abstractApiDocService.transformApiDoc(serviceId, apiDocInfo);
}
public String transformApiDoc(String serviceId, ApiDocInfo apiDocInfo) {
SwaggerParseResult parseResult = new OpenAPIV3Parser().readContents(apiDocInfo.getApiDocContent());
OpenAPI openAPI = parseResult.getOpenAPI();
if (openAPI == null) {
log.debug("Could not convert response body to an OpenAPI object for service {}. {}", serviceId, parseResult.getMessages());
if (parseResult.getMessages() == null) {
throw new UnexpectedTypeException("Response is not an OpenAPI type object.");
} else {
throw new UnexpectedTypeException(parseResult.getMessages().toString());
}
}
boolean hidden = isHidden(openAPI.getTags());
updatePaths(openAPI, serviceId, apiDocInfo, hidden);
updateServerAndLink(openAPI, serviceId, hidden);
updateExternalDoc(openAPI, apiDocInfo);
try {
return initializeObjectMapper().writeValueAsString(openAPI);
} catch (JsonProcessingException e) {
log.debug("Could not convert Swagger to JSON", e);
throw new ApiDocTransformationException("Could not convert Swagger to JSON");
}
}
@Test
public void givenSwaggerJsonNotAsExpectedFormat_whenConvertToSwagger_thenThrowIOException () throws IOException {
String apiDocContent = "Failed content";
ApiDocInfo apiDocInfo = new ApiDocInfo(null, apiDocContent, null);
exceptionRule.expect(UnexpectedTypeException.class);
exceptionRule.expectMessage("Response is not a Swagger type object.");
apiDocV2Service.transformApiDoc(SERVICE_ID, apiDocInfo);
}
@Test
public void testApiDocFactory_whenApDocVersionIsNotAsExpectedFormat() {
exceptionRule.expect(UnexpectedTypeException.class);
exceptionRule.expectMessage("Response is not a Swagger or OpenAPI type object");
AbstractApiDocService abstractApiDocService = beanApiDocFactory.apply("FAILED FORMAT");
assertNull(abstractApiDocService);
}
@Test
public void givenEmptyJson_whenApiDocTransform_thenThrowException() {
String invalidJson = "";
ApiInfo apiInfo = new ApiInfo("org.zowe.apicatalog", "api/v1", "3.0.0", "https://localhost:10014/apicatalog/api-doc", "https://www.zowe.org");
ApiDocInfo apiDocInfo = new ApiDocInfo(apiInfo, invalidJson, null);
exceptionRule.expect(UnexpectedTypeException.class);
exceptionRule.expectMessage("No swagger supplied");
apiDocV3Service.transformApiDoc(SERVICE_ID, apiDocInfo);
}
@Test
public void givenInvalidJson_whenApiDocTransform_thenThrowException() {
String invalidJson = "nonsense";
ApiInfo apiInfo = new ApiInfo("org.zowe.apicatalog", "api/v1", "3.0.0", "https://localhost:10014/apicatalog/api-doc", "https://www.zowe.org");
ApiDocInfo apiDocInfo = new ApiDocInfo(apiInfo, invalidJson, null);
exceptionRule.expect(UnexpectedTypeException.class);
exceptionRule.expectMessage("attribute openapi is not of type `object`");
apiDocV3Service.transformApiDoc(SERVICE_ID, apiDocInfo);
}
@Test
public void testTransformApiDoc_whenThereIsNotApiDocMatch() {
exceptionRule.expect(UnexpectedTypeException.class);
exceptionRule.expectMessage("Response is not a Swagger or OpenAPI type object");
ApiDocInfo apiDocInfo = new ApiDocInfo(null, "DOC4", null);
transformApiDocService.transformApiDoc(SERVICE_ID, apiDocInfo);
}
public String getLinkHint() {
if (isExternalLink()) {
try {
return new URL(link).getHost();
} catch (MalformedURLException e) {
//this should never happened because constructor protected with design contract.
throw new UnexpectedTypeException("malformed url");
}
} else {
return "/z/" + zone;
}
}
@Test
public void shouldThrowUnexpectedTypeException() throws NoSuchMethodException, SecurityException {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
Constructor<AnnotationTargetTypeBean> constructor = AnnotationTargetTypeBean.class.getConstructor();
EXECUTABLE_VALIDATOR.validateConstructorReturnValue(constructor, new AnnotationTargetTypeBean());
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedGreaterThanEqualsToTargetBean {
@FixedGreaterThanEqualsTo("0.5")
public Object value;
}
FixedGreaterThanEqualsToTargetBean target = new FixedGreaterThanEqualsToTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class RegexTargetBean {
@Regex("(ok|OK)")
public Object value;
}
RegexTargetBean target = new RegexTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class MinByteLengthTargetBean {
@MinByteLength(100)
public Object value;
}
MinByteLengthTargetBean target = new MinByteLengthTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedBeforeTargetBean {
@FixedBefore("2013/07/05")
public Object value;
}
FixedBeforeTargetBean target = new FixedBeforeTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class DateFormatTargetBean {
@DateFormat
public Object value;
}
DateFormatTargetBean target = new DateFormatTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class ASCIITargetBean {
@ASCII
public Object value;
}
ASCIITargetBean target = new ASCIITargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class AlphamericTargetBean {
@Alphameric
public Object value;
}
AlphamericTargetBean target = new AlphamericTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class MaxLengthTargetBean {
@MaxLength(5)
public Object value;
}
MaxLengthTargetBean target = new MaxLengthTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedAfterTargetBean {
@FixedAfter("2013/07/04")
public Object value;
}
FixedAfterTargetBean target = new FixedAfterTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedLessThanTargetBean {
@FixedLessThan("0.5")
public Object value;
}
FixedLessThanTargetBean target = new FixedLessThanTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class EmailTargetBean {
@Email
public Object value;
}
EmailTargetBean target = new EmailTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class CreditCardTargetBean {
@CreditCard(value = Type.VISA)
public Object value;
}
CreditCardTargetBean target = new CreditCardTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class NumberTargetBean {
@Number(precision = 2)
public Object value;
}
NumberTargetBean target = new NumberTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class MaxSizeTargetBean {
@MaxSize(5)
public Object value;
}
MaxSizeTargetBean target = new MaxSizeTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class AlphabetTargetBean {
@Alphabet
public Object value;
}
AlphabetTargetBean target = new AlphabetTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedAfterEqualsToTargetBean {
@FixedAfterEqualsTo("2013/07/04")
public Object value;
}
FixedAfterEqualsToTargetBean target = new FixedAfterEqualsToTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedGreaterThanTargetBean {
@FixedGreaterThan("0.5")
public Object value;
}
FixedGreaterThanTargetBean target = new FixedGreaterThanTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class ZenkakuTargetBean {
@Zenkaku
public Object value;
}
ZenkakuTargetBean target = new ZenkakuTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class FixedBeforeEqualsToTargetBean {
@FixedBeforeEqualsTo("2013/07/05")
public Object value;
}
FixedBeforeEqualsToTargetBean target = new FixedBeforeEqualsToTargetBean();
VALIDATOR.validate(target);
}
@Test
public void shouldThrowUnexpectedTypeException() {
thrown.expect(UnexpectedTypeException.class);
thrown.expectMessage(containsString("HV000030"));
class MaxByteLengthTargetBean {
@MaxByteLength(100)
public Object value;
}
MaxByteLengthTargetBean target = new MaxByteLengthTargetBean();
VALIDATOR.validate(target);
}