using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace raven_integration
{
public class PickListAllTests
{
//NOTE: in order not to interfere in each other will use Widget picklist to test with standard unmodified picklist template
//and will use User PickList to test the template functionality
///
/// Test all Template editing related routes
///
[Fact]
public async void UserPickListTemplates()
{
//NOTE: Due to there being only one possible template per type, this test will need to test ALL potential tests in one function
//and only for the User picklist in order to not step over other potential tests running in parallel
//REPLACE USER TEMPLATE
dynamic d = new JObject();
d.Id = 3;//User type
//template, simple test, nothing fancy
dynamic dTemplateArray = new JArray();
dynamic df = new JObject();
df.fld = "useremployeenumber";
dTemplateArray.Add(df);
d.Template = dTemplateArray.ToString(Newtonsoft.Json.Formatting.None);
//replace the User template at the server
ApiResponse a = await Util.PostAsync("PickList/Template", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
Util.ValidateHTTPStatusCode(a, 204);
//RETRIEVE
//Get one
a = await Util.GetAsync("PickList/Template/3/", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateDataReturnResponseOk(a);
//assert contains ONE record ONLY and it's the one we set
var templateArray = JArray.Parse(a.ObjectResponse["data"]["template"].Value());
templateArray.Count.Should().Be(1);
templateArray[0]["fld"].Value().Should().Be("useremployeenumber"); ;
// RESET TO DEFAULT
a = await Util.DeleteAsync("PickList/Template/3/", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateHTTPStatusCode(a, 204);
//RETRIEVE (Confirm it's back to default)
//Get one
a = await Util.GetAsync("PickList/Template/3/", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateDataReturnResponseOk(a);
//assert contains default template record ONLY and it's the one we set
templateArray = JArray.Parse(a.ObjectResponse["data"]["template"].Value());
templateArray.Count.Should().Be(3);
templateArray[0]["fld"].Value().Should().Be("username"); ;
//Now test error conditions....
//BAD FIELD NAME VALIDATION ERROR
d = new JObject();
d.Id = 3;//User type
//template, simple test, nothing fancy
dTemplateArray = new JArray();
df = new JObject();
df.fld = "DOES_NOT_EXIST";//<-- ERROR BAD FIELD NAME
dTemplateArray.Add(df);
d.Template = dTemplateArray.ToString(Newtonsoft.Json.Formatting.None);
a = await Util.PostAsync("PickList/Template", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
//"{\"error\":{\"code\":\"2200\",\"details\":[{\"message\":\"Template array item 0, fld property value \\\"DOES_NOT_EXIST\\\" is not a valid value for AyaType specified\",\"target\":\"Template\",\"error\":\"2203\"}],\"message\":\"Object did not pass validation\"}}"
Util.ValidateErrorCodeResponse(a, 2200, 400);
Util.ShouldContainValidationError(a, "Template", "2203");
//BAD AYATYPE ERROR
d = new JObject();
d.Id = 0;//<==ERROR NO_TYPE, INVALID
//template, simple test, nothing fancy
dTemplateArray = new JArray();
df = new JObject();
df.fld = "useremployeenumber";
dTemplateArray.Add(df);
d.Template = dTemplateArray.ToString(Newtonsoft.Json.Formatting.None);
//replace the User template at the server
a = await Util.PostAsync("PickList/Template", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
//"{\"error\":{\"code\":\"2200\",\"details\":[{\"message\":\"Template array item 0, fld property value \\\"DOES_NOT_EXIST\\\" is not a valid value for AyaType specified\",\"target\":\"Template\",\"error\":\"2203\"}],\"message\":\"Object did not pass validation\"}}"
Util.ValidateErrorCodeResponse(a, 2200, 400);
Util.ShouldContainValidationError(a, "ayaType", "2203");
//RIGHTS ISSUE,
//currently only bizadminfull can change a picklist template
d = new JObject();
d.Id = 3;//User
//template, simple test, nothing fancy
dTemplateArray = new JArray();
df = new JObject();
df.fld = "useremployeenumber";
dTemplateArray.Add(df);
d.Template = dTemplateArray.ToString(Newtonsoft.Json.Formatting.None);
//ERROR NO RIGHTS USER
a = await Util.PostAsync("PickList/Template", await Util.GetTokenAsync("CustomerLimited"), d.ToString(Newtonsoft.Json.Formatting.None));
//"{\"error\":{\"code\":\"2004\",\"message\":\"User not authorized for this resource operation (insufficient rights)\"}}"
Util.ValidateErrorCodeResponse(a, 2004, 403);
//EMPTY TEMPLATE VALIDATION ERROR
d = new JObject();
d.Id = 3;//User
d.Template = "";//<-- ERROR no template
a = await Util.PostAsync("PickList/Template", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
Util.ValidateErrorCodeResponse(a, 2200, 400);
Util.ShouldContainValidationError(a, "Template", "2201");
//MALFORMED TEMPLATE JSON ERROR
d = new JObject();
d.Id = 3;//User type
dTemplateArray = new JArray();
df = new JObject();
df.fld = "useremployeenumber";
dTemplateArray.Add(df);
string sTemplate = dTemplateArray.ToString(Newtonsoft.Json.Formatting.None);
d.Template = sTemplate.Substring(2);//<-- ERROR missing first two characters of json template array
a = await Util.PostAsync("PickList/Template", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
//"{\"error\":{\"code\":\"2200\",\"details\":[{\"message\":\"Template is not valid JSON string: Error reading JArray from JsonReader. Current JsonReader item is not an array: String. Path '', line 1, position 5.\",\"target\":\"Template\",\"error\":\"2203\"}],\"message\":\"Object did not pass validation\"}}"
Util.ValidateErrorCodeResponse(a, 2200, 400);
Util.ShouldContainValidationError(a, "Template", "2203");
}
///
/// test get templates list
///
[Fact]
public async void UserPickListTemplateList()
{
//RETRIEVE
ApiResponse a = await Util.GetAsync("PickList/TemplateList", await Util.GetTokenAsync("BizAdminFull"));
Util.ValidateDataReturnResponseOk(a);
//assert contains at least two records (as we only have two at time of writing this test)
var templateList = ((JArray)a.ObjectResponse["data"]);
templateList.Count.Should().BeGreaterThan(1);
templateList[0]["id"].Value().Should().Be(2);//first one should be a widget
}
// ///
// ///
// ///
// [Fact]
// public async void InvalidListKeyShouldFail()
// {
// //CREATE
// dynamic d = new JObject();
// d.name = Util.Uniquify("Test DataListView");
// d["public"] = true;
// d.listKey = "nonexistant";
// //"[{fld:"name",op:"!=",value:"Notequaltothis"},{fld:"tags",op:"Eq",value:"[23,456,54]"}]
// // dynamic dfilter = new JArray();
// // dynamic df = new JObject();
// // df.fld = "name";
// // df.op = "%-";
// // df.value = "Generic";//lots of seed widgets start with Generic
// // dfilter.Add(df);
// // d.filter = dfilter.ToString();//it expects it to be a json string, not actual json
// // ApiResponse a = await Util.PostAsync("DataListView", await Util.GetTokenAsync("BizAdminFull"), d.ToString());
// dynamic dListView = new JArray();
// // dynamic df = new JObject();
// // df.fld = "widgetname";
// // df.op = "%-";
// // df.value = "Generic";//lots of seed widgets start with Generic
// // dListView.Add(df);
// // d.filter = dListView.ToString();//it expects it to be a json string, not actual json
// dListView.Add(Util.BuildSimpleFilterDataListViewColumn("widgetname", Util.OpStartsWith, "Generic"));
// d.listView=dListView.ToString(Newtonsoft.Json.Formatting.None);
// ApiResponse a = await Util.PostAsync("DataListView", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
// Util.ValidateErrorCodeResponse(a, 2200, 400);
// Util.ShouldContainValidationError(a, "ListKey", "2203");
// }
// ///
// ///
// ///
// [Fact]
// public async void InvalidFieldNameShouldFail()
// {
// //CREATE
// dynamic d = new JObject();
// d.name = Util.Uniquify("Test DataListView");
// d["public"] = true;
// d.listKey="TestWidgetDataList";
// //"[{fld:"name",op:"!=",value:"Notequaltothis"},{fld:"tags",op:"Eq",value:"[23,456,54]"}]
// // dynamic dfilter = new JArray();
// // dynamic df = new JObject();
// // df.fld = "doesntexist";
// // df.op = "%-";
// // df.value = "Generic";//lots of seed widgets start with Generic
// // dfilter.Add(df);
// // d.filter = dfilter.ToString();//it expects it to be a json string, not actual json
// // ApiResponse a = await Util.PostAsync("DataListView", await Util.GetTokenAsync("BizAdminFull"), d.ToString());
// dynamic dListView = new JArray();
// // dynamic df = new JObject();
// // df.fld = "widgetname";
// // df.op = "%-";
// // df.value = "Generic";//lots of seed widgets start with Generic
// // dListView.Add(df);
// // d.filter = dListView.ToString();//it expects it to be a json string, not actual json
// dListView.Add(Util.BuildSimpleFilterDataListViewColumn("doesntexist", Util.OpStartsWith, "Generic"));
// d.listView=dListView.ToString(Newtonsoft.Json.Formatting.None);
// ApiResponse a = await Util.PostAsync("DataListView", await Util.GetTokenAsync("BizAdminFull"), d.ToString(Newtonsoft.Json.Formatting.None));
// Util.ValidateErrorCodeResponse(a, 2200, 400);
// Util.ShouldContainValidationError(a, "ListView", "2203");
// }
// //REMOVED THIS VALIDATION TEST AS SWITCH TO DATALISTVIEW NO LONGER VALIDATES FILTER ANYWAY
// //ONLY COLUMN NAMES AND LIST NAME
// // ///
// // ///
// // ///
// // [Fact]
// // public async void InvalidOperatorShouldFail()
// // {
// // //CREATE
// // dynamic d = new JObject();
// // d.name = Util.Uniquify("Test DataListView");
// // d["public"] = true;
// // d.listKey="TestWidgetDataList";
// // //"[{fld:"name",op:"!=",value:"Notequaltothis"},{fld:"tags",op:"Eq",value:"[23,456,54]"}]
// // dynamic dfilter = new JArray();
// // dynamic df = new JObject();
// // df.fld = "name";
// // df.op = "wtf";
// // df.value = "Generic";//lots of seed widgets start with Generic
// // dfilter.Add(df);
// // d.filter = dfilter.ToString();//it expects it to be a json string, not actual json
// // ApiResponse a = await Util.PostAsync("DataListView", await Util.GetTokenAsync("BizAdminFull"), d.ToString());
// // Util.ValidateErrorCodeResponse(a, 2200, 400);
// // Util.ShouldContainValidationError(a, "Filter", "2203");
// // }
//==================================================
}//eoc
}//eons