├── README.md ├── pom.xml └── src ├── main └── java │ └── utils │ └── Utils.java └── test └── java ├── ApiTests ├── HelperTestMethods.java ├── UseCase1Test.java └── UseCase2Test.java └── TestSuite └── AllApiTest.java /README.md: -------------------------------------------------------------------------------- 1 | # RestAssuredExample 2 | RestAssured Example 3 | You can find details here: https://www.swtestacademy.com/api-testing-with-rest-assured/ 4 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.5min.apitest 8 | 5min-apitest 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | junit 14 | junit 15 | 4.12 16 | 17 | 18 | 19 | com.jayway.restassured 20 | rest-assured 21 | 2.8.0 22 | 23 | 24 | 25 | com.jayway.restassured 26 | json-schema-validator 27 | 2.8.0 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import com.jayway.restassured.RestAssured; 4 | import com.jayway.restassured.http.ContentType; 5 | import com.jayway.restassured.path.json.JsonPath; 6 | import com.jayway.restassured.response.Response; 7 | 8 | import static com.jayway.restassured.RestAssured.*; 9 | 10 | public class Utils { 11 | //Global Setup Variables 12 | public static String path; 13 | public static String jsonPathTerm; 14 | 15 | //Sets Base URI 16 | public static void setBaseURI (){ 17 | RestAssured.baseURI = "http://api.5min.com"; 18 | } 19 | 20 | //Sets base path 21 | public static void setBasePath(String basePathTerm){ 22 | RestAssured.basePath = basePathTerm; 23 | } 24 | 25 | //Reset Base URI (after test) 26 | public static void resetBaseURI (){ 27 | RestAssured.baseURI = null; 28 | } 29 | 30 | //Reset base path 31 | public static void resetBasePath(){ 32 | RestAssured.basePath = null; 33 | } 34 | 35 | //Sets ContentType 36 | public static void setContentType (ContentType Type){ 37 | given().contentType(Type); 38 | } 39 | 40 | //Sets Json path term 41 | public static void setJsonPathTerm(String jsonPath){ 42 | jsonPathTerm = jsonPath; 43 | } 44 | 45 | //Created search query path 46 | public static void createSearchQueryPath(String searchTerm, String param, String paramValue) { 47 | path = searchTerm + "/" + jsonPathTerm + "?" + param + "=" + paramValue; 48 | } 49 | 50 | //Returns response 51 | public static Response getResponse() { 52 | //System.out.print("path: " + path +"\n"); 53 | return get(path); 54 | } 55 | 56 | //Returns JsonPath object 57 | public static JsonPath getJsonPath (Response res) { 58 | String json = res.asString(); 59 | //System.out.print("returned json: " + json +"\n"); 60 | return new JsonPath(json); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/ApiTests/HelperTestMethods.java: -------------------------------------------------------------------------------- 1 | package ApiTests; 2 | 3 | import com.jayway.restassured.path.json.JsonPath; 4 | import com.jayway.restassured.response.Response; 5 | 6 | import java.util.*; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | public class HelperTestMethods { 11 | //Verify the http response status returned. Check Status Code is 200? 12 | public void checkStatusIs200 (Response res) { 13 | assertEquals("Status Check Failed!", 200, res.getStatusCode()); 14 | } 15 | 16 | //Get Video Ids (For use case-1) 17 | public ArrayList getVideoIdList (JsonPath jp) { 18 | ArrayList videoIdList = jp.get("items.id"); 19 | return videoIdList; 20 | } 21 | 22 | //Get Related Video Ids (For use case-2) 23 | public ArrayList getRelatedVideoIdList (JsonPath jp) { 24 | ArrayList relatedVideoList = jp.get("items.related.id"); 25 | //In order to split first element of ArrayList and assign it to a new ArrayList, I did below operation. 26 | ArrayList splittedRelatedVideoList = (ArrayList) relatedVideoList.get(0); 27 | return splittedRelatedVideoList; 28 | } 29 | 30 | //Merge videoIdList and relatedVideoIdList as mergedVideoList 31 | public ArrayList mergeLists (ArrayList videoList, ArrayList relatedVideoList){ 32 | ArrayList mergedVideoList = new ArrayList(videoList); 33 | mergedVideoList.addAll(relatedVideoList); 34 | return mergedVideoList; 35 | } 36 | 37 | //Find Duplicate Videos 38 | public boolean findDuplicateVideos (List videoIdList) { 39 | for (int i=0; i< videoIdList.size(); i++) { 40 | if(Collections.frequency(videoIdList, videoIdList.get(i)) > 1){ 41 | System.out.println("This video id is duplicated: " + videoIdList.get(i)); 42 | return false; 43 | } 44 | } 45 | return true; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/ApiTests/UseCase1Test.java: -------------------------------------------------------------------------------- 1 | package ApiTests; 2 | 3 | import utils.Utils; 4 | import com.jayway.restassured.http.ContentType; 5 | import com.jayway.restassured.path.json.JsonPath; 6 | import com.jayway.restassured.response.Response; 7 | import org.junit.*; 8 | import org.junit.runners.MethodSorters; 9 | 10 | import static org.hamcrest.Matchers.containsString; 11 | import static org.junit.Assert.assertThat; 12 | import static org.junit.Assert.assertTrue; 13 | 14 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) //For Ascending order test execution 15 | public class UseCase1Test { 16 | 17 | private Response res = null; //Response 18 | private JsonPath jp = null; //JsonPath 19 | 20 | //Instantiate a Helper Test Methods (htm) Object 21 | HelperTestMethods htm = new HelperTestMethods(); 22 | 23 | @Before 24 | public void setup (){ 25 | //Test Setup 26 | Utils.setBaseURI(); //Setup Base URI 27 | Utils.setBasePath("search"); //Setup Base Path 28 | Utils.setContentType(ContentType.JSON); //Setup Content Type 29 | Utils.setJsonPathTerm("videos.json"); //Setup Json Path Term 30 | Utils.createSearchQueryPath("paris hilton", "num_of_videos", "5"); //Set up search term, param and param value 31 | res = Utils.getResponse(); //Get response 32 | jp = Utils.getJsonPath(res); //Set JsonPath 33 | } 34 | 35 | @Test 36 | public void T01_StatusCodeTest() { 37 | //Verify the http response status returned. Check Status Code is 200? 38 | htm.checkStatusIs200(res); 39 | } 40 | 41 | @Test 42 | public void T02_SearchTermTest() { 43 | //Verify the title is correct 44 | Assert.assertEquals("Title is wrong!", ("Search results for \"paris hilton\""), jp.get("api-info.title")); 45 | } 46 | 47 | @Test 48 | public void T03_verifyOnlyFiveVideosReturned() { 49 | //Verify that only 5 video entries were returned 50 | Assert.assertEquals("Video Size is not equal to 5", 5, htm.getVideoIdList(jp).size()); 51 | } 52 | 53 | @Test 54 | public void T04_duplicateVideoVerification() { 55 | //Verify that there is no duplicate video 56 | assertTrue("Duplicate videos exist!", htm.findDuplicateVideos(htm.getVideoIdList(jp))); 57 | } 58 | 59 | @Test 60 | public void T05_printAttributes() { 61 | //Print video title, pubDate & duration 62 | printTitlePubDateDuration(jp); 63 | } 64 | 65 | @After 66 | public void afterTest (){ 67 | //Reset Values 68 | Utils.resetBaseURI(); 69 | Utils.resetBasePath(); 70 | } 71 | 72 | //******************* 73 | //***Local Methods*** 74 | //******************* 75 | //Prints Attributes 76 | private void printTitlePubDateDuration (JsonPath jp) { 77 | for(int i=0; i