diff --git a/explain b/explain new file mode 100644 index 0000000..3ba0d43 --- /dev/null +++ b/explain @@ -0,0 +1,21 @@ +$touch test-area +$nano test-area +$ git add test-area +$ git commit -m "create test area" +$ git checkout -b branch-for-conflict #2nd branch +$ nano test-area #1st change +$ git commit -a -m "add change A" +$ git checkout main +$ nano test-area #2nd branch +$ git commit -a -m "add change B" +$ git merge branch-for-conflict #try to merge + +merge conflict occured: + +Auto-merging test-area +CONFLICT (content): Merge conflict in test-area +Automatic merge failed; fix conflicts and then commit the result. + +$ nano test-area #solve merge conflict +$ git commit -a -m "merge into branch 'main' branch 'branch-for-conflict'" + diff --git a/src/main/java/apiclient/ApiClientUsers.java b/src/main/java/apiclient/ApiClientUsers.java new file mode 100644 index 0000000..47c23ca --- /dev/null +++ b/src/main/java/apiclient/ApiClientUsers.java @@ -0,0 +1,173 @@ +package apiclient; + +import config.Config; +import data.Endpoints; +import org.json.JSONObject; + + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; + +public class ApiClientUsers { + private static String usersUrl () {return Config.BASE_URL + Endpoints.USERS;} + private static String userUrl (int id) {return Config.BASE_URL + Endpoints.USERS + "/" + id;} + private static String registerUrl () {return Config.BASE_URL + Endpoints.REGISTER;} + private static String loginUrl () {return Config.BASE_URL + Endpoints.LOGIN;} + + private static HttpURLConnection usersResource () throws IOException { + URL url = new URL (usersUrl()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + return connection; + } + + + + private static HttpURLConnection userResource (int id) throws IOException { + + URL url = new URL(userUrl(id)); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + return connection; + } + + private static HttpURLConnection registerResource () throws IOException { + URL url = new URL(registerUrl()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + return connection; + } + private static HttpURLConnection loginResource () throws IOException { + URL url = new URL(loginUrl()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + return connection; + } + + private static String rawResponse (HttpURLConnection connection) throws IOException { + InputStream stream = connection.getErrorStream(); + if (stream == null) stream = connection.getInputStream(); + String line; + StringBuilder response = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + + + return response.toString(); + } + + private static JSONObject jsonResponse (HttpURLConnection connection) throws IOException { + JSONObject response = new JSONObject(rawResponse(connection)); + + return response; + } + + private static void post (HttpURLConnection connection, String body) throws IOException { + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + try(OutputStream os = connection.getOutputStream()) { + byte[] input = body.getBytes("utf-8"); + for (int i = 0; i < input.length; i++) { + os.write(input[i]); + } + } + } + + private static void put (HttpURLConnection connection, String body) throws IOException { + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + connection.setRequestMethod("PUT"); + try(OutputStream os = connection.getOutputStream()) { + byte[] input = body.getBytes("utf-8"); + for (int i = 0; i < input.length; i++) { + os.write(input[i]); + } + } + } + + + + + public Response getUser (int id) throws IOException { + HttpURLConnection connection = userResource(id); + connection.setRequestMethod("GET"); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode(), jsonResponse(connection)); + + return response; + } + + public Response getUsers () throws IOException { + HttpURLConnection connection = usersResource(); + connection.setRequestMethod("GET"); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode(), jsonResponse(connection)); + + return response; + } + + public Response createUser (String name, String job ) throws IOException { + HttpURLConnection connection = usersResource(); + post(connection, "{\"name\": \"" + name + "\", \"job\": \""+job+"\"}"); + //System.out.println("Status code: "+connection.getResponseCode()); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode(), jsonResponse(connection)); + + return response; + } + + public Response updateUserPut (int id, String name, String job ) throws IOException { + HttpURLConnection connection = userResource(id); + put(connection, "{\"name\": \"" + name + "\", \"job\": \""+job+"\"}"); + //System.out.println("Status code: "+connection.getResponseCode()); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode(), jsonResponse(connection)); + + return response; + } + + public Response register (String email, String password) throws IOException { + HttpURLConnection connection = registerResource(); + post(connection, "{\"email\": \"" + email + "\", \"password\": \""+password+"\"}"); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode(), jsonResponse(connection)); + + return response; + } + + public Response login (String email, String password) throws IOException { + HttpURLConnection connection = loginResource(); + post(connection, "{\"email\": \"" + email + "\", \"password\": \""+password+"\"}"); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode(), jsonResponse(connection)); + + return response; + } + + public Response deleteUser (int id) throws IOException { + HttpURLConnection connection = userResource(id); + connection.setRequestMethod("DELETE"); + connection.disconnect(); + + Response response = new Response(connection.getResponseCode()); + + return response; + } + + + +} diff --git a/src/main/java/apiclient/Response.java b/src/main/java/apiclient/Response.java new file mode 100644 index 0000000..c853362 --- /dev/null +++ b/src/main/java/apiclient/Response.java @@ -0,0 +1,17 @@ +package apiclient; + +import org.json.JSONObject; + +public class Response { + public int statusCode; + public JSONObject body; + + public Response (int statusCode) { + this.statusCode = statusCode; + } + + public Response (int statusCode, JSONObject body) { + this.statusCode = statusCode; + this.body = body; + } +} diff --git a/src/main/java/config/Config.java b/src/main/java/config/Config.java new file mode 100644 index 0000000..03c9fba --- /dev/null +++ b/src/main/java/config/Config.java @@ -0,0 +1,5 @@ +package config; + +public class Config { + public static final String BASE_URL = "https://reqres.in"; +} diff --git a/src/main/java/data/Endpoints.java b/src/main/java/data/Endpoints.java new file mode 100644 index 0000000..61ab46c --- /dev/null +++ b/src/main/java/data/Endpoints.java @@ -0,0 +1,7 @@ +package data; + +public class Endpoints { + public static final String USERS = "/api/users"; + public static final String REGISTER = "/api/register"; + public static final String LOGIN = "/api/login"; +} diff --git a/src/main/java/test/TestUsers.java b/src/main/java/test/TestUsers.java new file mode 100644 index 0000000..2aabe1b --- /dev/null +++ b/src/main/java/test/TestUsers.java @@ -0,0 +1,212 @@ +package test; + +import apiclient.ApiClientUsers; +import apiclient.Response; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +public class TestUsers { + public static void main(String[] args) { + try { + //positive + test.itCanCreateUser(1, true, "Yarik", "QA"); + test.itCanUpdateUser(2, true, 3, "Oleg", "support"); + test.itCanDeleteUser(3, true, 3); + test.itCanGetUser(4, true, 2); + test.itCanGetUsers(5, true); + test.itCanRegister(6, true, "eve.holt@reqres.in", "password123"); + test.itCanLogin(7, true, "eve.holt@reqres.in", "password123"); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + static ApiClientUsers api = new ApiClientUsers(); + static TestUsers test = new TestUsers(); + + + @Test + public void itCanCreateUser (int testId, boolean logsOn, String name, String job) throws IOException { + //data + Response user = api.createUser(name, job); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(user); + } + + //asserts + assertCode(user, 201); + Assert.assertEquals(name, user.body.getString("name")); + Assert.assertEquals(job, user.body.getString("job")); + Assert.assertTrue(user.body.toString().contains("\"id\"")); + Assert.assertTrue(user.body.toString().contains("\"createdAt\"")); + + //report + passed(testId); + + + } + + @Test + public void itCanUpdateUser (int testId, boolean logsOn, int userId, String name, String job) throws IOException { + //data + Response user = api.updateUserPut(userId, name, job); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(user); + } + + //asserts + assertCode(user, 200); + Assert.assertEquals(name, user.body.getString("name")); + Assert.assertEquals(job, user.body.getString("job")); + Assert.assertTrue(user.body.toString().contains("\"updatedAt\"")); + + //report + passed(testId); + } + + public void itCanDeleteUser (int testId, boolean logsOn, int userId) throws IOException { + //data + Response user = api.deleteUser(userId); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(user); + } + + //asserts + assertCode(user, 204); + + //report + passed(testId); + } + + public void itCanGetUser (int testId, boolean logsOn, int userId) throws IOException { + //data + Response user = api.getUser(userId); + JSONObject data = user.body.getJSONObject("data"); + JSONObject support = user.body.getJSONObject("support"); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(user); + } + + //asserts + assertCode(user, 200); + dataValidate(data); + supportValidate(support); + + //report + passed(testId); + } + + public void itCanGetUsers (int testId, boolean logsOn) throws IOException { + //data + Response users = api.getUsers(); + JSONArray data = users.body.getJSONArray("data"); + JSONObject support = users.body.getJSONObject("support"); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(users); + } + + //asserts + assertCode(users, 200); + for (int i = 0; i < data.length(); i++) { + dataValidate(data.getJSONObject(i)); + } + supportValidate(support); + + //report + passed(testId); + + } + + public void itCanRegister (int testId, boolean logsOn, String email, String password) throws IOException { + //data + Response user = api.register(email, password); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(user); + } + + //asserts + assertCode(user, 200); + Assert.assertTrue(user.body.toString().contains("id")); + Assert.assertTrue(user.body.toString().contains("token")); + + //report + passed(testId); + + } + + public void itCanLogin (int testId, boolean logsOn, String email, String password) throws IOException { + //data + Response user = api.login(email, password); + + //logs + if (logsOn) { + System.out.print(testId+": "); + responseLog(user); + } + + //asserts + assertCode(user, 200); + Assert.assertTrue(user.body.toString().contains("token")); + + //report + passed(testId); + + } + + + + + + + + static void assertCode (Response resp, int code) { + Assert.assertEquals(resp.statusCode, code); + } + + static void dataValidate(JSONObject data) { + Assert.assertTrue(data.toString().contains("id")); + Assert.assertTrue(data.toString().contains("email")); + Assert.assertTrue(data.toString().contains("first_name")); + Assert.assertTrue(data.toString().contains("last_name")); + Assert.assertTrue(data.toString().contains("avatar")); + } + + static void supportValidate (JSONObject support) { + Assert.assertTrue(support.toString().contains("url")); + Assert.assertTrue(support.toString().contains("text")); + + } + + static void passed (int testId) { + System.out.println(testId +": passed"); + } + + static void responseLog (Response response) { + System.out.println(response.body); + } + + +} diff --git a/test-area b/test-area new file mode 100644 index 0000000..6502e9c --- /dev/null +++ b/test-area @@ -0,0 +1,3 @@ +There is a place where I will make some work. +I triggered a merge conflict and decided to leane only this cnahge: +*make change B for triggering merge conflict*