예제 #1
0
파일: PersonTest.cs 프로젝트: nissan/Beef
 public void A110_Validation_Empty()
 {
     ExpectValidationException.Throws(
         () => PersonValidator.Default.Validate(new Person()).ThrowOnError(),
         "First Name is required.",
         "Last Name is required.",
         "Gender is required.",
         "Birthday is required.");
 }
예제 #2
0
파일: PersonTest.cs 프로젝트: mtikoian/Beef
        public void A110_Validation_Null()
        {
            ExpectValidationException.Throws(
                () => new PersonManager(new Mock <IPersonDataSvc>().Object).CreateAsync(null),
                "Value is required.");

            ExpectValidationException.Throws(
                () => new PersonManager(new Mock <IPersonDataSvc>().Object).UpdateAsync(null, 1.ToGuid()),
                "Value is required.");
        }
예제 #3
0
파일: PersonTest.cs 프로젝트: nissan/Beef
 public void A120_Validation_Invalid()
 {
     ExpectValidationException.Throws(
         () => PersonValidator.Default.Validate(new Person {
         FirstName = 'x'.ToLongString(), LastName = 'x'.ToLongString(), Gender = "X", Birthday = DateTime.Now.AddDays(1)
     }).ThrowOnError(),
         "First Name must not exceed 100 characters in length.",
         "Last Name must not exceed 100 characters in length.",
         "Gender is invalid.",
         "Birthday must be less than or equal to Today.");
 }
예제 #4
0
        public void A110_Validation_Null()
        {
            TestSetUp.CreateMock <IPersonData>();
            ExpectValidationException.Run(
                () => (new PersonManager()).CreateAsync(null),
                "Value is required.");

            ExpectValidationException.Run(
                () => (new PersonManager()).UpdateAsync(null, 1.ToGuid()),
                "Value is required.");
        }
예제 #5
0
 public void A150_ArgsValidator_Invalid()
 {
     ExpectValidationException.Throws(() =>
     {
         var ta = new TransactionArgs {
             FromDate = new DateTime(2020, 03, 01), ToDate = new DateTime(2020, 02, 01), MinAmount = 100m, MaxAmount = 80m, Text = "Best*Buy"
         };
         TransactionArgsValidator.Default.Validate(ta).ThrowOnError();
     },
                                      "Oldest time must be less than or equal to Newest time.",
                                      "Min Amount must be less than or equal to Max Amount.",
                                      "Text contains invalid or non-supported wildcard selection.");
 }
예제 #6
0
파일: PersonTest.cs 프로젝트: mtikoian/Beef
 public void A130_Validation_Invalid()
 {
     ExpectValidationException.Throws(
         () => new PersonManager(new Mock <IPersonDataSvc>().Object).CreateAsync(new Person()
     {
         FirstName = 'x'.ToLongString(), LastName = 'x'.ToLongString(), Birthday = DateTime.Now.AddDays(1), Gender = "X", EyeColor = "Y"
     }),
         "First Name must not exceed 50 characters in length.",
         "Last Name must not exceed 50 characters in length.",
         "Gender is invalid.",
         "Eye Color is invalid.",
         "Birthday must be less than or equal to Today.");
 }
예제 #7
0
파일: PersonTest.cs 프로젝트: mtikoian/Beef
        public async Task A110_Validation_Empty()
        {
            await ExpectValidationException.ThrowsAsync(
                () => new PersonManager(new Mock <IPersonDataSvc>().Object).CreateAsync(new Person()),
                "First Name is required.",
                "Last Name is required.",
                "Gender is required.",
                "Birthday is required.");

            await ExpectValidationException.ThrowsAsync(
                () => new PersonManager(new Mock <IPersonDataSvc>().Object).UpdateAsync(new Person(), 1.ToGuid()),
                "First Name is required.",
                "Last Name is required.",
                "Gender is required.",
                "Birthday is required.");
        }
예제 #8
0
        public async Task A110_Validation_Empty()
        {
            TestSetUp.CreateMock <IPersonData>();
            await ExpectValidationException.RunAsync(
                () => (new PersonManager()).CreateAsync(new Person()),
                "First Name is required.",
                "Last Name is required.",
                "Gender is required.",
                "Birthday is required.");

            await ExpectValidationException.RunAsync(
                () => (new PersonManager()).UpdateAsync(new Person(), 1.ToGuid()),
                "First Name is required.",
                "Last Name is required.",
                "Gender is required.",
                "Birthday is required.");
        }
