Ejemplo n.º 1
0
        private void describe_()
        {
            PingController pingController = null;

            before = () =>
            {
                pingController = new PingController
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
            };

            describe[Controller.Show] = () =>
            {
                IHttpActionResult result = null;
                before = () =>
                {
                    result = pingController.Show();
                };

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };

                it["returns OK"] = () =>
                {
                    string jsonString = result.ExecuteAsync(new CancellationToken()).Result.Content.ReadAsString();
                    jsonString.should_be("\"OK\"");
                };
            };
        }
Ejemplo n.º 2
0
        private void describe_()
        {
            Mock <IContainerInfoService> mockContainerInfoService = null;
            MetricsController            metricsController        = null;
            string      containerHandle = null;
            const ulong privateBytes    = 28;
            ContainerMetricsApiModel containerMetrics = null;

            before = () =>
            {
                mockContainerInfoService = new Mock <IContainerInfoService>();
                metricsController        = new MetricsController(mockContainerInfoService.Object)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
                containerHandle  = Guid.NewGuid().ToString();
                containerMetrics = new ContainerMetricsApiModel
                {
                    MemoryStat = new ContainerMemoryStatApiModel
                    {
                        TotalBytesUsed = privateBytes
                    }
                };

                mockContainerInfoService.Setup(x => x.GetMetricsByHandle(containerHandle))
                .Returns(() => containerMetrics);
            };

            describe[Controller.Show] = () =>
            {
                IHttpActionResult result = null;

                act = () => result = metricsController.Show(containerHandle);

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };

                it["returns the container metrics as a json"] = () =>
                {
                    var message = result.should_cast_to <JsonResult <ContainerMetricsApiModel> >();
                    message.Content.should_be(containerMetrics);
                };

                context["when the container does not exist"] = () =>
                {
                    before = () => containerMetrics = null;

                    it["returns a 404"] = () =>
                    {
                        var message = result.should_cast_to <ResponseMessageResult>();
                        message.Response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };
            };
        }
Ejemplo n.º 3
0
        private void describe_()
        {
            Mock <IContainerService> mockContainerService = null;
            Mock <IContainer>        mockContainer        = null;
            PropertiesController     propertiesController = null;
            string containerHandle = null;
            string key             = null;
            string value           = null;

            before = () =>
            {
                mockContainerService = new Mock <IContainerService>();
                mockContainer        = new Mock <IContainer>();
                propertiesController = new PropertiesController(mockContainerService.Object)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
                containerHandle = Guid.NewGuid().ToString();
                key             = "some:key";
                value           = "value";

                mockContainerService.Setup(x => x.GetContainerByHandle(containerHandle))
                .Returns(() =>
                {
                    return(mockContainer != null ? mockContainer.Object : null);
                });
            };

            /*
             * describe[Controller.Index] = () =>
             * {
             *  Dictionary<string,string> result = null;
             *  Dictionary<string,string> properties = null;
             *
             *  before = () =>
             *  {
             *      properties = new Dictionary<string,string> {
             *          { "wardrobe", "a lion" },
             *          { "a hippo", "the number 25" }
             *      };
             *      mockPropertyService.Setup(x => x.GetProperties(mockContainer.Object))
             *          .Returns(() => properties);
             *
             *     result = propertiesController.Index(containerHandle);
             *  };
             *
             *  it["returns the correct property value"] = () =>
             *  {
             *      result.should_be(properties);
             *  };
             * };
             * */

            describe[Controller.Show] = () =>
            {
                IHttpActionResult result        = null;
                string            propertyValue = null;

                before = () =>
                {
                    propertyValue = "a lion, a hippo, the number 25";
                    mockContainer.Setup(x => x.GetProperty(key)).Returns(() => propertyValue);
                };

                act = () => result = propertiesController.Show(containerHandle, key);

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };

                it["returns the correct property value"] = () =>
                {
                    var actualValue = result.should_cast_to <JsonResult <string> >();
                    actualValue.Content.should_be(propertyValue);
                };

                context["when the property does not exist"] = () =>
                {
                    before = () => propertyValue = null;
                    it["returns a 404"] = () =>
                    {
                        var message = result.should_cast_to <ResponseMessageResult>();
                        message.Response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };

                context["when the container does not exist"] = () =>
                {
                    before = () => mockContainer = null;
                    it["returns a 404"] = () =>
                    {
                        var message = result.should_cast_to <ResponseMessageResult>();
                        message.Response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };
            };

            describe[Controller.Update] = () =>
            {
                IHttpActionResult result = null;
                before = () =>
                {
                    propertiesController.Request.Content = new StringContent(value);
                    result = propertiesController.Update(containerHandle, key).Result;
                };

                it["calls the propertyService set method"] = () =>
                {
                    mockContainer.Verify(x => x.SetProperty(key, value));
                };

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };
            };

            describe[Controller.Destroy] = () =>
            {
                IHttpActionResult result = null;
                before = () =>
                {
                    result = propertiesController.Destroy(containerHandle, key).Result;
                };

                it["calls the propertyService destroy method"] = () =>
                {
                    mockContainer.Verify(x => x.RemoveProperty(key));
                };

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };
            };
        }
Ejemplo n.º 4
0
        private void describe_()
        {
            FilesController          filesController      = null;
            Mock <IStreamInService>  mockStreamInService  = null;
            Mock <IStreamOutService> mockStreamOutService = null;

            before = () =>
            {
                mockStreamOutService = new Mock <IStreamOutService>();
                mockStreamInService  = new Mock <IStreamInService>();
                filesController      = new FilesController(mockStreamInService.Object, mockStreamOutService.Object)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
            };

            describe[Controller.Show] = () =>
            {
                context["when the file exists"] = () =>
                {
                    HttpResponseMessage result = null;
                    before = () =>
                    {
                        mockStreamOutService.Setup(x => x.StreamOutFile(It.IsAny <string>(), It.IsAny <string>()))
                        .Returns(() =>
                        {
                            var stream = new MemoryStream();
                            var writer = new StreamWriter(stream);
                            writer.Write("hello");
                            writer.Flush();
                            stream.Position = 0;
                            return(stream);
                        });

                        result = filesController
                                 .Show("guid", "file.txt").GetAwaiter().GetResult();
                    };


                    it["returns a successful status code"] = () =>
                    {
                        result.VerifiesSuccessfulStatusCode();
                    };
                };
            };

            describe[Controller.Update] = () =>
            {
                context["when it receives a new file"] = () =>
                {
                    string            id       = null;
                    const string      fileName = "file.txt";
                    IHttpActionResult result   = null;
                    string            content  = null;

                    before = () =>
                    {
                        id = Guid.NewGuid().ToString();
                        Stream stream = new MemoryStream();
                        var    sr     = new StreamWriter(stream);
                        content = Guid.NewGuid().ToString();
                        sr.Write(content);
                        sr.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        filesController.Request.Content = new StreamContent(stream);
                        result = filesController.Update(id, fileName).Result;
                    };

                    it["calls the stream in service with the correct paramaters"] = () =>
                    {
                        mockStreamInService.Verify(x => x.StreamInFile(
                                                       It.Is((Stream y) => new StreamReader(y).ReadToEnd() == content),
                                                       id,
                                                       fileName));
                    };


                    it["returns a successful status code"] = () =>
                    {
                        result.VerifiesSuccessfulStatusCode();
                    };
                };
            };
        }