Пример #1
0
        public void CompareByteArrays_AreEqual_Return2Diffs(byte[] a, byte[] b)
        {
            ComparisonResult result = BitsDiff.CompareByteArrays(a, b);

            Assert.AreEqual(result.AreEqual, ComparisonResultEnum.Equal);
            Assert.AreEqual(result.OffsetsLength, 2);
            Assert.AreEqual(result.Offsets, new int[] { 12, 13 });
        }
Пример #2
0
        public void CompareByteArrays_AreEqual_ReturnZeroDiffs(byte[] a, byte[] b)
        {
            ComparisonResult result = BitsDiff.CompareByteArrays(a, b);

            Assert.AreEqual(result.AreEqual, ComparisonResultEnum.Equal);
            Assert.AreEqual(result.Offsets.Length, 0);
            Assert.AreEqual(result.OffsetsLength, 0);
        }
        public IActionResult Common(int id, MessageBinding model, string method)
        {
            _logger.LogDebug(string.Format("({0}-id:{1},model:{2})", method, id,
                                           JsonConvert.SerializeObject(model, Formatting.Indented)));

            if (id > 0 && model != null && !string.IsNullOrEmpty(model.Payload) && Methods.IsValidBase64(model.Payload))
            {
                using (var db = new DatabaseContext())
                {
                    List <Message> messages = db.Messages.Where(i => i.MessageId == id).ToList();

                    if (messages.Count > 0)
                    {
                        Message dbMessage = messages[messages.Count - 1];

//                        _logger.LogDebug(string.Format("{0}", JsonConvert.SerializeObject(dbMessage, Formatting.Indented)));
//
                        byte[] Base64ToByteArrayOfDatabase = Methods.DecodeBase64ToByteArray(dbMessage.Payload);
                        byte[] Base64ToByteArrayOfModel    = Methods.DecodeBase64ToByteArray(model.Payload);


                        ComparisonResult res = BitsDiff.CompareByteArrays(Base64ToByteArrayOfDatabase, Base64ToByteArrayOfModel);


                        string middleEndpointUrl = Configuration["EndPoints:middle"];
                        int    v = Convert.ToInt32(HttpContext.GetRequestedApiVersion().ToString());

                        _logger.LogDebug(string.Format("({0})", middleEndpointUrl));

                        var resultFromMiddleEndPoint = DifferencesClient.Middle(middleEndpointUrl, v, id, res).Result;

                        _logger.LogDebug(string.Format("({0} id:{1},message:{2})", method, id,
                                                       JsonConvert.SerializeObject(resultFromMiddleEndPoint, Formatting.Indented)));

                        return(Ok(resultFromMiddleEndPoint));
                    }
                    else
                    {
                        try
                        {
                            Message message = new Message()
                            {
                                MessageId = id,
                                Payload   = model.Payload
                            };

                            db.Messages.Add(message);
                            db.SaveChanges();

                            _logger.LogDebug(string.Format(
                                                 "({0}:{1}, Message with ID: {2} does not exist and added to the database.", method,
                                                 message,
                                                 id));

                            return(Ok());
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(string.Format("({0}:{1})", method, e));
                            return(BadRequest());
                        }
                    }
                }
            }

            _logger.LogDebug(string.Format("({0}:BadRequest)", method));
            return(BadRequest());
        }