public void DiscountSerializeTest() { ItemFilter filter1 = new CategoryFilter("cat"); IBooleanExpression leaf1 = new MaxAmount(10, filter1); ItemFilter filter2 = new AllProductsFilter(); IBooleanExpression leaf2 = new UserCountry("Wakanda forever", filter2); IBooleanExpression complex = new XorExpression(); //we add manually ids for the leaves becuase there is no stub for them at the moment. //Not adding these ids will cause the json serializer to falsly think it has a loop (parent and children have the same id -> .Equals() = true) leaf2.id = 10; leaf1.id = 11; complex.addChildren(leaf1, leaf2); //TODO: when there are concrete Outcomes, we can test this //IOutcome outcome = new //Discount discount = new Discount(complex,) string json = JsonConvert.SerializeObject(complex, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); IBooleanExpression result = JsonConvert.DeserializeObject <IBooleanExpression>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Assert.IsInstanceOfType(result, typeof(XorExpression)); Assert.IsInstanceOfType(((XorExpression)result).firstChild, typeof(MaxAmount)); Assert.IsInstanceOfType(((XorExpression)result).secondChild, typeof(UserCountry)); }
public void BooleanExpressionComplexSerializeTest() { ItemFilter filter1 = new CategoryFilter("cat"); IBooleanExpression leaf1 = new MaxAmount(10, filter1); ItemFilter filter2 = new AllProductsFilter(); IBooleanExpression leaf2 = new UserCountry("Wakanda forever", filter2); IBooleanExpression complex = new XorExpression(); leaf1.id = 1; leaf2.id = 2; complex.addChildren(leaf1, leaf2); string json = JsonConvert.SerializeObject(complex, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); IBooleanExpression result = JsonConvert.DeserializeObject <IBooleanExpression>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Assert.IsInstanceOfType(result, typeof(XorExpression)); Assert.IsInstanceOfType(((XorExpression)result).firstChild, typeof(MaxAmount)); Assert.IsInstanceOfType(((XorExpression)result).secondChild, typeof(UserCountry)); }
//**********POLICIES********************* //policies /// <summary> /// /// </summary> /// <param name="info">info ["data"]</param> /// <returns></returns> private IBooleanExpression createLeaf(JObject info) { string leafType = (string)info["type"]; //create products filter string products = (string)info["products"]; ItemFilter filter; if (products == "-1") { filter = new AllProductsFilter(); } else { string[] productsArr = products.Split(',').ToArray(); List <int> productsList = new List <int>(); foreach (string curr in productsArr) { productsList.Add(Convert.ToInt32(curr)); } filter = new ProductListFilter(productsList); } switch (leafType) { case "min": return(new MinAmount((int)info["data"], filter)); case "max": return(new MaxAmount((int)info["data"], filter)); case "country": return(new UserCountry((string)info["data"], filter)); case "age": return(new UserAge((int)info["data"], filter)); default: Logger.Log("error", logLevel.ERROR, "can't create leaf"); throw new Exception("can't create leaf"); } }
private void addDiscountPolicy() { bridge.Login(getStoreOwner1(), password); ItemFilter filter1 = new AllProductsFilter(); IBooleanExpression leaf1 = new MinAmount(5, filter1); ItemFilter filter2 = new AllProductsFilter(); IBooleanExpression leaf2 = new MinAmount(10, filter2); IBooleanExpression complex = new XorExpression(); //we add manually ids for the leaves becuase there is no stub for them at the moment. //Not adding these ids will cause the json serializer to falsly think it has a loop (parent and children have the same id -> .Equals() = true) leaf2.id = 20; leaf1.id = 21; complex.addChildren(leaf1, leaf2); IOutcome outcome = new FreeProduct(productId, 1); Discount discount = new Discount(complex, outcome); string json = JsonHandler.SerializeObject(discount); policyId = bridge.addDiscountPolicy(storeId, json); Assert.IsTrue(policyId >= 0); }