コード例 #1
0
 private IEnumerable <ContainerCreationDetails> ReadContainers(IDataReader reader)
 {
     while (reader.Read())
     {
         var contatiner = new ContainerCreationDetails();
         contatiner.Id           = (string)reader["Id"];
         contatiner.ProductCount = (int)reader["Product Count"];
         yield return(contatiner);
     }
 }
コード例 #2
0
        public void CreateContainer_ChecksTripExists()
        {
            // Arrange
            var containerCreationDetails = new ContainerCreationDetails();
            var containerSpoilage        = MockRepository.GenerateMock <IContainerSpoilage>();

            containerSpoilage.Stub(x => x.TripExists("1234")).Return(false);
            var controller = new ContainerSpoilageController(containerSpoilage);

            // Act
            var response = controller.CreateContainer("1234", containerCreationDetails);

            // Assert
            Assert.AreEqual(typeof(NotFoundResult), response.GetType());
        }
コード例 #3
0
        public IHttpActionResult CreateContainer(string tripId, ContainerCreationDetails containerCreationDetails)
        {
            if (!containerSpoilage.TripExists(tripId))
            {
                return(NotFound());
            }
            var validation = containerSpoilage.ValidateContainerCreationDetails(containerCreationDetails);

            if (!validation.isValid)
            {
                return(BadRequest(validation.errorMessage));
            }
            containerSpoilage.CreateContainer(tripId, containerCreationDetails);
            return(Created <ContainerCreationDetails>("", null));
        }
コード例 #4
0
        public void CreatesContainer()
        {
            // Arrange
            var containerCreationDetails = new ContainerCreationDetails();
            var containerSpoilage        = MockRepository.GenerateMock <IContainerSpoilage>();

            containerSpoilage.Stub(x => x.TripExists("1234")).Return(true);
            containerSpoilage.Stub(x => x.ValidateContainerCreationDetails(containerCreationDetails))
            .Return((true, ""));
            var controller = new ContainerSpoilageController(containerSpoilage);

            // Act
            var response = controller.CreateContainer("1234", containerCreationDetails);

            // Assert
            Assert.AreEqual(typeof(CreatedNegotiatedContentResult <ContainerCreationDetails>), response.GetType());
        }
コード例 #5
0
        public void CreateContainer_ValidatesContainer()
        {
            // Arrange
            var containerCreationDetails = new ContainerCreationDetails();
            var containerSpoilage        = MockRepository.GenerateMock <IContainerSpoilage>();

            containerSpoilage.Stub(x => x.TripExists("1234")).Return(true);
            containerSpoilage.Stub(x => x.ValidateContainerCreationDetails(containerCreationDetails))
            .Return((false, "error"));
            var controller = new ContainerSpoilageController(containerSpoilage);

            // Act
            var response = controller.CreateContainer("1234", containerCreationDetails);

            // Assert
            Assert.AreEqual(typeof(BadRequestErrorMessageResult), response.GetType());
        }
コード例 #6
0
        public void ContainerCreation_UnsetIdInvalid(string id, bool isValid)
        {
            // Arrange
            var containerDetails = new ContainerCreationDetails
            {
                Id           = id,
                ProductCount = 1000
            };
            var containerSpoilage = new ContainerSpoilage(null);

            // Act
            var validation = containerSpoilage.ValidateContainerCreationDetails(containerDetails);

            // Assert
            Assert.AreEqual(isValid, validation.isValid);
            Assert.AreEqual(isValid, string.IsNullOrEmpty(validation.errorMessage));
        }
コード例 #7
0
        public void ContainerCreation_NegativeProductCountInvalid(int productCount, bool isValid)
        {
            // Arrange
            var containerDetails = new ContainerCreationDetails
            {
                Id           = Guid.NewGuid().ToString(),
                ProductCount = productCount
            };
            var containerSpoilage = new ContainerSpoilage(null);

            // Act
            var validation = containerSpoilage.ValidateContainerCreationDetails(containerDetails);

            // Assert
            Assert.AreEqual(isValid, validation.isValid);
            Assert.AreEqual(isValid, string.IsNullOrEmpty(validation.errorMessage));
        }
コード例 #8
0
        static void Main(string[] args)
        {
            var httpClient = new HttpClient();
            var random     = new Random(0);
            var container1 = new ContainerCreationDetails
            {
                Id           = GetContainerId(),
                ProductCount = 15000,
            };
            var container2 = new ContainerCreationDetails
            {
                Id           = GetContainerId(),
                ProductCount = 18000
            };
            var measurementsContainer1 = new List <TemperatureRecord>();
            var measurementsContainer2 = new List <TemperatureRecord>();

            for (var i = 0; i < 7200; i++)
            {
                measurementsContainer1.Add(new TemperatureRecord
                {
                    Time  = DateTime.UtcNow + TimeSpan.FromMinutes(i),
                    Value = GetRandomTemperature(random)
                });
                measurementsContainer2.Add(new TemperatureRecord
                {
                    Time  = DateTime.UtcNow + TimeSpan.FromMinutes(i),
                    Value = GetRandomTemperature(random)
                });
            }

            container1.Measurements = measurementsContainer1.ToArray();
            container2.Measurements = measurementsContainer2.ToArray();

            var container1json = JsonConvert.SerializeObject(container1);
            var container2json = JsonConvert.SerializeObject(container2);

            var url     = "http://shippingcontainerspoilagewebapi.azurewebsites.net/trips/containers?tripId=1";
            var result1 = httpClient.PostAsync(url, new StringContent(container1json, Encoding.UTF8, "application/json")).Result;
            var result2 = httpClient.PostAsync(url, new StringContent(container2json, Encoding.UTF8, "application/json")).Result;
        }
