Example #1
0
    public void GetCommandsApiTest()
    {
     
      /*
      . создать нового робота и комманду
      . получить эту комманду через апи
      . удалить нового робота и комманду
      */

      var amountRobots = context.Robots.Count();
      var amountCommands = context.RobotCommands.Count(); 
      var robot = new Robot();
      var command = new RobotCommand
      {
        Argument = MoqDataGenerator.GetRandomNumber(1, 10),
        Type = MoqDataGenerator.GetRandomNumber(1, 10),
        Robot = robot
      };
      context.Robots.Add(robot);
      context.RobotCommands.Add(command);
      context.SaveChanges();
      Assert.AreEqual(amountCommands, context.RobotCommands.Count() - 1);
      Assert.AreEqual(amountRobots, context.Robots.Count() - 1);

      try
      {
        using (var wb = new WebClient())
        {
          wb.Encoding = Encoding.UTF8;
          var json = wb.DownloadString(serverUrl + "/api/GetCommands?robotId=" + robot.RobotID);
          var resp = JsonConvert.DeserializeObject<IEnumerable<GetCommandsOutput>>(json);
          Assert.AreEqual(1, resp.Count());
          var comm = resp.First();
          Assert.AreEqual(comm.RobotCommandID, command.RobotCommandID);
          Assert.AreEqual(comm.Type, command.Type);
          Assert.AreEqual(comm.Argument, command.Argument);
        }
      }
      finally
      {
        context.Robots.Remove(robot);
        context.RobotCommands.Remove(command);
        context.SaveChanges();
        Assert.AreEqual(amountCommands, context.RobotCommands.Count());
        Assert.AreEqual(amountRobots, context.Robots.Count());
      }
    }
Example #2
0
    public async Task ReportCommandExecutedApiTest()
    {
      /*
      - создать комманду и робота
      - проверить функцию
      - удалить робота, команда уйдет каскадно
      */
      var amountRobots = context.Robots.Count();
      var amountCommands = context.RobotCommands.Count();
      var robot = new Robot();
      var command = new RobotCommand
      {
        Robot = robot,
        Received = false
      };
      context.Robots.Add(robot);
      context.RobotCommands.Add(command);
      context.SaveChanges();
      var commandId = command.RobotCommandID;
      try
      {
        using (var client = new HttpClient())
        {
          var values = new Dictionary<string, string>
          {
            {"CommandId", "" + command.RobotCommandID}
          };
          var content = new FormUrlEncodedContent(values);
          await client.PostAsync(serverUrl + "/api/ReportCommandExecuted", content);

          context.RefreshModified();

          Assert.AreEqual(amountCommands, context.RobotCommands.Count());
          Assert.AreEqual(null, context.RobotCommands.FirstOrDefault(x => x.RobotCommandID == commandId));
        }
      }
      finally
      {
        context.Robots.Remove(robot);
        context.SaveChanges();
        Assert.AreEqual(amountRobots, context.Robots.Count());
        Assert.AreEqual(amountCommands, context.RobotCommands.Count());
      }
    }
Example #3
0
    public async Task ReportCommandGotApiTest()
    {
      /*
      - создать комманду и робота
      - проверить функцию
      - удалить робота, команда уйдет каскадно
      */

      var amountRobots = context.Robots.Count();
      var amountCommands = context.RobotCommands.Count();
      var robot = new Robot();
      var command = new RobotCommand
      {
        Robot = robot,
        Received = false
      };
      context.Robots.Add(robot);
      context.RobotCommands.Add(command);
      context.SaveChanges();
      try
      {
        using (var client = new HttpClient())
        {
          var values = new Dictionary<string, string>
          {
            {"CommandId", "" + command.RobotCommandID}
          };
          var content = new FormUrlEncodedContent(values);
          await client.PostAsync(serverUrl + "/api/ReportCommandGot", content);
          var result = (bool)context.Entry(command).GetDatabaseValues()["Received"];
          Assert.AreEqual(result, true);
        }
      }
      finally
      {
        context.Robots.Remove(robot);
        context.SaveChanges();
        Assert.AreEqual(amountRobots, context.Robots.Count());
        Assert.AreEqual(amountCommands, context.RobotCommands.Count());
      }

  }