private void NullMessagesOnlyIsNotAddedToMessagesCollection()
        {
            var ex = new ValidationException();

            ex.AddMessage(null);
            ex.AddMessage(string.Empty);
            Assert.Empty(ex.Messages);
        }
Ejemplo n.º 2
0
        private void TwoMessagesAreAddedWithTwoKeys()
        {
            var ex = new ValidationException();

            ex.AddMessage("key1", "aMessage1");
            ex.AddMessage("key2", "aMessage2");
            Assert.Equal(2, ex.ModelState.Count);
            Assert.Collection(ex.ModelState.Keys, x => Assert.Equal("key1", x), x => Assert.Equal("key2", x));
            Assert.Collection(ex.ModelState.Values, x => Assert.Collection(x, y => Assert.Equal("aMessage1", y)),
                              x => Assert.Collection(x, y => Assert.Equal("aMessage2", y)));
        }
Ejemplo n.º 3
0
        private void TwoMessagesAreAddedWithEmptyKey()
        {
            var ex = new ValidationException();

            ex.AddMessage("aMessage1");
            ex.AddMessage("aMessage2");
            Assert.Equal(1, ex.ModelState.Count);
            Assert.Collection(ex.ModelState.Keys, x => Assert.Empty(x));
            Assert.Collection(ex.ModelState.Values, x => Assert.Collection(x, y => Assert.Equal("aMessage1", y),
                                                                           y => Assert.Equal("aMessage2", y)));
        }
        internal void FillCorrelationContext(string correlationHeader, CorrelationContext context)
        {
            try
            {
                byte[] data = Convert.FromBase64String(correlationHeader);
                string json = Encoding.UTF8.GetString(data);

                dynamic parsedHeader  = JObject.Parse(json);
                string  correlationId = (string)parsedHeader.id;

                if (string.IsNullOrEmpty(correlationId))
                {
                    _logger.LogWarning("Invalid correlationheader, id is required." + correlationHeader);
                    var exception = new ValidationException("Invalid correlationHeader, id is required.", "REQCOR");
                    exception.AddMessage("Invalid correlationHeader, id is required.");
                    throw exception;
                }

                context.TrySetValues(correlationId,
                                     (string)parsedHeader.sourceId,
                                     (string)parsedHeader.sourceName,
                                     (string)parsedHeader.instanceId,
                                     (string)parsedHeader.instanceName,
                                     (string)parsedHeader.userId,
                                     (string)parsedHeader.ipAddress,
                                     correlationHeader);
            }
            catch (FormatException)
            {
                _logger.LogWarning("Invalid correlationheader " + correlationHeader);
                var exception = new ValidationException("Invalid correlationHeader.", "REQCOR");
                exception.AddMessage("Invalid correlationHeader, id is required.");
                throw exception;
            }
        }
        public Task Invoke(HttpContext context, IOptions <CorrelationOptions> options)
        {
            string correlation        = string.Empty;
            var    correlationContext = context.RequestServices.GetService(typeof(ICorrelationContext)) as CorrelationContext;
            var    correlationHeader  = context.Request.Headers[CorrelationHeaders.HeaderKey];

            if (!StringValues.IsNullOrEmpty(correlationHeader))
            {
                FillCorrelationContext(correlationHeader, correlationContext);
            }
            else if (options.Value.CorrelationHeaderRequired && !Regex.IsMatch(context.Request.Path, options.Value.CorrelationHeaderNotRequiredRouteRegex))
            {
                _logger.LogWarning("CorrelationHeader is required.");
                var exception = new ValidationException("CorrelationHeader is required.", "REQCOR");
                exception.AddMessage("CorrelationHeader", "CorrelationHeader is required.");

                throw exception;
            }
            else
            {
                correlationContext.TrySetValues(Guid.NewGuid().ToString(),
                                                _applicationContext.ApplicationId,
                                                _applicationContext.ApplicationName,
                                                _applicationContext.InstanceId,
                                                _applicationContext.InstanceName);

                _logger.LogDebug("CorrelationHeader not found, created correlationId.");
            }
            context.Items.Add("Dgp-Correlation", correlationContext.DgpHeader);
            return(_next.Invoke(context));
        }
 public CorrelationContext ValidateAndSetPropertiesFromDgpHeader(string header)
 {
     try
     {
         var context = JsonConvert.DeserializeObject <CorrelationContext>(Encoding.UTF8.GetString(Convert.FromBase64String(header)));
         if (String.IsNullOrEmpty(context.Id))
         {
             ReplaceInvalidContextByDefault();
             _logger.LogWarning($"Invalid correlationheader, id is required. Header value: '{header}'");
             var exception = new ValidationException("Invalid correlationheader, id is required.", ErrorCode.InvalidCorrelationHeader);
             exception.AddMessage($"Invalid correlationheader, id is required. Header value: '{header}'");
             throw exception;
         }
         context.DgpHeader = header;
         return(context);
     }
     catch (Exception)
     {
         ReplaceInvalidContextByDefault();
         _logger.LogError($"Invalid correlationheader '{header}'");
         var exception = new ValidationException("Invalid correlationheader.", ErrorCode.InvalidCorrelationHeader);
         exception.AddMessage($"Invalid correlationheader '{header}'");
         throw exception;
     }
 }
        private void NullMessageWithValidKeyIsNotAddedToMessagesCollection()
        {
            var ex = new ValidationException();

            ex.AddMessage("aKey", null);
            Assert.Equal(0, ex.Messages.Count);
        }
        private void NullKeyThrowsArgumentNullException()
        {
            var ex     = new ValidationException();
            var result = Assert.Throws <ArgumentNullException>(() => ex.AddMessage(null, "aMessage"));

            Assert.Equal("key", result.ParamName);
        }
