Skip to content

Commit

Permalink
few api tests implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo committed Jun 4, 2022
1 parent 34687e3 commit 204da8f
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/pablojuice/framework/base/BaseAPITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pablojuice.framework.base;

import io.restassured.RestAssured;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.BeforeTest;

public abstract class BaseAPITest extends AbstractTestNGSpringContextTests {
@BeforeTest
public void setup() {
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
}

public abstract void test();
}
4 changes: 4 additions & 0 deletions src/main/java/com/pablojuice/framework/base/BaseBiz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.pablojuice.framework.base;

public abstract class BaseBiz {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pablojuice.framework.ui.base;
package com.pablojuice.framework.base;

import org.openqa.selenium.WebDriver;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pablojuice.framework.ui.base;
package com.pablojuice.framework.base;

import com.pablojuice.framework.drivers.DriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/com/pablojuice/framework/ui/base/BaseBiz.java

This file was deleted.

199 changes: 199 additions & 0 deletions src/test/java/com/pablojuice/framework/api/TrelloApiBiz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.pablojuice.framework.api;

import com.pablojuice.framework.base.BaseBiz;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.testng.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class TrelloApiBiz extends BaseBiz {

private static final String BASE_URI = "https://api.trello.com";
private static final String BASE_PATH = "/1";

private final String apiKey;
private final String apiToken;

private final List<String> boardIDs = new ArrayList<>();
private final List<String> listIDs = new ArrayList<>();
private final List<String> cardIDs = new ArrayList<>();

public TrelloApiBiz(String apiKey, String apiToken) {
this.apiKey = apiKey;
this.apiToken = apiToken;
RestAssured.baseURI = BASE_URI;
RestAssured.basePath = BASE_PATH;
}

public TrelloApiBiz createBoard() {
String randomBoardName = "newBoard" + getRandomString();
String boardID = givenTrelloRequest()
.queryParam("name", randomBoardName)
.post("/boards")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.assertThat()
.body("name", equalTo(randomBoardName))
.extract().path("id");
Assert.assertNotNull(boardID);
boardIDs.add(boardID);
return this;
}

public TrelloApiBiz checkIfBoardExists() {
Assert.assertNotNull(getLatestBoardID(), "Board id is null");
givenTrelloRequest()
.get("/boards/" + getLatestBoardID())
.then()
.assertThat()
.statusCode(200);
return this;
}

public TrelloApiBiz renameBoard() {
Assert.assertNotNull(getLatestBoardID(), "Board id is null");
String newBoardName = "newBoard" + getRandomString();
givenTrelloRequest()
.queryParam("name", newBoardName)
.put("/boards/" + getLatestBoardID())
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.assertThat()
.body("name", equalTo(newBoardName));
return this;
}

public TrelloApiBiz deleteBoard() {
Assert.assertNotNull(getLatestBoardID(), "Board id is null");
givenTrelloRequest()
.delete("/boards/" + getLatestBoardID())
.then()
.statusCode(200);
return this;
}

public TrelloApiBiz createCardList() {
Assert.assertNotNull(getLatestBoardID(), "Board id is null");
String randomListName = "newList" + getRandomString();
listIDs.add(
givenTrelloRequest()
.queryParam("name", randomListName)
.post("/boards/" + getLatestBoardID() + "/lists")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.assertThat()
.body("name", equalTo(randomListName))
.extract().path("id")
);
return this;
}

public TrelloApiBiz moveCardsToAnotherList() {
Assert.assertNotNull(getLatestListID(), "List id is null");
Assert.assertTrue(listIDs.size() > 1);
givenTrelloRequest()
.queryParam("idList", getLatestListID())
.post("/lists/" + listIDs.get(listIDs.size() - 2) + "/moveAllCards")
.then()
.statusCode(200)
.contentType(ContentType.JSON);
return this;
}

public TrelloApiBiz createCard() {
Assert.assertNotNull(getLatestListID(), "List id is null");
String randomCardName = "newCard" + getRandomString();
cardIDs.add(
givenTrelloRequest()
.queryParam("name", randomCardName)
.queryParam("idList", getLatestListID())
.queryParam("desc", "initial Test Description")
.post("/cards")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.assertThat()
.body("name", equalTo(randomCardName))
.extract().path("id")
);
return this;
}

public TrelloApiBiz editCard() {
Assert.assertNotNull(getLatestCardID(), "Card id is null");
givenTrelloRequest()
.queryParam("name", "newCard0")
.queryParam("desc", "edited Test Description")
.put("/cards/" + getLatestCardID())
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.assertThat()
.body("desc", equalTo("edited Test Description"))
.extract().path("id");
return this;
}

public TrelloApiBiz commentCard() {
Assert.assertNotNull(getLatestCardID(), "Card id is null");
givenTrelloRequest()
.queryParam("text", "testComment")
.post("/cards/" + getLatestCardID() + "/actions/comments")
.then()
.statusCode(200);
return this;
}

public TrelloApiBiz deleteCard() {
Assert.assertNotNull(getLatestCardID(), "Card id is null");
givenTrelloRequest()
.delete("/cards/" + getLatestCardID())
.then()
.statusCode(200);
return this;
}

private String getLatestBoardID() {
if (boardIDs.size() > 0) {
return boardIDs.get(boardIDs.size() - 1);
}
return null;
}

private String getLatestListID() {
if (listIDs.size() > 0) {
return listIDs.get(listIDs.size() - 1);
}
return null;
}

private String getLatestCardID() {
if (cardIDs.size() > 0) {
return cardIDs.get(cardIDs.size() - 1);
}
return null;
}

private RequestSpecification givenTrelloRequest() {
return given()
.contentType(ContentType.JSON)
.log().all()
.when()
.queryParam("key", apiKey)
.queryParam("token", apiToken);
}

private static String getRandomString() {
return UUID.randomUUID().toString();
}
}
54 changes: 54 additions & 0 deletions src/test/java/com/pablojuice/framework/api/TrelloApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.pablojuice.framework.api;

import org.testng.annotations.Test;

public class TrelloApiTest {
private static final String KEY = "f037495712ba064a957e97dad510b26b";
private static final String TOKEN = "f02f1be70c984f4535604d200aa9d074e0fa2eb12f1f937a9ed8076d6fd31816";

private final TrelloApiBiz trelloApiBiz = new TrelloApiBiz(KEY, TOKEN);

@Test
public void testTrelloBoardApi() {
trelloApiBiz
.createBoard()
.checkIfBoardExists()
.renameBoard()
.deleteBoard();
}

@Test
public void testTrelloCardListApi() {
trelloApiBiz
.createBoard()
.checkIfBoardExists()
.createCardList()
.deleteBoard();
}

@Test
public void testTrelloCardApi() {
trelloApiBiz
.createBoard()
.checkIfBoardExists()
.createCardList()
.createCard().editCard()
.createCard().commentCard()
.createCard().deleteCard()
.deleteBoard();
}

@Test
public void testTrelloMoveApi() {
trelloApiBiz
.createBoard()
.checkIfBoardExists()
.createCardList()
.createCard()
.createCard()
.createCard()
.createCardList()
.moveCardsToAnotherList()
.deleteBoard();
}
}

0 comments on commit 204da8f

Please sign in to comment.