diff options
Diffstat (limited to 'src')
6 files changed, 25 insertions, 26 deletions
diff --git a/src/main/java/com/mavlushechka/a1qa/constants/RequestMethod.java b/src/main/java/com/mavlushechka/a1qa/constants/RequestMethod.java new file mode 100644 index 0000000..0f7e15c --- /dev/null +++ b/src/main/java/com/mavlushechka/a1qa/constants/RequestMethod.java @@ -0,0 +1,7 @@ +package com.mavlushechka.a1qa.constants; + +public enum RequestMethod { + + GET, POST + +} diff --git a/src/main/java/com/mavlushechka/a1qa/driverUtils/HttpURLConnectionFactory.java b/src/main/java/com/mavlushechka/a1qa/driverUtils/HttpURLConnectionFactory.java index 6b60b64..13145c4 100644 --- a/src/main/java/com/mavlushechka/a1qa/driverUtils/HttpURLConnectionFactory.java +++ b/src/main/java/com/mavlushechka/a1qa/driverUtils/HttpURLConnectionFactory.java @@ -1,5 +1,6 @@ package com.mavlushechka.a1qa.driverUtils; +import com.mavlushechka.a1qa.constants.RequestMethod; import com.mavlushechka.a1qa.utils.JSONParser; import java.io.IOException; @@ -11,10 +12,10 @@ public class HttpURLConnectionFactory { private HttpURLConnectionFactory() { } - public static HttpURLConnection createHttpURLConnection(String spec, String requestMethod, boolean doOutput) throws IOException { + public static HttpURLConnection createHttpURLConnection(String spec, RequestMethod requestMethod, boolean doOutput) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(JSONParser.parseData("config", "browser.url") + spec).openConnection(); - httpURLConnection.setRequestMethod(requestMethod); + httpURLConnection.setRequestMethod(requestMethod.name()); httpURLConnection.setConnectTimeout(Integer.parseInt(JSONParser.parseData("config", "httpURLConnection.connectTimeout"))); httpURLConnection.setReadTimeout(Integer.parseInt(JSONParser.parseData("config", "httpURLConnection.readTimeout"))); httpURLConnection.setDoOutput(doOutput); diff --git a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java index 464c1af..54df569 100644 --- a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java +++ b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPIManager.java @@ -1,6 +1,7 @@ package com.mavlushechka.a1qa.utils; import com.mavlushechka.a1qa.constants.Endpoint; +import com.mavlushechka.a1qa.constants.RequestMethod; import com.mavlushechka.a1qa.models.Post; import com.mavlushechka.a1qa.models.User; @@ -18,7 +19,7 @@ public class JSONPlaceholderAPIManager { return JSONParser.convertToObject(URLConnectionManager.get(postsSpec + "/" + id), Post.class); } - public static int getPostResponseCode(int id, String requestMethod) throws IOException { + public static int getPostResponseCode(int id, RequestMethod requestMethod) throws IOException { return URLConnectionManager.getResponseCode(postsSpec + "/" + id, requestMethod); } @@ -26,7 +27,7 @@ public class JSONPlaceholderAPIManager { return getObjects(postsSpec, Post.class); } - public static int getPostsResponseCode(String requestMethod) throws IOException { + public static int getPostsResponseCode(RequestMethod requestMethod) throws IOException { return URLConnectionManager.getResponseCode(postsSpec, requestMethod); } @@ -42,7 +43,7 @@ public class JSONPlaceholderAPIManager { return getObjects(usersSpec, User.class); } - public static int getUsersResponseCode(String requestMethod) throws IOException { + public static int getUsersResponseCode(RequestMethod requestMethod) throws IOException { return URLConnectionManager.getResponseCode(usersSpec, requestMethod); } diff --git a/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java b/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java index 286df12..c6c9fae 100644 --- a/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java +++ b/src/main/java/com/mavlushechka/a1qa/utils/URLConnectionManager.java @@ -1,5 +1,6 @@ package com.mavlushechka.a1qa.utils; +import com.mavlushechka.a1qa.constants.RequestMethod; import com.mavlushechka.a1qa.driverUtils.HttpURLConnectionFactory; import java.io.BufferedReader; @@ -13,7 +14,7 @@ public class URLConnectionManager { public static String get(String spec) throws IOException { StringBuilder stringBuilder = new StringBuilder(); - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(HttpURLConnectionFactory.createHttpURLConnection(spec, "GET", false).getInputStream()))) { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(HttpURLConnectionFactory.createHttpURLConnection(spec, RequestMethod.GET, false).getInputStream()))) { String inputLine; while ((inputLine = bufferedReader.readLine()) != null) { @@ -24,7 +25,7 @@ public class URLConnectionManager { } public static String post(String spec, String content) throws IOException { - HttpURLConnection httpURLConnection = HttpURLConnectionFactory.createHttpURLConnection(spec, "POST", true); + HttpURLConnection httpURLConnection = HttpURLConnectionFactory.createHttpURLConnection(spec, RequestMethod.POST, true); StringBuilder stringBuilder = new StringBuilder(); try (DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream())) { @@ -41,7 +42,7 @@ public class URLConnectionManager { return stringBuilder.toString(); } - public static int getResponseCode(String spec, String requestMethod) throws IOException { + public static int getResponseCode(String spec, RequestMethod requestMethod) throws IOException { return HttpURLConnectionFactory.createHttpURLConnection(spec, requestMethod, false).getResponseCode(); } diff --git a/src/main/resources/testData.json b/src/main/resources/testData.json index 6403d5b..b0e3aca 100644 --- a/src/main/resources/testData.json +++ b/src/main/resources/testData.json @@ -3,10 +3,8 @@ { "steps": [ { - "requestMethod": "GET" }, { - "requestMethod": "GET", "post": { "id": "99", "userId": "10", @@ -15,20 +13,17 @@ } }, { - "requestMethod": "GET", "isResponseBodyEmpty": "true", "post": { "id": "150" } }, { - "requestMethod": "POST", "post": { "userId": "1" } }, { - "requestMethod": "GET", "user": { "id": "5", "name": "Chelsey Dietrich", @@ -54,7 +49,6 @@ } }, { - "requestMethod": "GET", "user": { "id": "5" } diff --git a/src/test/java/com/mavlushechka/a1qa/TestCase1.java b/src/test/java/com/mavlushechka/a1qa/TestCase1.java index 65c0c17..d641dc2 100644 --- a/src/test/java/com/mavlushechka/a1qa/TestCase1.java +++ b/src/test/java/com/mavlushechka/a1qa/TestCase1.java @@ -1,6 +1,7 @@ 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; @@ -15,8 +16,7 @@ public class TestCase1 extends BaseTest { @Test public void test1() throws IOException { - String postsRequestMethod = JSONParser.parseData("testData", "testCases[0].steps[0].requestMethod"); - int postsResponseCode = JSONPlaceholderAPIManager.getPostsResponseCode(postsRequestMethod); + 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); @@ -25,8 +25,7 @@ public class TestCase1 extends BaseTest { Assert.assertTrue(Posts.isPostsAscending(postsList), "Posts are not sorted ascending."); 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 = JSONPlaceholderAPIManager.getPostResponseCode(post99Id, post99RequestMethod); + 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); @@ -36,8 +35,7 @@ public class TestCase1 extends BaseTest { 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")); - String post150RequestMethod = JSONParser.parseData("testData", "testCases[0].steps[2].requestMethod"); - int post150ResponseCode = JSONPlaceholderAPIManager.getPostResponseCode(post150Id, post150RequestMethod); + 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; @@ -48,7 +46,6 @@ public class TestCase1 extends BaseTest { } Assert.assertEquals(JSONParser.isBodyEmpty(post150Json), Boolean.parseBoolean(JSONParser.parseData("testData", "testCases[0].steps[2].isResponseBodyEmpty")), "Response body is not correct."); - 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")); int randomTextLength = Integer.parseInt(JSONParser.parseData("config", "randomTextGenerator.length")); @@ -58,13 +55,12 @@ public class TestCase1 extends BaseTest { StringUtils.generateRandomText(randomTextLettersLowerBound, randomTextLettersUpperBound, randomTextLength), JSONParser.parseData("testData", "testCases[0].steps[3].post.userId") ); - int postResponseCode = JSONPlaceholderAPIManager.getPostsResponseCode(postRequestMethod); + 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."); - String usersRequestMethod = JSONParser.parseData("testData", "testCases[0].steps[4].requestMethod"); - int usersResponseCode = JSONPlaceholderAPIManager.getUsersResponseCode(usersRequestMethod); + 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(); @@ -78,8 +74,7 @@ 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."); 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 = JSONPlaceholderAPIManager.getPostResponseCode(user5Id, user5RequestMethod); + 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"))); |