Skip to content

Commit

Permalink
Push With readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserFaraaz committed Jul 15, 2020
0 parents commit 2ae95bb
Show file tree
Hide file tree
Showing 17 changed files with 753 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target
test-output
log.out
.DS_Store
.classpath
.project
.settings
../.DS_Store

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Rest Assured API testing with Java and TestNG runner

This is a Rest Assured testing framework using Java, testNG, POJO, concepts

## Installation
Just do a maven install (usually IDE take care of dowloading dependancies when you import eg: Eclipse)

```bash
mvn install
mvn test
```

## Project Structure
```
API_Framework
src.main.java
api `To have all End Points `
pojo `To have easy access to properties or fields, and methods that you can use to access and modify the values `
utils `T0 have supporting scripts for reports, reading property file`
src.test.java
Test cases
```
129 changes: 129 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Java-API-Testing</groupId>
<artifactId>API-Framework</artifactId>
<version>1.0-SNAPSHOT</version>

<name>API Framework</name>
<url>http://13.126.80.194:8080</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<forkMode>never</forkMode>

<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
62 changes: 62 additions & 0 deletions src/main/java/api/BaseAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;

import java.io.IOException;

/**
* Created by Yasser Khan on 15-06-2020.
*/
public abstract class BaseAPI {

protected String baseURI;
protected RequestSpecBuilder requestSpecBuilder;
protected RequestSpecification requestSpecification;
protected ResponseSpecBuilder responseSpecBuilder;
protected ResponseSpecification responseSpecification;
protected Response apiResponse;
protected int expectedStatusCode;

public BaseAPI(String baseURI) {
this.baseURI = baseURI;
requestSpecBuilder = new RequestSpecBuilder();
responseSpecBuilder = new ResponseSpecBuilder();
}

protected Response getApiResponse() {
return apiResponse;
}

public String getApiResponseAsString() {
return apiResponse.asString();
}

public <T> T getAPIResponseAsPOJO(Class<T> type) throws IOException {
return new ObjectMapper().readValue(getApiResponseAsString(), type);
}

public int getExpectedStatusCode() {
return expectedStatusCode;
}

public void setExpectedStatusCode(int expectedStatusCode) {
this.expectedStatusCode = expectedStatusCode;
}

protected abstract void createRequest();

protected abstract void executeRequest();

protected abstract void validateResponse();

public void perform() {
createRequest();
executeRequest();
validateResponse();
}
}
66 changes: 66 additions & 0 deletions src/main/java/api/auth/PostToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package api.auth;

import api.BaseAPI;
import com.google.common.base.Joiner;
import com.jayway.jsonpath.JsonPath;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;

/**
* Created by Yasser Khan on 15-06-2020.
*/
public class PostToken extends BaseAPI {

public static final Logger logger = Logger.getLogger(BaseAPI.class);

String apiPath = "/authenticate";
String contentType;
Map<String, String> bodyParams;
String token;

public PostToken(String baseURI) {
super(baseURI);
bodyParams = new HashMap<String, String>();
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public void addBodyParam(String key, String value) {
bodyParams.put(key, value);
}

public String getAccessToken() {
return token;
}

@Override
protected void createRequest() {
logger.info("Creating request");
requestSpecBuilder.setBaseUri(baseURI);
requestSpecBuilder.setBasePath(apiPath);
requestSpecBuilder.setContentType(contentType);
requestSpecBuilder.setBody(bodyParams);
requestSpecification = requestSpecBuilder.build();
}

@Override
protected void executeRequest() {
logger.info("Executing request");
apiResponse = given().spec(requestSpecification).post();
}

@Override
protected void validateResponse() {
logger.info("Validating response");
responseSpecBuilder.expectStatusCode(expectedStatusCode);
responseSpecification = responseSpecBuilder.build();
apiResponse.then().spec(responseSpecification);
token = JsonPath.read(apiResponse.asString(), "token");
}
}
51 changes: 51 additions & 0 deletions src/main/java/api/users/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package api.users;

import api.BaseAPI;
import io.restassured.builder.RequestSpecBuilder;

import static io.restassured.RestAssured.given;

public class Users extends BaseAPI {

String token;
String apiPath = "/api/v1/users/";
String phoneNumber;

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public Users(String baseURI, String token) {
super(baseURI);
this.token = token;
}

private RequestSpecBuilder addQueryParam(RequestSpecBuilder requestSpecBuilder) {
if (this.phoneNumber != null) {
requestSpecBuilder.addQueryParam(phoneNumber);
}

return requestSpecBuilder;
}

@Override
protected void createRequest() {
requestSpecBuilder.setBaseUri(baseURI);
requestSpecBuilder.setBasePath(apiPath);
requestSpecification = this.addQueryParam(requestSpecBuilder).build();
requestSpecification = requestSpecBuilder.build();

}

@Override
protected void executeRequest() {
apiResponse = given().spec(requestSpecification).auth().oauth2(token).get();
}

@Override
protected void validateResponse() {
responseSpecBuilder.expectStatusCode(expectedStatusCode);
responseSpecification = responseSpecBuilder.build();
apiResponse.then().spec(responseSpecification);
}
}
42 changes: 42 additions & 0 deletions src/main/java/pojo/UserListPOJO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pojo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "users" })
public class UserListPOJO {

@JsonProperty("users")
private List<UsersPOJO> users;

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("users")
public List<UsersPOJO> getUsers() {
return users;
}

@JsonProperty("users")
public void setUsers(List<UsersPOJO> users) {
this.users = users;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
Loading

0 comments on commit 2ae95bb

Please sign in to comment.