Skip to content

Commit

Permalink
Merge pull request #238 from Coveros/feature/serviceMatchers
Browse files Browse the repository at this point in the history
Matching Check
  • Loading branch information
Max Saperstone authored Jan 21, 2020
2 parents 6675eed + 13a8da6 commit 4b78311
Show file tree
Hide file tree
Showing 10 changed files with 1,020 additions and 9 deletions.
35 changes: 28 additions & 7 deletions src/main/java/com/coveros/selenified/services/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author Max Saperstone
* @version 3.3.1
* @lastupdate 10/24/2019
* @lastupdate 1/6/2020
*/
public class Response {

Expand All @@ -46,11 +46,13 @@ public class Response {
private final AssertContains assertContains;
private final AssertEquals assertEquals;
private final AssertExcludes assertExcludes;
private final AssertMatches assertMatches;

// the verify class to check information about the response
private final VerifyContains verifyContains;
private final VerifyEquals verifyEquals;
private final VerifyExcludes verifyExcludes;
private final VerifyMatches verifyMatches;

public Response(Reporter reporter, Map<String, Object> headers, int code, JsonObject object, JsonArray array, String message) {
this.headers = headers;
Expand All @@ -61,9 +63,11 @@ public Response(Reporter reporter, Map<String, Object> headers, int code, JsonOb
this.assertContains = new AssertContains(this, reporter);
this.assertEquals = new AssertEquals(this, reporter);
this.assertExcludes = new AssertExcludes(this, reporter);
this.assertMatches = new AssertMatches(this, reporter);
this.verifyContains = new VerifyContains(this, reporter);
this.verifyEquals = new VerifyEquals(this, reporter);
this.verifyExcludes = new VerifyExcludes(this, reporter);
this.verifyMatches = new VerifyMatches(this, reporter);
}

/**
Expand Down Expand Up @@ -100,12 +104,22 @@ public AssertExcludes assertExcludes() {
}

/**
* Verifies that the services response will contain expected data.
* Asserts that the services response will match the expected data.
* These asserts are custom to the framework, and in addition to providing
* easy object oriented they provide additional traceability, and assist in
* troubleshooting and debugging failing tests. A failed assert will cause
* the test to immediately stop on the error.
*/
public AssertMatches assertMatches() {
return assertMatches;
}

/**
* Verifies that the services response will contain expected data.
* These asserts are custom to the framework, and in addition to providing
* easy object oriented they provide additional traceability, and assist in
* troubleshooting and debugging failing tests.
*/
public VerifyContains verifyContains() {
return verifyContains;
}
Expand All @@ -114,25 +128,32 @@ public VerifyContains verifyContains() {
* Verifies that the services response will equals expected data.
* These asserts are custom to the framework, and in addition to providing
* easy object oriented they provide additional traceability, and assist in
* troubleshooting and debugging failing tests. A failed assert will cause
* the test to immediately stop on the error.
* troubleshooting and debugging failing tests.
*/
public VerifyEquals verifyEquals() {
return verifyEquals;
}


/**
* Verifies that the services response will not contain expected data.
* These asserts are custom to the framework, and in addition to providing
* easy object oriented they provide additional traceability, and assist in
* troubleshooting and debugging failing tests. A failed assert will cause
* the test to immediately stop on the error.
* troubleshooting and debugging failing tests.
*/
public VerifyExcludes verifyExcludes() {
return verifyExcludes;
}

/**
* Verifies that the services response will match the expected data.
* These asserts are custom to the framework, and in addition to providing
* easy object oriented they provide additional traceability, and assist in
* troubleshooting and debugging failing tests.
*/
public VerifyMatches verifyMatches() {
return verifyMatches;
}

public Map<String, Object> getHeaders() {
return headers;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2019 Coveros, Inc.
*
* This file is part of Selenified.
*
* Selenified is licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.coveros.selenified.services.check;

import com.coveros.selenified.services.Response;
import com.coveros.selenified.utilities.Reporter;

import java.util.List;

import static com.coveros.selenified.utilities.Constants.DOES_NOT_MATCH_PATTERN;
import static org.testng.AssertJUnit.assertTrue;

/**
* Assert will handle all verifications performed on the actual web services
* calls themselves. These asserts are custom to the framework, and in addition to
* providing easy object oriented capabilities, they assist in
* troubleshooting and debugging failing tests.
*
* @author Max Saperstone
* @version 3.3.1
* @lastupdate 1/6/2020
*/
public class AssertMatches extends Matches {

/**
* The default constructor passing in the app and output file
*
* @param response - the response from the web services call
* @param reporter - the file to write all logging out to
*/
public AssertMatches(Response response, Reporter reporter) {
this.response = response;
this.reporter = reporter;
}

///////////////////////////////////////////////////////
// assertions about the page in general
///////////////////////////////////////////////////////

/**
* Asserts the actual response code matches the expected response
* code, and writes that out to the output file. If this fails, the code will
* immediately exit, and record the error.
*
* @param expectedPattern - the expected pattern of the response code
*/
@Override
public void code(String expectedPattern) {
int code = checkCode(expectedPattern);
assertTrue("Code Mismatch: code of '" + code + DOES_NOT_MATCH_PATTERN + expectedPattern + "'", String.valueOf(code).matches(expectedPattern));
}

/**
* Asserts the actual response json payload contains a key with a value matching the expected
* value. The jsonKeys should be passed in as crumbs of the keys leading to the field with
* the expected value. This result will be written out to the output file. If this fails, the code will
* immediately exit, and record the error.
*
* @param jsonKeys - the crumbs of json object keys leading to the field with the expected value
* @param expectedPattern - the expected pattern of the value
*/
@Override
public void nestedValue(List<String> jsonKeys, String expectedPattern) {
String nestedValue = checkNestedValue(jsonKeys, expectedPattern);
assertTrue("JsonElement Response Mismatch: nested value of '" + nestedValue + DOES_NOT_MATCH_PATTERN + expectedPattern + "'", nestedValue.matches(expectedPattern));
}

/**
* Asserts the actual response payload matches the expected
* response payload, and writes that out to the output file. If this fails, the code will
* immediately exit, and record the error.
*
* @param expectedPattern - the expected pattern of the message
*/
@Override
public void message(String expectedPattern) {
String message = checkMessage(expectedPattern);
assertTrue("Response Message Mismatch: message of '" + message + DOES_NOT_MATCH_PATTERN + expectedPattern, message != null && message.matches(expectedPattern));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.testng.log4testng.Logger;

import java.util.List;
import java.util.Map;
Expand All @@ -37,10 +38,11 @@
*
* @author Max Saperstone
* @version 3.3.1
* @lastupdate 10/24/2019
* @lastupdate 1/6/2020
*/
abstract class Check {

static final Logger log = Logger.getLogger(Check.class);
static final String ARROW = " &#8594; ";

// this will be the name of the file we write all commands out to
Expand Down
126 changes: 126 additions & 0 deletions src/main/java/com/coveros/selenified/services/check/Matches.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2019 Coveros, Inc.
*
* This file is part of Selenified.
*
* Selenified is licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.coveros.selenified.services.check;

import com.coveros.selenified.utilities.Reporter;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.List;

import static com.coveros.selenified.utilities.Constants.*;

/**
* Contains will handle all checks performed on the actual web services
* calls themselves involving whether or not the response has certain information.
* These asserts are custom to the framework, and in addition to
* providing easy object oriented capabilities, they assist in
* troubleshooting and debugging failing tests.
*
* @author Max Saperstone
* @version 3.3.1
* @lastupdate 1/6/2020
*/
abstract class Matches extends Check {

/**
* Checks the actual response code is matching the expected response
* code, and writes that out to the output file
*
* @param expectedPattern - the expected pattern of the response code
*/
abstract void code(String expectedPattern);

/**
* Checks the actual response code is matching the expected response
* code, and writes that out to the output file
*
* @param expectedPattern - the expected pattern of the response code
*/
int checkCode(String expectedPattern) {
int actualCode = this.response.getCode();
recordResult("Expected to find a response code matching a pattern of: '<i>" + expectedPattern + ENDI,
"Found a response code of <b>" + actualCode + ENDB, String.valueOf(actualCode).matches(expectedPattern));
return actualCode;
}

/**
* Checks the actual response json payload contains a key with a value matching the expected
* value. The jsonKeys should be passed in as crumbs of the keys leading to the field with
* the expected value. This result will be written out to the output file.
*
* @param jsonKeys - the crumbs of json object keys leading to the field with the expected value
* @param expectedPattern - the expected pattern of the value
*/
abstract void nestedValue(List<String> jsonKeys, String expectedPattern);

/**
* Checks the actual response json payload contains a key with a value matching the expected
* value. The jsonCrumbs should be passed in as crumbs of the keys leading to the field with
* the expected value. This result will be written out to the output file.
*
* @param jsonCrumbs - the crumbs of json object keys leading to the field with the expected value
* @param expectedPattern - the expected pattern of the value
*/
String checkNestedValue(List<String> jsonCrumbs, String expectedPattern) {
JsonElement actualValue = this.response.getObjectData();
for (String jsonCrumb : jsonCrumbs) {
if (!(actualValue instanceof JsonObject)) {
actualValue = null;
break;
}
actualValue = actualValue.getAsJsonObject().get(jsonCrumb);
}
String stringValue = String.valueOf(actualValue);
if (actualValue != null) {
try {
stringValue = actualValue.getAsString();
} catch (UnsupportedOperationException e) {
log.info(e);
}
}
recordResult(EXPECTED_TO_FIND_A_RESPONSE_OF + STARTI + Reporter.formatHTML(String.join(ARROW, jsonCrumbs)) + ENDI +
" matching a pattern of: " + DIV_I + expectedPattern + END_IDIV,
FOUND + DIV_I + Reporter.formatHTML(GSON.toJson(actualValue)) + END_IDIV, stringValue.matches(expectedPattern));
return stringValue;
}

/**
* Checks the actual response payload matches the expected
* response payload, and writes that out to the output file
*
* @param expectedPattern - the expected pattern of the response message
*/
abstract void message(String expectedPattern);

/**
* Checks the actual response payload matches the expected
* response payload, and writes that out to the output file
*
* @param expectedPattern - the expected pattern of the response message
*/
String checkMessage(String expectedPattern) {
String actualMessage = this.response.getMessage();
recordResult(EXPECTED_TO_FIND_A_RESPONSE_MATCHING + STARTI + expectedPattern + ENDI,
FOUND + STARTI + this.response.getMessage() + ENDI, actualMessage != null && actualMessage.matches(expectedPattern));
return actualMessage;
}
}
Loading

0 comments on commit 4b78311

Please sign in to comment.