How to use JsonValidator class of com.qaprosoft.apitools.validation package

Best Carina code snippet using com.qaprosoft.apitools.validation.JsonValidator

Source:AbstractApiMethodV2.java Github

copy

Full Screen

...23import com.jayway.restassured.response.Response;24import com.qaprosoft.apitools.builder.PropertiesProcessorMain;25import com.qaprosoft.apitools.message.TemplateMessage;26import com.qaprosoft.apitools.validation.JsonKeywordsComparator;27import com.qaprosoft.apitools.validation.JsonValidator;28public abstract class AbstractApiMethodV2 extends AbstractApiMethod {29 private Properties properties;30 private String rqPath;31 private String rsPath;32 private String actualRsBody;33 public AbstractApiMethodV2(String rqPath, String rsPath, String propertiesPath) {34 super("application/json");35 setHeaders("Accept=*/*");36 URL baseResource = ClassLoader.getSystemResource(propertiesPath);37 if (baseResource != null) {38 properties = new Properties();39 try {40 properties.load(baseResource.openStream());41 } catch (IOException e) {42 throw new RuntimeException("Properties can't be loaded by path: " + propertiesPath, e);43 }44 LOGGER.info("Base properties loaded: " + propertiesPath);45 } else {46 throw new RuntimeException("Properties can't be found by path: " + propertiesPath);47 }48 properties = PropertiesProcessorMain.processProperties(properties);49 this.rqPath = rqPath;50 this.rsPath = rsPath;51 }52 public AbstractApiMethodV2(String rqPath, String rsPath, Properties properties) {53 super("application/json");54 setHeaders("Accept=*/*");55 if (properties != null) {56 this.properties = PropertiesProcessorMain.processProperties(properties);57 }58 this.rqPath = rqPath;59 this.rsPath = rsPath;60 }61 public AbstractApiMethodV2(String rqPath, String rsPath) {62 this(rqPath, rsPath, (Properties) null);63 }64 @Override65 @Deprecated66 public String call() {67 if (rqPath != null) {68 TemplateMessage tm = new TemplateMessage();69 tm.setTemplatePath(rqPath);70 tm.setPropertiesStorage(properties);71 setBodyContent(tm.getMessageText());72 }73 String rs = super.call();74 actualRsBody = rs;75 return rs;76 }77 @Override78 public Response callAPI() {79 if (rqPath != null) {80 TemplateMessage tm = new TemplateMessage();81 tm.setTemplatePath(rqPath);82 tm.setPropertiesStorage(properties);83 setBodyContent(tm.getMessageText());84 }85 Response rs = super.callAPI();86 actualRsBody = rs.asString();87 return rs;88 }89 public void addProperty(String key, Object value) {90 if (properties == null) {91 throw new RuntimeException("API method properties are not initialized!");92 }93 properties.put(key, value);94 }95 public void removeProperty(String key) {96 if (properties == null) {97 throw new RuntimeException("API method properties are not initialized!");98 }99 properties.remove(key);100 }101 public Properties getProperties() {102 return properties;103 }104 /**105 * Validates JSON response using custom options106 * 107 * @param mode108 * - determines how to compare 2 JSONs. See type description for more details. Mode is not applied for109 * arrays comparison110 * @param validationFlags111 * - used for JSON arrays validation when we need to check presence of some array items in result array.112 * Use JsonCompareKeywords.ARRAY_CONTAINS.getKey() construction for that113 */114 public void validateResponse(JSONCompareMode mode, String... validationFlags) {115 if (rsPath == null) {116 throw new RuntimeException("Please specify rsPath to make Response body validation");117 }118 if (properties == null) {119 properties = new Properties();120 }121 if (actualRsBody == null) {122 throw new RuntimeException("Actual response body is null. Please make API call before validation response");123 }124 TemplateMessage tm = new TemplateMessage();125 tm.setTemplatePath(rsPath);126 tm.setPropertiesStorage(properties);127 String expectedRs = tm.getMessageText();128 try {129 JSONAssert.assertEquals(expectedRs, actualRsBody, new JsonKeywordsComparator(mode, validationFlags));130 } catch (JSONException e) {131 throw new RuntimeException(e);132 }133 }134 /**135 * @param validationFlags136 * parameter that specifies how to validate JSON response. Currently only array validation flag is supported.137 * Use JsonCompareKeywords.ARRAY_CONTAINS enum value for that138 */139 public void validateResponse(String... validationFlags) {140 validateResponse(JSONCompareMode.NON_EXTENSIBLE, validationFlags);141 }142 public void validateResponseAgainstJSONSchema(String schemaPath) {143 if (actualRsBody == null) {144 throw new RuntimeException("Actual response body is null. Please make API call before validation response");145 }146 TemplateMessage tm = new TemplateMessage();147 tm.setTemplatePath(schemaPath);148 String schema = tm.getMessageText();149 JsonValidator.validateJsonAgainstSchema(schema, actualRsBody);150 }151 public void setAuth(String jSessionId) {152 addCookie("pfJSESSIONID", jSessionId);153 }154}...

