コード例 #1
0
ファイル: Program.cs プロジェクト: kseniadovg/Announcements
        static void Main(string[] args)
        {
            ContextProvider contextProvider = new ContextProvider();
            Context         context         = contextProvider.GetContext();

            Announcement a = context.Announcements.FirstOrDefault();
            //a.Body = "20% discount for first 3 month!";

            //AnnouncementService announcementService = new AnnouncementService(contextProvider, new TagService(contextProvider));
            //announcementService.Update(a);

            //Announcement an = new Announcement { Title = "Take your kitten", Body = "Nice kitten for you", Email = "*****@*****.**", PhoneNumber1 = "+380677654325", CreatedTime = DateTime.UtcNow, AuthorId = "user3", Tags = "kitten;cat;pet" };
            //announcementService.Add(an);


            //TagService tagService = new TagService(contextProvider);
            //tagService.FillTagsSet();
            //var v = TagsSingletonContainer.Tags;

            //AnnouncementService announcementService = new AnnouncementService(contextProvider, new TagService(contextProvider));
            //Announcement an = new Announcement { Title = "Take your kitten", Body = "Nice kitten for you", Email = "*****@*****.**", PhoneNumber1 = "+380677654325", CreatedTime = DateTime.UtcNow, AuthorId = "user3", Tags = "kitten;cat;pet" };
            //announcementService.Add(an);

            //v = TagsSingletonContainer.Tags;
        }
コード例 #2
0
        private Task HandleGeneralRequest(HttpContext ctx)
        {
            var requestContext = ContextProvider.GetContext(ctx);

            ctx.Features.Set(requestContext);

            return(HandleRequest(requestContext, ctx.Response));
        }
コード例 #3
0
        public void AddType(Type type, byte[] bytes, ref int index)
        {
            var context = ContextProvider.GetContext(type);

            bytes[index++] = this.Version;

            bytes.AddBytes <int>(context.SpaceId, 4, ref index); // space type
            bytes.AddBytes <short>(context.TypeId, ref index);   // data type
        }
        public AdicionarDroneSteps(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
            _context         = ContextProvider <Drone> .GetContext(null);

            _droneStatusExecutor = Substitute.For <ICommandExecutor <DroneStatusResult> >();
            _statusDroneExecutor = Substitute.For <ICommandExecutor <StatusDroneDto> >();
            _droneRepository     = new DroneRepository(_context, _statusDroneExecutor, _droneStatusExecutor);
            _droneService        = null;
            _droneFacade         = new DroneFacade(_droneService, _droneRepository);
        }
コード例 #5
0
ファイル: ScenarioRunner.cs プロジェクト: vfarah-net/LightBDD
 private static object InstantiateSubStepsContext(ContextProvider provider)
 {
     try
     {
         return(provider.GetContext());
     }
     catch (Exception e)
     {
         throw new InvalidOperationException($"Sub-steps context initialization failed: {e.Message}", e);
     }
 }
コード例 #6
0
        private Task HandleMockRequest(HttpContext ctx)
        {
            var mockId = ctx.GetRouteId();

            var path = mockId.Path.Substring(6);
            var id   = new RouteId(path, mockId.Verb);

            var requestContext = ContextProvider.GetContext(id, ctx);

            requestContext.Config.Handler = "mock";

            return(HandleRequest(requestContext, ctx.Response));
        }
コード例 #7
0
        public CriarPedidoUnitTest()
        {
            _dataContext = ContextProvider <Pedido> .GetContext(null);

            _pedidoService         = Substitute.For <IPedidoService>();
            _clienteRepository     = Substitute.For <IClienteRepository>();
            _pedidoRepository      = Substitute.For <IPedidoRepository>();
            _pedidoDroneRepository = Substitute.For <IPedidoDroneRepository>();

            _droneRepository         = Substitute.For <IDroneRepository>();
            _pagamentoServiceFactory = Substitute.For <IPagamentoServiceFactory>();
            _pagamentoServico        = Substitute.For <IPagamentoServico>();
        }
コード例 #8
0
        public async Task CriarDrone()
        {
            IDroneService _droneService = null;
            var           context       = ContextProvider <Drone> .GetContext(SetupTests.GetDrones());

            IDroneRepository _droneRepository = new MockDroneRepository(context);

            var droneFacade = new DroneFacade(_droneService, _droneRepository);


            var drone = new Drone(120, 80);
            await droneFacade.SaveDroneAsync(drone);

            Assert.True(drone.Perfomance == 160);
        }
コード例 #9
0
        public Type GetType(byte[] bytes, ref int index)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length < (index + 3))
            {
                throw new IndexOutOfRangeException(nameof(bytes.Length));
            }

            byte version = bytes[index++];

            if (version != this.Version)
            {
                throw new VersionMismatchException($"Should be {this.Version} but was {version}");
            }


            int   spaceId = bytes.GetValue <int>(4, ref index);
            short typeId  = bytes.GetValue <short>(2, ref index);
            var   context = ContextProvider.GetContext((spaceId << 4) + typeId);

            return(context.Type);


            //ushort count = bytes.GetValue<ushort>(2, ref index);
            //var startIndex = index;

            //byte version = bytes[index++]; //bytes.GetValue<short>(2, ref index); // data converter versija

            //if (version != this.Version)
            //    throw new VersionMismatchException($"Should be {this.Version} but was {version}");

            //short typeId = bytes.GetValue<short>(2, ref index);

            //var context = ContextProvider.GetContext(typeId);

            //return context.Type;
        }
