public void TestSerialisingInterfaceTypes() { // tests control of the serialisation type // - interface types using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true))) { using (CoreServer server = new CoreServer(loggerRef, "UTT", NodeType.Router)) { // start server server.Start(); using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) { 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(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); }); } } // shutdown server.Stop(); } } }
public void DataItemsDontHaveToContainvalues() { //given an DataItem type created with "Empty" var anyItem = new DataItem <Stream>(); //we can see that it contains no value - it is set to "Empty" Assert.IsTrue(anyItem.IsEmpty); Assert.IsFalse(anyItem.HasValue); Stream tmp = null; UnitTestHelper.AssertThrows <InvalidOperationException>( () => tmp = anyItem.Value); }
public void TestSerialisationFailures() { var props = new NamedValueSet(); props.Set("prop1", 123); props.Set("prop2", 456); IExpression expr1 = Expr.BoolAND(props); Assert.AreEqual <bool>(false, expr1.HasErrors()); string text1 = expr1.Serialise(); Assert.AreEqual <string>( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<QuerySpec xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/ExprFormat.xsd\">\r\n" + " <version>1</version>\r\n" + " <v1QueryExpr node=\"EXPR\" name=\"AND\">\r\n" + " <args node=\"EXPR\" name=\"EQU\">\r\n" + " <args node=\"FIELD\" name=\"prop1\" />\r\n" + " <args node=\"CONST\" name=\"Int32\" value=\"123\" />\r\n" + " </args>\r\n" + " <args node=\"EXPR\" name=\"EQU\">\r\n" + " <args node=\"FIELD\" name=\"prop2\" />\r\n" + " <args node=\"CONST\" name=\"Int32\" value=\"456\" />\r\n" + " </args>\r\n" + " </v1QueryExpr>\r\n" + "</QuerySpec>", text1); string text2a = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<QuerySpec xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/ExprFormat.xsd\">\r\n" + " <version>1</version>\r\n" + " <v1QueryExpr node=\"EXPR\" name=\"AND\">\r\n" + " <args node=\"EXPR\" name=\"EQU\">\r\n" + " <args node=\"FIELD\" name=\"prop1\" />\r\n" + " <args node=\"CONSTqq\" name=\"Int32\" value=\"123\" />\r\n" + " </args>\r\n" + " <args node=\"EXPR\" name=\"EQU\">\r\n" + " <args node=\"FIELD\" name=\"prop2\" />\r\n" + " <args node=\"CONST\" name=\"Int32\" value=\"456\" />\r\n" + " </args>\r\n" + " </v1QueryExpr>\r\n" + "</QuerySpec>"; // deserialisation should succeed, but with errors IExpression expr2 = Expr.Create(text2a); Assert.AreEqual <bool>(true, expr2.HasErrors()); // evaluation should fail UnitTestHelper.AssertThrows <InvalidOperationException>( () => expr2.Evaluate(props)); }
public void TestManualRecovery() { // demonstrates how an application should use the client in its simplest (transient) mode: // - exceptions should be handled by the application; // - client must be disposed and recreated after an exception has occurred; // - subscriptions are not supported. //TimeSpan outageDuration = TimeSpan.FromMinutes(1); //TimeSpan requestTimeout = TimeSpan.FromSeconds(30); using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true))) { loggerRef.Target.LogDebug("----------> test commenced"); // create unit test storage IStoreEngine unitTestStore = new UnitTestStoreEngine(loggerRef.Target); CoreServer server = null; try { var serverSettings = new NamedValueSet(); serverSettings.Set(CfgPropName.NodeType, (int)NodeType.Server); serverSettings.Set(CfgPropName.EnvName, "UTT"); server = new CoreServer(loggerRef, serverSettings) { StoreEngine = unitTestStore }; loggerRef.Target.LogDebug("----------> starting server"); server.Start(); // create 1st client and save object - should succeed Guid id1; using (ICoreClient client1 = new CoreClientFactory(loggerRef) .SetEnv("UTT") .Create()) { id1 = client1.SaveObject(new TestData("Test1", 1), "Test1", null, TimeSpan.MaxValue); // stop server and begin save - should fail with timeout loggerRef.Target.LogDebug("----------> stopping server"); server.Stop(); DisposeHelper.SafeDispose(ref server); client1.RequestTimeout = TimeSpan.FromSeconds(10); UnitTestHelper.AssertThrows <TimeoutException>(() => { client1.SaveObject(new TestData("Test2a", 2), "Test2", null, TimeSpan.MaxValue); }); // further use of client throws more timeout errors UnitTestHelper.AssertThrows <TimeoutException>(() => { client1.LoadObject <TestData>("Test2a"); }); } // create 2nd client - should fail to connect UnitTestHelper.AssertThrows <EndpointNotFoundException>("No server in list 'localhost:8113' found!", () => { using (ICoreClient client2 = new CoreClientFactory(loggerRef) .SetEnv("UTT") .Create()) { client2.SaveObject <TestData>(new TestData("Test2b", 2), "Test2", null, TimeSpan.MaxValue); } }); // restart server server = new CoreServer(loggerRef, serverSettings) { StoreEngine = unitTestStore }; loggerRef.Target.LogDebug("----------> restarting server"); server.Start(); // load 1st object - should succeed ICoreItem item1; ICoreItem item2; using (ICoreClient client3 = new CoreClientFactory(loggerRef) .SetEnv("UTT") .Create()) { item1 = client3.LoadItem <TestData>("Test1"); item2 = client3.LoadItem <TestData>("Test2"); } Assert.IsNotNull(item1); Assert.AreEqual(id1, item1.Id); Assert.IsNull(item2); // done loggerRef.Target.LogDebug("----------> test completed"); server.Stop(); DisposeHelper.SafeDispose(ref server); } finally { DisposeHelper.SafeDispose(ref server); } } }
public void ChainingOptionsArgumentException() { DataItem <int> tmp = new DataItem <int>(1); UnitTestHelper.AssertThrows <ArgumentNullException>(() => tmp.Select <int, int>(null)); }
public void ChainingOptionsThisException() { DataItem <int> tmp = null; UnitTestHelper.AssertThrows <ArgumentNullException>(() => tmp.Select(value => new DataItem <int>(value++))); }