Ejemplo n.º 1
0
        public void TestAndQueryable()
        {
            // Arrange
            var translator = new ExpressionToFtsRequestTranslator();
            Expression <Func <IQueryable <EmployeeEntity>, IQueryable <EmployeeEntity> > > expression
                = query => query.Where(e => e.Workstation == "EPRUIZHW006" && e.Manager.StartsWith("John"));

            /*
             * The expression above should be converted to the following FTSQueryRequest and then serialized inside FTSRequestGenerator:
             * "statements": [
             *  { "query":"Workstation:(EPRUIZHW006)"},
             *  { "query":"Manager:(John*)"}
             *  // Operator between queries is AND, in other words result set will fit to both statements above
             * ],
             */
            var expected  = "https://my.com/searchFts?metaType=meta%3Apeople-suite%3Apeople-api%3Acom.epam.e3s.app.people.api.data.EmployeeEntity&query=%7B%22statements%22%3A%5B%7B%22query%22%3A%22Workstation%3A(EPRUIZHW006)%22%7D,%7B%22query%22%3A%22Manager%3A(John*)%22%7D%5D,%22filters%22%3Anull,%22sorting%22%3Anull,%22start%22%3A0,%22limit%22%3A10%7D";
            var generator = new FtsRequestGenerator(@"https://my.com");

            // Act
            string translated = translator.Translate(expression);
            var    uri        = generator.GenerateRequestUrl(typeof(EmployeeEntity), translated);

            // todo: create asserts for this test by yourself, because they will depend on your final implementation
            // Assert
            Assert.Equal(expected, uri.OriginalString);
        }
        public IEnumerable <T> SearchFts <T>(string query, int start = 0, int limit = 0) where T : BaseE3SEntity
        {
            var requestGenerator = new FtsRequestGenerator(_baseAddress);

            Uri request = requestGenerator.GenerateRequestUrl <T>(query, start, limit);

            string resultString = _httpClient.GetStringAsync(request).Result;

            return(JsonConvert.DeserializeObject <FtsResponse <T> >(resultString).Items.Select(t => t.Data));
        }
        public IEnumerable SearchFts(Type type, string query, int start = 0, int limit = 0)
        {
            Type finalType = typeof(FtsResponse <>).MakeGenericType(type);

            if (finalType == null)
            {
                throw new ArgumentNullException(nameof(finalType));
            }
            var items = finalType.GetProperty("items");

            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            var requestGenerator = new FtsRequestGenerator(_baseAddress);
            Uri request          = requestGenerator.GenerateRequestUrl(type, query, start, limit);

            string resultString = _httpClient.GetStringAsync(request).Result;

            object result = JsonConvert.DeserializeObject(resultString, finalType);

            var list = Activator.CreateInstance(typeof(List <>).MakeGenericType(type)) as IList;

            foreach (object item in (IEnumerable)items.GetValue(result))
            {
                var data = item.GetType().GetProperty("data");
                if (data == null)
                {
                    throw new ArgumentNullException(nameof(data));
                }

                list.Add(data.GetValue(item));
            }

            return(list);
        }