BAEL-6258 | Article code (#13782)
* BAEL-6258 | Article code * Renaming test class
This commit is contained in:
parent
d19a53c2d3
commit
e606682a0b
@ -134,6 +134,11 @@
|
|||||||
<version>${byte-buddy.version}</version>
|
<version>${byte-buddy.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.swagger.parser.v3</groupId>
|
||||||
|
<artifactId>swagger-parser</artifactId>
|
||||||
|
<version>${swagger-parser.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -171,6 +176,7 @@
|
|||||||
<derive4j.version>1.1.0</derive4j.version>
|
<derive4j.version>1.1.0</derive4j.version>
|
||||||
<awaitility.version>3.0.0</awaitility.version>
|
<awaitility.version>3.0.0</awaitility.version>
|
||||||
<univocity.version>2.8.4</univocity.version>
|
<univocity.version>2.8.4</univocity.version>
|
||||||
|
<swagger-parser.version>2.1.13</swagger-parser.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,92 @@
|
|||||||
|
package com.baeldung.swaggerparser;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.swagger.parser.OpenAPIParser;
|
||||||
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
|
import io.swagger.v3.oas.models.PathItem;
|
||||||
|
import io.swagger.v3.oas.models.Paths;
|
||||||
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
|
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||||
|
import io.swagger.v3.oas.models.responses.ApiResponses;
|
||||||
|
import io.swagger.v3.oas.models.servers.Server;
|
||||||
|
import io.swagger.v3.parser.core.models.SwaggerParseResult;
|
||||||
|
|
||||||
|
public class SwaggerParser {
|
||||||
|
|
||||||
|
private static String OPENAPI_SPECIFICATION_STRING = "{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"User APIs\",\"version\":\"1.0\"},\"servers\":[{\"url\":\"https://jsonplaceholder.typicode.com\",\"description\":\"Json Place Holder Service\"}],\"paths\":{\"/users/{id}\":{\"parameters\":[{\"schema\":{\"type\":\"integer\"},\"name\":\"id\",\"in\":\"path\",\"required\":true}],\"get\":{\"summary\":\"Fetch user by ID\",\"tags\":[\"User\"],\"responses\":{\"200\":{\"description\":\"OK\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}}}}},\"operationId\":\"get-users-user_id\",\"description\":\"Retrieve a specific user by ID\"}}}}";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
parseYAMLFile();
|
||||||
|
parseJSONFile();
|
||||||
|
parseString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void parseString() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readContents(OPENAPI_SPECIFICATION_STRING, null, null);
|
||||||
|
|
||||||
|
OpenAPI openAPI = result.getOpenAPI();
|
||||||
|
|
||||||
|
if (openAPI != null) {
|
||||||
|
printData(openAPI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void parseJSONFile() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readLocation("sample.yml", null, null);
|
||||||
|
|
||||||
|
OpenAPI openAPI = result.getOpenAPI();
|
||||||
|
|
||||||
|
if (openAPI != null) {
|
||||||
|
printData(openAPI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void parseYAMLFile() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readLocation("sample.json", null, null);
|
||||||
|
|
||||||
|
OpenAPI openAPI = result.getOpenAPI();
|
||||||
|
|
||||||
|
if (openAPI != null) {
|
||||||
|
printData(openAPI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printData(OpenAPI openAPI) {
|
||||||
|
System.out.println(openAPI.getSpecVersion());
|
||||||
|
|
||||||
|
Info info = openAPI.getInfo();
|
||||||
|
System.out.println(info.getTitle());
|
||||||
|
System.out.println(info.getVersion());
|
||||||
|
|
||||||
|
List<Server> servers = openAPI.getServers();
|
||||||
|
for (Server server : servers) {
|
||||||
|
System.out.println(server.getUrl());
|
||||||
|
System.out.println(server.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
Paths paths = openAPI.getPaths();
|
||||||
|
paths.entrySet()
|
||||||
|
.forEach(pathEntry -> {
|
||||||
|
System.out.println(pathEntry.getKey());
|
||||||
|
|
||||||
|
PathItem path = pathEntry.getValue();
|
||||||
|
System.out.println(path.getGet()
|
||||||
|
.getSummary());
|
||||||
|
System.out.println(path.getGet()
|
||||||
|
.getDescription());
|
||||||
|
System.out.println(path.getGet()
|
||||||
|
.getOperationId());
|
||||||
|
|
||||||
|
ApiResponses responses = path.getGet()
|
||||||
|
.getResponses();
|
||||||
|
responses.entrySet()
|
||||||
|
.forEach(responseEntry -> {
|
||||||
|
System.out.println(responseEntry.getKey());
|
||||||
|
|
||||||
|
ApiResponse response = responseEntry.getValue();
|
||||||
|
System.out.println(response.getDescription());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
55
libraries-data-2/src/main/resources/sample.json
Normal file
55
libraries-data-2/src/main/resources/sample.json
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
{
|
||||||
|
"openapi": "3.0.0",
|
||||||
|
"info": {
|
||||||
|
"title": "User APIs",
|
||||||
|
"version": "1.0"
|
||||||
|
},
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"url": "https://jsonplaceholder.typicode.com",
|
||||||
|
"description": "Json Place Holder Service"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"/users/{id}": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"get": {
|
||||||
|
"summary": "Fetch user by ID",
|
||||||
|
"tags": [
|
||||||
|
"User"
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "integer"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"operationId": "get-users-user_id",
|
||||||
|
"description": "Retrieve a specific user by ID"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
libraries-data-2/src/main/resources/sample.yml
Normal file
33
libraries-data-2/src/main/resources/sample.yml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
openapi: 3.0.0
|
||||||
|
info:
|
||||||
|
title: User APIs
|
||||||
|
version: '1.0'
|
||||||
|
servers:
|
||||||
|
- url: https://jsonplaceholder.typicode.com
|
||||||
|
description: Json Place Holder Service
|
||||||
|
paths:
|
||||||
|
/users/{id}:
|
||||||
|
parameters:
|
||||||
|
- schema:
|
||||||
|
type: integer
|
||||||
|
name: id
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
get:
|
||||||
|
summary: Fetch user by ID
|
||||||
|
tags:
|
||||||
|
- User
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: integer
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
operationId: get-users-user_id
|
||||||
|
description: Retrieve a specific user by ID
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.baeldung.swaggerparser;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import io.swagger.parser.OpenAPIParser;
|
||||||
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
|
import io.swagger.v3.parser.core.models.SwaggerParseResult;
|
||||||
|
|
||||||
|
public class SwaggerParserUnitTest {
|
||||||
|
|
||||||
|
private static String OPENAPI_SPECIFICATION_STRING = "{\"openapi\":\"3.0.0\",\"info\":{\"title\":\"User APIs\",\"version\":\"1.0\"},\"servers\":[{\"url\":\"https://jsonplaceholder.typicode.com\",\"description\":\"Json Place Holder Service\"}],\"paths\":{\"/users/{id}\":{\"parameters\":[{\"schema\":{\"type\":\"integer\"},\"name\":\"id\",\"in\":\"path\",\"required\":true}],\"get\":{\"summary\":\"Fetch user by ID\",\"tags\":[\"User\"],\"responses\":{\"200\":{\"description\":\"OK\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}}}}},\"operationId\":\"get-users-user_id\",\"description\":\"Retrieve a specific user by ID\"}}}}";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAValidOpenAPIYAMLFile_whenParsing_thenCheckIfParsed() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readLocation("valid-openapi-sample.yml", null, null);
|
||||||
|
|
||||||
|
OpenAPI openAPI = result.getOpenAPI();
|
||||||
|
|
||||||
|
assertNotNull(openAPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAValidOpenAPIYAMLFile_whenParsing_thenCheckIfNotErrors() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readLocation("valid-openapi-sample.yml", null, null);
|
||||||
|
|
||||||
|
List<String> messages = result.getMessages();
|
||||||
|
|
||||||
|
assertTrue(messages.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnInvalidOpenAPIYAMLFile_whenParsing_thenCheckIfErrors() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readLocation("invalid-openapi-sample.yml", null, null);
|
||||||
|
|
||||||
|
List<String> messages = result.getMessages();
|
||||||
|
|
||||||
|
assertFalse(messages.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAValidOpenAPISpecificationString_whenParsing_thenCheckIfParsed() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readContents(OPENAPI_SPECIFICATION_STRING, null, null);
|
||||||
|
|
||||||
|
OpenAPI openAPI = result.getOpenAPI();
|
||||||
|
|
||||||
|
assertNotNull(openAPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAValidOpenAPISpecificationString_whenParsing_thenCheckIfNotErrors() {
|
||||||
|
SwaggerParseResult result = new OpenAPIParser().readLocation(OPENAPI_SPECIFICATION_STRING, null, null);
|
||||||
|
|
||||||
|
List<String> messages = result.getMessages();
|
||||||
|
|
||||||
|
assertNull(messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
openapi: 3.0.0
|
||||||
|
info:
|
||||||
|
title: User APIs
|
||||||
|
version: '1.0'
|
||||||
|
servers:
|
||||||
|
- description: Json Place Holder Service
|
||||||
|
paths:
|
||||||
|
/users/{id}:
|
||||||
|
parameters:
|
||||||
|
- schema:
|
||||||
|
type: integer
|
||||||
|
name: id
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
get:
|
||||||
|
summary: Fetch user by ID
|
||||||
|
tags:
|
||||||
|
- User
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: integer
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
operationId: get-users-user_id
|
||||||
|
description: Retrieve a specific user by ID
|
33
libraries-data-2/src/test/resources/valid-openapi-sample.yml
Normal file
33
libraries-data-2/src/test/resources/valid-openapi-sample.yml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
openapi: 3.0.0
|
||||||
|
info:
|
||||||
|
title: User APIs
|
||||||
|
version: '1.0'
|
||||||
|
servers:
|
||||||
|
- url: https://jsonplaceholder.typicode.com
|
||||||
|
description: Json Place Holder Service
|
||||||
|
paths:
|
||||||
|
/users/{id}:
|
||||||
|
parameters:
|
||||||
|
- schema:
|
||||||
|
type: integer
|
||||||
|
name: id
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
get:
|
||||||
|
summary: Fetch user by ID
|
||||||
|
tags:
|
||||||
|
- User
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: integer
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
operationId: get-users-user_id
|
||||||
|
description: Retrieve a specific user by ID
|
Loading…
x
Reference in New Issue
Block a user