From 86e297d27fbcb072a569848c22902b683ceca96a Mon Sep 17 00:00:00 2001 From: Mavlushechka Date: Tue, 4 Oct 2022 01:12:37 +0500 Subject: Add getResponseCode methods to JSONPlaceholderAPIManager utility class --- .../a1qa/utils/JSONPlaceholderAPIManager.java | 26 ++++++++++---------- src/main/resources/testData.json | 11 ++++----- src/test/java/com/mavlushechka/a1qa/TestCase1.java | 28 ++++++++++------------ 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java index 2a66576..aecf885 100644 --- a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java +++ b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java @@ -15,31 +15,35 @@ public class JSONPlaceholderAPIManager { public static Post getPost(int id) throws IOException { - return getObject(postsSpec + "/" + id, Post.class); + return JSONParser.convertToObject(URLConnectionManager.get(postsSpec + "/" + id), Post.class); + } + + public static int getPostResponseCode(int id, String requestMethod) throws IOException { + return URLConnectionManager.getResponseCode(postsSpec + "/" + id, requestMethod); } public static List getPosts() throws IOException { return getObjects(postsSpec, Post.class); } + public static int getPostsResponseCode(String requestMethod) throws IOException { + return URLConnectionManager.getResponseCode(postsSpec, requestMethod); + } + public static Post postPost(Post post) throws IOException { - return postObject(postsSpec, post, Post.class); + return JSONParser.convertToObject(URLConnectionManager.post(postsSpec, JSONParser.convertToJSON(post)), Post.class); } public static User getUser(int id) throws IOException { - return getObject(usersSpec + "/" + id, User.class); + return JSONParser.convertToObject(URLConnectionManager.get(usersSpec + "/" + id), User.class); } public static List getUsers() throws IOException { return getObjects(usersSpec, User.class); } - public static User postUser(User user) throws IOException { - return postObject(usersSpec, user, User.class); - } - - private static T getObject(String spec, Class classOfObject) throws IOException { - return JSONParser.convertToObject(URLConnectionManager.get(spec), classOfObject); + public static int getUsersResponseCode(String requestMethod) throws IOException { + return URLConnectionManager.getResponseCode(usersSpec, requestMethod); } private static List getObjects(String spec, Class classOfObject) throws IOException { @@ -52,8 +56,4 @@ public class JSONPlaceholderAPIManager { return objectsArrayList.stream().toList(); } - private static T postObject(String spec, T object, Class classOfObject) throws IOException { - return JSONParser.convertToObject(URLConnectionManager.post(spec, JSONParser.convertToJSON(object)), classOfObject); - } - } diff --git a/src/main/resources/testData.json b/src/main/resources/testData.json index 24ac764..f3fc4ca 100644 --- a/src/main/resources/testData.json +++ b/src/main/resources/testData.json @@ -3,12 +3,10 @@ { "steps": [ { - "spec": "/posts", "requestMethod": "GET", "responseCode": "200" }, { - "spec": "/posts/99", "requestMethod": "GET", "responseCode": "200", "post": { @@ -19,13 +17,14 @@ } }, { - "spec": "/posts/150", "requestMethod": "GET", "responseCode": "404", - "isResponseBodyEmpty": "true" + "isResponseBodyEmpty": "true", + "post": { + "id": "150" + } }, { - "spec": "/posts", "requestMethod": "POST", "responseCode": "201", "post": { @@ -33,7 +32,6 @@ } }, { - "spec": "/users", "requestMethod": "GET", "responseCode": "200", "user": { @@ -61,7 +59,6 @@ } }, { - "spec": "/users/5", "requestMethod": "GET", "responseCode": "200", "user": { diff --git a/src/test/java/com/mavlushechka/a1qa/TestCase1.java b/src/test/java/com/mavlushechka/a1qa/TestCase1.java index 5edac39..45ba975 100644 --- a/src/test/java/com/mavlushechka/a1qa/TestCase1.java +++ b/src/test/java/com/mavlushechka/a1qa/TestCase1.java @@ -1,5 +1,6 @@ package com.mavlushechka.a1qa; +import com.mavlushechka.a1qa.constants.Endpoint; import com.mavlushechka.a1qa.models.Post; import com.mavlushechka.a1qa.models.User; import com.mavlushechka.a1qa.utils.*; @@ -13,41 +14,39 @@ public class TestCase1 extends BaseTest { @Test public void test1() throws IOException { - String postsSpec = JSONParser.parseData("testData", "testCases[0].steps[0].spec"); String postsRequestMethod = JSONParser.parseData("testData", "testCases[0].steps[0].requestMethod"); - int postsResponseCode = URLConnectionManager.getResponseCode(postsSpec, postsRequestMethod); + int postsResponseCode = JSONPlaceholderAPIManager.getPostsResponseCode(postsRequestMethod); Assert.assertEquals(postsResponseCode, Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[0].responseCode")), "Response code is not correct."); LoggerUtils.step("Send GET Request to get all posts (/posts)."); - String postsJson = URLConnectionManager.get(postsSpec); + String postsJson = URLConnectionManager.get(Endpoint.POSTS.getSpec()); Assert.assertTrue(JSONParser.isJson(postsJson), "Response is not json."); List postsList = JSONPlaceholderAPIManager.getPosts(); Assert.assertTrue(Posts.isPostsAscending(postsList), "Posts are not sorted ascending."); - String post99Spec = JSONParser.parseData("testData", "testCases[0].steps[1].spec"); + int post99Id = Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[1].post.id")); String post99RequestMethod = JSONParser.parseData("testData", "testCases[0].steps[1].requestMethod"); - int post99ResponseCode = URLConnectionManager.getResponseCode(post99Spec, post99RequestMethod); + int post99ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(post99Id, post99RequestMethod); Assert.assertEquals(post99ResponseCode, Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[1].responseCode")), "Response code is not correct."); LoggerUtils.step("Send GET request to get post with id=99 (/posts/99)."); - Post post99 = JSONPlaceholderAPIManager.getPost(Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[1].post.id"))); + Post post99 = JSONPlaceholderAPIManager.getPost(post99Id); Assert.assertEquals(post99.id, JSONParser.parseData("testData", "testCases[0].steps[1].post.id"), "Post id is not correct."); Assert.assertEquals(post99.userId, JSONParser.parseData("testData", "testCases[0].steps[1].post.userId"), "Post user id is not correct."); Assert.assertEquals(post99.title.isEmpty(), Boolean.parseBoolean(JSONParser.parseData("testData", "testCases[0].steps[1].post.isTitleEmpty")), "Post title is not correct."); Assert.assertEquals(post99.body.isEmpty(), Boolean.parseBoolean(JSONParser.parseData("testData", "testCases[0].steps[1].post.isBodyEmpty")), "Post body is not correct."); - String post150Spec = JSONParser.parseData("testData", "testCases[0].steps[2].spec"); + int post150Id = Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[2].post.id")); String post150RequestMethod = JSONParser.parseData("testData", "testCases[0].steps[2].requestMethod"); - int post150ResponseCode = URLConnectionManager.getResponseCode(post150Spec, post150RequestMethod); + int post150ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(post150Id, post150RequestMethod); Assert.assertEquals(post150ResponseCode, Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[2].responseCode")), "Response code is not correct."); LoggerUtils.step("Send GET request to get post with id=150 (/posts/150)."); String post150Json; try { - post150Json = URLConnectionManager.get(post150Spec); + post150Json = URLConnectionManager.get(Endpoint.POSTS.getSpec() + "/" + post150Id); } catch (IOException ioException) { post150Json = "{}"; } Assert.assertEquals(JSONParser.isBodyEmpty(post150Json), Boolean.parseBoolean(JSONParser.parseData("testData", "testCases[0].steps[2].isResponseBodyEmpty")), "Response body is not correct."); - String postSpec = JSONParser.parseData("testData", "testCases[0].steps[3].spec"); String postRequestMethod = JSONParser.parseData("testData", "testCases[0].steps[3].requestMethod"); int randomTextLettersLowerBound = Integer.parseInt(JSONParser.parseData("config", "randomTextGenerator.lettersLowerBound")); int randomTextLettersUpperBound = Integer.parseInt(JSONParser.parseData("config", "randomTextGenerator.lettersUpperBound")); @@ -58,14 +57,13 @@ public class TestCase1 extends BaseTest { StringUtils.generateRandomText(randomTextLettersLowerBound, randomTextLettersUpperBound, randomTextLength), JSONParser.parseData("testData", "testCases[0].steps[3].post.userId") ); - int postResponseCode = URLConnectionManager.getResponseCode(postSpec, postRequestMethod); + int postResponseCode = JSONPlaceholderAPIManager.getPostsResponseCode(postRequestMethod); Assert.assertEquals(postResponseCode, Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[3].responseCode")), "Response code is not correct."); LoggerUtils.step("Send POST request to create post with userId=1 and random body and random title (/posts)."); Assert.assertEquals(post, JSONPlaceholderAPIManager.postPost(post), "Post information is not correct."); - String usersSpec = JSONParser.parseData("testData", "testCases[0].steps[4].spec"); String usersRequestMethod = JSONParser.parseData("testData", "testCases[0].steps[4].requestMethod"); - int usersResponseCode = URLConnectionManager.getResponseCode(usersSpec, usersRequestMethod); + int usersResponseCode = JSONPlaceholderAPIManager.getUsersResponseCode(usersRequestMethod); Assert.assertEquals(usersResponseCode, Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[4].responseCode")), "Response code is not correct."); LoggerUtils.step("Send GET request to get users (/users)."); List users = JSONPlaceholderAPIManager.getUsers(); @@ -78,9 +76,9 @@ public class TestCase1 extends BaseTest { } Assert.assertEquals(JSONParser.convertToObject(JSONParser.parseObject("testData", "testCases[0].steps[4].user"), User.class), user, "User information is not correct."); - String user5Spec = JSONParser.parseData("testData", "testCases[0].steps[5].spec"); + int user5Id = Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[5].user.id")); String user5RequestMethod = JSONParser.parseData("testData", "testCases[0].steps[5].requestMethod"); - int user5ResponseCode = URLConnectionManager.getResponseCode(user5Spec, user5RequestMethod); + int user5ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(user5Id, user5RequestMethod); Assert.assertEquals(user5ResponseCode, Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[5].responseCode")), "Response code is not correct."); LoggerUtils.step("Send GET request to get user with id=5 (/users/5)."); User user5 = JSONPlaceholderAPIManager.getUser(Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[5].user.id"))); -- cgit v1.2.3