Beispiel #1
0
        public void LoadMetadataFromJson()
        {
            var svcProvider = new ServiceContainer();

            // Use StandardMetadataProvider
            var metadataProvider = new StandardMetadataProvider();

            svcProvider.AddService(typeof(IMetadataProvider), metadataProvider);
            svcProvider.AddService(typeof(ISerializerConfigService), new StandardSerializerConfigService(null));

            var metadataRepoFilePath = Path.Combine(Directory.GetCurrentDirectory(), @"MockData/SuperheroMetadata.json");

            var metadataRepo = new FlatFileRepo <DynamicObjectType>(
                metadataRepoFilePath,
                svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService,
                svcProvider
                );

            var dynamicMetadataProvider = new DynamicMetadataProvider();

            dynamicMetadataProvider.Load(metadataRepo);

            var metahumanObjType = dynamicMetadataProvider.GetDataType <ObjectType>("Headway.Dynamo.UnitTests.Mockdata.Metahuman");

            Assert.IsNotNull(metahumanObjType);
            var propSuperpower1 = metahumanObjType.GetPropertyByName("Superpower1");

            Assert.IsNotNull(propSuperpower1);
        }
Beispiel #2
0
        public void CreateDynamicObject()
        {
            // Use StandardMetadataProvider
            var svcProvider = new ServiceContainer();

            svcProvider.AddService(typeof(IMetadataProvider), this.MetadataProvider);
            svcProvider.AddService(typeof(ISerializerConfigService), new StandardSerializerConfigService(null));

            var metadataRepoFilePath = Path.Combine(Directory.GetCurrentDirectory(), @"MockData/SuperheroMetadata.json");

            var metadataRepo = new FlatFileRepo <DynamicObjectType>(
                metadataRepoFilePath,
                svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService,
                svcProvider
                );

            this.MetadataProvider.DynamicProvider.Load(metadataRepo);

            dynamic metaHuman = this.MetadataProvider.CreateInstance <DynamoPerson>(null,
                                                                                    "Headway.Dynamo.UnitTests.Mockdata.Metahuman",
                                                                                    null);

            Assert.IsNotNull(metaHuman);
            metaHuman.Superpower1 = "Invisibility";
            var sp1 = metaHuman.Superpower1;

            Assert.AreEqual(sp1, "Invisibility");
            metaHuman.FirstName = "Fred";
        }
Beispiel #3
0
        public void ReadWriteDefaultLocation()
        {
            var serializerConfigSvc = this.svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService;

            Assert.IsNotNull(serializerConfigSvc);

            var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/" + "Headway",
                                        typeof(Person).FullName + ".json");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            // Do not specify file path and allow default
            var repo = new FlatFileRepo <Person>(serializerConfigSvc, this.svcProvider);

            repo.Add(this.personColl);
            repo.SaveChanges();

            Assert.IsTrue(File.Exists(filePath));

            var repo2 = new FlatFileRepo <Person>(serializerConfigSvc, this.svcProvider);

            Assert.AreEqual(repo2.GetQueryable().Count(), this.personColl.Count());
        }
Beispiel #4
0
        public void TestInitialize()
        {
            this.svcProvider = new ServiceContainer();

            var metadataProvider = new StandardMetadataProvider();

            this.svcProvider.AddService(typeof(IMetadataProvider), metadataProvider);
            this.svcProvider.AddService(typeof(ISerializerConfigService), new StandardSerializerConfigService(null));

            var restApiRepo = new FlatFileRepo <RestApi>(
                @"MockData/RestApis.json",
                svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService,
                svcProvider
                );

            var restApiService = new RestApiServiceImpl();

            restApiService.Load(restApiRepo.GetQueryable());

            this.svcProvider.AddService(typeof(IRestApiService), restApiService);
        }
