public IAnalysis CreateInstance(dynamic jsonConfig)
        {
            if (this.QuantityAnalysisFactory == null || this.QuantitySearchTypesFactory == null || string.IsNullOrEmpty(this.Type))
            {
                throw new MissingRequiredDependencyException();
            }

            int?min = null;
            int?max = null;

            if (!DynamicExtensions.HasProperty(jsonConfig, "search"))
            {
                throw new InvalidJSONException();
            }

            if (DynamicExtensions.HasProperty(jsonConfig, "min"))
            {
                min = DynamicExtensions.AsInt(jsonConfig.min);
            }

            if (DynamicExtensions.HasProperty(jsonConfig, "max"))
            {
                max = DynamicExtensions.AsInt(jsonConfig.max);
            }

            List <IQuantitySearch> quantitySearchList = new List <IQuantitySearch>();

            foreach (var searchItem in jsonConfig.search)
            {
                quantitySearchList.Add(this.QuantitySearchTypesFactory.CreateInstance(searchItem));
            }

            return(this.QuantityAnalysisFactory.CreateInstance(min, max, quantitySearchList));
        }
        public void HasPropertyShouldReturnTrueWhenPropertyExistsAndFalseWhenPropertyDontExist()
        {
            dynamic obj = new ExpandoObject();

            obj.property = "Hola";
            Assert.IsTrue(DynamicExtensions.HasProperty(obj, "property"));
            Assert.IsFalse(DynamicExtensions.HasProperty(obj, "inexistentproperty"));

            dynamic obj2 = new CustomClassForDynamicTest();

            Assert.IsTrue(DynamicExtensions.HasProperty(obj2, "Value"));
            Assert.IsFalse(DynamicExtensions.HasProperty(obj2, "inexistentproperty"));
        }
Exemplo n.º 3
0
        public IQuantitySearch CreateInstance(dynamic jsonConfig)
        {
            if (this.QuantitySearchFactory == null || string.IsNullOrEmpty(this.Type))
            {
                throw new MissingRequiredDependencyException();
            }

            if (!DynamicExtensions.HasProperty(jsonConfig, "sequence"))
            {
                throw new InvalidJSONException();
            }

            return(this.QuantitySearchFactory.CreateInstance(jsonConfig.sequence));
        }
Exemplo n.º 4
0
        public IAnalysis CreateInstance(dynamic jsonConfig)
        {
            if (this.AvailableAnalysisFactories == null)
            {
                throw new MissingRequiredDependencyException();
            }

            if (DynamicExtensions.HasProperty(jsonConfig, "type") && this.AvailableAnalysisFactories.ContainsKey(jsonConfig.type))
            {
                return(this.AvailableAnalysisFactories[jsonConfig.type].CreateInstance(jsonConfig));
            }
            else
            {
                throw new UnknownTypeException();
            }
        }
        public void HasPropertyShouldReturnFalseWhenDynamicObjectIsNull()
        {
            dynamic obj = null;

            Assert.IsFalse(DynamicExtensions.HasProperty(obj, "property"));
        }
        public IEnumerable <IAnalysisSet> CreateInstance(string json)
        {
            ///TODO: separar la conversion de json con la generacion del objeto
            List <IAnalysisSet> result = new List <IAnalysisSet>();

            ExpandoObjectConverter converter = new ExpandoObjectConverter();
            dynamic jsonObject;

            try
            {
                jsonObject = JsonConvert.DeserializeObject <ExpandoObject>(json, converter);
            }
            catch (ArgumentNullException e)
            {
                result = null;
                throw new InvalidJSONException(e.Message, e.InnerException);
            }
            catch (JsonSerializationException e)
            {
                result = null;
                throw new InvalidJSONException(e.Message, e.InnerException);
            }

            if (jsonObject == null || !DynamicExtensions.HasProperty(jsonObject, "configuration") || !DynamicExtensions.HasProperty(jsonObject.configuration, "Count"))
            {
                throw new InvalidJSONException();
            }

            foreach (var config in jsonObject.configuration)
            {
                if (!DynamicExtensions.HasProperty(config, "type"))
                {
                    throw new InvalidJSONException();
                }

                switch (config.type)
                {
                case AnalysisSet.ClassType:
                    List <IAnalysis> analysis = new List <IAnalysis>();

                    if (!DynamicExtensions.HasProperty(config, "name") || !DynamicExtensions.HasProperty(config, "analyses") || !DynamicExtensions.HasProperty(config.analyses, "Count"))
                    {
                        throw new InvalidJSONException();
                    }

                    foreach (var analisysConfig in config.analyses)
                    {
                        analysis.Add(this.AnalysisTypesFactory.CreateInstance(analisysConfig));
                    }

                    IAnalysisSet analysisSet = this.AnalysisSetFactory.CreateInstance(config.name, analysis);
                    result.Add(analysisSet);
                    break;

                default:
                    throw new InvalidJSONException();
                }
            }

            return(result);
        }