For a project I am using a bunch of custom Personalization rules, the only problem I am facing at the moment is that it takes quite a while to load the rules and I can't find any good examples of a unit test for a custom rule like one of these:
public class TemperatureHigherThanRule<T> : StringOperatorCondition<T> where T : RuleContext
{
public string Value { get; set; }
protected override bool Execute(T ruleContext)
{
////Convert the string Value to and int
int intValue = Convert.ToInt32(Value);
////Get the current weather with the API
HttpClient httpClient = new HttpClient();
var response = httpClient.GetAsync("http://api.openweathermap.org/data/2.5/weather?q=Tilburg,nl&units=metric&APPID=***********").Result;
var responseContent = response.Content;
var json = responseContent.ReadAsStringAsync().Result;
var currentWeather = Newtonsoft.Json.JsonConvert.DeserializeObject<CurrentWeather>(json);
//Check if the parameter is higher than the current weather
return (currentWeather.main.temp > intValue);
}
Does anyone have a good example? Already had a look a these: