Last active
March 18, 2016 16:26
-
-
Save marisks/29f0d4b197908006ae98 to your computer and use it in GitHub Desktop.
EPiServer Service API smoke tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Xunit; | |
public class ServiceApiSmokeTests | |
{ | |
readonly string _username = "admin"; | |
readonly string _password = "Myp@ssword1"; | |
[Fact] | |
public void it_can_authenticate_and_do_CRUD() | |
{ | |
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; | |
var warehouseCode = "default"; | |
var entryCode = "MY-PRODUCT"; | |
var integrationUrl = "https://myproject.localtest.me"; | |
var model = new WarehouseInventory | |
{ | |
AllowBackorder = true, | |
AllowPreorder = true, | |
BackorderAvailabilityDate = DateTime.UtcNow, | |
BackorderQuantity = 2, | |
CatalogEntryCode = entryCode, | |
InStockQuantity = 23, | |
InventoryStatus = "Enabled", | |
PreorderAvailabilityDate = DateTime.UtcNow, | |
PreorderQuantity = 3, | |
ReorderMinQuantity = 1, | |
ReservedQuantity = 1, | |
WarehouseCode = warehouseCode | |
}; | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(integrationUrl); | |
Authenticate(client); | |
GetCatalogs(client); | |
GetFirst10Entries(client); | |
GetEntry(entryCode, client); | |
Post(model, client); | |
Get(model, client); | |
Delete(model, client); | |
} | |
} | |
private void GetCatalogs(HttpClient client) | |
{ | |
var response = | |
client.GetAsync("/episerverapi/commerce/catalogs") | |
.Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Get Catalogs failed! Status: {response.StatusCode}"); | |
} | |
} | |
private void GetFirst10Entries(HttpClient client) | |
{ | |
var response = | |
client.GetAsync("/episerverapi/commerce/entries/1/10") | |
.Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Get first 10 Entries failed! Status: {response.StatusCode}"); | |
} | |
} | |
private static void GetEntry(string catalogEntryCode, HttpClient client) | |
{ | |
var response = | |
client.GetAsync($"/episerverapi/commerce/entries/{catalogEntryCode}") | |
.Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Get Entry failed! Status: {response.StatusCode}"); | |
} | |
} | |
private static void Get(WarehouseInventory model, HttpClient client) | |
{ | |
var response = | |
client.GetAsync($"/episerverapi/commerce/entries/{model.CatalogEntryCode}/inventories") | |
.Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Get failed! Status: {response.StatusCode}"); | |
} | |
} | |
private static void Delete(WarehouseInventory model, HttpClient client) | |
{ | |
var response = | |
client.DeleteAsync($"/episerverapi/commerce/entries/{model.CatalogEntryCode}/inventories/{model.WarehouseCode}") | |
.Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Delete failed! Status: {response.StatusCode}"); | |
} | |
} | |
private static void Post(WarehouseInventory model, HttpClient client) | |
{ | |
var json = JsonConvert.SerializeObject(model); | |
var response = | |
client.PostAsync($"/episerverapi/commerce/entries/{model.CatalogEntryCode}/inventories", | |
new StringContent(json, Encoding.UTF8, "application/json")) | |
.Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Post failed! Status: {response.StatusCode}"); | |
} | |
} | |
private void Authenticate(HttpClient client) | |
{ | |
var fields = new Dictionary<string, string> | |
{ | |
{ "grant_type", "password" }, | |
{ "username", _username }, | |
{ "password", _password } | |
}; | |
var response = client.PostAsync("/episerverapi/token", new FormUrlEncodedContent(fields)).Result; | |
if (!response.IsSuccessStatusCode) | |
{ | |
throw new Exception($"Authentication failed! Status: {response.StatusCode}"); | |
} | |
var content = response.Content.ReadAsStringAsync().Result; | |
var token = JObject.Parse(content).GetValue("access_token").ToString(); | |
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); | |
} | |
} | |
[Serializable] | |
public class WarehouseInventory | |
{ | |
public string CatalogEntryCode { get; set; } | |
public Guid ApplicationId { get; set; } | |
public string WarehouseCode { get; set; } | |
public string WarehouseLink { get; set; } | |
public decimal InStockQuantity { get; set; } | |
public decimal ReservedQuantity { get; set; } | |
public decimal ReorderMinQuantity { get; set; } | |
public decimal PreorderQuantity { get; set; } | |
public decimal BackorderQuantity { get; set; } | |
public bool AllowBackorder { get; set; } | |
public bool AllowPreorder { get; set; } | |
public string InventoryStatus { get; set; } | |
public DateTime? PreorderAvailabilityDate { get; set; } | |
public DateTime? BackorderAvailabilityDate { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment