Java源码示例:org.eclipse.microprofile.openapi.OASFactory
示例1
/**
* Test method for
* {@link OASFactoryResolverImpl#createObject(java.lang.Class)}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateObject_All() {
Class modelClasses[] = { APIResponse.class, APIResponses.class, Callback.class, Components.class,
Contact.class, Content.class, Discriminator.class, Encoding.class, Example.class,
ExternalDocumentation.class, Header.class, Info.class, License.class, Link.class, MediaType.class,
OAuthFlow.class, OAuthFlows.class, OpenAPI.class, Operation.class, Parameter.class, PathItem.class,
Paths.class, RequestBody.class, Schema.class, SecurityRequirement.class,
SecurityScheme.class, Server.class, ServerVariable.class, Tag.class, XML.class };
for (Class modelClass : modelClasses) {
Constructible object = OASFactory.createObject(modelClass);
Assert.assertNotNull(object);
}
}
示例2
/**
* Test method for
* {@link OASFactoryResolverImpl#createObject(java.lang.Class)}.
*/
@Test
public void testCreateObject_License() {
License license = OASFactory.createObject(License.class).name("Test License").url("urn:test-url");
Assert.assertNotNull(license);
Assert.assertEquals(LicenseImpl.class, license.getClass());
Assert.assertEquals("Test License", license.getName());
Assert.assertEquals("urn:test-url", license.getUrl());
}
示例3
/**
* Test method for
* {@link OASFactoryResolverImpl#createObject(java.lang.Class)}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateObject_RTE() {
Class c = String.class;
try {
OASFactory.createObject(c);
Assert.fail("Expected a runtime error.");
} catch (RuntimeException e) {
Assert.assertEquals("SROAP09000: Class 'java.lang.String' is not Constructible.", e.getMessage());
}
}
示例4
private <T extends Constructible> T createConstructibleInstance(Class<T> clazz) {
// Check that the OASFactory is able to create an instance of the given Class.
final T o1 = OASFactory.createObject(clazz);
assertNotNull(o1, "The return value of OASFactory.createObject(" + clazz.getName() + ") must not be null.");
assertTrue(clazz.isInstance(o1), "The return value of OASFactory.createObject() is expected to be an instance of: " + clazz.getName());
final T o2 = OASFactory.createObject(clazz);
assertNotNull(o2, "The return value of OASFactory.createObject(" + clazz.getName() + ") must not be null.");
assertTrue(clazz.isInstance(o2), "The return value of OASFactory.createObject() is expected to be an instance of: " + clazz.getName());
assertNotSame(o2, o1, "OASFactory.createObject(" + clazz.getName() + ") is expected to create a new object on each invocation.");
return o1;
}
示例5
@Override
public PathItem filterPathItem(PathItem pathItem){
if(pathItem.getGET() != null && "Retrieve all available flights".equals(pathItem.getGET().getSummary())){
//Add new operation
pathItem.PUT(OASFactory.createObject(Operation.class).
summary("filterPathItem - added put operation")
.responses(OASFactory.createObject(APIResponses.class).addAPIResponse("200",
OASFactory.createObject(APIResponse.class).description("filterPathItem - successfully put airlines"))));
//Spec states : All filterable descendant elements of a filtered element must be called before its ancestor
//Override the operatioId value that was previously overridden by the filterOperation method
pathItem.getGET().setOperationId("filterPathItemGetFlights");
}
if (pathItem.getPOST() != null && "createBooking".equals(pathItem.getPOST().getOperationId())) {
Map<String, Callback> callbacks = pathItem.getPOST().getCallbacks();
if (callbacks.containsKey("bookingCallback")) {
Callback callback = callbacks.get("bookingCallback");
if (callback.hasPathItem("http://localhost:9080/airlines/bookings")
&& callback.getPathItem("http://localhost:9080/airlines/bookings").getGET() != null) {
if ("child - Retrieve all bookings for current user".equals(callback.getPathItem("http://localhost:9080/airlines/bookings")
.getGET().getDescription())) {
callback.getPathItem("http://localhost:9080/airlines/bookings").getGET()
.setDescription("parent - Retrieve all bookings for current user");
}
}
}
}
return pathItem;
}
示例6
@Override
public OpenAPI buildModel() {
return OASFactory.createObject(OpenAPI.class);
}
示例7
@Test(expectedExceptions = { NullPointerException.class })
public void nullValueTest() {
@SuppressWarnings("unused")
final Object o = OASFactory.createObject(null);
}
示例8
@Test(expectedExceptions = { IllegalArgumentException.class })
public void baseInterfaceTest() {
@SuppressWarnings("unused")
final Constructible c = OASFactory.createObject(Constructible.class);
}
示例9
@Test(expectedExceptions = { IllegalArgumentException.class })
public void extendedBaseInterfaceTest() {
@SuppressWarnings("unused")
final MyConstructible m = OASFactory.createObject(MyConstructible.class);
}
示例10
@Test(expectedExceptions = { IllegalArgumentException.class })
public void extendedInterfaceTest() {
@SuppressWarnings("unused")
final MyLicense m = OASFactory.createObject(MyLicense.class);
}
示例11
@Test(expectedExceptions = { IllegalArgumentException.class })
public void customAbstractClassTest() {
@SuppressWarnings("unused")
final MyAbstractLicenseImpl m = OASFactory.createObject(MyAbstractLicenseImpl.class);
}
示例12
@Test(expectedExceptions = { IllegalArgumentException.class })
public void customClassTest() {
@SuppressWarnings("unused")
final MyLicenseImpl m = OASFactory.createObject(MyLicenseImpl.class);
}
示例13
private static OpenAPI createSimplePingOpenAPI() {
return OASFactory.createOpenAPI()
.openapi("3.0.1")
.info(
OASFactory.createInfo()
.title("Ping Specification")
.version("1.0")
.license(OASFactory.createLicense()
.name("Eclipse Public License - v2.0")
.url("https://www.eclipse.org/legal/epl-2.0/"))
.description("This is a test spec")
.contact(
OASFactory.createContact()
.name("OpenAPI Tools")
.email("[email protected]"))
)
.addServer(
OASFactory.createServer()
.url("http://localhost:8000/")
)
.paths(
OASFactory.createPaths()
.addPathItem(
"/ping", OASFactory.createPathItem()
.GET(
OASFactory.createOperation()
.operationId("pingGet")
.summary("A simple get call")
.description("When this method is called, the server answers with 200 OKs")
.addTag("demo")
.responses(
OASFactory.createAPIResponses()
.addAPIResponse(
"200", OASFactory.createAPIResponse()
.description("OK")
)
)
)
)
);
}
示例14
private static OpenAPI createPingOpenAPI() {
return OASFactory.createOpenAPI()
.openapi("3.0.1")
.info(
OASFactory.createInfo()
.title("Ping Specification")
.version("1.0")
.license(OASFactory.createLicense()
.name("Eclipse Public License - v2.0")
.url("https://www.eclipse.org/legal/epl-2.0/"))
.description("This is a test spec")
.contact(
OASFactory.createContact()
.name("OpenAPI Tools")
.email("[email protected]"))
)
.addServer(
OASFactory.createServer()
.url("http://localhost:8000/")
)
.paths(
OASFactory.createPaths()
.addPathItem(
"/ping", OASFactory.createPathItem()
.GET(
OASFactory.createOperation()
.operationId("pingGet")
.summary("A simple get call")
.description("When this method is called, the server answers with 200 OKs")
.addTag("demo")
.responses(
OASFactory.createAPIResponses()
.addAPIResponse(
"200", OASFactory.createAPIResponse()
.description("OK")
)
)
)
)
.addPathItem(
"/ping2", OASFactory.createPathItem()
.GET(
OASFactory.createOperation()
.operationId("ping2Get")
.summary("A get that return an object response")
.description("When this method is called, the server answers with 200 and a json object as response")
.addTag("demo")
.responses(
OASFactory.createAPIResponses()
.addAPIResponse(
"200", OASFactory.createAPIResponse()
.content(OASFactory.createContent()
.addMediaType("application/json", OASFactory.createMediaType()
.schema(OASFactory.createSchema()
.ref("'#/components/schemas/Myobject'")))
)
.description("A second path to test validation of ref examples")
)
)
)
)
).components(OASFactory
.createComponents()
.addSchema("Myobject", OASFactory.createSchema()
.type(SchemaType.OBJECT)
.addProperty("name", OASFactory.createSchema()
.type(SchemaType.STRING))
.addProperty("prop1", OASFactory.createSchema()
.type(SchemaType.STRING)
.example("prop 1 description"))
.addProperty("status", OASFactory.createSchema()
.ref("#/components/schemas/Status"))
).addSchema("Status", OASFactory.createSchema()
.type(SchemaType.STRING)
.description("status description")
.example("status example")
.addEnumeration("item1")
.addEnumeration("item2")
)
);
}