public void GenerateSchemaClassesTest()
        {
            var generator = new GoogleSchemaGenerator(new List<ISchemaDecorator>(), "Fish.Chips");
            var mockService = new MockService();

            var mockSchema = new MockSchema();
            mockSchema.Id = "Fish";
            mockSchema.Name = "Fish";
            mockSchema.SchemaDetails = new JsonSchema();
            mockService.Schemas.Add("Fish", mockSchema);

            mockSchema = new MockSchema();
            mockSchema.Id = "Chips";
            mockSchema.Name = "Chips";
            mockSchema.SchemaDetails = new JsonSchema();
            mockService.Schemas.Add("Chips", mockSchema);

            CodeNamespace result = generator.GenerateSchemaClasses(mockService);

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Types.Count);
            List<string> classNames = new List<string>(2);
            classNames.Add(result.Types[0].Name);
            classNames.Add(result.Types[1].Name);

            Assert.That(classNames.Contains("Fish"));
            Assert.That(classNames.Contains("Chips"));
        }
        public void DecorateClassGenerationTest()
        {
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();
            var decorator = new ArraySchemaDecorator();
            var internalClassProvider = new ObjectInternalClassProvider();
            var decl = new CodeTypeDeclaration();

            var schema = new MockSchema();
            schema.Name = "test";
            schema.SchemaDetails = new JsonSchema();
            schema.SchemaDetails.Type = JsonSchemaType.Array;
            schema.SchemaDetails.Items = new List<JsonSchema>();
            schema.SchemaDetails.Items.Clear();
            schema.SchemaDetails.Items.Add(
                new JsonSchema { Description = "Test", Id = "TestSchema", Type = JsonSchemaType.Object });
            implDetails.Add(schema.SchemaDetails, new SchemaImplementationDetails());
            implDetails.Add(schema.SchemaDetails.Items[0], new SchemaImplementationDetails());
            Assert.DoesNotThrow(() => decorator.DecorateClass(decl, schema, implDetails, internalClassProvider));

            Assert.That(decl.BaseTypes.Count, Is.EqualTo(1));
            Assert.That(decl.BaseTypes[0].BaseType, Is.StringStarting("List<"));

            // Subtype will only be created later on by the NestedClassGenerator,
            // and therefore cannot be tested here
        }
        public void DecorateClassValidationTest()
        {
            var decl = new CodeTypeDeclaration();
            var dec = new AdditionalPropertiesSchemaDecorator();
            var schema = new MockSchema();
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();
            var provider = new SchemaGenerator.NestedClassGenerator(decl, new ISchemaDecorator[0], "");

            // Test all parameters.
            Assert.Throws<ArgumentNullException>(() => dec.DecorateClass(null, schema, implDetails, provider));
            Assert.Throws<ArgumentNullException>(() => dec.DecorateClass(decl, null, implDetails, provider));
            Assert.Throws<ArgumentNullException>(() => dec.DecorateClass(decl, schema, null, provider));
            Assert.Throws<ArgumentNullException>(() => dec.DecorateClass(decl, schema, implDetails, null));
            dec.DecorateClass(decl, schema, implDetails, provider);
        }
 public void CreateClassSingle()
 {
     var empty = new List<ISchemaDecorator>(0);
     var schemaGen = new SchemaGenerator(empty);
     MockSchema schema = new MockSchema
                             {
                                 Id = "mockSchemaObject",
                                 Name = "mockSchemaObject",
                                 SchemaDetails = MockSchema.CreateSimpleSchema("mockSchemaObject")
                             };
     var typeDeclaration = schemaGen.CreateClass(
         schema, new Dictionary<JsonSchema, SchemaImplementationDetails>(), Enumerable.Empty<string>());
     Assert.IsNotNull(typeDeclaration);
     Assert.AreEqual("MockSchemaObject", typeDeclaration.Name);
     Assert.IsEmpty(typeDeclaration.Members);
 }
        public void CreateClassSingle()
        {
            var        empty     = new List <ISchemaDecorator>(0);
            var        schemaGen = new SchemaGenerator(empty);
            MockSchema schema    = new MockSchema
            {
                Id            = "mockSchemaObject",
                Name          = "mockSchemaObject",
                SchemaDetails = MockSchema.CreateSimpleSchema("mockSchemaObject")
            };
            var typeDeclaration = schemaGen.CreateClass(
                schema, new Dictionary <JsonSchema, SchemaImplementationDetails>(), Enumerable.Empty <string>());

            Assert.IsNotNull(typeDeclaration);
            Assert.AreEqual("MockSchemaObject", typeDeclaration.Name);
            Assert.IsEmpty(typeDeclaration.Members);
        }
        public void GenerateAllPropertiesTest()
        {
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();
            var schema = new MockSchema();
            var internalClassProvider = new ObjectInternalClassProvider();
            schema.SchemaDetails = new JsonSchema();
            schema.SchemaDetails.Type = JsonSchemaType.Object;
            schema.SchemaDetails.Properties = new Dictionary<string, JsonSchema>();
            implDetails.Add(schema.SchemaDetails, new SchemaImplementationDetails());
            string name = "test";

            foreach (var pair in StandardPropertyFieldDecoratorTest.NamesToType)
            {
                JsonSchema property = new JsonSchema();
                property.Type = pair.Value;
                property.Id = pair.Key;
                property.Description = StandardPropertyFieldDecoratorTest.NamesToDescription[pair.Key];
                schema.SchemaDetails.Properties.Add(pair.Key, property);
                implDetails.Add(property, new SchemaImplementationDetails());
            }

            var decorator = new StandardPropertyDecorator();
            IList<CodeMemberProperty> generatedProperties = decorator.GenerateAllProperties(
                name, schema.SchemaDetails, implDetails, internalClassProvider);

            Assert.NotNull(generatedProperties);
            Assert.AreEqual(StandardPropertyFieldDecoratorTest.NamesToType.Count, generatedProperties.Count);
            foreach (var field in generatedProperties)
            {
                Assert.That(
                    StandardPropertyFieldDecoratorTest.NamesToType.ContainsKey(field.Name.ToLower()),
                    "field name was not present in namesToType " + field.Name);
            }

            int item = 0;
            foreach (var pair in StandardPropertyFieldDecoratorTest.NamesToType)
            {
                var field = generatedProperties[item++];
                Assert.AreEqual(
                    pair.Key, field.Name.ToLower(),
                    string.Format("Name different for expected at index {0}", item - 1));
                Assert.AreEqual(
                    SchemaDecoratorUtil.GetCodeType(new JsonSchema { Type = pair.Value }, null, internalClassProvider)
                        .BaseType, field.Type.BaseType);
            }
        }
        public void CreateClassWithSingleDecorator()
        {
            var oneDecorator = new List <ISchemaDecorator>(1)
            {
                new CountingDecorator()
            };
            var        schemaGen = new SchemaGenerator(oneDecorator);
            MockSchema schema    = new MockSchema
            {
                Id            = "mockSchemaObject",
                Name          = "mockSchemaObject",
                SchemaDetails = MockSchema.CreateSimpleSchema("mockSchemaObject")
            };

            schemaGen.CreateClass(
                schema, new Dictionary <JsonSchema, SchemaImplementationDetails>(), Enumerable.Empty <string>());
            Assert.AreEqual(1, ((CountingDecorator)oneDecorator[0]).Count);
        }
        public void DecorateClassTest()
        {
            // Init required vars.
            var decorator = new ResponseInterfaceDecorator();
            var declaration = new CodeTypeDeclaration();
            var schema = new MockSchema { SchemaDetails = new JsonSchema() };
            var internalClassProvider = new ObjectInternalClassProvider();
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();
            implDetails.Add(schema.SchemaDetails, new SchemaImplementationDetails());

            // Test edge cases.
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(null, schema, implDetails, internalClassProvider));
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(declaration, null, implDetails, internalClassProvider));
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(declaration, schema, null, internalClassProvider));

            decorator.DecorateClass(declaration, schema, implDetails, internalClassProvider);
            Assert.AreEqual(0, declaration.BaseTypes.Count);
            Assert.AreEqual(0, declaration.Members.Count);

            // Test simple functionality.
            var details = new SchemaImplementationDetails { IsMethodResult = true };
            implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>
                                  { { schema.SchemaDetails, details } };

            decorator.DecorateClass(declaration, schema, implDetails, internalClassProvider);

            Assert.AreEqual(1, declaration.BaseTypes.Count);
            Assert.AreEqual(2, declaration.Members.Count); // 1 property with a field.

            // Test with already existing e-tag field.
            declaration = new CodeTypeDeclaration();
            declaration.Members.Add(new CodeMemberProperty() { Name = "ETag" });
            decorator.DecorateClass(declaration, schema, implDetails, internalClassProvider);

            Assert.AreEqual(1, declaration.BaseTypes.Count);
            Assert.AreEqual(1, declaration.Members.Count);
        }
        public void DecorateClassTest()
        {
            var decorator = new StandardPropertyDecorator();
            var declaration = new CodeTypeDeclaration();
            var schema = new MockSchema() { Name = "TestSchema", SchemaDetails = new JsonSchema() };
            var internalClassProvider = new ObjectInternalClassProvider();
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();

            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(null, schema, implDetails, internalClassProvider));
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(declaration, null, implDetails, internalClassProvider));
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(declaration, schema, null, internalClassProvider));

            decorator.DecorateClass(declaration, schema, implDetails, internalClassProvider);
        }
