From 7917462876ded93303fd89447940acf619d383d2 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Mon, 20 Jun 2022 02:15:23 +0530 Subject: [PATCH 01/11] feat(java): :sparkles: add feature for api response json schema validation --- core-java/pom.xml | 6 ++ .../wasiqb/boyka/builders/ApiResponse.java | 35 ++++++++ .../wasiqb/boyka/config/api/ApiSetting.java | 3 +- .../github/wasiqb/boyka/enums/Messages.java | 6 +- .../wasiqb/boyka/testng/api/TestApi.java | 6 +- .../src/test/resources/boyka-config.json | 3 +- .../resources/schema/CreateUserSchema.json | 33 ++++++++ .../test/resources/schema/GetUserSchema.json | 80 +++++++++++++++++++ .../resources/schema/PatchUserSchema.json | 28 +++++++ .../test/resources/schema/PutUserSchema.json | 28 +++++++ 10 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 core-java/src/test/resources/schema/CreateUserSchema.json create mode 100644 core-java/src/test/resources/schema/GetUserSchema.json create mode 100644 core-java/src/test/resources/schema/PatchUserSchema.json create mode 100644 core-java/src/test/resources/schema/PutUserSchema.json diff --git a/core-java/pom.xml b/core-java/pom.xml index eced07e25..59d805a6f 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -157,6 +157,12 @@ ok2curl 0.8.0 + + + org.everit.json + org.everit.json.schema + 1.5.1 + diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java index 55d1f08ca..b05144bc2 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java @@ -17,6 +17,8 @@ package com.github.wasiqb.boyka.builders; import static com.github.wasiqb.boyka.enums.Messages.NO_BODY_TO_PARSE; +import static com.github.wasiqb.boyka.enums.Messages.RESPONSE_SCHEMA_NOT_MATCHING; +import static com.github.wasiqb.boyka.utils.SettingUtils.loadSetting; import static com.google.common.truth.Truth.assertThat; import static com.jayway.jsonpath.JsonPath.compile; import static com.jayway.jsonpath.JsonPath.parse; @@ -25,6 +27,7 @@ import java.util.Map; +import com.github.wasiqb.boyka.exception.FrameworkError; import com.google.common.truth.BooleanSubject; import com.google.common.truth.IntegerSubject; import com.google.common.truth.StringSubject; @@ -33,6 +36,11 @@ import lombok.Getter; import lombok.ToString; import org.apache.logging.log4j.Logger; +import org.everit.json.schema.Schema; +import org.everit.json.schema.ValidationException; +import org.everit.json.schema.loader.SchemaLoader; +import org.json.JSONObject; +import org.json.JSONTokener; /** * Response container class. @@ -150,6 +158,33 @@ public StringSubject verifyTextField (final String expression) { return assertThat (getResponseData (expression)); } + /** + * Verify schema of response. + * + *@param config String expression + * + *@param schemaName String expression + * + */ + public void verifySchema(final String config , final String schemaName) { + LOGGER.traceEntry (); + LOGGER.info ("Verifying Response Schema"); + try { + final Schema schema = SchemaLoader.load(new JSONObject( + new JSONTokener(requireNonNull(getClass().getClassLoader().getResourceAsStream( + loadSetting().getApiSetting(config).getSchemaPath() + schemaName))))); + schema.validate(new JSONObject(getBody())); + } catch (ValidationException e) { + LOGGER.info(e.getMessage()); + e.getCausingExceptions().stream() + .map(ValidationException::getMessage) + .forEach(LOGGER::info); + throw new FrameworkError(RESPONSE_SCHEMA_NOT_MATCHING.getMessage()); + } + LOGGER.info("API response schema validation successfully verified"); + LOGGER.traceExit (); + } + private DocumentContext jsonPath () { LOGGER.traceEntry (); return LOGGER.traceExit (parse (requireNonNull (this.body, NO_BODY_TO_PARSE.getMessage ()))); diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/config/api/ApiSetting.java b/core-java/src/main/java/com/github/wasiqb/boyka/config/api/ApiSetting.java index 388296458..885c80b19 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/config/api/ApiSetting.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/config/api/ApiSetting.java @@ -33,4 +33,5 @@ public class ApiSetting { private int port; private int readTimeout = 5; private int writeTimeout = 5; -} \ No newline at end of file + private String schemaPath; +} diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java b/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java index e09746b7e..a62fb4710 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java @@ -101,7 +101,11 @@ public enum Messages { /** * User name required for cloud execution */ - USER_NAME_REQUIRED_FOR_CLOUD ("User name is required for cloud execution..."); + USER_NAME_REQUIRED_FOR_CLOUD ("User name is required for cloud execution..."), + /** + * Schema validation assert failure + */ + RESPONSE_SCHEMA_NOT_MATCHING("Schema validation assert failure..."); private final String message; diff --git a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java index cffbf7713..e11ac4a39 100644 --- a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java +++ b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java @@ -71,6 +71,7 @@ public void testGetUser () { .isEqualTo ("Janet"); response.verifyTextField ("data.last_name") .isEqualTo ("Weaver"); + response.verifySchema(API_CONFIG_KEY,"GetUserSchema.json"); } /** @@ -99,6 +100,7 @@ public void testPatchUser () { .isEqualTo (user.getJob ()); response.verifyTextField ("updatedAt") .isNotNull (); + response.verifySchema(API_CONFIG_KEY,"PatchUserSchema.json"); } /** @@ -127,6 +129,7 @@ public void testPutUser () { .isEqualTo (user.getJob ()); response.verifyTextField ("updatedAt") .isNotNull (); + response.verifySchema(API_CONFIG_KEY,"PutUserSchema.json"); } /** @@ -156,5 +159,6 @@ public void testUserCreation () { .isEqualTo (user.getJob ()); response.verifyTextField ("createdAt") .isNotNull (); + response.verifySchema(API_CONFIG_KEY,"CreateUserSchema.json"); } -} \ No newline at end of file +} diff --git a/core-java/src/test/resources/boyka-config.json b/core-java/src/test/resources/boyka-config.json index 8282a4a6c..5f3e2ba67 100644 --- a/core-java/src/test/resources/boyka-config.json +++ b/core-java/src/test/resources/boyka-config.json @@ -116,7 +116,8 @@ "logging": { "request": true, "response": true - } + }, + "schema_path": "schema/" } } } diff --git a/core-java/src/test/resources/schema/CreateUserSchema.json b/core-java/src/test/resources/schema/CreateUserSchema.json new file mode 100644 index 000000000..def745b56 --- /dev/null +++ b/core-java/src/test/resources/schema/CreateUserSchema.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-06/schema#", + "$ref": "#/definitions/Welcome10", + "definitions": { + "Welcome10": { + "type": "object", + "additionalProperties": false, + "properties": { + "job": { + "type": "string" + }, + "name": { + "type": "string" + }, + "id": { + "type": "string", + "format": "integer" + }, + "createdAt": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "createdAt", + "id", + "job", + "name" + ], + "title": "Welcome10" + } + } +} diff --git a/core-java/src/test/resources/schema/GetUserSchema.json b/core-java/src/test/resources/schema/GetUserSchema.json new file mode 100644 index 000000000..43eac9a77 --- /dev/null +++ b/core-java/src/test/resources/schema/GetUserSchema.json @@ -0,0 +1,80 @@ +{ + "$schema": "http://json-schema.org/draft-06/schema#", + "$ref": "#/definitions/Welcome2", + "definitions": { + "Welcome2": { + "type": "object", + "additionalProperties": false, + "properties": { + "data": { + "$ref": "#/definitions/Data" + }, + "support": { + "$ref": "#/definitions/Support" + } + }, + "required": [ + "data", + "support" + ], + "title": "Welcome2" + }, + "Data": { + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "type": "integer" + }, + "email": { + "type": "string" + }, + "first_name": { + "type": "string" + }, + "last_name": { + "type": "string" + }, + "avatar": { + "type": "string", + "format": "uri", + "qt-uri-protocols": [ + "https" + ], + "qt-uri-extensions": [ + ".jpg" + ] + } + }, + "required": [ + "avatar", + "email", + "first_name", + "id", + "last_name" + ], + "title": "Data" + }, + "Support": { + "type": "object", + "additionalProperties": false, + "properties": { + "url": { + "type": "string", + "format": "uri", + "qt-uri-protocols": [ + "https" + ] + }, + "text": { + "type": "string" + } + }, + "required": [ + "text", + "url" + ], + "title": "Support" + } + } +} diff --git a/core-java/src/test/resources/schema/PatchUserSchema.json b/core-java/src/test/resources/schema/PatchUserSchema.json new file mode 100644 index 000000000..07b0fd9f8 --- /dev/null +++ b/core-java/src/test/resources/schema/PatchUserSchema.json @@ -0,0 +1,28 @@ +{ + "$schema": "http://json-schema.org/draft-06/schema#", + "$ref": "#/definitions/Welcome2", + "definitions": { + "Welcome2": { + "type": "object", + "additionalProperties": false, + "properties": { + "job": { + "type": "string" + }, + "name": { + "type": "string" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "job", + "name", + "updatedAt" + ], + "title": "Welcome2" + } + } +} diff --git a/core-java/src/test/resources/schema/PutUserSchema.json b/core-java/src/test/resources/schema/PutUserSchema.json new file mode 100644 index 000000000..11c4fc132 --- /dev/null +++ b/core-java/src/test/resources/schema/PutUserSchema.json @@ -0,0 +1,28 @@ +{ + "$schema": "http://json-schema.org/draft-06/schema#", + "$ref": "#/definitions/Welcome5", + "definitions": { + "Welcome5": { + "type": "object", + "additionalProperties": false, + "properties": { + "job": { + "type": "string" + }, + "name": { + "type": "string" + }, + "updatedAt": { + "type": "string", + "format": "date-time" + } + }, + "required": [ + "job", + "name", + "updatedAt" + ], + "title": "Welcome5" + } + } +} From 654e05f99fffca28a743aa3b12a7a297daeb907a Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Mon, 20 Jun 2022 11:06:05 +0530 Subject: [PATCH 02/11] docs(java): :memo: add documentation for issue-86 (api schema validation) --- website/docs/framework-docs/guides/api/setup-config.md | 3 ++- website/docs/framework-docs/guides/api/verify-response.md | 1 + website/docs/framework-docs/guides/configuration.md | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/website/docs/framework-docs/guides/api/setup-config.md b/website/docs/framework-docs/guides/api/setup-config.md index db082ef59..64e88e20e 100644 --- a/website/docs/framework-docs/guides/api/setup-config.md +++ b/website/docs/framework-docs/guides/api/setup-config.md @@ -22,7 +22,8 @@ Let's see how to set configuration in the configuration file for API end-points. "logging": { "request": true, "response": true - } + }, + "schema_path":"schema/" } } } diff --git a/website/docs/framework-docs/guides/api/verify-response.md b/website/docs/framework-docs/guides/api/verify-response.md index bd0093ffa..86d46456e 100644 --- a/website/docs/framework-docs/guides/api/verify-response.md +++ b/website/docs/framework-docs/guides/api/verify-response.md @@ -18,6 +18,7 @@ response.verifyTextField ("job") .isEqualTo (user.getJob ()); response.verifyTextField ("createdAt") .isNotNull (); +response.verifySchema("CreateUserSchema.json"); ``` :::info diff --git a/website/docs/framework-docs/guides/configuration.md b/website/docs/framework-docs/guides/configuration.md index 560b78dde..2db649d77 100644 --- a/website/docs/framework-docs/guides/configuration.md +++ b/website/docs/framework-docs/guides/configuration.md @@ -183,7 +183,7 @@ COMING SOON, STAY TUNED! | `read_timeout` | Read timeout in seconds for the API. | `number` | 5 | | `write_timeout` | Write timeout in seconds for the API. | `number` | 5 | | `logging` | Logging configuration. See [Logging Config below](#logging-config). | `object` | | - +| `schema_path` | Path of schema file at location src/test/resources | `string` | | :::info API Configuration In `api` configuration block, you can provide different versions of API settings having different key names. From ecfb5dd84ff3763343baac446eb778eb783af581 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Mon, 20 Jun 2022 11:06:40 +0530 Subject: [PATCH 03/11] build(java): :bug: merge conflict resolution --- .../main/java/com/github/wasiqb/boyka/enums/Messages.java | 6 ++---- .../java/com/github/wasiqb/boyka/manager/ApiManager.java | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java b/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java index 4fd506a36..f97506049 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/enums/Messages.java @@ -103,15 +103,13 @@ public enum Messages { */ USER_NAME_REQUIRED_FOR_CLOUD ("User name is required for cloud execution..."), /** -<<<<<<< HEAD * Schema validation assert failure */ - RESPONSE_SCHEMA_NOT_MATCHING("Schema validation assert failure..."); -======= + RESPONSE_SCHEMA_NOT_MATCHING("Schema validation assert failure..."), + /** * No such key found */ INVALID_HEADER_KEY ("No such key {0} found..."); ->>>>>>> main private final String message; diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java b/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java index ad76a4111..e717f2306 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java @@ -270,4 +270,4 @@ private void pathParam (final String param, final String value) { this.pathParams.put (param, value); LOGGER.traceExit (); } -} +} \ No newline at end of file From f1c23f4f8740f1b12ea99eb8f5ff79d326372952 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Mon, 20 Jun 2022 20:13:10 +0530 Subject: [PATCH 04/11] build(java): :memo: documentation changes --- website/docs/api/builders/api-response.md | 11 +++++++++++ .../docs/framework-docs/guides/api/execute-request.md | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/website/docs/api/builders/api-response.md b/website/docs/api/builders/api-response.md index a71c600fc..77383b708 100644 --- a/website/docs/api/builders/api-response.md +++ b/website/docs/api/builders/api-response.md @@ -107,6 +107,17 @@ This parameter expects a valid `key` value. Returns the [`StringSubject`][string-subject] object. +## `verifyHeader` {#verify-response-header} + +This method will verify the header keys in the response body. + +### Parameters + +#### `key` + +This parameter expects a valid `key` value. + + [boolean-subject]: https://truth.dev/api/latest/com/google/common/truth/BooleanSubject.html#method.summary [string-subject]: https://truth.dev/api/latest/com/google/common/truth/StringSubject.html#method.summary [int-subject]: https://truth.dev/api/latest/com/google/common/truth/IntegerSubject.html#method.summary diff --git a/website/docs/framework-docs/guides/api/execute-request.md b/website/docs/framework-docs/guides/api/execute-request.md index dfca5da3e..98d0a0b6a 100644 --- a/website/docs/framework-docs/guides/api/execute-request.md +++ b/website/docs/framework-docs/guides/api/execute-request.md @@ -29,7 +29,7 @@ Following are the methods exposed in `ApiResponse` class to verify the response - `verifyStatusCode`: Verifies the status code of response. - `verifyStatusMessage`: Verifies the status message of response. - `verifyTextField`: Verifies the text field in response body. - +- `verifySchema`: Verifies the json schema of resonse body. ### Methods to get response data Following are the methods exposed in `ApiResponse` class to get response data: From 780c81ef18c10c7f2acf95930569f4b9593d7708 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Tue, 21 Jun 2022 00:35:04 +0530 Subject: [PATCH 05/11] fix(java): :bug: review comments changes --- .../java/com/github/wasiqb/boyka/testng/api/TestApi.java | 8 ++++---- .../{CreateUserSchema.json => create-user-schema.json} | 0 .../schema/{GetUserSchema.json => get-user-schema.json} | 0 .../{PatchUserSchema.json => patch-user-schema.json} | 0 .../schema/{PutUserSchema.json => put-user-schema.json} | 0 5 files changed, 4 insertions(+), 4 deletions(-) rename core-java/src/test/resources/schema/{CreateUserSchema.json => create-user-schema.json} (100%) rename core-java/src/test/resources/schema/{GetUserSchema.json => get-user-schema.json} (100%) rename core-java/src/test/resources/schema/{PatchUserSchema.json => patch-user-schema.json} (100%) rename core-java/src/test/resources/schema/{PutUserSchema.json => put-user-schema.json} (100%) diff --git a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java index 2fcc3f907..eb45e4b8c 100644 --- a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java +++ b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java @@ -73,7 +73,7 @@ public void testGetUser () { .isEqualTo ("Janet"); response.verifyTextField ("data.last_name") .isEqualTo ("Weaver"); - response.verifySchema(API_CONFIG_KEY,"GetUserSchema.json"); + response.verifySchema(request.getConfigKey(),"get-user-schema.json"); } /** @@ -126,7 +126,7 @@ public void testPatchUser () { .isEqualTo (user.getJob ()); response.verifyTextField ("updatedAt") .isNotNull (); - response.verifySchema(API_CONFIG_KEY,"PatchUserSchema.json"); + response.verifySchema(request.getConfigKey(),"patch-user-schema.json"); } /** @@ -155,7 +155,7 @@ public void testPutUser () { .isEqualTo (user.getJob ()); response.verifyTextField ("updatedAt") .isNotNull (); - response.verifySchema(API_CONFIG_KEY,"PutUserSchema.json"); + response.verifySchema(request.getConfigKey(),"put-user-schema.json"); } /** @@ -185,6 +185,6 @@ public void testUserCreation () { .isEqualTo (user.getJob ()); response.verifyTextField ("createdAt") .isNotNull (); - response.verifySchema(API_CONFIG_KEY,"CreateUserSchema.json"); + response.verifySchema(request.getConfigKey(),"create-user-schema.json"); } } diff --git a/core-java/src/test/resources/schema/CreateUserSchema.json b/core-java/src/test/resources/schema/create-user-schema.json similarity index 100% rename from core-java/src/test/resources/schema/CreateUserSchema.json rename to core-java/src/test/resources/schema/create-user-schema.json diff --git a/core-java/src/test/resources/schema/GetUserSchema.json b/core-java/src/test/resources/schema/get-user-schema.json similarity index 100% rename from core-java/src/test/resources/schema/GetUserSchema.json rename to core-java/src/test/resources/schema/get-user-schema.json diff --git a/core-java/src/test/resources/schema/PatchUserSchema.json b/core-java/src/test/resources/schema/patch-user-schema.json similarity index 100% rename from core-java/src/test/resources/schema/PatchUserSchema.json rename to core-java/src/test/resources/schema/patch-user-schema.json diff --git a/core-java/src/test/resources/schema/PutUserSchema.json b/core-java/src/test/resources/schema/put-user-schema.json similarity index 100% rename from core-java/src/test/resources/schema/PutUserSchema.json rename to core-java/src/test/resources/schema/put-user-schema.json From dd7f73b58a4a814bf0bee54ff01d61514cc4517a Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Tue, 21 Jun 2022 00:35:34 +0530 Subject: [PATCH 06/11] docs(java): :memo: review comment changes --- website/docs/api/builders/api-response.md | 11 +++++++---- .../docs/framework-docs/guides/api/execute-request.md | 1 + website/docs/framework-docs/guides/configuration.md | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/website/docs/api/builders/api-response.md b/website/docs/api/builders/api-response.md index 77383b708..3cfd1d4ee 100644 --- a/website/docs/api/builders/api-response.md +++ b/website/docs/api/builders/api-response.md @@ -107,16 +107,19 @@ This parameter expects a valid `key` value. Returns the [`StringSubject`][string-subject] object. -## `verifyHeader` {#verify-response-header} +## `verifySchema` {#verify-schema} -This method will verify the header keys in the response body. +This method will verify the api response body json schema. ### Parameters -#### `key` +#### `config` -This parameter expects a valid `key` value. +This parameter expects a valid `config` value. + +#### `schemaName` +This parameter expects a valid `schemaName` value. The schemaName is a json file stored at location src/tst/resources/ [boolean-subject]: https://truth.dev/api/latest/com/google/common/truth/BooleanSubject.html#method.summary [string-subject]: https://truth.dev/api/latest/com/google/common/truth/StringSubject.html#method.summary diff --git a/website/docs/framework-docs/guides/api/execute-request.md b/website/docs/framework-docs/guides/api/execute-request.md index 98d0a0b6a..7735dcd99 100644 --- a/website/docs/framework-docs/guides/api/execute-request.md +++ b/website/docs/framework-docs/guides/api/execute-request.md @@ -30,6 +30,7 @@ Following are the methods exposed in `ApiResponse` class to verify the response - `verifyStatusMessage`: Verifies the status message of response. - `verifyTextField`: Verifies the text field in response body. - `verifySchema`: Verifies the json schema of resonse body. + ### Methods to get response data Following are the methods exposed in `ApiResponse` class to get response data: diff --git a/website/docs/framework-docs/guides/configuration.md b/website/docs/framework-docs/guides/configuration.md index 2db649d77..4628aaf7d 100644 --- a/website/docs/framework-docs/guides/configuration.md +++ b/website/docs/framework-docs/guides/configuration.md @@ -183,7 +183,7 @@ COMING SOON, STAY TUNED! | `read_timeout` | Read timeout in seconds for the API. | `number` | 5 | | `write_timeout` | Write timeout in seconds for the API. | `number` | 5 | | `logging` | Logging configuration. See [Logging Config below](#logging-config). | `object` | | -| `schema_path` | Path of schema file at location src/test/resources | `string` | | +| `schema_path` | Path of schema file at location `src/test/resources` | `string` | | :::info API Configuration In `api` configuration block, you can provide different versions of API settings having different key names. From a8f5580b837c5f025b3fd858711ad1331587d7e3 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Tue, 21 Jun 2022 00:40:04 +0530 Subject: [PATCH 07/11] docs(java): :bug: bug fix for PR checks --- website/docs/api/builders/api-response.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/api/builders/api-response.md b/website/docs/api/builders/api-response.md index 3cfd1d4ee..247df10c1 100644 --- a/website/docs/api/builders/api-response.md +++ b/website/docs/api/builders/api-response.md @@ -119,7 +119,7 @@ This parameter expects a valid `config` value. #### `schemaName` -This parameter expects a valid `schemaName` value. The schemaName is a json file stored at location src/tst/resources/ +This parameter expects a valid `schemaName` value. The schemaName is a json file stored at location `src/tst/resources/.json` [boolean-subject]: https://truth.dev/api/latest/com/google/common/truth/BooleanSubject.html#method.summary [string-subject]: https://truth.dev/api/latest/com/google/common/truth/StringSubject.html#method.summary From 60a9f0269a6539e5c75277878340a8a243602565 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Wed, 22 Jun 2022 11:29:56 +0530 Subject: [PATCH 08/11] fix(java): :bug: review comments fix --- .../wasiqb/boyka/builders/ApiResponse.java | 41 +++++------ .../wasiqb/boyka/manager/ApiManager.java | 71 ++++++++----------- .../wasiqb/boyka/testng/api/TestApi.java | 36 +++++----- 3 files changed, 66 insertions(+), 82 deletions(-) diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java index 3051dc430..c257d090d 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java @@ -16,19 +16,7 @@ package com.github.wasiqb.boyka.builders; -import static com.github.wasiqb.boyka.enums.Messages.INVALID_HEADER_KEY; -import static com.github.wasiqb.boyka.enums.Messages.NO_BODY_TO_PARSE; -import static com.github.wasiqb.boyka.enums.Messages.RESPONSE_SCHEMA_NOT_MATCHING; -import static com.github.wasiqb.boyka.utils.SettingUtils.loadSetting; -import static com.google.common.truth.Truth.assertThat; -import static com.jayway.jsonpath.JsonPath.compile; -import static com.jayway.jsonpath.JsonPath.parse; -import static java.text.MessageFormat.format; -import static java.util.Objects.requireNonNull; -import static org.apache.logging.log4j.LogManager.getLogger; - -import java.util.Map; - +import com.github.wasiqb.boyka.config.api.ApiSetting; import com.github.wasiqb.boyka.exception.FrameworkError; import com.google.common.truth.BooleanSubject; import com.google.common.truth.IntegerSubject; @@ -44,6 +32,16 @@ import org.json.JSONObject; import org.json.JSONTokener; +import java.util.Map; + +import static com.github.wasiqb.boyka.enums.Messages.*; +import static com.google.common.truth.Truth.assertThat; +import static com.jayway.jsonpath.JsonPath.compile; +import static com.jayway.jsonpath.JsonPath.parse; +import static java.text.MessageFormat.format; +import static java.util.Objects.requireNonNull; +import static org.apache.logging.log4j.LogManager.getLogger; + /** * Response container class. * @@ -64,7 +62,8 @@ public class ApiResponse { private ApiRequest request; private long sentRequestAt; private int statusCode; - private String statusMessage; + private String statusMessage; + private ApiSetting apiSetting; /** * Get response body field data. @@ -180,18 +179,16 @@ public StringSubject verifyTextField (final String expression) { /** * Verify schema of response. * - *@param config String expression - * - *@param schemaName String expression - * + * @param schemaName String expression */ - public void verifySchema(final String config , final String schemaName) { - LOGGER.traceEntry (); - LOGGER.info ("Verifying Response Schema"); + public void verifySchema(final String schemaName) { + LOGGER.traceEntry(); + LOGGER.info("Verifying Response Schema"); try { + final Schema schema = SchemaLoader.load(new JSONObject( new JSONTokener(requireNonNull(getClass().getClassLoader().getResourceAsStream( - loadSetting().getApiSetting(config).getSchemaPath() + schemaName))))); + this.apiSetting.getSchemaPath() + schemaName))))); schema.validate(new JSONObject(getBody())); } catch (ValidationException e) { LOGGER.info(e.getMessage()); diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java b/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java index e61bc512b..925c1317d 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java @@ -16,12 +16,25 @@ package com.github.wasiqb.boyka.manager; +import com.github.wasiqb.boyka.builders.ApiRequest; +import com.github.wasiqb.boyka.builders.ApiResponse; +import com.github.wasiqb.boyka.config.api.ApiSetting; +import com.github.wasiqb.boyka.enums.ContentType; +import com.github.wasiqb.boyka.enums.RequestMethod; +import com.github.wasiqb.boyka.exception.FrameworkError; +import com.github.wasiqb.boyka.utils.JsonParser; +import com.moczul.ok2curl.Configuration; +import com.moczul.ok2curl.CurlCommandGenerator; +import okhttp3.*; +import okio.Buffer; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import static com.github.wasiqb.boyka.enums.ContentType.JSON; -import static com.github.wasiqb.boyka.enums.Messages.AUTH_PASSWORD_REQUIRED; -import static com.github.wasiqb.boyka.enums.Messages.CONTENT_TYPE_NOT_SET; -import static com.github.wasiqb.boyka.enums.Messages.ERROR_EXECUTING_REQUEST; -import static com.github.wasiqb.boyka.enums.Messages.ERROR_PARSING_REQUEST_BODY; -import static com.github.wasiqb.boyka.enums.Messages.ERROR_PARSING_RESPONSE_BODY; +import static com.github.wasiqb.boyka.enums.Messages.*; import static com.github.wasiqb.boyka.utils.SettingUtils.loadSetting; import static com.github.wasiqb.boyka.utils.StringUtils.interpolate; import static java.text.MessageFormat.format; @@ -35,29 +48,6 @@ import static org.apache.commons.lang3.StringUtils.isNotEmpty; import static org.apache.logging.log4j.LogManager.getLogger; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import com.github.wasiqb.boyka.builders.ApiRequest; -import com.github.wasiqb.boyka.builders.ApiResponse; -import com.github.wasiqb.boyka.config.api.ApiSetting; -import com.github.wasiqb.boyka.enums.ContentType; -import com.github.wasiqb.boyka.enums.RequestMethod; -import com.github.wasiqb.boyka.exception.FrameworkError; -import com.github.wasiqb.boyka.utils.JsonParser; -import com.moczul.ok2curl.Configuration; -import com.moczul.ok2curl.CurlCommandGenerator; -import okhttp3.HttpUrl; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; -import okhttp3.ResponseBody; -import okio.Buffer; -import org.apache.logging.log4j.Logger; - /** * API manager to handle all API request executions. * @@ -254,17 +244,18 @@ private ApiResponse parseResponse (final Response res) { res.headers () .forEach (entry -> headers.put (entry.getFirst (), entry.getSecond ())); try { - return LOGGER.traceExit (ApiResponse.createResponse () - .request (parseRequest (res.request ())) - .statusCode (res.code ()) - .statusMessage (res.message ()) - .sentRequestAt (res.sentRequestAtMillis ()) - .headers (headers) - .networkResponse (parseResponse (res.networkResponse ())) - .previousResponse (parseResponse (res.priorResponse ())) - .receivedResponseAt (res.receivedResponseAtMillis ()) - .body (requireNonNullElse (res.body (), ResponseBody.create (EMPTY, parse (JSON.getType ()))).string ()) - .create ()); + return LOGGER.traceExit (ApiResponse.createResponse() + .request(parseRequest(res.request())) + .statusCode(res.code()) + .statusMessage(res.message()) + .sentRequestAt(res.sentRequestAtMillis()) + .headers(headers).networkResponse(parseResponse(res.networkResponse())) + .apiSetting(this.apiSetting) + .networkResponse (parseResponse (res.networkResponse ())) + .previousResponse (parseResponse (res.priorResponse ())) + .receivedResponseAt (res.receivedResponseAtMillis ()) + .body (requireNonNullElse (res.body (), ResponseBody.create (EMPTY, parse (JSON.getType ()))).string ()) + .create ()); } catch (final IOException e) { LOGGER.catching (e); throw new FrameworkError (ERROR_PARSING_RESPONSE_BODY.getMessage (), e); @@ -276,4 +267,4 @@ private void pathParam (final String param, final String value) { this.pathParams.put (param, value); LOGGER.traceExit (); } -} \ No newline at end of file +} diff --git a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java index eb45e4b8c..7c1798f3c 100644 --- a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java +++ b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java @@ -16,19 +16,15 @@ package com.github.wasiqb.boyka.testng.api; -import static com.github.wasiqb.boyka.builders.ApiRequest.createRequest; -import static com.github.wasiqb.boyka.enums.RequestMethod.DELETE; -import static com.github.wasiqb.boyka.enums.RequestMethod.GET; -import static com.github.wasiqb.boyka.enums.RequestMethod.PATCH; -import static com.github.wasiqb.boyka.enums.RequestMethod.POST; -import static com.github.wasiqb.boyka.enums.RequestMethod.PUT; -import static com.github.wasiqb.boyka.manager.ApiManager.execute; - import com.github.wasiqb.boyka.builders.ApiRequest; import com.github.wasiqb.boyka.builders.ApiResponse; import com.github.wasiqb.boyka.testng.api.requests.User; import org.testng.annotations.Test; +import static com.github.wasiqb.boyka.builders.ApiRequest.createRequest; +import static com.github.wasiqb.boyka.enums.RequestMethod.*; +import static com.github.wasiqb.boyka.manager.ApiManager.execute; + /** * Test class for testing Api manager class. * @@ -71,9 +67,9 @@ public void testGetUser () { .isEqualTo ("application/json; charset=utf-8"); response.verifyTextField ("data.first_name") .isEqualTo ("Janet"); - response.verifyTextField ("data.last_name") - .isEqualTo ("Weaver"); - response.verifySchema(request.getConfigKey(),"get-user-schema.json"); + response.verifyTextField("data.last_name") + .isEqualTo("Weaver"); + response.verifySchema("get-user-schema.json"); } /** @@ -124,9 +120,9 @@ public void testPatchUser () { .isEqualTo (user.getName ()); response.verifyTextField ("job") .isEqualTo (user.getJob ()); - response.verifyTextField ("updatedAt") - .isNotNull (); - response.verifySchema(request.getConfigKey(),"patch-user-schema.json"); + response.verifyTextField("updatedAt") + .isNotNull(); + response.verifySchema("patch-user-schema.json"); } /** @@ -153,9 +149,9 @@ public void testPutUser () { .isEqualTo (user.getName ()); response.verifyTextField ("job") .isEqualTo (user.getJob ()); - response.verifyTextField ("updatedAt") - .isNotNull (); - response.verifySchema(request.getConfigKey(),"put-user-schema.json"); + response.verifyTextField("updatedAt") + .isNotNull(); + response.verifySchema("put-user-schema.json"); } /** @@ -183,8 +179,8 @@ public void testUserCreation () { .isEqualTo (user.getName ()); response.verifyTextField ("job") .isEqualTo (user.getJob ()); - response.verifyTextField ("createdAt") - .isNotNull (); - response.verifySchema(request.getConfigKey(),"create-user-schema.json"); + response.verifyTextField("createdAt") + .isNotNull(); + response.verifySchema("create-user-schema.json"); } } From 69f733d24af73fd1e86b3f4ec8b472af0c563345 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Wed, 22 Jun 2022 14:56:24 +0530 Subject: [PATCH 09/11] fix(java): :bug: imports restructuring fix --- .../wasiqb/boyka/builders/ApiResponse.java | 26 ++++++----- .../wasiqb/boyka/manager/ApiManager.java | 46 +++++++++++-------- .../wasiqb/boyka/testng/api/TestApi.java | 12 +++-- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java index c257d090d..4dc3396a5 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java @@ -16,6 +16,18 @@ package com.github.wasiqb.boyka.builders; +import static com.github.wasiqb.boyka.enums.Messages.INVALID_HEADER_KEY; +import static com.github.wasiqb.boyka.enums.Messages.NO_BODY_TO_PARSE; +import static com.github.wasiqb.boyka.enums.Messages.RESPONSE_SCHEMA_NOT_MATCHING; +import static com.google.common.truth.Truth.assertThat; +import static com.jayway.jsonpath.JsonPath.compile; +import static com.jayway.jsonpath.JsonPath.parse; +import static java.text.MessageFormat.format; +import static java.util.Objects.requireNonNull; +import static org.apache.logging.log4j.LogManager.getLogger; + +import java.util.Map; + import com.github.wasiqb.boyka.config.api.ApiSetting; import com.github.wasiqb.boyka.exception.FrameworkError; import com.google.common.truth.BooleanSubject; @@ -32,16 +44,6 @@ import org.json.JSONObject; import org.json.JSONTokener; -import java.util.Map; - -import static com.github.wasiqb.boyka.enums.Messages.*; -import static com.google.common.truth.Truth.assertThat; -import static com.jayway.jsonpath.JsonPath.compile; -import static com.jayway.jsonpath.JsonPath.parse; -import static java.text.MessageFormat.format; -import static java.util.Objects.requireNonNull; -import static org.apache.logging.log4j.LogManager.getLogger; - /** * Response container class. * @@ -62,8 +64,8 @@ public class ApiResponse { private ApiRequest request; private long sentRequestAt; private int statusCode; - private String statusMessage; - private ApiSetting apiSetting; + private String statusMessage; + private ApiSetting apiSetting; /** * Get response body field data. diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java b/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java index 925c1317d..c03927434 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/manager/ApiManager.java @@ -16,25 +16,12 @@ package com.github.wasiqb.boyka.manager; -import com.github.wasiqb.boyka.builders.ApiRequest; -import com.github.wasiqb.boyka.builders.ApiResponse; -import com.github.wasiqb.boyka.config.api.ApiSetting; -import com.github.wasiqb.boyka.enums.ContentType; -import com.github.wasiqb.boyka.enums.RequestMethod; -import com.github.wasiqb.boyka.exception.FrameworkError; -import com.github.wasiqb.boyka.utils.JsonParser; -import com.moczul.ok2curl.Configuration; -import com.moczul.ok2curl.CurlCommandGenerator; -import okhttp3.*; -import okio.Buffer; -import org.apache.logging.log4j.Logger; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - import static com.github.wasiqb.boyka.enums.ContentType.JSON; -import static com.github.wasiqb.boyka.enums.Messages.*; +import static com.github.wasiqb.boyka.enums.Messages.AUTH_PASSWORD_REQUIRED; +import static com.github.wasiqb.boyka.enums.Messages.CONTENT_TYPE_NOT_SET; +import static com.github.wasiqb.boyka.enums.Messages.ERROR_EXECUTING_REQUEST; +import static com.github.wasiqb.boyka.enums.Messages.ERROR_PARSING_REQUEST_BODY; +import static com.github.wasiqb.boyka.enums.Messages.ERROR_PARSING_RESPONSE_BODY; import static com.github.wasiqb.boyka.utils.SettingUtils.loadSetting; import static com.github.wasiqb.boyka.utils.StringUtils.interpolate; import static java.text.MessageFormat.format; @@ -48,6 +35,29 @@ import static org.apache.commons.lang3.StringUtils.isNotEmpty; import static org.apache.logging.log4j.LogManager.getLogger; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import com.github.wasiqb.boyka.builders.ApiRequest; +import com.github.wasiqb.boyka.builders.ApiResponse; +import com.github.wasiqb.boyka.config.api.ApiSetting; +import com.github.wasiqb.boyka.enums.ContentType; +import com.github.wasiqb.boyka.enums.RequestMethod; +import com.github.wasiqb.boyka.exception.FrameworkError; +import com.github.wasiqb.boyka.utils.JsonParser; +import com.moczul.ok2curl.Configuration; +import com.moczul.ok2curl.CurlCommandGenerator; +import okhttp3.HttpUrl; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; +import okio.Buffer; +import org.apache.logging.log4j.Logger; + /** * API manager to handle all API request executions. * diff --git a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java index 7c1798f3c..6c4f81386 100644 --- a/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java +++ b/core-java/src/test/java/com/github/wasiqb/boyka/testng/api/TestApi.java @@ -16,15 +16,19 @@ package com.github.wasiqb.boyka.testng.api; +import static com.github.wasiqb.boyka.builders.ApiRequest.createRequest; +import static com.github.wasiqb.boyka.enums.RequestMethod.DELETE; +import static com.github.wasiqb.boyka.enums.RequestMethod.GET; +import static com.github.wasiqb.boyka.enums.RequestMethod.PATCH; +import static com.github.wasiqb.boyka.enums.RequestMethod.POST; +import static com.github.wasiqb.boyka.enums.RequestMethod.PUT; +import static com.github.wasiqb.boyka.manager.ApiManager.execute; + import com.github.wasiqb.boyka.builders.ApiRequest; import com.github.wasiqb.boyka.builders.ApiResponse; import com.github.wasiqb.boyka.testng.api.requests.User; import org.testng.annotations.Test; -import static com.github.wasiqb.boyka.builders.ApiRequest.createRequest; -import static com.github.wasiqb.boyka.enums.RequestMethod.*; -import static com.github.wasiqb.boyka.manager.ApiManager.execute; - /** * Test class for testing Api manager class. * From fd3d400458e2493f425497c9ea28a4ff43e29989 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Wed, 22 Jun 2022 15:04:00 +0530 Subject: [PATCH 10/11] docs(java): :memo: updating documentation --- website/docs/api/builders/api-response.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/website/docs/api/builders/api-response.md b/website/docs/api/builders/api-response.md index 247df10c1..fd8045083 100644 --- a/website/docs/api/builders/api-response.md +++ b/website/docs/api/builders/api-response.md @@ -113,13 +113,9 @@ This method will verify the api response body json schema. ### Parameters -#### `config` - -This parameter expects a valid `config` value. - #### `schemaName` -This parameter expects a valid `schemaName` value. The schemaName is a json file stored at location `src/tst/resources/.json` +This parameter expects a valid `schemaName` value. The schemaName is a json file containing the expected json schema version stored at location `src/test/resources/schema/.json`. [boolean-subject]: https://truth.dev/api/latest/com/google/common/truth/BooleanSubject.html#method.summary [string-subject]: https://truth.dev/api/latest/com/google/common/truth/StringSubject.html#method.summary From 222b8c2bfc3746ebd6171425e0be9b207deba8a2 Mon Sep 17 00:00:00 2001 From: SahilPuri2020 Date: Wed, 22 Jun 2022 15:37:02 +0530 Subject: [PATCH 11/11] fix(java): :bug: review comment fix --- .../main/java/com/github/wasiqb/boyka/builders/ApiResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java index 4dc3396a5..184bafb31 100644 --- a/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java +++ b/core-java/src/main/java/com/github/wasiqb/boyka/builders/ApiResponse.java @@ -197,7 +197,7 @@ public void verifySchema(final String schemaName) { e.getCausingExceptions().stream() .map(ValidationException::getMessage) .forEach(LOGGER::info); - throw new FrameworkError(RESPONSE_SCHEMA_NOT_MATCHING.getMessage()); + throw new FrameworkError(RESPONSE_SCHEMA_NOT_MATCHING.getMessage(),e); } LOGGER.info("API response schema validation successfully verified"); LOGGER.traceExit ();