コード例 #9
0
        public void AddContainer(long tripId, ContainerCreationDetails containerCreationDetails)
        {
            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (var command = new SqlCommand("Add Container", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@tripId", SqlDbType.BigInt);
                    command.Parameters["@tripId"].Value = tripId;
                    command.Parameters.Add("@containerId", SqlDbType.NVarChar);
                    command.Parameters["@containerId"].Value = containerCreationDetails.Id;
                    command.Parameters.Add("@productCount", SqlDbType.Int);
                    command.Parameters["@productCount"].Value = containerCreationDetails.ProductCount;

                    command.ExecuteNonQuery();
                }

                foreach (var measurement in containerCreationDetails.Measurements)
                {
                    using (var command = new SqlCommand("Add Temperature Record", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.Add("@containerId", SqlDbType.NVarChar);
                        command.Parameters["@containerId"].Value = containerCreationDetails.Id;
                        command.Parameters.Add("@temperature", SqlDbType.Decimal);
                        command.Parameters["@temperature"].Value = measurement.Value;
                        command.Parameters.Add("@recordedAt", SqlDbType.DateTime);
                        command.Parameters["@recordedAt"].Value = measurement.Time;

                        command.ExecuteNonQuery();
                    }
                }
            }
        }
コード例 #10
0
ファイル: DefaultApi.cs プロジェクト: gplumb/CertainSix
        public virtual IActionResult CreateContainer([FromRoute][Required] string tripId, [FromBody] ContainerCreationDetails containerCreationDetails)
        {
            var trip = GetTripById(tripId);

            if (trip == null)
            {
                return(StatusCode(404));
            }

            // Has the container already been created?
            var container = _repo.Containers.FirstOrDefault(x => x.ContainerId.Equals(containerCreationDetails.Id));

            if (container != null)
            {
                return(StatusCode(400));
            }

            // No, then let's create it
            var model = new Models.Container()
            {
                ContainerId  = containerCreationDetails.Id,
                TripId       = trip.Id,
                ProductCount = containerCreationDetails.ProductCount,

                // Use projection to convert from view model to DTO
                // Note. We don't need to populate ContainerID as EF will take care of referential integrity for us
                Temperatures = containerCreationDetails.Measurements.Select(tr => new Models.TemperatureRecord()
                {
                    Time   = tr.Time,
                    Value  = tr.Value,
                    TripId = trip.Id
                }).ToList()
            };

            model.IsSpoiled      = SpoilageHelpers.IsSpoiled(model.Temperatures, trip.SpoilTemperature, trip.SpoilDuration);
            model.MaxTemperature = model.Temperatures.Max(x => x.Value);

            trip.Updated = DateTime.UtcNow;
            _repo.Containers.Add(model);
            _repo.SaveChanges();
            return(StatusCode(201));
        }
コード例 #11
0
        public void GetsTrip()
        {
            // Arrange
            var tripId      = 123456L;
            var dalfacade   = MockRepository.GenerateMock <IDalFacade>();
            var tripDetails = new TripWithSpoilDetails
            {
                Id               = tripId,
                SpoilDuration    = 3,
                SpoilTemperature = 27m
            };
            var container1 = new ContainerCreationDetails
            {
                ProductCount = 1035,
                Measurements = new[]
                {
                    new TemperatureRecord {
                        Time = new DateTime(2018, 6, 26, 12, 0, 0), Value = 24.6m
                    },
                    new TemperatureRecord {
                        Time = new DateTime(2018, 6, 26, 12, 1, 1), Value = 26.2m
                    },
                    new TemperatureRecord {
                        Time = new DateTime(2018, 6, 26, 12, 3, 9), Value = 27.3m
                    },
                }
            };
            var container2 = new ContainerCreationDetails
            {
                ProductCount = 1039,
                Measurements = new[]
                {
                    new TemperatureRecord {
                        Time = new DateTime(2018, 6, 26, 12, 30, 2), Value = 29.6m
                    },
                    new TemperatureRecord {
                        Time = new DateTime(2018, 6, 26, 12, 35, 7), Value = 26.2m
                    },
                    new TemperatureRecord {
                        Time = new DateTime(2018, 6, 26, 12, 36, 5), Value = 22.3m
                    },
                }
            };

            dalfacade.Stub(x => x.TryGetTripDetails(tripId, out _))
            .OutRef(tripDetails)
            .Return(true);
            dalfacade.Stub(x => x.GetContainers(tripId)).Return(new[] { container1, container2 });
            var containerSpoilage = new ContainerSpoilage(dalfacade);

            // Act
            var trip = containerSpoilage.GetTrip(tripId.ToString());

            // Assert
            Assert.AreEqual(tripId, trip.Id);
            Assert.AreEqual(2, trip.ContainerCount);
            Assert.AreEqual(29.60m, trip.MaxTemperature);
            var roundedMeanTemperature = decimal.Round(GetMeanTemperature(container1, container2), 2);

            Assert.AreEqual(roundedMeanTemperature, trip.MeanTemperature);
            Assert.AreEqual(1, trip.SpoiledContainerCount);
            Assert.AreEqual(1039, trip.SpoiledProductCount);
        }
コード例 #12
0
        private decimal GetMeanTemperature(ContainerCreationDetails container1, ContainerCreationDetails container2)
        {
            var measurements = container1.Measurements.Concat(container2.Measurements);

            return(measurements.Average(measurement => measurement.Value));
        }