|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using MicroserviceAutomation.Helper; |
| 7 | +using MicroserviceAutomation.Model; |
| 8 | +using MicroserviceAutomation.Model.JsonModelGetAll; |
| 9 | +using MicroserviceAutomation.Model.JsonModelGetUniqueUser; |
| 10 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 11 | +using Newtonsoft.Json; |
| 12 | + |
| 13 | +namespace MicroserviceAutomation.GetEndPoint |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class TestGetEndpoint |
| 17 | + { |
| 18 | + private string baseUrl = "https://gorest.co.in/public/v1/users/"; |
| 19 | + private string getInvalidUserUrl = "https://gorest.co.in/public/v1/users/20063"; |
| 20 | + private string jsonMediaType = "application/json"; |
| 21 | + private string authHeader = AuthToken.Auth; |
| 22 | + private RestResponse restResponse; |
| 23 | + private Random random = new Random(); |
| 24 | + private int userIdPost; |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void TestAssertGetAllUsers_shouldBe200() |
| 28 | + { |
| 29 | + using (HttpClient httpClient = new HttpClient()) |
| 30 | + { |
| 31 | + using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage()) |
| 32 | + { |
| 33 | + httpRequestMessage.RequestUri = new Uri(baseUrl); |
| 34 | + httpRequestMessage.Method = HttpMethod.Get; |
| 35 | + httpRequestMessage.Headers.Add("Accept", "application/json"); |
| 36 | + |
| 37 | + Task<HttpResponseMessage> httpResponse = httpClient.SendAsync(httpRequestMessage); |
| 38 | + |
| 39 | + using (HttpResponseMessage httpResponseMessage = httpResponse.Result) |
| 40 | + { |
| 41 | + Console.WriteLine(httpResponseMessage.ToString()); |
| 42 | + |
| 43 | + HttpStatusCode statusCode = httpResponseMessage.StatusCode; |
| 44 | + |
| 45 | + HttpContent responseContent = httpResponseMessage.Content; |
| 46 | + Task<string> responseData = responseContent.ReadAsStringAsync(); |
| 47 | + string data = responseData.Result; |
| 48 | + |
| 49 | + RestResponse restResponse = new RestResponse((int)statusCode, responseData.Result); |
| 50 | + |
| 51 | + JsonRootObjectGetAll jsonRootObjectGetAll = JsonConvert.DeserializeObject<JsonRootObjectGetAll>(restResponse.ResponseContent); |
| 52 | + Console.WriteLine(jsonRootObjectGetAll.ToString()); |
| 53 | + |
| 54 | + Assert.AreEqual(200, restResponse.StatusCode); |
| 55 | + |
| 56 | + Assert.IsNotNull(restResponse.ResponseContent); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public void TestAssertGetUniqueUser_shouldBe200() |
| 64 | + { |
| 65 | + AddRecord(); |
| 66 | + using (HttpClient httpClient = new HttpClient()) |
| 67 | + { |
| 68 | + using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage()) |
| 69 | + { |
| 70 | + httpRequestMessage.RequestUri = new Uri(baseUrl + userIdPost); |
| 71 | + httpRequestMessage.Method = HttpMethod.Get; |
| 72 | + httpRequestMessage.Headers.Add("Accept", "application/json"); |
| 73 | + |
| 74 | + Task<HttpResponseMessage> httpResponse = httpClient.SendAsync(httpRequestMessage); |
| 75 | + |
| 76 | + using (HttpResponseMessage httpResponseMessage = httpResponse.Result) |
| 77 | + { |
| 78 | + Console.WriteLine(httpResponseMessage.ToString()); |
| 79 | + |
| 80 | + HttpStatusCode statusCode = httpResponseMessage.StatusCode; |
| 81 | + |
| 82 | + HttpContent responseContent = httpResponseMessage.Content; |
| 83 | + Task<string> responseData = responseContent.ReadAsStringAsync(); |
| 84 | + string data = responseData.Result; |
| 85 | + |
| 86 | + RestResponse restResponse = new RestResponse((int)statusCode, responseData.Result); |
| 87 | + |
| 88 | + JsonRootObjectGetUniqueUser JsonRootObjectGetUniqueUser = JsonConvert.DeserializeObject<JsonRootObjectGetUniqueUser>(restResponse.ResponseContent); |
| 89 | + Console.WriteLine(JsonRootObjectGetUniqueUser.ToString()); |
| 90 | + |
| 91 | + Assert.AreEqual(200, restResponse.StatusCode); |
| 92 | + |
| 93 | + Assert.IsNotNull(restResponse.ResponseContent); |
| 94 | + |
| 95 | + Assert.AreEqual(userIdPost, JsonRootObjectGetUniqueUser.data.id); |
| 96 | + Assert.AreEqual("Catarina Nina Moraes", JsonRootObjectGetUniqueUser.data.name); |
| 97 | + Assert.AreEqual("female", JsonRootObjectGetUniqueUser.data.gender); |
| 98 | + Assert.AreEqual("active", JsonRootObjectGetUniqueUser.data.status); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public void AddRecord() |
| 105 | + { |
| 106 | + int randonEmailNumber = random.Next(999); |
| 107 | + var jsonData = new |
| 108 | + { |
| 109 | + name = "Catarina Nina Moraes", |
| 110 | + gender = "female", |
| 111 | + email = $"catarinaninamoraes{randonEmailNumber}@autvale.com", |
| 112 | + status = "active" |
| 113 | + }; |
| 114 | + |
| 115 | + using (HttpClient httpClient = new HttpClient()) |
| 116 | + { |
| 117 | + using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage()) |
| 118 | + { |
| 119 | + |
| 120 | + httpClient.DefaultRequestHeaders.Add("Accept", jsonMediaType); |
| 121 | + httpClient.DefaultRequestHeaders.Add("Authorization", authHeader); |
| 122 | + |
| 123 | + httpRequestMessage.Method = HttpMethod.Post; |
| 124 | + httpRequestMessage.RequestUri = new Uri(baseUrl); |
| 125 | + httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(jsonData), Encoding.UTF8, "application/json"); |
| 126 | + |
| 127 | + Task<HttpResponseMessage> httpResponseMessage = httpClient.SendAsync(httpRequestMessage); |
| 128 | + |
| 129 | + restResponse = new RestResponse((int)httpResponseMessage.Result.StatusCode, httpResponseMessage.Result.Content.ReadAsStringAsync().Result); |
| 130 | + |
| 131 | + JsonRootObjectGetUniqueUser JsonRootObjectGetUniqueUser = JsonConvert.DeserializeObject<JsonRootObjectGetUniqueUser>(restResponse.ResponseContent); |
| 132 | + Assert.AreEqual(201, restResponse.StatusCode); |
| 133 | + |
| 134 | + userIdPost = JsonRootObjectGetUniqueUser.data.id; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void TestAssertGetInvalidUser_shouldBe404() |
| 141 | + { |
| 142 | + |
| 143 | + using (HttpClient httpClient = new HttpClient()) |
| 144 | + { |
| 145 | + using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage()) |
| 146 | + { |
| 147 | + httpRequestMessage.RequestUri = new Uri(getInvalidUserUrl); |
| 148 | + httpRequestMessage.Method = HttpMethod.Get; |
| 149 | + httpRequestMessage.Headers.Add("Accept", "application/json"); |
| 150 | + |
| 151 | + Task<HttpResponseMessage> httpResponse = httpClient.SendAsync(httpRequestMessage); |
| 152 | + |
| 153 | + using (HttpResponseMessage httpResponseMessage = httpResponse.Result) |
| 154 | + { |
| 155 | + Console.WriteLine(httpResponseMessage.ToString()); |
| 156 | + |
| 157 | + HttpStatusCode statusCode = httpResponseMessage.StatusCode; |
| 158 | + |
| 159 | + HttpContent responseContent = httpResponseMessage.Content; |
| 160 | + Task<string> responseData = responseContent.ReadAsStringAsync(); |
| 161 | + string data = responseData.Result; |
| 162 | + |
| 163 | + RestResponse restResponse = new RestResponse((int)statusCode, responseData.Result); |
| 164 | + |
| 165 | + JsonRootObjectGetUniqueUser JsonRootObjectGetUniqueUser = JsonConvert.DeserializeObject<JsonRootObjectGetUniqueUser>(restResponse.ResponseContent); |
| 166 | + Console.WriteLine(JsonRootObjectGetUniqueUser.ToString()); |
| 167 | + |
| 168 | + Assert.AreEqual(404, restResponse.StatusCode); |
| 169 | + |
| 170 | + Assert.AreEqual("Resource not found", JsonRootObjectGetUniqueUser.data.message); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments