Example #1
0
        internal BaseService(IServiceFactory factory, JsonDictionary values, FactoryParameters param)
            : this(factory)
        {
            values.ThrowIfNull("values");
            param = param ?? new FactoryParameters();

            // Set required properties
            Version     = values.GetMandatoryValue <string>("version");
            Name        = values.GetMandatoryValue <string>("name");
            information = values;

            // Set optional properties
            Id                = values.GetValueAsNull("id") as string;
            Labels            = values.GetValueAsStringListOrEmpty("labels").ToList().AsReadOnly();
            Features          = values.GetValueAsStringListOrEmpty("features").ToList().AsReadOnly();
            DocumentationLink = values.GetValueAsNull("documentationLink") as string;
            Protocol          = values.GetValueAsNull("protocol") as string;
            Description       = values.GetValueAsNull("description") as string;
            Title             = values.GetValueAsNull("title") as string;
            Scopes            = LoadScopes();
            Parameters        = LoadParameters();

            // Load resources
            rootResource = CreateResource(new KeyValuePair <string, object>("", information));

            // Determine the Server URL and (optional) Base Path
            param.ServerUrl.ThrowIfNull("param.ServerUrl");
            ServerUrl = param.ServerUrl;
            BasePath  = param.BasePath;
        }
        public void ServiceFactoryDiscoveryV1_0ConstructorSuccessTest()
        {
            var param = new FactoryParameters("http://server/");
            var json = (JsonDictionary) JsonReader.Parse(ServiceFactoryTest.DiscoveryV1_0Example);
            var service = ServiceFactory.Get(DiscoveryVersion.Version_1_0).CreateService(json, param);

            Assert.IsInstanceOf(typeof(ServiceV1_0), service);
            Assert.AreEqual("adsense", service.Name);
        }
Example #3
0
 /// <summary>
 /// Creates a v0.3 service
 /// </summary>
 public ServiceV0_3(IServiceFactory factory, JsonDictionary values, FactoryParameters param)
     : base(factory, values, param)
 {
     // If no BasePath has been set, then retrieve it from the json document
     if (BasePath.IsNullOrEmpty())
     {
         BasePath = information.GetMandatoryValue <string>(RestBasePathField);
     }
 }
Example #4
0
 /// <summary>
 /// Create a service object for a specific version of discovery.
 /// </summary>
 /// <param name="version">Version of discovery to use.</param>
 /// <param name="param">Parameters intended to initialize the factory.</param>
 /// <returns></returns>
 public IService GetService(DiscoveryVersion version, FactoryParameters param)
 {
     var factory = ServiceFactory.Get(version);
     using (var documentStream = DiscoveryDevice.Fetch())
     {
         // Parse the document.
         var dictionary = Json.JsonReader.Parse(documentStream) as Json.JsonDictionary;
         return factory.CreateService(dictionary, param);
     }
 }
 /// <summary>
 /// Sets BasePath property. If the the given factory params contain BasePath use it, otherwise use the given
 /// base path field to query the dicsovery doc.
 /// </summary>
 protected void SetBasePath(FactoryParameters param, string basePathField)
 {
     if (param != null && param.BasePath != null)
     {
         BasePath = param.BasePath;
     }
     else
     {
         BasePath = discoveryDoc.GetMandatoryValue <string>(basePathField);
     }
 }
Example #6
0
        /// <summary>
        /// Create a service object for a specific version of discovery.
        /// </summary>
        /// <param name="version">Version of discovery to use.</param>
        /// <param name="param">Parameters intended to initialize the factory.</param>
        /// <returns></returns>
        public IService GetService(DiscoveryVersion version, FactoryParameters param)
        {
            var factory = ServiceFactory.Get(version);

            using (var documentStream = DiscoveryDevice.Fetch())
            {
                // Parse the document.
                var dictionary = Json.JsonReader.Parse(documentStream) as Json.JsonDictionary;
                return(factory.CreateService(dictionary, param));
            }
        }
        public void ServiceFactoryDiscoveryV1_0ConstructorFailTest()
        {
            Assert.Throws<ArgumentNullException>(() => new FactoryParameters((string)null));
            var json = (JsonDictionary) JsonReader.Parse(ServiceFactoryTest.DiscoveryV1_0Example);
            var factory = ServiceFactory.Get(DiscoveryVersion.Version_1_0);

            // Test if the constructor will fail if required arguments are missing
            var param = new FactoryParameters();
            Assert.Throws(typeof(ArgumentNullException), () => factory.CreateService(null, param));
            Assert.DoesNotThrow(() => factory.CreateService(json, null));

            json = (JsonDictionary) JsonReader.Parse(BadDiscoveryv1_0_No_Name);
            Assert.Throws(typeof(ArgumentException), () => factory.CreateService(json, param));
        }
