public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_SortingAlgo_HappyPath()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);

            var flightMatrix = new FlightMatrix
            {
                ContestId = "sdfasdf"
            };

            flightMatrix.Matrix.Add(new FlightMatrixRound
            {
                RoundOrdinal = 0,
                PilotSlots   = new List <FlightMatrixPilotSlot>
                {
                    new FlightMatrixPilotSlot
                    {
                        PilotId     = "sadfxcvcxasdf",
                        FlightGroup = FlightGroup.A
                    }
                }
            });

            this.mockSortingAlgo.Setup(sa => sa.GenerateInitialMatrix(It.IsAny <IEnumerable <PilotRegistration> >(), It.IsAny <int>(), It.IsAny <int>())).Returns(flightMatrix);

            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, 7);

            Assert.IsFalse(result.IsFaulted);
            Assert.IsNull(result.Error);
            Assert.IsNotNull(result.Value);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_NullPilotRegistrationParameter()
        {
            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(null, 1, 7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsNotNull(result.Error.ErrorMessage);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_NegativeSuggestionParameter()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);
            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, -7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsNotNull(result.Error.ErrorMessage);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_SortingAlgo_ReturnsNull()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);

            this.mockSortingAlgo.Setup(sa => sa.GenerateInitialMatrix(It.IsAny <IEnumerable <PilotRegistration> >(), It.IsAny <int>(), It.IsAny <int>())).Returns <FlightMatrix>(null);

            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, 7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsNotNull(result.Error.ErrorMessage);
        }
        public void FlightMatrixGenerationInteractor_CreateSortedFlightMatrix_SortingAlgo_Exception()
        {
            var pilotRegistrations = GenerateValidPilotRegistration(10);

            this.mockSortingAlgo.Setup(sa => sa.GenerateInitialMatrix(It.IsAny <IEnumerable <PilotRegistration> >(), It.IsAny <int>(), It.IsAny <int>())).Throws(new ArgumentException("test"));

            var fmgi   = new FlightMatrixGenInteractor(mockSortingAlgo.Object, mockLogger.Object);
            var result = fmgi.CreateSortedFlightMatrix(pilotRegistrations, 1, 7);

            Assert.IsTrue(result.IsFaulted);
            Assert.IsNotNull(result.Error);
            Assert.IsInstanceOfType(result.Error.Exception, typeof(ArgumentException));
            Assert.IsNotNull(result.Error.ErrorMessage);
            Assert.AreEqual("test", result.Error.ErrorMessage);
        }
        /// <summary>
        /// Handles a click on the Generate matrix button.
        /// </summary>
        /// <returns></returns>
        public async Task GenerateMatrix()
        {
            var contestRegistrationsResult = await this.registrationQueryIntr.Value.GetPilotRegistrationsForContest(this.contest.Id);

            if (contestRegistrationsResult.IsFaulted || contestRegistrationsResult.Value == null)
            {
                return;
            }

            try
            {
                var flightMatrixGenerationIntr = new FlightMatrixGenInteractor(
                    App.SortingAlgos.Where(algo => algo.GetUniqueId() == this.contest.SortingAlgoId).Single(),
                    App.Logger);

                var sortedMatrixResult = flightMatrixGenerationIntr.CreateSortedFlightMatrix(
                    contestRegistrationsResult.Value,
                    // Don't include the fly-off rounds
                    this.contest.Rounds.Where(r => !r.Value.IsFlyOffRound).Count(),
                    this.contest.SuggestedNumberOfPilotsPerGroup);

                if (sortedMatrixResult.IsFaulted || sortedMatrixResult.Value == null)
                {
                    return;
                }

                this.matrix = sortedMatrixResult.Value;

                // Assign it to the current contest.
                this.matrix.ContestId = this.contest.Id;

                // Save it
                var flightMatrixSaveResult = await this.flightMatrixCmdIntr.Value.SaveFlightMatrixForContestAsync(this.matrix);

                if (flightMatrixSaveResult.IsFaulted)
                {
                    base.Alert($"{nameof(FlightMatrixPageViewModel)}:{nameof(GenerateMatrix)} - Failed to save the flight matrix.");
                    return;
                }

                await InflateMatrix(sortedMatrixResult.Value);
            }
            catch (Exception ex)
            {
                App.Logger.LogException(ex);
            }
        }