예제 #9
0
        /// <summary>
        /// Check the result to make sure it is valid.
        /// </summary>
        /// <param name="result">The <see cref="WebApiAgentResult"/>.</param>
        /// <param name="sw">The <see cref="Stopwatch"/> used to measure <see cref="WebApiAgentBase"/> invocation.</param>
        protected void ResultCheck(WebApiAgentResult result, Stopwatch sw)
        {
            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            // Log to output.
            TestContext.Out.WriteLine("");
            TestContext.Out.WriteLine("AGENT TESTER...");
            TestContext.Out.WriteLine("");
            TestContext.Out.WriteLine($"REQUEST >");
            TestContext.Out.WriteLine($"Request: {result.Request.Method} {result.Request.RequestUri}");

            if (!string.IsNullOrEmpty(Username))
            {
                TestContext.Out.WriteLine($"Username: {Username}");
            }

            TestContext.Out.WriteLine($"Headers: {(result.Request.Headers == null || !result.Request.Headers.Any() ? "none" : "")}");
            if (result.Request.Headers != null && result.Request.Headers.Any())
            {
                foreach (var hdr in result.Request.Headers)
                {
                    var sb = new StringBuilder();
                    foreach (var v in hdr.Value)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(", ");
                        }

                        sb.Append(v);
                    }

                    TestContext.Out.WriteLine($"  {hdr.Key}: {sb}");
                }
            }

            JToken?json = null;

            if (result.Request.Content != null)
            {
                // HACK: The Request Content is a forward only stream that is already read; we need to reset this private variable back to the start.
                if (result.Request.Content is StreamContent)
                {
                    var fi = typeof(StreamContent).GetField("_content", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    var ms = (MemoryStream)fi !.GetValue(result.Request.Content) !;
                    ms.Position = 0;
                }

                // Parse out the content.
                try
                {
                    json = JToken.Parse(result.Request.Content.ReadAsStringAsync().Result);
                }
#pragma warning disable CA1031 // Do not catch general exception types; by-design.
                catch (Exception) { }
#pragma warning restore CA1031

                TestContext.Out.WriteLine($"Content: [{result.Request.Content?.Headers?.ContentType?.MediaType ?? "None"}]");
                TestContext.Out.WriteLine(json == null ? result.Request.Content?.ToString() : json.ToString());
            }

            TestContext.Out.WriteLine("");
            TestContext.Out.WriteLine($"RESPONSE >");
            TestContext.Out.WriteLine($"HttpStatusCode: {result.StatusCode} ({(int)result.StatusCode})");
            TestContext.Out.WriteLine($"Elapsed (ms): {(sw == null ? "none" : sw.ElapsedMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture))}");

            var hdrs = result.Response?.Headers?.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
            TestContext.Out.WriteLine($"Headers: {(hdrs == null || !hdrs.Any() ? "none" : "")}");
            if (hdrs != null && hdrs.Any())
            {
                foreach (var hdr in hdrs)
                {
                    TestContext.Out.WriteLine($"  {hdr}");
                }
            }

            TestContext.Out.WriteLine($"Messages: {(result.Messages == null || result.Messages.Count == 0 ? "none" : "")}");

            if (result.Messages != null && result.Messages.Count > 0)
            {
                foreach (var m in result.Messages)
                {
                    TestContext.Out.WriteLine($" {m.Type}: {m.Text} {(m.Property == null ? "" : "(" + m.Property + ")")}");
                }

                TestContext.Out.WriteLine("");
            }

            json = null;
            if (!string.IsNullOrEmpty(result.Content) && result.Response?.Content?.Headers?.ContentType?.MediaType == "application/json")
            {
                try
                {
                    json = JToken.Parse(result.Content);
                }
#pragma warning disable CA1031 // Do not catch general exception types; by-design.
                catch (Exception) { /* This is being swallowed by design. */ }
#pragma warning restore CA1031
            }

            TestContext.Out.Write($"Content: [{result.Response?.Content?.Headers?.ContentType?.MediaType ?? "none"}]");
            if (json != null)
            {
                TestContext.Out.WriteLine("");
                TestContext.Out.WriteLine(json.ToString());
            }
            else
            {
                TestContext.Out.WriteLine($"{(string.IsNullOrEmpty(result.Content) ? "none" : result.Content)}");
            }

            TestContext.Out.WriteLine("");
            TestContext.Out.WriteLine($"EVENTS PUBLISHED >");
            var events = ExpectEvent.GetEvents(CorrelationId);
            if (events.Count == 0)
            {
                TestContext.Out.WriteLine("  None.");
            }
            else
            {
                foreach (var e in events)
                {
                    TestContext.Out.WriteLine($"  Subject: {e.Subject}, Action: {e.Action}");
                }
            }

            TestContext.Out.WriteLine("");
            TestContext.Out.WriteLine($"LOGGING >");
            var messages = CorrelationIdLogger.GetMessages(CorrelationId);
            if (messages.Count == 0)
            {
                TestContext.Out.WriteLine("  None.");
            }
            else
            {
                foreach (var l in messages)
                {
                    TestContext.Out.WriteLine($"{l}");
                }
            }

            TestContext.Out.WriteLine("");
            TestContext.Out.WriteLine(new string('=', 80));
            TestContext.Out.WriteLine("");

            // Perform checks.
            if (_expectedStatusCode.HasValue && _expectedStatusCode != result.StatusCode)
            {
                Assert.Fail($"Expected HttpStatusCode was '{_expectedStatusCode} ({(int)_expectedStatusCode})'; actual was {result.StatusCode} ({(int)result.StatusCode}).");
            }

            if (_expectedErrorType.HasValue && _expectedErrorType != result.ErrorType)
            {
                Assert.Fail($"Expected ErrorType was '{_expectedErrorType}'; actual was '{result.ErrorType}'.");
            }

            if (_expectedErrorMessage != null && _expectedErrorMessage != result.ErrorMessage)
            {
                Assert.Fail($"Expected ErrorMessage was '{_expectedErrorMessage}'; actual was '{result.ErrorMessage}'.");
            }

            if (_expectedMessages != null)
            {
                ExpectValidationException.CompareExpectedVsActual(_expectedMessages, result.Messages);
            }
        }