Example #8
0
        public void ConstuctorArgumentValidationTest()
        {
            var param = new FactoryParameters("http://server/");
            var js = new JsonDictionary();
            js["name"] = "TestName";
            js["version"] = "v1";
            js["restBasePath"] = "test/path";

            var factory = ServiceFactoryDiscoveryV0_3.GetInstance();

            Assert.DoesNotThrow(() => factory.CreateService(js, null));
            Assert.Throws(typeof(ArgumentNullException), () => factory.CreateService(null, param));

            factory.CreateService(js, param);
        }
        internal BaseService(IServiceFactory factory, JsonDictionary discoveryDoc, FactoryParameters param)
            : this(factory)
        {
            discoveryDoc.ThrowIfNull("discoveryDoc");
            this.discoveryDoc = discoveryDoc;

            // Set required properties
            Version       = discoveryDoc.GetMandatoryValue <string>("version");
            Name          = discoveryDoc.GetMandatoryValue <string>("name").Replace(" ", "");
            CanonicalName = discoveryDoc.GetValueAsNull("canonicalName") as string;
            if (!string.IsNullOrEmpty(CanonicalName))
            {
                CanonicalName = CanonicalName.Replace(" ", "");
            }

            // Set optional properties
            Id                = discoveryDoc.GetValueAsNull("id") as string;
            Labels            = discoveryDoc.GetValueAsStringListOrEmpty("labels").ToList().AsReadOnly();
            Features          = discoveryDoc.GetValueAsStringListOrEmpty("features").ToList().AsReadOnly();
            DocumentationLink = discoveryDoc.GetValueAsNull("documentationLink") as string;
            Protocol          = discoveryDoc.GetValueAsNull("protocol") as string;
            Description       = discoveryDoc.GetValueAsNull("description") as string;
            Title             = discoveryDoc.GetValueAsNull("title") as string;
            Scopes            = LoadScopes();
            Parameters        = LoadParameters();

            // Load resources
            rootResource = CreateResource(new KeyValuePair <string, object>("", discoveryDoc));

            // Sets the Server URL (Base Path will be set on the concete class)
            if (param != null && param.ServerUrl != null)
            {
                ServerUrl = param.ServerUrl;
            }
            else
            {
                ServerUrl = discoveryDoc.GetMandatoryValue("rootUrl") as string;
            }
        }
Example #10
0
 private ServiceV0_3 CreateService()
 {
     var param = new FactoryParameters("http://example.com/");
     var json = (JsonDictionary) JsonReader.Parse(ServiceFactoryImplTest.BuzzV0_3_Json);
     return (ServiceV0_3)ServiceFactoryDiscoveryV0_3.GetInstance().CreateService(json, param);
 }
 /// <summary>
 /// Creates a v0.3 service
 /// </summary>
 public ServiceV0_3(IServiceFactory factory, JsonDictionary values, FactoryParameters param)
     : base(factory, values, param)
 {
     SetBasePath(param, RestBasePathField);
 }
Example #12
0
 public virtual IService CreateService(JsonDictionary dictionary, FactoryParameters param)
 {
     return(new ServiceV0_3(this, dictionary, param));
 }
 public virtual IService CreateService(JsonDictionary dictionary, FactoryParameters param)
 {
     return new ServiceV1_0(this, dictionary, param);
 }
        public void ServiceFactoryDiscovery_ConstructorSuccessTest()
        {
            var param = new FactoryParameters("http://example.com", "base");
            var json = (JsonDictionary) JsonReader.Parse(BuzzV0_3_Json);
            var fact = ServiceFactory.Get(DiscoveryVersion.Version_0_3);
            var service = fact.CreateService(json, param);

            Assert.IsInstanceOf(typeof(ServiceV0_3), service);
            Assert.That(service.Name, Is.EqualTo("buzz"));
            Assert.That(service.BaseUri.ToString(), Is.EqualTo("http://example.com/base"));
        }
        public void ServiceFactoryDiscovery_ConstructorFailTest()
        {
            var param = new FactoryParameters("http://example.com", "base");
            var json = (JsonDictionary) JsonReader.Parse(BuzzV0_3_Json);

            json = (JsonDictionary) JsonReader.Parse(Discovery.ServiceFactoryImplTest.BadDiscoveryv1_0_No_Name);
            Assert.Throws(typeof(ArgumentException), () => ServiceFactory.Get(DiscoveryVersion.Version_0_3).CreateService(json, param));
        }