Ejemplo n.º 9
0
        private void NullMessageIsNotAdded()
        {
            var ex = new ValidationException();

            ex.AddMessage(null);
            Assert.Empty(ex.ModelState);
        }
        private void DefaultEmptyKeyAddsToCollection()
        {
            var ex = new ValidationException();

            ex.AddMessage(Defaults.ErrorMessage.Key, "aMessage");

            Assert.Equal(1, ex.Messages.Count);
            Assert.Contains("aMessage", ex.Messages.First().Value);
        }
Ejemplo n.º 11
0
        private void MessageIsAddedWithKey()
        {
            var ex = new ValidationException();

            ex.AddMessage("aKey", "aMessage");
            Assert.Equal(1, ex.ModelState.Count);
            Assert.Collection(ex.ModelState.Keys, x => Assert.Equal("aKey", x));
            Assert.Collection(ex.ModelState.Values, x => Assert.Collection(x, y => Assert.Equal("aMessage", y)));
        }
        private void KeyAndMessageAreAddedToMessagesCollection()
        {
            var ex = new ValidationException();

            ex.AddMessage("aKey", "aMessage");

            Assert.Equal(1, ex.Messages.Count);
            Assert.Equal("aKey", ex.Messages.First().Key);
            Assert.Contains("aMessage", ex.Messages.First().Value);
        }
        private void MessageIsAddedToMessagesCollectionWithDefaultKey()
        {
            var ex = new ValidationException();

            ex.AddMessage("aMessage");

            Assert.Equal(1, ex.Messages.Count);
            Assert.Contains("aMessage", ex.Messages.First().Value);
            Assert.Equal(Defaults.ErrorMessage.Key, ex.Messages.First().Key);
        }
Ejemplo n.º 14
0
        public Task Invoke(HttpContext context)
        {
            var correlationHeader = context.Request.Headers[CorrelationHeader.Key];

            if (_options.Value.CorrelationHeaderRequired &&
                !Regex.IsMatch(context.Request.Path, _options.Value.CorrelationHeaderNotRequiredRouteRegex))
            {
                if (StringValues.IsNullOrEmpty(correlationHeader))
                {
                    _logger.LogWarning("CorrelationHeader is required.");
                    var exception = new ValidationException("CorrelationHeader is required.", ErrorCode.RequiredCorrelationHeader);
                    exception.AddMessage("CorrelationHeader", "CorrelationHeader is required.");
                    throw exception;
                }
                else
                {
                    _correlationContextFormatter.ValidateAndSetPropertiesFromDgpHeader(correlationHeader);
                }
            }

            return(_next.Invoke(context));
        }
Ejemplo n.º 15
0
        public Task Invoke(HttpContext context)
        {
            var correlationHeader      = context.Request?.Headers?.FirstOrDefault(x => x.Key.ToLowerInvariant() == CorrelationHeader.Key.ToLowerInvariant()).Value;
            var correlationHeaderValue = !correlationHeader.HasValue || !correlationHeader.Value.Any() ? String.Empty : correlationHeader.Value.FirstOrDefault();

            if (_options.Value.CorrelationHeaderRequired &&
                !Regex.IsMatch(context.Request.Path, _options.Value.CorrelationHeaderNotRequiredRouteRegex))
            {
                if (StringValues.IsNullOrEmpty(correlationHeaderValue))
                {
                    _logger.LogWarning("CorrelationHeader is required.");
                    var exception = new ValidationException("CorrelationHeader is required.", ErrorCode.RequiredCorrelationHeader);
                    exception.AddMessage("CorrelationHeader", "CorrelationHeader is required.");
                    throw exception;
                }
            }
            if (string.IsNullOrWhiteSpace(correlationHeaderValue))
            {
                correlationHeaderValue = _correlationService.GetContext().DgpHeader;
            }
            _correlationContextFormatter.ValidateAndSetPropertiesFromDgpHeader(correlationHeaderValue);

            return(_next.Invoke(context));
        }