Beispiel #5
0
        public void DeserializeNestedDynamicObject()
        {
            // Use StandardMetadataProvider
            var svcProvider = new ServiceContainer();

            svcProvider.AddService(typeof(IMetadataProvider), this.MetadataProvider);
            svcProvider.AddService(typeof(ISerializerConfigService), new StandardSerializerConfigService(null));

            var metadataRepoFilePath = Path.Combine(Directory.GetCurrentDirectory(), @"MockData/SuperheroMetadata.json");

            var metadataRepo = new FlatFileRepo <DynamicObjectType>(
                metadataRepoFilePath,
                svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService,
                svcProvider
                );

            this.MetadataProvider.DynamicProvider.Load(metadataRepo);

            // Create an instance of a metahuman
            dynamic metaHuman = this.MetadataProvider.CreateInstance <DynamoPerson>(null,
                                                                                    "Headway.Dynamo.UnitTests.Mockdata.Metahuman",
                                                                                    null);

            Assert.IsNotNull(metaHuman);
            metaHuman.Superpower1 = "Invisibility";
            var sp1 = metaHuman.Superpower1;

            Assert.AreEqual(sp1, "Invisibility");
            metaHuman.FirstName = "Fred";
            metaHuman.Addr      = new Address()
            {
                Line1 = "123 Somewhere Pl",
                City  = "Tulsa",
                State = "OK"
            };
            metaHuman.ExtraStuff = "Stuff";

            // Create model that wraps the dynamo object
            var superHeroMdl = new SuperheroModel()
            {
                Superhero  = metaHuman,
                StatusCode = 2
            };

            // Serialize object
            var serializerConfigSvc = svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService;

            if (serializerConfigSvc == null)
            {
                throw new ServiceNotFoundException(typeof(ISerializerConfigService));
            }

            var jsonSettings = serializerConfigSvc.ConfigureJsonSerializerSettings(
                typeof(SuperheroModel),
                svcProvider);

            var jsonModel = JsonConvert.SerializeObject(superHeroMdl, jsonSettings);

            byte[] buffer;

            using (var stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream))
                {
                    sw.WriteLine(jsonModel);
                }
                buffer = stream.GetBuffer();
            }

            using (var stream = new MemoryStream(buffer))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    dynamic superHeroMdl_2 = JsonConvert.DeserializeObject <SuperheroModel>(sr.ReadToEnd(), jsonSettings);
                    Assert.IsNotNull(superHeroMdl_2);
                    var sp1_2 = superHeroMdl_2.Superhero.Superpower1;
                    Assert.AreEqual(sp1_2, "Invisibility");
                }
            }
        }