コード例 #10
0
 public Persister()
 {
     _context = ContextProvider.GetContext();
     _dbSet   = _context.Set <TE>();
 }
コード例 #11
0
        public void AddBytes(object obj, byte[] bytes, ref int index)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var context         = ContextProvider.GetContext(obj.GetType());
            var fieldProperties = context.Properties;

            var startIndex = index;

            index += 4; // rezervuojam ilgiui;

            bytes[index++] = this.Version;
            //bytes.AddBytes<short>(1, 2, ref index); // data converter versija
            bytes.AddBytes <int>(context.SpaceId, 4, ref index);  // space type
            bytes.AddBytes <short>(context.TypeId, 2, ref index); // data type

            for (int p = 0; p < fieldProperties.Length; p++)
            {
                var currentIndex = index;

                var property = fieldProperties[p];

                if (property.Array != null && false)
                {
                    var objValue = property.PropertyInfo.GetValue(obj);
                    if (objValue == null)
                    {
                        if (property.Array.MinLength > 0 || property.Include.IsRequired)
                        {
                            throw new Exception($"Property '{property.PropertyInfo.Name}' min length is '{property.Array.MinLength}'");
                        }

                        continue;
                    }

                    Array value = null;

                    if (objValue is ByteArray)
                    {
                        value = ((ByteArray)objValue).Bytes;
                    }
                    else if (property.PropertyInfo.PropertyType.IsArray)
                    {
                        value = (Array)objValue;
                    }
                    else
                    {
                        throw new Exception($"Property '{property.PropertyInfo.Name}' shoul by array");
                    }


                    if (value.Length > short.MaxValue || value.Length < ushort.MinValue)
                    {
                        throw new Exception($"Property '{property.PropertyInfo.Name}' length should be between '{ushort.MinValue}' and '{short.MaxValue}' but  is '{value.Length}'");
                    }

                    short length       = (short)value.Length;
                    short minLength    = property.Array.MinLength;
                    short maxLength    = property.Array.MaxLength;
                    bool  onlyMinOrMax = property.Array.OnlyMinOrMax;

                    if (length < 0)
                    {
                        throw new Exception($"Property '{property.PropertyInfo.Name}' length should be greater than 0");
                    }

                    if ((bytes.Length - length) < index)
                    {
                        throw new IndexOutOfRangeException("Byte array length is less than needed");
                    }

                    if (length < minLength)
                    {
                        throw new Exception($"Property '{property.PropertyInfo.Name}' min length is '{minLength}' but is '{length}'");
                    }
                    if (length > maxLength)
                    {
                        throw new Exception($"Property '{property.PropertyInfo.Name}' max length is '{maxLength}' but is '{length}'");
                    }
                    if (onlyMinOrMax)
                    {
                        if (length != minLength && length != maxLength)
                        {
                            throw new Exception($"Property '{property.PropertyInfo.Name}' should be '{minLength}' or '{maxLength}' length but is '{length}'");
                        }
                    }

                    if (length == 0)
                    {
                        if (property.Include.IsRequired)
                        {
                            throw new Exception($"Property '{property.PropertyInfo.Name}' is required'");
                        }

                        continue;
                    }

                    bytes[index++] = property.Include.Order;
                    bytes.AddBytes <short>(length, 2, ref index);

                    if (value is byte[] || value is bool[])
                    {
                        Buffer.BlockCopy(value, 0, bytes, index, value.Length);
                        index += value.Length;
                        continue;
                    }

                    var converter = property.Converter;
                    if (converter == null)
                    {
                        throw new KeyNotFoundException($"Converter for property '{property.PropertyInfo.Name}' not found");
                    }

                    for (int i = 0; i < value.Length; i++)
                    {
                        converter.AddBytes(value.GetValue(i), bytes, ref index);
                    }
                    continue;
                }
                else
                {
                    var objValue = property.PropertyInfo.GetValue(obj);
                    if (objValue == null)
                    {
                        if (property.Include.IsRequired)
                        {
                            throw new ArgumentException($"Property '{property.PropertyInfo.Name}' is required but is null");
                        }

                        continue;
                    }

                    var converter = property.Converter;
                    if (converter == null)
                    {
                        throw new KeyNotFoundException($"Converter for property '{property.PropertyInfo.Name}' not found");
                    }

                    bytes[index++] = property.Include.Order;
                    converter.AddBytes(objValue, bytes, ref index);

                    continue;
                }

                if (index <= currentIndex) // at least one byte shoud be added per property
                {
                    throw new Exception("Something went wrong");
                }
            }

            var count = index - (startIndex + 4);

            bytes.AddBytes <int>(count, 4, ref startIndex);
        }
