summaryrefslogtreecommitdiff
path: root/src/test/java/com/mavlushechka/a1qa/TestCase1.java
blob: d641dc2fecf0bca96d10be4ff5c034c427cd7647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<Post> 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<User> 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.");
    }

}