package com.mavlushechka.a1qa; import com.mavlushechka.a1qa.constants.Endpoint; import com.mavlushechka.a1qa.constants.RequestMethod; import com.mavlushechka.a1qa.constants.Status; import com.mavlushechka.a1qa.models.Post; import com.mavlushechka.a1qa.models.User; import com.mavlushechka.a1qa.utils.*; import org.testng.Assert; import org.testng.annotations.Test; import java.io.IOException; import java.util.List; public class TestCase1 extends BaseTest { @Test public void test1() throws IOException { int postsResponseCode = JSONPlaceholderAPIManager.getPostsResponseCode(RequestMethod.GET); Assert.assertEquals(postsResponseCode, Status.OK.code, "Response code is not correct."); LoggerUtils.step("Send GET Request to get all posts (/posts)."); String postsJson = URLConnectionManager.get(Endpoint.POSTS.spec); Assert.assertTrue(JSONParser.isJson(postsJson), "Response is not json."); List postsList = JSONPlaceholderAPIManager.getPosts(); Assert.assertTrue(Posts.isPostsAscending(postsList), "Posts are not sorted ascending."); int post99Id = Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[1].post.id")); int post99ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(post99Id, RequestMethod.GET); Assert.assertEquals(post99ResponseCode, Status.OK.code, "Response code is not correct."); LoggerUtils.step("Send GET request to get post with id=99 (/posts/99)."); 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."); int post150Id = Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[2].post.id")); int post150ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(post150Id, RequestMethod.GET); Assert.assertEquals(post150ResponseCode, Status.NOT_FOUND.code, "Response code is not correct."); LoggerUtils.step("Send GET request to get post with id=150 (/posts/150)."); String post150Json; try { post150Json = URLConnectionManager.get(Endpoint.POSTS.spec + "/" + 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."); int randomTextLettersLowerBound = Integer.parseInt(JSONParser.parseData("config", "randomTextGenerator.lettersLowerBound")); int randomTextLettersUpperBound = Integer.parseInt(JSONParser.parseData("config", "randomTextGenerator.lettersUpperBound")); int randomTextLength = Integer.parseInt(JSONParser.parseData("config", "randomTextGenerator.length")); Post post = new Post( String.valueOf(postsList.size()+1), StringUtils.generateRandomText(randomTextLettersLowerBound, randomTextLettersUpperBound, randomTextLength), StringUtils.generateRandomText(randomTextLettersLowerBound, randomTextLettersUpperBound, randomTextLength), JSONParser.parseData("testData", "testCases[0].steps[3].post.userId") ); int postResponseCode = JSONPlaceholderAPIManager.getPostsResponseCode(RequestMethod.POST); Assert.assertEquals(postResponseCode, Status.CREATED.code, "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."); int usersResponseCode = JSONPlaceholderAPIManager.getUsersResponseCode(RequestMethod.GET); Assert.assertEquals(usersResponseCode, Status.OK.code, "Response code is not correct."); LoggerUtils.step("Send GET request to get users (/users)."); List users = JSONPlaceholderAPIManager.getUsers(); User user = null; for (User userObject : users) { if (Integer.parseInt(userObject.id) == Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[4].user.id"))) { user = userObject; break; } } Assert.assertEquals(JSONParser.convertToObject(JSONParser.parseObject("testData", "testCases[0].steps[4].user"), User.class), user, "User information is not correct."); int user5Id = Integer.parseInt(JSONParser.parseData("testData", "testCases[0].steps[5].user.id")); int user5ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(user5Id, RequestMethod.GET); Assert.assertEquals(user5ResponseCode, Status.OK.code, "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"))); Assert.assertEquals(user, user5, "User information is not correct."); } }