Beispiel #10
0
        public void CreateClassWithMultipleDecorators()
        {
            var once       = new CountingDecorator();
            var threeTimes = new CountingDecorator();
            var decorators = new List <ISchemaDecorator>(4)
            {
                once, threeTimes, threeTimes, threeTimes
            };
            var        schemaGen = new SchemaGenerator(decorators);
            MockSchema schema    = new MockSchema
            {
                Id            = "mockSchemaObject",
                Name          = "mockSchemaObject",
                SchemaDetails = MockSchema.CreateSimpleSchema("mockSchemaObject")
            };

            schemaGen.CreateClass(
                schema, new Dictionary <JsonSchema, SchemaImplementationDetails>(), Enumerable.Empty <string>());
            Assert.AreEqual(1, ((CountingDecorator)decorators[0]).Count);
            Assert.AreEqual(3, ((CountingDecorator)decorators[1]).Count);
            Assert.AreEqual(3, ((CountingDecorator)decorators[2]).Count);
            Assert.AreEqual(3, ((CountingDecorator)decorators[3]).Count);
        }
        public void GenerateAllFieldsTestEdgeCases()
        {
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();
            var decorator = new StandardPropertyFieldDecorator();
            var schema = new MockSchema();
            string name = "test";
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.GenerateAllFields(name, null, implDetails, internalClassProvider));
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.GenerateAllFields(name, schema.SchemaDetails, null, internalClassProvider));
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.GenerateAllFields(name, schema.SchemaDetails, implDetails, null));

            schema.Name = "test";
            schema.SchemaDetails = null;
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.GenerateAllFields(name, schema.SchemaDetails, implDetails, internalClassProvider));

            schema.SchemaDetails = new JsonSchema();
            schema.SchemaDetails.Properties = null;
            var expectedEmpty = decorator.GenerateAllFields(
                name, schema.SchemaDetails, implDetails, internalClassProvider);
            Assert.IsNotNull(expectedEmpty);
            Assert.AreEqual(0, expectedEmpty.Count);
        }
        public void DecorateClassTestEdgeCases()
        {
            var implDetails = new Dictionary<JsonSchema, SchemaImplementationDetails>();
            var decorator = new ArraySchemaDecorator();
            var internalClassProvider = new ObjectInternalClassProvider();
            CodeTypeDeclaration declaration = null;

            var schema = new MockSchema();
            schema.Name = "test";
            schema.SchemaDetails = null;
            Assert.Throws(
                typeof(ArgumentNullException),
                () => decorator.DecorateClass(new CodeTypeDeclaration(), schema, implDetails, internalClassProvider));

            schema.SchemaDetails = new JsonSchema();
            schema.SchemaDetails.Type = JsonSchemaType.Float;
            Assert.DoesNotThrow(
                () =>
                decorator.DecorateClass(
                    declaration = new CodeTypeDeclaration(), schema, implDetails, internalClassProvider));
            Assert.That(declaration.BaseTypes.Count, Is.EqualTo(0));

            schema.SchemaDetails.Type = JsonSchemaType.Array;
            schema.SchemaDetails.Items = new List<JsonSchema>();
            schema.SchemaDetails.Items.Add(new JsonSchema());
            schema.SchemaDetails.Items.Add(new JsonSchema());
            Assert.DoesNotThrow(
                () =>
                decorator.DecorateClass(
                    declaration = new CodeTypeDeclaration(), schema, implDetails, internalClassProvider));
            Assert.That(declaration.BaseTypes.Count, Is.EqualTo(0));
        }
 public void CreateClassWithMultipleDecorators()
 {
     var once = new CountingDecorator();
     var threeTimes = new CountingDecorator();
     var decorators = new List<ISchemaDecorator>(4) { once, threeTimes, threeTimes, threeTimes };
     var schemaGen = new SchemaGenerator(decorators);
     MockSchema schema = new MockSchema
                             {
                                 Id = "mockSchemaObject",
                                 Name = "mockSchemaObject",
                                 SchemaDetails = MockSchema.CreateSimpleSchema("mockSchemaObject")
                             };
     schemaGen.CreateClass(
         schema, new Dictionary<JsonSchema, SchemaImplementationDetails>(), Enumerable.Empty<string>());
     Assert.AreEqual(1, ((CountingDecorator) decorators[0]).Count);
     Assert.AreEqual(3, ((CountingDecorator) decorators[1]).Count);
     Assert.AreEqual(3, ((CountingDecorator) decorators[2]).Count);
     Assert.AreEqual(3, ((CountingDecorator) decorators[3]).Count);
 }
 public void CreateClassWithSingleDecorator()
 {
     var oneDecorator = new List<ISchemaDecorator>(1) { new CountingDecorator() };
     var schemaGen = new SchemaGenerator(oneDecorator);
     MockSchema schema = new MockSchema
                             {
                                 Id = "mockSchemaObject",
                                 Name = "mockSchemaObject",
                                 SchemaDetails = MockSchema.CreateSimpleSchema("mockSchemaObject")
                             };
     schemaGen.CreateClass(
         schema, new Dictionary<JsonSchema, SchemaImplementationDetails>(), Enumerable.Empty<string>());
     Assert.AreEqual(1, ((CountingDecorator) oneDecorator[0]).Count);
 }