public void TestUsingInterfaceTypes()
 {
     // tests control of the serialisation type
     // - interface types
     using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
     {
         using (ICoreClient client = new PrivateCore(loggerRef.Target))
         {
             AssetMeasureValue codeValueInstance = new AssetMeasureValue()
             {
                 Code        = "Test",
                 Description = "This is a test",
                 Source      = "UnitTest"
             };
             IFpMLCodeValue codeValueInterface = codeValueInstance;
             // save reference item
             client.SaveObject(codeValueInstance, "Test0", null, TimeSpan.MaxValue);
             ICoreItem itemA = client.LoadItem("Test0");
             Assert.AreEqual(typeof(AssetMeasureValue).FullName, itemA.DataTypeName);
             // test interface and instance both serialise identically
             {
                 client.SaveUntypedObject(codeValueInterface, "Test1", null);
                 ICoreItem itemB = client.LoadItem("Test1");
                 Assert.AreEqual(typeof(AssetMeasureValue).FullName, itemB.DataTypeName);
                 Assert.AreEqual(itemA.Text, itemB.Text);
             }
             {
                 client.SaveObject((AssetMeasureValue)codeValueInterface, "Test2", null, TimeSpan.MaxValue);
                 ICoreItem itemB = client.LoadItem("Test2");
                 Assert.AreEqual(typeof(AssetMeasureValue).FullName, itemB.DataTypeName);
                 Assert.AreEqual(itemA.Text, itemB.Text);
             }
             {
                 // this should fail because interfaces cant be serialised
                 UnitTestHelper.AssertThrows <ArgumentException>("Cannot be an interface type!\r\nParameter name: dataType", () =>
                 {
                     client.SaveObject <IFpMLCodeValue>(codeValueInterface, "Test3", null, TimeSpan.MaxValue);
                 });
             }
             {
                 // note: this silently binds to SaveObject<IFpMLCodeValue>(...) which should fail
                 UnitTestHelper.AssertThrows <ArgumentException>("Cannot be an interface type!\r\nParameter name: dataType", () =>
                 {
                     client.SaveObject(codeValueInterface, "Test4", null, TimeSpan.MaxValue);
                 });
             }
         }
     }
 }
