public void ModuleSetSerialize() { var serializerInputTable = new Dictionary <string, Type>() { { "test", typeof(TestModule) } }; var myModuleSetSerde = new ModuleSetSerde(serializerInputTable); string jsonFromTestModuleSet = myModuleSetSerde.Serialize(ModuleSet1); string jsonFromTestModuleSet2 = myModuleSetSerde.Serialize(ModuleSet2); ModuleSet myModuleSet = myModuleSetSerde.Deserialize(jsonFromTestModuleSet); ModuleSet myModuleSet2 = myModuleSetSerde.Deserialize(jsonFromTestModuleSet2); IModule module1 = ModuleSet1.Modules["mod1"]; IModule module2 = myModuleSet.Modules["mod1"]; IModule module3 = ModuleSet2.Modules["mod5"]; IModule module4 = myModuleSet2.Modules["mod5"]; IModule module5 = ModuleSet2.Modules["mod3"]; IModule module6 = myModuleSet2.Modules["mod3"]; Assert.True(module1.Equals(module2)); Assert.True(module3.Equals(module4)); Assert.True(module5.Equals(module6)); }
public void TestDeserialize() { string validJson = "{\"Modules\":{\"mod1\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"Settings\":{\"Image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"StartupOrder\":0,\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"settings\":{\"image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string validJsonAllLower = "{\"modules\":{\"mod1\":{\"version\":\"version1\",\"type\":\"test\",\"status\":\"running\",\"settings\":{\"image\":\"image1\"},\"restartpolicy\":\"on-unhealthy\",\"imagepullpolicy\":\"on-create\",\"startuporder\":0,\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"version\":\"version1\",\"type\":\"test\",\"status\":\"running\",\"settings\":{\"image\":\"image1\"},\"restartpolicy\":\"on-unhealthy\",\"imagepullpolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string validJsonAllCap = "{\"MODULES\":{\"mod1\":{\"NAME\":\"mod1\",\"VERSION\":\"version1\",\"TYPE\":\"test\",\"STATUS\":\"RUNNING\",\"SETTINGS\":{\"IMAGE\":\"image1\"},\"RESTARTPOLICY\":\"on-unhealthy\",\"IMAGEPULLPOLICY\":\"on-create\",\"STARTUPORDER\":0,\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"NAME\":\"mod2\",\"VERSION\":\"version1\",\"TYPE\":\"test\",\"STATUS\":\"RUNNING\",\"SETTINGS\":{\"IMAGE\":\"image1\"},\"RESTARTPOLICY\":\"on-unhealthy\",\"IMAGEPULLPOLICY\":\"on-create\",\"CONFIGURATION\":{\"id\":\"1\"}}}}"; string noVersionJson = "{\"Modules\":{\"mod1\":{\"Type\":\"test\",\"Status\":\"Running\",\"Settings\":{\"Image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Type\":\"test\",\"Status\":\"Running\",\"settings\":{\"image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string noTypeJson = "{\"Modules\":{\"mod1\":{\"Version\":\"version1\",\"Status\":\"Running\",\"Settings\":{\"Image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Version\":\"version1\",\"Status\":\"Running\",\"settings\":{\"image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string noStatusJson = "{\"Modules\":{\"mod1\":{\"Version\":\"version1\",\"Type\":\"test\",\"Settings\":{\"Image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Version\":\"version1\",\"Type\":\"test\",\"settings\":{\"image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string noConfigJson = "{\"Modules\":{\"mod1\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"Settings\":{\"Image\":\"image1\"},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string noConfigImageJson = "{\"Modules\":{\"mod1\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"Settings\":{},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"settings\":{},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; string notATestType = "{\"Modules\":{\"mod1\":{\"Version\":\"version1\",\"Type\":\"not_a_test\",\"Status\":\"Running\",\"Settings\":{},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"Configuration\":{\"id\":\"1\"}},\"mod2\":{\"Version\":\"version1\",\"Type\":\"test\",\"Status\":\"Running\",\"settings\":{},\"RestartPolicy\":\"on-unhealthy\",\"ImagePullPolicy\":\"on-create\",\"configuration\":{\"id\":\"1\"}}}}"; var serializerInputTable = new Dictionary <string, Type>() { { "Test", typeof(TestModule) } }; var myModuleSetSerde = new ModuleSetSerde(serializerInputTable); ModuleSet myModuleSet1 = myModuleSetSerde.Deserialize(validJson); ModuleSet myModuleSet2 = myModuleSetSerde.Deserialize(validJsonAllLower); ModuleSet myModuleSet3 = myModuleSetSerde.Deserialize(validJsonAllCap); IModule myModule1 = myModuleSet1.Modules["mod1"]; IModule myModule2 = myModuleSet1.Modules["mod2"]; IModule myModule3 = myModuleSet2.Modules["mod1"]; IModule myModule4 = myModuleSet2.Modules["mod2"]; IModule myModule5 = myModuleSet3.Modules["mod1"]; IModule myModule6 = myModuleSet3.Modules["mod2"]; Assert.True(Module1.Equals(myModule1)); Assert.True(Module2.Equals(myModule2)); Assert.True(Module1.Equals(myModule3)); Assert.True(Module2.Equals(myModule4)); Assert.True(Module1.Equals(myModule5)); Assert.True(Module2.Equals(myModule6)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(noVersionJson)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(noStatusJson)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(noTypeJson)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(noConfigJson)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(noConfigImageJson)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(notATestType)); Assert.Throws <JsonSerializationException>(() => myModuleSetSerde.Deserialize(null)); }