Beispiel #6
0
        public void SerializeDynamicObjectCollection()
        {
            // Use StandardMetadataProvider
            var svcProvider = new ServiceContainer();

            svcProvider.AddService(typeof(IMetadataProvider), this.MetadataProvider);
            svcProvider.AddService(typeof(ISerializerConfigService), new StandardSerializerConfigService(null));

            var metadataRepoFilePath = Path.Combine(Directory.GetCurrentDirectory(), @"MockData/SuperheroMetadata.json");

            var metadataRepo = new FlatFileRepo <DynamicObjectType>(
                metadataRepoFilePath,
                svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService,
                svcProvider
                );

            this.MetadataProvider.DynamicProvider.Load(metadataRepo);

            var metaHumans = new List <DynamoPerson>();

            dynamic metaHuman1 = this.MetadataProvider.CreateInstance <DynamoPerson>(null,
                                                                                     "Headway.Dynamo.UnitTests.Mockdata.Metahuman",
                                                                                     null);

            Assert.IsNotNull(metaHuman1);
            metaHuman1.Id          = 1;
            metaHuman1.Superpower1 = "Kindness";
            var sp1 = metaHuman1.Superpower1;

            Assert.AreEqual(sp1, "Kindness");
            metaHuman1.FirstName   = "Fred";
            metaHuman1.LastName    = "Rogers";
            metaHuman1.DateOfBirth = new DateTime(1928, 3, 20);
            metaHuman1.Addr        = new Address()
            {
                Line1 = "4802 Fifth Ave",
                City  = "Pittsburgh",
                State = "PA"
            };

            metaHumans.Add(metaHuman1);

            dynamic metaHuman2 = this.MetadataProvider.CreateInstance <DynamoPerson>(null,
                                                                                     "Headway.Dynamo.UnitTests.Mockdata.Metahuman",
                                                                                     null);

            Assert.IsNotNull(metaHuman2);
            metaHuman2.Id          = 2;
            metaHuman2.Superpower1 = "Future Vision";
            metaHuman2.Superpower2 = "Money";
            metaHuman2.FirstName   = "Elon";
            metaHuman2.LastName    = "Musk";
            metaHuman2.DateOfBirth = new DateTime(1971, 6, 28);
            metaHuman2.Addr        = new Address()
            {
                Line1 = "3500 Deer Creek Road",
                City  = "Palo Alto",
                State = "CA"
            };

            metaHumans.Add(metaHuman2);

            dynamic metaHuman3 = this.MetadataProvider.CreateInstance <DynamoPerson>(null,
                                                                                     "Headway.Dynamo.UnitTests.Mockdata.Metahuman",
                                                                                     null);

            Assert.IsNotNull(metaHuman2);
            metaHuman3.Id          = 3;
            metaHuman3.Superpower1 = "Egomania";
            metaHuman3.Superpower2 = "Lying";
            metaHuman3.FirstName   = "Donald";
            metaHuman3.LastName    = "Trump";
            metaHuman3.DateOfBirth = new DateTime(1946, 6, 14);
            metaHuman3.Addr        = new Address()
            {
                Line1      = "1600 Pennsylvania Ave NW",
                City       = "Washington",
                State      = "DC",
                PostalCode = "20500"
            };

            metaHumans.Add(metaHuman3);

            // Serialize object
            var serializerConfigSvc = svcProvider.GetService(typeof(ISerializerConfigService)) as ISerializerConfigService;

            if (serializerConfigSvc == null)
            {
                throw new ServiceNotFoundException(typeof(ISerializerConfigService));
            }

            var objType = this.MetadataProvider.GetDataType <ObjectType>("Headway.Dynamo.UnitTests.Mockdata.Metahuman");

            var jsonSettings = serializerConfigSvc.ConfigureJsonSerializerSettings(
                typeof(DynamoPerson),
                svcProvider);

            var jsonMetaHumans = JsonConvert.SerializeObject(metaHumans, jsonSettings);

            byte[] buffer;

            using (var stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream))
                {
                    sw.WriteLine(jsonMetaHumans);
                }
                buffer = stream.GetBuffer();
            }

            using (var stream = new MemoryStream(buffer))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    var deserializedMetaHumans = JsonConvert.DeserializeObject <List <DynamoPerson> >(sr.ReadToEnd(), jsonSettings);
                    Assert.IsNotNull(deserializedMetaHumans);

                    int deserializedMetaHumanCount = 0;

                    foreach (dynamic curDeserializedMetaHuman in deserializedMetaHumans)
                    {
                        var originalMetaHuman = (from mh in metaHumans
                                                 where mh.Id == curDeserializedMetaHuman.Id
                                                 select mh).FirstOrDefault();

                        Assert.IsNotNull(originalMetaHuman);
                        Assert.AreEqual(curDeserializedMetaHuman.FirstName,
                                        originalMetaHuman.FirstName);
                        Assert.AreEqual(curDeserializedMetaHuman.MiddleName,
                                        originalMetaHuman.MiddleName);
                        Assert.AreEqual(curDeserializedMetaHuman.LastName,
                                        originalMetaHuman.LastName);
                        Assert.AreEqual(curDeserializedMetaHuman.DateOfBirth,
                                        originalMetaHuman.DateOfBirth);
                        if (curDeserializedMetaHuman.Addr != null &&
                            originalMetaHuman.Addr != null)
                        {
                            Assert.AreEqual(curDeserializedMetaHuman.Addr.Line1,
                                            originalMetaHuman.Addr.Line1);
                            Assert.AreEqual(curDeserializedMetaHuman.Addr.Line2,
                                            originalMetaHuman.Addr.Line2);
                            Assert.AreEqual(curDeserializedMetaHuman.Addr.City,
                                            originalMetaHuman.Addr.City);
                            Assert.AreEqual(curDeserializedMetaHuman.Addr.State,
                                            originalMetaHuman.Addr.State);
                            Assert.AreEqual(curDeserializedMetaHuman.Addr.PostalCode,
                                            originalMetaHuman.Addr.PostalCode);
                        }
                        else
                        {
                            Assert.AreEqual(curDeserializedMetaHuman.Addr,
                                            originalMetaHuman.Addr);
                        }

                        deserializedMetaHumanCount++;
                    }

                    Assert.AreEqual(deserializedMetaHumanCount, metaHumans.Count);
                }
            }
        }