From 840c5a4594c94b6b3e7962c9bea77a5662c39e48 Mon Sep 17 00:00:00 2001 From: Mavlushechka Date: Tue, 4 Oct 2022 00:18:19 +0500 Subject: Rename JSONPlaceholderAPI utility class to JSONPlaceholderAPIManager --- .../a1qa/utils/JSONPlaceholderAPI.java | 54 ---------------------- .../a1qa/utils/JSONPlaceholderAPIManager.java | 54 ++++++++++++++++++++++ src/test/java/com/mavlushechka/a1qa/TestCase1.java | 10 ++-- 3 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java create mode 100644 src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java diff --git a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java deleted file mode 100644 index 17e2a87..0000000 --- a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.mavlushechka.a1qa.utils; - -import com.mavlushechka.a1qa.models.Post; -import com.mavlushechka.a1qa.models.User; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -public class JSONPlaceholderAPI { - - public static Post getPost(String spec) throws IOException { - return getObject(spec, Post.class); - } - - public static List getPosts(String spec) throws IOException { - return getObjects(spec, Post.class); - } - - public static Post postPost(String spec, Post post) throws IOException { - return postObject(spec, post, Post.class); - } - - public static User getUser(String spec) throws IOException { - return getObject(spec, User.class); - } - - public static List getUsers(String spec) throws IOException { - return getObjects(spec, User.class); - } - - public static User postUser(String spec, User user) throws IOException { - return postObject(spec, user, User.class); - } - - private static T getObject(String spec, Class classOfObject) throws IOException { - return JSONParser.convertToObject(URLConnectionManager.get(spec), classOfObject); - } - - private static List getObjects(String spec, Class classOfObject) throws IOException { - Object[] objects = JSONParser.convertArray(URLConnectionManager.get(spec), classOfObject); - ArrayList objectsArrayList = new ArrayList<>(); - - for (Object object : objects) { - objectsArrayList.add((T) object); - } - 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/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java new file mode 100644 index 0000000..2305daf --- /dev/null +++ b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java @@ -0,0 +1,54 @@ +package com.mavlushechka.a1qa.utils; + +import com.mavlushechka.a1qa.models.Post; +import com.mavlushechka.a1qa.models.User; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class JSONPlaceholderAPIManager { + + public static Post getPost(String spec) throws IOException { + return getObject(spec, Post.class); + } + + public static List getPosts(String spec) throws IOException { + return getObjects(spec, Post.class); + } + + public static Post postPost(String spec, Post post) throws IOException { + return postObject(spec, post, Post.class); + } + + public static User getUser(String spec) throws IOException { + return getObject(spec, User.class); + } + + public static List getUsers(String spec) throws IOException { + return getObjects(spec, User.class); + } + + public static User postUser(String spec, User user) throws IOException { + return postObject(spec, user, User.class); + } + + private static T getObject(String spec, Class classOfObject) throws IOException { + return JSONParser.convertToObject(URLConnectionManager.get(spec), classOfObject); + } + + private static List getObjects(String spec, Class classOfObject) throws IOException { + Object[] objects = JSONParser.convertArray(URLConnectionManager.get(spec), classOfObject); + ArrayList objectsArrayList = new ArrayList<>(); + + for (Object object : objects) { + objectsArrayList.add((T) object); + } + 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/test/java/com/mavlushechka/a1qa/TestCase1.java b/src/test/java/com/mavlushechka/a1qa/TestCase1.java index cd23dfd..7bcaae1 100644 --- a/src/test/java/com/mavlushechka/a1qa/TestCase1.java +++ b/src/test/java/com/mavlushechka/a1qa/TestCase1.java @@ -20,7 +20,7 @@ public class TestCase1 extends BaseTest { LoggerUtils.step("Send GET Request to get all posts (/posts)."); String postsJson = URLConnectionManager.get(postsSpec); Assert.assertTrue(JSONParser.isJson(postsJson), "Response is not json."); - List postsList = JSONPlaceholderAPI.getPosts(postsSpec); + List postsList = JSONPlaceholderAPIManager.getPosts(postsSpec); Assert.assertTrue(Posts.isPostsAscending(postsList), "Posts are not sorted ascending."); String post99Spec = JSONParser.parseData("testData", "testCases[0].steps[1].spec"); @@ -28,7 +28,7 @@ public class TestCase1 extends BaseTest { int post99ResponseCode = URLConnectionManager.getResponseCode(post99Spec, 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 = JSONPlaceholderAPI.getPost(post99Spec); + Post post99 = JSONPlaceholderAPIManager.getPost(post99Spec); 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."); @@ -58,14 +58,14 @@ public class TestCase1 extends BaseTest { int postResponseCode = URLConnectionManager.getResponseCode(postSpec, 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, JSONPlaceholderAPI.postPost(postSpec, post), "Post information is not correct."); + Assert.assertEquals(post, JSONPlaceholderAPIManager.postPost(postSpec, 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); 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 = JSONPlaceholderAPI.getUsers(usersSpec); + List users = JSONPlaceholderAPIManager.getUsers(usersSpec); User user = null; for (User userObject : users) { if (Integer.parseInt(userObject.id) == Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[4].user.id"))) { @@ -80,7 +80,7 @@ public class TestCase1 extends BaseTest { int user5ResponseCode = URLConnectionManager.getResponseCode(user5Spec, 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 = JSONPlaceholderAPI.getUser(user5Spec); + User user5 = JSONPlaceholderAPIManager.getUser(user5Spec); Assert.assertEquals(user, user5, "User information is not correct."); } -- cgit v1.2.3