Full Screen

Full Screen

Source:LoginTest.java Github

copy

Full Screen

1package com.longmao.api;2import com.qaprosoft.apitools.validation.JsonValidator;3import com.qaprosoft.carina.core.foundation.AbstractTest;4import com.qaprosoft.carina.core.foundation.api.http.HttpResponseStatusType;5import com.qaprosoft.carina.core.foundation.dataprovider.annotations.XlsDataSourceParameters;6import com.qaprosoft.carina.core.foundation.utils.ownership.MethodOwner;7import org.skyscreamer.jsonassert.JSONCompareMode;8import org.testng.annotations.Test;9public class LoginTest extends AbstractTest {10 @Test(description = "JIRA#API-1")11 @MethodOwner(owner = "longmao")12 public void testLoginUsingFile(){13 String path = "api/login/";14 String rqFiles[] = {"login_success_rq.json", "login_pass_wrong_rq.json", "login_user_wrong_rq.json"};15 String rsFiles[] = {"login_success_rs.json", "login_pass_wrong_rs.json", "login_user_wrong_rs.json"};16 for(int i = 0; i < 3; i++) {17 LoginUsingFile api = new LoginUsingFile(path+rqFiles[i], path+rsFiles[i]);18 if(i == 0) {19 api.expectResponseStatus(HttpResponseStatusType.OK_200);20 }21 else{22 api.expectResponseStatus(HttpResponseStatusType.UNAUTHORIZED_401);23 }24 api.callAPI();25 api.validateResponse(JSONCompareMode.LENIENT);26 }27 }28 @Test(dataProvider = "DataProvider", description = "JIRA#API-1")29 @MethodOwner(owner = "longmao")30 @XlsDataSourceParameters(path = "xls/API.xlsx", sheet = "login", dsUid = "testcase", dsArgs = "body,expect,status")31 public void testLoginUsingExcel(String body, String expect, String status){32 LoginUsingExcel api = new LoginUsingExcel();33 api.setBodyContent(body);34 if(status.equals("success")){35 api.expectResponseStatus(HttpResponseStatusType.OK_200);36 }37 else{38 api.expectResponseStatus(HttpResponseStatusType.UNAUTHORIZED_401);39 }40 String rs = api.callAPI().asString();41 JsonValidator.validateJson(expect, rs, JSONCompareMode.LENIENT);42 }43}...

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.demo.api;2import java.lang.reflect.Method;3import org.testng.Assert;4import org.testng.annotations.Test;5import com.qaprosoft.apitools.validation.JsonCompareMode;6import com.qaprosoft.apitools.validation.JsonValidator;7import com.qaprosoft.carina.core.foundation.api.AbstractApiMethodV2;8import com.qaprosoft.carina.core.foundation.api.http.HttpResponseStatusType;9import com.qaprosoft.carina.core.foundation.utils.Configuration;10import com.qaprosoft.carina.core.foundation.utils.ownership.MethodOwner;11public class PostUserMethodTest extends AbstractApiMethodV2 {12 public PostUserMethodTest() {13 super("api/user/_post/rq.json", "api/user/_post/rs.json", "api/user/_post/user.properties");14 replaceUrlPlaceholder("base_url", Configuration.getEnvArg("api_url"));15 }16 public void testPostUserMethod() {17 PostUserMethodTest test = new PostUserMethodTest();18 test.expectResponseStatus(HttpResponseStatusType.CREATED_201);19 String rs = test.callAPI().asString();20 test.validateResponseAgainstJSONSchema("api/user/_post/user_schema.json");21 test.validateResponse(JSONCompareMode.NON_EXTENSIBLE, JsonCompareKeywords.ARRAY_CONTAINS.getKey());22 Assert.assertEquals(rs, "1", "Response doesn't match to expected result!");23 }24}25package com.qaprosoft.carina.demo.api;26import org.testng.Assert;27import org.testng.annotations.Test;28import com.qaprosoft.apitools.validation.JsonCompareMode;29import com.qaprosoft.carina.core.foundation.api.AbstractApiMethodV2;30import com.qaprosoft.carina.core.foundation.api.http.HttpResponseStatusType;31import com.qaprosoft.carina.core.foundation.utils.Configuration;32import com.qaprosoft.carina.core.foundation.utils.ownership.MethodOwner;33public class PostUserMethodTest extends AbstractApiMethodV2 {34 public PostUserMethodTest() {35 super("api/user/_post/rq.json", "api/user/_post/rs.json", "api/user/_post/user.properties");36 replaceUrlPlaceholder("base_url", Configuration.getEnvArg("api_url"));37 }38 public void testPostUserMethod() {39 PostUserMethodTest test = new PostUserMethodTest();40 test.expectResponseStatus(HttpResponseStatusType.CREATED_201);41 String rs = test.callAPI().asString();

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.apitools.validation;2import java.io.File;3import java.io.IOException;4import org.apache.log4j.Logger;5import org.json.JSONException;6import org.json.JSONObject;7import org.json.JSONTokener;8import com.qaprosoft.apitools.validation.JsonValidator;9public class JsonValidatorTest {10 private static final Logger LOGGER = Logger.getLogger(JsonValidatorTest.class);11 public static void main(String[] args) throws IOException, JSONException {12 JsonValidator jsonValidator = new JsonValidator();13 File schema = new File("src/test/java/com/qaprosoft/apitools/validation/schema.json");14 File json = new File("src/test/java/com/qaprosoft/apitools/validation/json.json");15 JSONObject jsonSchema = new JSONObject(new JSONTokener(schema.toURI().toURL().openStream()));16 JSONObject jsonObject = new JSONObject(new JSONTokener(json.toURI().toURL().openStream()));17 boolean isValid = jsonValidator.validateJSON(jsonObject, jsonSchema);18 LOGGER.info("Is json valid? " + isValid);19 }20}21{ "name" : "John", "age" : 30, "address" : { "streetAddress" : "21 2nd Street", "city" : "New York", "state" : "NY", "postalCode" : 10021 } }

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.zafira.api;2import com.qaprosoft.apitools.validation.JsonCompareKeywords;3import com.qaprosoft.apitools.validation.JsonValidator;4import com.qaprosoft.zafira.api.testRunController.CreateTestRunV1Method;5import com.qaprosoft.zafira.api.testRunController.DeleteTestRunByIdV1Method;6import com.qaprosoft.zafira.api.testRunController.GetTestRunByIdV1Method;7import com.qaprosoft.zafira.api.testRunController.PostTestRunByIdV1Method;8import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method;9import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder;10import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder.PutTestRunByIdV1MethodBuilderImpl;11import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder.PutTestRunByIdV1MethodBuilderImpl.PutTestRunByIdV1MethodBuilderImplImpl;12import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder.PutTestRunByIdV1MethodBuilderImpl.PutTestRunByIdV1MethodBuilderImplImpl.PutTestRunByIdV1MethodBuilderImplImplImpl;13import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder.PutTestRunByIdV1MethodBuilderImpl.PutTestRunByIdV1MethodBuilderImplImpl.PutTestRunByIdV1MethodBuilderImplImplImpl.PutTestRunByIdV1MethodBuilderImplImplImplImpl;14import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder.PutTestRunByIdV1MethodBuilderImpl.PutTestRunByIdV1MethodBuilderImplImpl.PutTestRunByIdV1MethodBuilderImplImplImpl.PutTestRunByIdV1MethodBuilderImplImplImplImpl.PutTestRunByIdV1MethodBuilderImplImplImplImplImpl;15import com.qaprosoft.zafira.api.testRunController.PutTestRunByIdV1Method.PutTestRunByIdV1MethodBuilder.PutTestRunByIdV1MethodBuilderImpl.PutTestRunByIdV

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.apitools.validation;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import org.apache.commons.io.FileUtils;7import org.json.JSONException;8import org.json.JSONObject;9import org.skyscreamer.jsonassert.JSONCompareMode;10import org.testng.Assert;11import org.testng.annotations.Test;12public class JsonValidatorTest {13public void testJsonValidator() throws JSONException, IOException {14String expected = FileUtils.readFileToString(new File("src/test/resources/expected.json"), "UTF-8");15String actual = FileUtils.readFileToString(new File("src/test/resources/actual.json"), "UTF-8");16JSONObject expectedJson = new JSONObject(expected);17JSONObject actualJson = new JSONObject(actual);18List<String> missingKeys = new ArrayList<String>();19missingKeys.add("id");20missingKeys.add("name");21missingKeys.add("age");22missingKeys.add("address");23missingKeys.add("company");24List<String> extraKeys = new ArrayList<String>();25extraKeys.add("company");26extraKeys.add("address");27List<String> emptyKeys = new ArrayList<String>();28emptyKeys.add("company");29emptyKeys.add("address");30JsonValidator jsonValidator = new JsonValidator(expectedJson, actualJson, JSONCompareMode.LENIENT);31jsonValidator.validateJson();32Assert.assertTrue(jsonValidator.validateJson());33Assert.assertTrue(jsonValidator.validateMissingKeys(missingKeys));34Assert.assertTrue(jsonValidator.validateExtraKeys(extraKeys));35Assert.assertTrue(jsonValidator.validateEmptyKeys(emptyKeys));36}37}38package com.qaprosoft.apitools.validation;39import java.io.File;40import java.io.IOException;41import java.util.ArrayList;42import java.util.List;43import org.apache.commons.io.FileUtils;44import org.json.JSONException;45import org.json.JSONObject;46import org.skyscreamer.jsonassert.JSONCompareMode;47import org.testng.Assert;48import org.testng.annotations.Test;49public class JsonValidatorTest {50public void testJsonValidator() throws JSONException, IOException {51String expected = FileUtils.readFileToString(new File("src/test/resources/expected.json"), "UTF-8");52String actual = FileUtils.readFileToString(new File("src/test/resources/actual.json"), "UTF-8");53JSONObject expectedJson = new JSONObject(expected);54JSONObject actualJson = new JSONObject(actual);55List<String> missingKeys = new ArrayList<String>();56missingKeys.add("id");57missingKeys.add("name");58missingKeys.add("age

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.apitools.validation.JsonValidator;2import com.qaprosoft.apitools.validation.JsonCompareMode;3import com.qaprosoft.apitools.validation.JsonSchemaValidator;4import com.qaprosoft.apitools.validation.ResponseType;5import com.qaprosoft.apitools.validation.JsonCompareKeywords;6public class JsonValidatorTest {7 public static void main(String[] args) {8 JsonValidator validator = new JsonValidator();9 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.STRICT);10 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.LENIENT);11 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.NON_EXTENSIBLE);12 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.ONLY_REQUIRED);13 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.STRICT_ORDER);14 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.LENIENT_ORDER);15 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.STRICT, JsonCompareKeywords.ARRAY_CONTAINS);16 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.STRICT, JsonCompareKeywords.ARRAY_CONTAINS, JsonCompareKeywords.ARRAY_CONTAINS_ORDER);17 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.STRICT, JsonCompareKeywords.ARRAY_CONTAINS, JsonCompareKeywords.ARRAY_CONTAINS_ORDER, JsonCompareKeywords.ARRAY_CONTAINS_UNIQUE);18 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.STRICT, JsonCompareKeywords.ARRAY_CONTAINS, JsonCompareKeywords.ARRAY_CONTAINS_ORDER, JsonCompareKeywords.ARRAY_CONTAINS_UNIQUE, JsonCompareKeywords.ARRAY_CONTAINS_UNIQUE_ORDER);19 validator.validateResponseByPath("src/test/resources/json_schema.json", "src/test/resources/json_response.json", JsonCompareMode.ST

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.apitools.validation.JsonValidator;2import java.io.File;3import java.io.IOException;4public class 1 {5 public static void main(String[] args) throws IOException {6 String json1 = "{ \"key1\": \"value1\", \"key2\": \"value2\", \"key3\": \"value3\" }";7 String json2 = "{ \"key1\": \"value1\", \"key2\": \"value2\", \"key3\": \"value3\", \"key4\": \"value4\" }";8 String json3 = "{ \"key1\": \"value1\", \"key2\": \"value2\", \"key3\": \"value3\", \"key4\": \"value4\" }";9 String schema1 = "{ \"type\": \"object\", \"properties\": { \"key1\": { \"type\": \"string\" }, \"key2\": { \"type\": \"string\" }, \"key3\": { \"type\": \"string\" } }, \"required\": [\"key1\", \"key2\", \"key3\"], \"additionalProperties\": false }";10 String schema2 = "{ \"type\": \"object\", \"properties\": { \"key1\": { \"type\": \"string\" }, \"key2\": { \"type\": \"string\" }, \"key3\": { \"type\": \"string\" } }, \"required\": [\"key1\", \"key2\", \"key3\", \"key4\"], \"additionalProperties\": false }";11 String schema3 = "{ \"type\": \"object\", \"properties\": { \"key1\": { \"type\": \"string\" }, \"key2\": { \"type\": \"string\" }, \"key3\": { \"type\": \"string\" }, \"key4\": { \"type\": \"string\" } }, \"required\": [\"key1\", \"key2\", \"key3\", \"key4\"], \"additionalProperties\": false }";12 JsonValidator validator = new JsonValidator();13 validator.validateJSON(json1, schema1, true);14 validator.validateJSON(json2, schema2, true);15 validator.validateJSON(json3, schema3, true);16 validator.validateJSON(json1, new File("schema1.json"), true);17 validator.validateJSON(json2, new File("schema2.json"), true);18 validator.validateJSON(json3, new File("schema3.json"), true);

Full Screen

Full Screen

JsonValidator

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.demo.api;2import com.qaprosoft.carina.core.foundation.api.AbstractApiMethodV2;3import com.qaprosoft.carina.core.foundation.api.http.HttpResponseStatusType;4import com.qaprosoft.carina.core.foundation.utils.Configuration;5import com.qaprosoft.carina.core.foundation.utils.R;6import com.qaprosoft.carina.core.foundation.utils.ownership.MethodOwner;7import com.qaprosoft.carina.core.foundation.utils.tag.Priority;8import com.qaprosoft.carina.core.foundation.utils.tag.TestTag;9import com.qaprosoft.carina.core.foundation.utils.tag.TestTagType;10import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;11import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageOpeningStrategy;12import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageOpeningStrategy.PageOpeningStrategyType;13import com.qaprosoft.carina.core.foundation.webdriver.decorator.Type;14import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFindBy;15import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFindBy.Sorting;16import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFindByType;17import com.qaprosoft.carina.core.foundation.webdriver.locator.Locator;18import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatorType;19import com.qaprosoft.carina.core.foundation.webdriver.locator.MatchingStrategy;20import com.qaprosoft.carina.core.foundation.webdriver.locator.MatchingStrategyType;21import com.qaprosoft.carina.core.foundation.webdriver.locator.MatchingType;22import com.qaprosoft.carina.core.foundation.webdriver.locator.MatchingType.MatchingLogic;23import com.qaprosoft.carina.core.foundation.webdriver.locator.RelativeLocator;24import com.qaprosoft.carina.core.foundation.webdriver.locator.RelativeLocator.RelativeLocatorType;25import com.qaprosoft.carina.core.foundation.webdriver.screenshot.Screenshot;26import com.qaprosoft.carina.core.foundation.webdriver.screenshot.Screenshot.ScreenshotType;27import com.qaprosoft.carina.core.foundation.webdriver.screenshot.Screenshot.ScreenshotVisibility;28import com.qaprosoft.carina.core.foundation.webdriver.screenshot.Screenshot.ScreenshotVisibilityType;29import com.qaprosoft.carina.core.foundation.webdriver.screenshot.Screenshot.ScreenshotVisibilityType.Visibility;30import com.qaprosoft.carina.core.foundation.webdriver.screenshot.Screenshot.ScreenshotVisibilityType.VisibilityType;31import com.q

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Carina automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful