예제 #1
0
        public void WhenErrorCodeThenProcessorReturnsBody()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.InternalServerError,
                ContentType = "application/json",
                Content     = "{\"StatusCode\": 500, \"Message\": \"Generic error\", \"Details\": \"Details\"}"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <int, IJsonSerializer>(
                new RestExceptionProcessor <int>().Default());

            ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

            // Act
            try
            {
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);
            }
            // arrange
            catch (RestException ex)
            {
                Assert.AreEqual(HttpStatusCode.InternalServerError, ex.HttpError.StatusCode);
                Assert.AreEqual("Generic error", ex.HttpError.Message);
                Assert.AreEqual("Details", ex.HttpError.Details);
                throw;
            }

            Assert.IsTrue(processor.CanProcess(response));
        }
예제 #2
0
            public void WhenComplexProcessorThenAddNeutralProcessorReturnsDefaultAtTheEnd()
            {
                // Arrange
                var processor = new ProcessorStructure <string, IJsonSerializer>(
                    new ExampleRecursiveProcessor().AddProcessors(
                        new ExampleSimpleProcessor()
                        ),
                    new ExampleSimpleProcessor()
                    );

                // Act
                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);

                // Assert
                Assert.AreEqual(3, processor.Count);
                Assert.IsInstanceOfType(processor[0], typeof(ExampleRecursiveProcessor));
                Assert.IsInstanceOfType(processor[1], typeof(ExampleSimpleProcessor));
                Assert.IsInstanceOfType(processor[2], typeof(SuccessProcessor <string, IJsonSerializer>));

                var recNode = (ExampleRecursiveProcessor)processor[0];

                Assert.AreEqual(2, recNode.ProcessorStructure.Count);
                Assert.IsInstanceOfType(recNode.ProcessorStructure[0], typeof(ExampleSimpleProcessor));
                Assert.IsInstanceOfType(recNode.ProcessorStructure[1], typeof(SuccessProcessor <string, IJsonSerializer>));
            }
예제 #3
0
            public void WhenErrorRestThenReturnLeft()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.InternalServerError,
                    ContentType = "application/json",
                    Content     = "{'StatusCode': 400, 'Message': 'Message', 'Details': 'Details'}"
                };

                var processor = new ProcessorStructure <EitherStrict <RestBusinessError, int>, IJsonSerializer>(
                    new EitherRestErrorProcessor <int>().Default()
                    .AddProcessors(new SuccessProcessor <int>().Default())
                    );

                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.IsTrue(res.IsLeft);
                Assert.AreEqual(RestErrorType.ValidationError, res.Left.ErrorType);
                Assert.AreEqual("Message", res.Left.Message);
                Assert.AreEqual("Details", res.Left.Details);
            }
예제 #4
0
            public void WhenOkThenReturnRight()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.OK,
                    ContentType = "application/json",
                    Content     = "10"
                };
                var processor = new ProcessorStructure <EitherStrict <RestBusinessError, OptionStrict <int> >, IJsonSerializer>(
                    new EitherRestErrorProcessor <OptionStrict <int> >().Default()
                    .AddProcessors(new OptionAsNotFoundProcessor <int>()
                                   .AddProcessors(new SuccessProcessor <int>().Default()))
                    );

                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.IsTrue(res.IsRight);
                Assert.IsTrue(res.Right.HasValue);
                Assert.AreEqual(10, res.Right.Value);
            }
예제 #5
0
 protected override EitherStrict <TLeft, TRight> ProcessSub(IRestResponse response, TSerializer serializer)
 {
     if (ProcessorStructure.CanProcess(response))
     {
         return(ProcessorStructure.Process(response, serializer));
     }
     else
     {
         return(leftProcessorStructure.Process(response, serializer));
     }
 }
 protected override OptionStrict <TResult> ProcessSub(IRestResponse response, TSerializer serializer)
 {
     if (response.StatusCode == HttpStatusCode.NotFound)
     {
         return(OptionStrict <TResult> .Nothing);
     }
     else
     {
         return(ProcessorStructure.Process(response, serializer));
     }
 }
예제 #7
0
            public void WhenSingleProcessorIsDefaultWithSuccessThenAddNeutralProcessorReturnsTheSameOne()
            {
                // Arrange
                var processor = new ProcessorStructure <string, IJsonSerializer>(new SuccessProcessor <string>());

                // Act
                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);

                // Assert
                Assert.AreEqual(1, processor.Count);
                Assert.IsInstanceOfType(processor[0], typeof(SuccessProcessor <string, IJsonSerializer>));
            }
예제 #8
0
            public void WhenSingleProcessorThenAddNeutralProcessorReturnsDefaultAtTheEnd()
            {
                // Arrange
                var processor = new ProcessorStructure <string, IJsonSerializer>(new ExampleSimpleProcessor());

                // Act
                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);

                // Assert
                Assert.AreEqual(2, processor.Count);
                Assert.IsInstanceOfType(processor[0], typeof(ExampleSimpleProcessor));
                Assert.IsInstanceOfType(processor[1], typeof(SuccessProcessor <string, IJsonSerializer>));
            }
예제 #9
0
        public void WhenOkCodeThenProcessorCantProcessIt()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.OK,
                ContentType = "application/json",
                Content     = "{\"StatusCode\": 200, \"Message\": \"Generic error\", \"Details\": \"Details\"}"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <int, IJsonSerializer>(
                new RestExceptionProcessor <int>().Default());

            // Act - Assert
            Assert.IsFalse(processor.CanProcess(response));
        }
예제 #10
0
        public void WhenDifferentCodeThenCantProcessIt()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.InternalServerError,
                ContentType = "application/json",
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <OptionStrict <int>, IJsonSerializer>(
                new OptionAsNotFoundProcessor <int>().AddProcessors(new SuccessProcessor <int>().Default())
                );

            // Act - Assert
            Assert.IsFalse(processor.CanProcess(response));
        }
예제 #11
0
        public void WhenOptionAndUnitProcessorWith404ThenReturnsNothing()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.NotFound,
                ContentType = "application/json",
                Content     = "10"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <OptionStrict <Unit>, IJsonSerializer>(new OptionAsNotFoundProcessor <Unit>().AddProcessors(new UnitAsSuccessProcessor()));

            // Act
            Assert.IsTrue(processor.CanProcess(response));
            var res = processor.Process(response, jsonConverter);

            // Assert
            Assert.IsFalse(res.HasValue);
        }
예제 #12
0
            public void When500ThenThrowsException()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.InternalServerError,
                    ContentType = "application/json",
                    Content     = "\"Error\""
                };
                var jsonConverter = new JsonSerializer();
                var processor     = new ProcessorStructure <string, IJsonSerializer>(
                    new RestExceptionProcessor <string>().Default());

                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);
                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                processor.Process(response, jsonConverter);
            }
예제 #13
0
            public void WhenTwoProcessorsCanProcessItThenOnlyFirstOneDoes()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.InternalServerError,
                    ContentType = "application/json",
                    Content     = "10"
                };
                var jsonConverter = new JsonSerializer();
                var processor     = new ProcessorStructure <string, IJsonSerializer>(
                    new ExampleSimpleProcessorAlternate(),
                    new ExampleSimpleProcessor()
                    );

                // Act
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.AreEqual("20", res);
            }
예제 #14
0
        public void When200ThenProcessorReturnsJust()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.OK,
                ContentType = "application/json",
                Content     = "10"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <OptionStrict <int>, IJsonSerializer>(
                new OptionAsNotFoundProcessor <int>().AddProcessors(new SuccessProcessor <int>().Default())
                );

            // Act
            Assert.IsTrue(processor.CanProcess(response));
            var res = processor.Process(response, jsonConverter);

            // Assert
            Assert.IsTrue(res.HasValue);
            Assert.AreEqual(10, res.Value);
        }
예제 #15
0
            public void WhenRecursiveProcessorWithGenericParametersThenAddNeutralProcessorReturnsTheSame()
            {
                // Arrange
                var processor = new ProcessorStructure <string, IJsonSerializer>(
                    new ExampleRecursiveProcessorAlternate <string>().AddProcessors(
                        new ExampleSimpleProcessor()
                        )
                    );

                // Act
                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);

                // Assert
                Assert.AreEqual(2, processor.Count);
                Assert.IsInstanceOfType(processor[0], typeof(ExampleRecursiveProcessorAlternate <string>));
                Assert.IsInstanceOfType(processor[1], typeof(SuccessProcessor <string, IJsonSerializer>));

                var recNode = (ExampleRecursiveProcessorAlternate <string>)processor[0];

                Assert.AreEqual(2, recNode.ProcessorStructure.Count);
                Assert.IsInstanceOfType(recNode.ProcessorStructure[0], typeof(ExampleSimpleProcessor));
                Assert.IsInstanceOfType(recNode.ProcessorStructure[1], typeof(SuccessProcessor <string, IJsonSerializer>));
            }
예제 #16
0
            public void When200ThenReturnsData()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.OK,
                    ContentType = "application/json",
                    Content     = "\"Hello\""
                };
                var jsonConverter = new JsonSerializer();
                var processor     = new ProcessorStructure <string, IJsonSerializer>(
                    new RestExceptionProcessor <string>().Default());

                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);
                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.AreEqual("Hello", res);
            }
예제 #17
0
        public void WhenErrorIsNotRestErrorThenReturnsAnotherError()
        {
            // Arrange
            string        bodyContents = "{\"@class\": 'Clase', \"MessageError\": \"MessageError\", \"Otro\": \"Otro\"}";
            IRestResponse response     = new RestResponse()
            {
                StatusCode  = HttpStatusCode.InternalServerError,
                ContentType = "application/json",
                Content     = bodyContents
            };
            var jsonConverter       = new JsonSerializer();
            var jsonErrorSerializer = new JsonSerializer();

            jsonErrorSerializer.Settings.MissingMemberHandling           = Newtonsoft.Json.MissingMemberHandling.Error;
            jsonErrorSerializer.Settings.ContractResolver.ObjectContract = new RequiredAttributesObjectContract(RequiredLevel.AllowNull);

            var processor = new ProcessorStructure <int, IJsonSerializer>(
                new RestExceptionProcessor <int>().Default());

            ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonErrorSerializer);

            // Act
            try
            {
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);
            }
            // arrange
            catch (RestException ex)
            {
                Assert.AreEqual(HttpStatusCode.InternalServerError, ex.HttpError.StatusCode);
                Assert.AreEqual(bodyContents, ex.HttpError.Details);
                throw;
            }

            Assert.IsTrue(processor.CanProcess(response));
        }
예제 #18
0
        protected override TResult ProcessSub(IRestResponse response, TSerializer serializer)
        {
            var error = ProcessorStructure.Process(response, serializer);

            throw exceptionProvider.ProvideException(error);
        }
예제 #19
0
 protected override TResult ProcessSub(IRestResponse response, TSerializer serializer)
 {
     return(ProcessorStructure.Process(response, serializer));
 }
예제 #20
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     //Can only process it if the recursive node can process it and it's succesful.
     return(response.StatusCode.IsSuccessful() && ProcessorStructure.CanProcess(response));
 }
예제 #21
0
        protected override TError ProcessSub(IRestResponse response, TSerializer serializer)
        {
            var errorRest = ProcessorStructure.Process(response, serializer);

            return(errorConverterProvider.ProvideError(errorRest, response));
        }
예제 #22
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     return(ProcessorStructure.CanProcess(response));
 }
예제 #23
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     //It can process it only if either the Left or Right processors can
     return(ProcessorStructure.CanProcess(response) || leftProcessorStructure.CanProcess(response));
 }
예제 #24
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     return(!response.StatusCode.IsSuccessful() && ProcessorStructure.CanProcess(response));
 }
예제 #25
0
 protected override string ProcessSub(IRestResponse response, IJsonSerializer serializer)
 {
     return(ProcessorStructure.Process(response, serializer));
 }
예제 #26
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     //It processes the response if it's 404 or the inner processor can
     return(response.StatusCode == HttpStatusCode.NotFound || ProcessorStructure.CanProcess(response));
 }