コード例 #12
0
        public object GetObject(byte[] bytes, ref int index)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length == 0)
            {
                throw new ArgumentException(nameof(bytes.Length));
            }

            int count      = bytes.GetValue <int>(4, ref index);
            var startIndex = index;

            byte version = bytes[index++]; //bytes.GetValue<short>(2, ref index); // data converter versija

            if (version != this.Version)
            {
                throw new VersionMismatchException($"Should be {this.Version} but was {version}");
            }

            int   spaceId = bytes.GetValue <int>(4, ref index);
            short typeId  = bytes.GetValue <short>(2, ref index);

            var context         = ContextProvider.GetContext((spaceId << 4) + typeId);
            var fieldProperties = context.Properties;

            var obj = Activator.CreateInstance(context.Type);

            for (int p = 0; p < fieldProperties.Length; p++)
            {
                if ((index - startIndex) > count)
                {
                    throw new Exception("Something went wrong");
                }

                var property = fieldProperties[p];

                var order = -1;
                if ((index - startIndex) < count)
                {
                    order = bytes[index];
                }

                if (property.Include.Order != order || ((index - startIndex) == count))
                {
                    if (property.Include.IsRequired)
                    {
                        throw new ArgumentException($"Property '{property.PropertyInfo.Name}' is required but was null");
                    }
                    continue;
                }

                index += 1;

                if (property.Array != null && false)
                {
                    short length       = bytes.GetValue <short>(2, ref index);
                    short minLength    = property.Array.MinLength;
                    short maxLength    = property.Array.MaxLength;
                    bool  onlyMinOrMax = property.Array.OnlyMinOrMax;

                    if ((bytes.Length - length) < index)
                    {
                        throw new IndexOutOfRangeException("Byte array length is less than needed");
                    }

                    if (length < minLength)
                    {
                        throw new ArgumentException($"Property '{property.PropertyInfo.Name}' min length is '{minLength}' but is '{length}'");
                    }
                    if (length > maxLength)
                    {
                        throw new ArgumentException($"Property '{property.PropertyInfo.Name}' max length is '{maxLength}' but is '{length}'");
                    }
                    if (onlyMinOrMax)
                    {
                        if (length != minLength && length != maxLength)
                        {
                            throw new ArgumentException($"Property '{property.PropertyInfo.Name}' should be '{minLength}' or '{maxLength}' length but is '{length}'");
                        }
                    }

                    if (length <= 0)
                    {
                        if (property.Include.IsRequired)
                        {
                            throw new ArgumentException($"Property '{property.PropertyInfo.Name}' is required'");
                        }

                        continue;
                    }

                    if (property.PropertyInfo.PropertyType.Equals(typeof(byte[])) ||
                        property.PropertyInfo.PropertyType.Equals(typeof(ByteArray)))
                    {
                        var array = new byte[length];
                        Array.Copy(bytes, index, array, 0, array.Length);
                        index += array.Length;

                        if (property.PropertyInfo.PropertyType.Equals(typeof(byte[])))
                        {
                            property.PropertyInfo.SetValue(obj, array);
                            continue;
                        }

                        if (property.PropertyInfo.PropertyType.Equals(typeof(ByteArray)))
                        {
                            var byteArray = new ByteArray(array);
                            property.PropertyInfo.SetValue(obj, byteArray);
                            continue;
                        }

                        throw new ArgumentException($"Property '{property.PropertyInfo.Name}' type should be byte[] or ByteArray");
                    }
                    else if (property.PropertyInfo.PropertyType.Equals(typeof(bool[])))
                    {
                        var array = new bool[length];
                        Buffer.BlockCopy(bytes, index, array, 0, array.Length);
                        index += array.Length;

                        property.PropertyInfo.SetValue(obj, array);
                        continue;
                    }

                    var converter = property.Converter;
                    if (converter == null)
                    {
                        throw new KeyNotFoundException($"Converter for property '{property.PropertyInfo.Name}' not found");
                    }

                    var elArray = Array.CreateInstance(property.PropertyInfo.PropertyType
                                                       .GetElementType(), length);

                    for (int i = 0; i < length; i++)
                    {
                        var elObj = converter.GetObject(bytes, ref index);
                        elArray.SetValue(elObj, i);
                    }
                    property.PropertyInfo.SetValue(obj, elArray);
                    continue;
                }
                else
                {
                    var converter = property.Converter;
                    if (converter == null)
                    {
                        throw new KeyNotFoundException($"Converter for property '{property.PropertyInfo.Name}' not found");
                    }


                    var value = converter.GetObject(bytes, ref index);
                    property.PropertyInfo.SetValue(obj, value);

                    continue;
                }
            }

            if ((index - startIndex) != count)
            {
                throw new Exception("Something went wrong");
            }

            return(obj);
        }
コード例 #13
0
        public ClienteTest()
        {
            _context = ContextProvider <Cliente> .GetContext(SetupTests.GetClientes());

            _repository = new ClienteRepositoryFake(_context);
        }
コード例 #14
0
 public void BaseTestInitialize()
 {
     _context = ContextProvider.GetContext();
     _client  = new TestServerApiClient();
     ContextProvider.ResetDatabase();
 }