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 ServiceClassGenerator(IService service,
                              IEnumerable<IServiceDecorator> decorators,
                              ResourceContainerGenerator resourceContainerGenerator)
 {
     this.decorators = decorators;
     this.service = service;
     this.resourceContainerGenerator = resourceContainerGenerator;
 }
 public ServiceClassGenerator(IService service,
                              IEnumerable <IServiceDecorator> decorators,
                              ResourceContainerGenerator resourceContainerGenerator)
 {
     this.decorators = decorators;
     this.service    = service;
     this.resourceContainerGenerator = resourceContainerGenerator;
 }
        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);
        }