public void DecorateClassRenamingTest()
        {
            var dec = new SubresourceClassDecorator();
            var resource = new MockResource();
            const string className = "TestClass";
            var decl = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            var decorators = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Add subresources.
            resource.Resources.Add("Sub1", new MockResource { Name = "Sub1" });
            resource.Resources.Add("Sub2", new MockResource { Name = "Sub2" });

            // Add existing members
            decl.Members.Add(new CodeMemberField { Name = "Sub1Resource" });

            // Test decoration.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
            Assert.AreEqual(3, decl.Members.Count);
            Assert.AreEqual("Sub1Resource", decl.Members[0].Name);
            Assert.AreEqual("Sub1Res", decl.Members[1].Name);
            Assert.AreEqual("Sub2Resource", decl.Members[2].Name);
        }
        public void GenerateRequestClassesTest()
        {
            var decorator = new CountingRequestDecorator();
            var generator = new RequestClassGenerator(new IRequestDecorator[] { decorator });
            var decl = new CodeTypeDeclaration();

            // Create a test resource.
            var resource = new MockResource();
            resource.Methods.Add("MethodA", new MockMethod() { Name = "MethodA"});
            resource.Methods.Add("MethodB", new MockMethod() { Name = "MethodB"});

            // Run the generator
            CodeTypeMemberCollection collection = generator.GenerateRequestClasses(decl, resource);
            Assert.AreEqual(2, decorator.TimesCalled);
            Assert.AreEqual(2, collection.Count);
            Assert.IsInstanceOf<CodeTypeDeclaration>(collection[0]);
            Assert.IsInstanceOf<CodeTypeDeclaration>(collection[1]);
            CollectionAssert.AreEqual(
                new[]
                    {
                        string.Format(RequestClassGenerator.RequestClassNamingScheme, "MethodA"),
                        string.Format(RequestClassGenerator.RequestClassNamingScheme, "MethodB"),
                    },
                from CodeTypeMember m in collection select m.Name);
        } 
        internal CodeTypeDeclaration GenerateSubresource(IResource subresource,
                                     string serviceClassName,
                                     IEnumerable<IResourceDecorator> allDecorators,
                                     RequestClassGenerator requestClassGenerator,
                                     ResourceContainerGenerator containerGenerator,
                                     IEnumerable<string> forbiddenWords)
        {
            // Create a new generator for this subresource
            var subgenerator = new ResourceClassGenerator(
                subresource, serviceClassName, allDecorators, requestClassGenerator, containerGenerator,
                forbiddenWords);

            // Generate and add the subresource.
            return subgenerator.CreateClass();
        }
 public ResourceClassGenerator(IResource resource,
                               string serviceClassName,
                               IEnumerable <IResourceDecorator> decorators,
                               RequestClassGenerator requestClassGenerator,
                               ResourceContainerGenerator resourceContainerGenerator,
                               IEnumerable <string> otherResourceNames)
 {
     this.resource         = resource;
     this.serviceClassName = serviceClassName;
     this.decorators       = decorators;
     className             = resource.IsServiceResource
                     ? serviceClassName
                     : GeneratorUtils.GetClassName(resource, otherResourceNames);
     this.resourceContainerGenerator = resourceContainerGenerator;
     this.requestClassGenerator      = requestClassGenerator;
 }
 public ResourceClassGenerator(IResource resource,
     string serviceClassName,
     IEnumerable<IResourceDecorator> decorators,
     RequestClassGenerator requestClassGenerator,
     ResourceContainerGenerator resourceContainerGenerator,
     IEnumerable<string> otherResourceNames)
 {
     this.resource = resource;
     this.serviceClassName = serviceClassName;
     this.decorators = decorators;
     className = resource.IsServiceResource
                     ? serviceClassName
                     : GeneratorUtils.GetClassName(resource, otherResourceNames);
     this.resourceContainerGenerator = resourceContainerGenerator;
     this.requestClassGenerator = requestClassGenerator;
 }
        private void CreateResources(CodeNamespace clientNamespace,
            string serviceClassName,
            IResource resource,
            RequestClassGenerator requestClassGenerator,
            ResourceContainerGenerator resourceContainerGenerator)
        {
            foreach (var res in resource.Resources.Values.Concat(resource))
            {
                // Create the current list of used names.
                IEnumerable<string> usedNames = resource.Resources.Keys;

                // Create a class for the resource.
                logger.Debug("Adding Resource {0}", res.Name);
                var resourceGenerator = new ResourceClassGenerator(
                    res, serviceClassName, resourceDecorators, requestClassGenerator, resourceContainerGenerator,
                    usedNames);
                var generatedClass = resourceGenerator.CreateClass();
                clientNamespace.Types.Add(generatedClass);
            }
        }
        internal CodeNamespace GenerateClientCode()
        {
            var clientNamespace = CreateNamespace(codeClientNamespace);
            AddClientUsings(clientNamespace);

            ResourceContainerGenerator resourceContainerGenerator =
                new ResourceContainerGenerator(resourceContainerDecorators);
            var requestClassGenerator = new RequestClassGenerator(
                GetSchemaAwareRequestDecorators(codeClientNamespace + ".Data", service));

            var serviceClass =
                new ServiceClassGenerator(service, serviceDecorators, resourceContainerGenerator).CreateServiceClass();
            string serviceClassName = serviceClass.Name;

            clientNamespace.Types.Add(serviceClass);
            CreateResources(
                clientNamespace, serviceClassName, service, requestClassGenerator, resourceContainerGenerator);

            return clientNamespace;
        }
        public void DecorateClassTest()
        {
            var dec = new SubresourceClassDecorator();
            var resource = new MockResource();
            const string className = "TestClass";
            var decl = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            var decorators = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Add subresources.
            resource.Resources.Add("Sub1", new MockResource { Name = "Sub1" });
            resource.Resources.Add("Sub2", new MockResource { Name = "Sub2" });

            // Test decoration.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
            Assert.AreEqual(2, decl.Members.Count);
            Assert.IsInstanceOf<CodeTypeDeclaration>(decl.Members[0]);
            Assert.IsInstanceOf<CodeTypeDeclaration>(decl.Members[1]);
        }
        public void GenerateSubresourceTest()
        {
            var dec = new SubresourceClassDecorator();
            const string serviceName = "TestService";
            var decorators = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);

            // Test generation of the nested class
            var subresource = new MockResource { Name = "Sub" };
            CodeTypeDeclaration decl = dec.GenerateSubresource(
                subresource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            Assert.IsNotNull(decl);
            Assert.AreEqual(decl.Name, "SubResource");
        }
        public void DecorateClassValidationTest()
        {
            var dec = new SubresourceClassDecorator();
            var resource = new MockResource();
            const string className = "TestClass";
            var decl = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            IEnumerable<IResourceDecorator> decorators =
                GoogleServiceGenerator.GetSchemaAwareResourceDecorators("Generated.Data.");

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(
                GoogleServiceGenerator.StandardResourceContainerDecorator);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Validate method:
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(null, className, decl, gen, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, null, decl, gen, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, null, gen, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, null, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, gen, null, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, gen, serviceName, null));

            // Test normal use case.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
        }
        public void GenerateRequestClassTest()
        {
            var decorator = new CountingRequestDecorator();
            var generator = new RequestClassGenerator(new IRequestDecorator[] { decorator });
            var decl = new CodeTypeDeclaration();

            // Create a test resource.
            var resource = new MockResource();
            MockMethod method = new MockMethod() { Name = "MethodA" };
            resource.Methods.Add("MethodA", method);

            // Run the generator
            CodeTypeDeclaration newClass = generator.GenerateRequestClass(resource, method, decl, new string[0]);
            Assert.IsNotNull(newClass);
            Assert.AreEqual(1, decorator.TimesCalled);
            Assert.AreEqual(0, newClass.Members.Count);
        }
        public void GenerateRequestClassDecoratorOrderTest()
        {
            var generator =
                new RequestClassGenerator(
                    new IRequestDecorator[]
                        {
                            new MockRequestDecorator("One"), new MockRequestDecorator("Two"),
                            new MockRequestDecorator("Three")
                        });
            var decl = new CodeTypeDeclaration();
            var resource = new MockResource();
            var method = new MockMethod();

            // Check the order of the generated members.
            var genClass = generator.GenerateRequestClass(resource, method, decl, Enumerable.Empty<string>());
            Assert.AreEqual(3, genClass.Members.Count);
            Assert.AreEqual("One", genClass.Members[0].Name);
            Assert.AreEqual("Two", genClass.Members[1].Name);
            Assert.AreEqual("Three", genClass.Members[2].Name);
        }