예제 #1
0
        public void CreateContainerValidation()
        {
            var controller = InitController("POST");

            // Invalid payload
            var data = new ViewModels.ContainerCreationDetails()
            {
                Id           = null,
                Measurements = null,
                ProductCount = -1
            };

            controller.ValidateViewModel(data);
            Assert.Equal(3, controller.ModelState.ErrorCount);

            // Invalid .Measurements and .ProductCount
            data.Id = "Valid1";
            controller.ValidateViewModel(data);
            Assert.Equal(2, controller.ModelState.ErrorCount);

            // Invalid .ProductCount
            data.Measurements = new List <ViewModels.TemperatureRecord>();
            controller.ValidateViewModel(data);
            Assert.Equal(1, controller.ModelState.ErrorCount);

            // Valid payload
            data.ProductCount = 10;
            controller.ValidateViewModel(data);
            Assert.Equal(0, controller.ModelState.ErrorCount);
        }
예제 #2
0
        public void CreateContainerLogic()
        {
            // Positive test case (and tests)
            var tripId = CreateTripWithContainers("AnotherTestTrip");

            // Try and create a trip with bad trip ids
            var data = new ViewModels.ContainerCreationDetails()
            {
                Id           = "container1",
                Measurements = new List <ViewModels.TemperatureRecord>()
                {
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now, Value = 0
                    },
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now.AddSeconds(1), Value = 1
                    },
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now.AddSeconds(2), Value = 3
                    },
                },
                ProductCount = 10
            };

            var controller = InitController("POST");
            var result     = controller.CreateContainer("I am not a number", data) as StatusCodeResult;

            Assert.Equal((int)HttpStatusCode.NotFound, result.StatusCode);

            controller = InitController("POST");
            result     = controller.CreateContainer("100", data) as StatusCodeResult;
            Assert.Equal((int)HttpStatusCode.NotFound, result.StatusCode);
        }
예제 #3
0
        /// <summary>
        /// Helper that creates and validates a trip, complete with containers (returning its ID)
        /// </summary>
        private int CreateTripWithContainers(string tripName)
        {
            var controller = InitController("POST");
            var tripId     = CreateTrip(controller, new ViewModels.TripCreationDetails()
            {
                Name = tripName, SpoilDuration = 1, SpoilTemperature = 10,
            });
            var dateTime = DateTime.UtcNow;

            // Check that the trip's .Updated timestamp is updated by the controller
            // after we have added a container to it (tests field used for ETag functionality in GET /trip/x)
            IShippingRepository repo = _provider.GetRequiredService <IShippingRepository>();
            var trip = repo.Trips.SingleOrDefault(x => x.Id.Equals(tripId));

            Assert.NotNull(trip);
            var updatedTimeStamp = trip.Updated;

            // Create a container
            var data = new ViewModels.ContainerCreationDetails()
            {
                Id           = "container1",
                Measurements = new List <ViewModels.TemperatureRecord>()
                {
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now, Value = 0
                    },
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now.AddSeconds(1), Value = 1
                    },
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now.AddSeconds(2), Value = 3
                    },
                },
                ProductCount = 10
            };

            // Test the result
            var result = controller.CreateContainer(tripId.ToString(), data) as StatusCodeResult;

            Assert.Equal((int)HttpStatusCode.Created, result.StatusCode);

            // Ensure we can't replay
            controller = InitController("POST");
            result     = controller.CreateContainer(tripId.ToString(), data) as StatusCodeResult;
            Assert.Equal((int)HttpStatusCode.BadRequest, result.StatusCode);

            // Ensure that .Updated is newer
            // Note: We need to re-initialize the repo at this point to avoid caching)
            repo = _provider.GetRequiredService <IShippingRepository>();
            trip = repo.Trips.SingleOrDefault(x => x.Id.Equals(tripId));
            Assert.NotNull(trip);
            Assert.True(trip.Updated > updatedTimeStamp);

            // Add another container to this trip
            data = new ViewModels.ContainerCreationDetails()
            {
                Id           = "container2",
                Measurements = new List <ViewModels.TemperatureRecord>()
                {
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now.AddSeconds(2), Value = 30
                    },
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now, Value = 10
                    },
                    new ViewModels.TemperatureRecord()
                    {
                        Time = DateTime.Now.AddSeconds(1), Value = 20
                    },
                },
                ProductCount = 20
            };

            result = controller.CreateContainer(tripId.ToString(), data) as StatusCodeResult;
            Assert.Equal((int)HttpStatusCode.Created, result.StatusCode);
            return(tripId);
        }