public void CanGenerateCodeForSimpleMapping()
        {
            var tg     = new JsonTypeGenerator();
            var source = tg.Generate(File.ReadAllText(TestFiles.Person), TestFiles.Person, "Foo.Bar");
            var target = tg.Generate(File.ReadAllText(TestFiles.PersonBasic), TestFiles.PersonBasic, "Baz");

            Write(source.Code);
            Write(target.Code);

            var mapping = JsonConvert.DeserializeObject <Mapping>(File.ReadAllText(TestFiles.PersonMapping));

            mapping.RebuildRelationships();

            var mappings = new Dictionary <int, TypeMapping>();

            var sourceItem = mapping.SourceInfo.Roots[0];
            var targetItem = mapping.TargetInfo.Roots[0];


            var resolver = new TypeResolver();

            resolver.Resolve(mappings, source.RootType, target.RootType, sourceItem, targetItem, mapping);

            var generator = new Generator();
            var sb        = new StringBuilder();
            var writer    = new StringWriter(sb);

            generator.Generate(writer, mappings, source.RootType, target.RootType, mapping.TargetInfo.Roots[0], mapping);

            var result = sb.ToString();

            Assert.True(result.Contains("target.FirstName = Source?.FirstName;"));
            Write(result);
        }
        public void CanResolveTypesForSourceAndTarget()
        {
            var tg     = new JsonTypeGenerator();
            var source = tg.Generate(File.ReadAllText(TestFiles.Person), TestFiles.Person, "Foo.Bar");
            var target = tg.Generate(File.ReadAllText(TestFiles.PersonBasic), TestFiles.PersonBasic, "Baz");

            Write(source.Code);
            Write(target.Code);

            var mapping = JsonConvert.DeserializeObject <Mapping>(File.ReadAllText(TestFiles.PersonMapping));

            mapping.RebuildRelationships();

            var mappings = new Dictionary <int, TypeMapping>();

            var sourceItem = mapping.SourceInfo.Roots[0];
            var targetItem = mapping.TargetInfo.Roots[0];
            var sourcetype = source.Assembly.DefinedTypes.First(a => a.Name == ConversionUtilities.ConvertToUpperCamelCase(sourceItem.title, true));
            var targettype = target.Assembly.DefinedTypes.First(a => a.Name == ConversionUtilities.ConvertToUpperCamelCase(targetItem.title, true));

            var resolver = new TypeResolver();

            resolver.Resolve(mappings, sourcetype, targettype, sourceItem, targetItem, mapping);
            Assert.Equal(4, mappings.Keys.Count);

            foreach (var tm in mappings.Values)
            {
                foreach (var schemaItemMapping in tm.Mappings)
                {
                    Write(string.Format("Item {0} of type {1} will be mapped from {2}", schemaItemMapping.TargetSchemaItem.title, tm.TargetProperty, schemaItemMapping.SourceProperty));
                }
            }

            var tm1 = mappings[targetItem.children.First(a => a.title == "firstName").key];

            Assert.Equal(typeof(System.String), tm1.Mappings[0].SourceProperty.PropertyType);
            Assert.NotNull(tm1.Mappings[0].SourcePath);

            var tm2 = mappings[targetItem.children.First(a => a.title == "lastName").key];

            Assert.Equal(typeof(System.String), tm2.Mappings[0].SourceProperty.PropertyType);
            Assert.NotNull(tm2.Mappings[0].SourcePath);
        }
Beispiel #3
0
        public void TypesCanBeGeneratedFromPersonSchema()
        {
            var tg     = new JsonTypeGenerator();
            var result = tg.Generate(File.ReadAllText(TestFiles.Person), TestFiles.Person, "Foo.Bar");

            Assert.Null(result.Errors);
            Assert.NotNull(result.Assembly);
            Assert.NotNull(result.AssemblyBytes);
            Assert.False(string.IsNullOrEmpty(result.Code));
            Assert.Equal("person.dll", result.AssemblyName);

            Assert.Equal("Person", result.RootTypeName);

            Assert.NotNull(result.Assembly.DefinedTypes.First(a => a.Name == "Person"));
            Assert.NotNull(result.Assembly.DefinedTypes.First(a => a.Name == "Company"));
            Assert.NotNull(result.Assembly.DefinedTypes.First(a => a.Name == "Car"));

            Write(result.Code);
        }