Пример #2
0
 public void TestPrivateCore()
 {
     using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
     {
         using (ICoreClient client = new PrivateCore(loggerRef.Target))
         {
             // save/load single object
             const string name  = "Id1";
             TestData     data0 = new TestData("MyData", 123);
             client.SaveObject(data0, name, null, TimeSpan.MaxValue);
             ICoreItem item1a = client.LoadItem <TestData>(name);
             TestData  data1a = (TestData)item1a.Data;
             Assert.AreEqual("MyData", data1a.field1);
             Assert.AreEqual(123, data1a.field2);
             ICoreItem item1b = client.LoadItem <TestData>(name);
             TestData  data1b = (TestData)item1b.Data;
             Assert.AreEqual("MyData", data1b.field1);
             Assert.AreEqual(123, data1b.field2);
             // load multiple
             List <ICoreItem> list = client.LoadItems(Expr.ALL);
             Assert.AreEqual(1, list.Count);
             ICoreItem item1c = list[0];
             TestData  data1c = (TestData)item1c.Data;
             Assert.AreEqual("MyData", data1c.field1);
             Assert.AreEqual(123, data1c.field2);
             // update the object
             Guid      obj1bId = client.SaveObject <TestData>(new TestData(data0.field1, data0.field2 + 1), item1c.Name, item1c.AppProps, TimeSpan.MaxValue);
             ICoreItem item2   = client.LoadItem <TestData>(name);
             Assert.IsNotNull(item2);
             Assert.IsNotNull(item2.Data);
             Assert.AreEqual(typeof(TestData), item2.Data.GetType());
             Assert.AreEqual(obj1bId, item2.Id);
             TestData data3 = (TestData)item2.Data;
             Assert.AreEqual(data0.field1, data3.field1);
             Assert.AreEqual(data0.field2 + 1, data3.field2);
             // delete the object
             client.DeleteItem(item1b);
             ICoreItem item3c = client.LoadItem <TestData>(name);
             Assert.IsNull(item3c);
         }
     }
 }
        public void TestUsingDerivedTypes()
        {
            // tests control of the serialisation type
            // - type b (derived from a) is saved as b, loaded as b;
            // - type b (derived from a) is saved as a, loaded as a (but is type b).
            // (in this example a = PricingStructure, b = YieldCurve)
            using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
            {
                using (ICoreClient client = new PrivateCore(loggerRef.Target))
                {
                    {
                        YieldCurve dataA = new YieldCurve()
                        {
                            currency = new Currency()
                            {
                                Value = "USD"
                            },
                            algorithm = "FastCubicSpline"
                        };
                        // - save as derived type
                        client.SaveObject(dataA, "TestA", null, TimeSpan.MaxValue);
                        ICoreItem test1 = client.LoadItem <YieldCurve>("TestA");
                        Assert.IsNotNull(test1);
                        Assert.AreEqual(typeof(YieldCurve).FullName, test1.DataTypeName);
                        Assert.AreEqual(typeof(YieldCurve), test1.DataType);
                        Assert.IsNotNull(test1.Data);
                        Assert.AreEqual(typeof(YieldCurve), test1.Data.GetType());
                        // - save as base type
                        client.SaveObject <PricingStructure>(dataA, "TestA", null, TimeSpan.MaxValue);
                        ICoreItem test2 = client.LoadItem <PricingStructure>("TestA");
                        Assert.IsNotNull(test2);
                        Assert.AreEqual(typeof(PricingStructure).FullName, test2.DataTypeName);
                        Assert.AreEqual(typeof(PricingStructure), test2.DataType);
                        Assert.IsNotNull(test2.Data);
                        Assert.AreEqual(typeof(YieldCurve), test2.Data.GetType());
                    }
                    {
                        FxCurve dataB = new FxCurve()
                        {
                            quotedCurrencyPair = new QuotedCurrencyPair()
                            {
                                currency1 = new Currency {
                                    Value = "USD"
                                },
                                currency2 = new Currency {
                                    Value = "JPY"
                                },
                                quoteBasis = QuoteBasisEnum.Currency2PerCurrency1
                            }
                        };
                        // - save as derived type
                        client.SaveObject(dataB, "TestB", null, TimeSpan.MaxValue);
                        ICoreItem test1 = client.LoadItem <FxCurve>("TestB");
                        Assert.IsNotNull(test1);
                        Assert.AreEqual(typeof(FxCurve).FullName, test1.DataTypeName);
                        Assert.AreEqual(typeof(FxCurve), test1.DataType);
                        Assert.IsNotNull(test1.Data);
                        Assert.AreEqual(typeof(FxCurve), test1.Data.GetType());

                        // - save as base type
                        client.SaveObject <PricingStructure>(dataB, "TestB", null, TimeSpan.MaxValue);
                        ICoreItem test2 = client.LoadItem <PricingStructure>("TestB");
                        Assert.IsNotNull(test2);
                        Assert.AreEqual(typeof(PricingStructure).FullName, test2.DataTypeName);
                        Assert.AreEqual(typeof(PricingStructure), test2.DataType);
                        Assert.IsNotNull(test2.Data);
                        Assert.AreEqual(typeof(FxCurve), test2.Data.GetType());
                    }
                    {
                        // load a collection of the base type and verify specific types
                        List <ICoreItem> items = client.LoadItems <PricingStructure>(Expr.ALL);
                        Assert.AreEqual(2, items.Count);
                        Dictionary <string, PricingStructure> index = new Dictionary <string, PricingStructure>();
                        foreach (ICoreItem item in items)
                        {
                            index[item.Name] = (PricingStructure)item.Data;
                        }
                        Assert.AreEqual(typeof(YieldCurve), index["TestA"].GetType());
                        Assert.AreEqual(typeof(FxCurve), index["TestB"].GetType());
                    }
                }
            }
        }