public void SacarItemMethod_OK() { #region Arrange InventarioItemViewModel item = new InventarioItemViewModel() { Id = Guid.NewGuid(), Nombre = "item 1", Unidades = 100, FechaCaducidad = DateTime.UtcNow.AddDays(5).ToString() }; IInventarioItemManager manager = CreateInventarioItemManager(); _inventarioItemServiceMocked.Setup(s => s.SacarItem(It.IsAny <InventarioItem>())).Returns(Task.FromResult(true)); #endregion #region Act var actual = manager.SacarItem(item).Result; #endregion #region Assert Assert.IsNotNull(actual); Assert.IsTrue(actual); _inventarioItemServiceMocked.Verify(s => s.SacarItem(It.IsAny <InventarioItem>()), Times.Once); #endregion }
public async Task GetInsertAndGetAllInventarioItem_OK() { // Arrange var request = new HttpRequestMessage( HttpMethod.Put, "/api/Inventario"); var body = new InventarioItemViewModel() { Id = Guid.NewGuid(), Nombre = "Item1 Test", Unidades = 100, FechaCaducidad = DateTime.UtcNow.AddDays(5).ToString() }; request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); // Act: lanzamos la petición a la API var response = await _client.SendAsync(request); //Obtenemos toos los elementos var requestGetAll = new HttpRequestMessage( HttpMethod.Get, "/api/Inventario"); // Act: request the /todo route var responseGetAll = await _client.SendAsync(requestGetAll); var contentGetAllResponse = JsonConvert.DeserializeObject <IEnumerable <InventarioItemViewModel> >(await responseGetAll.Content.ReadAsStringAsync()); // Assert: Verificamos que todo está correcto Assert.Equal( HttpStatusCode.OK, response.StatusCode); Assert.Equal( HttpStatusCode.OK, responseGetAll.StatusCode); Assert.Contains(contentGetAllResponse, item => item.Id == body.Id); }
public async Task <bool> SacarItem(InventarioItemViewModel item) { try { return(await _inventariosService.SacarItem(_mapper.Map <InventarioItem>(item))); } catch (Exception ex) { _logger.LogError(ex, $"Error sacando un Item Id {item}."); throw ex; } }
public async Task <InventarioItemViewModel> InsertarItem(InventarioItemViewModel item) { try { await _inventariosService.InsertarItem(_mapper.Map <InventarioItem>(item)); return(item); } catch (Exception ex) { _logger.LogError(ex, $"Error insertando un Item Id {item}."); throw ex; } }
public async Task SubscribeExpiredInventarioItem_OK() { #region Arrange var request = new HttpRequestMessage( HttpMethod.Put, "/api/Inventario"); var body = new InventarioItemViewModel() { Id = Guid.NewGuid(), Nombre = "Item Expired Test", Unidades = 100, FechaCaducidad = DateTime.UtcNow.AddDays(-5).ToString() }; List <InventarioItemViewModel> expiredList = new List <InventarioItemViewModel>(); //Conectamos con el Hub var connection = new HubConnectionBuilder() .WithAutomaticReconnect() .WithUrl("ws://localhost:8888/Hubs/Inventarioitem", o => o.HttpMessageHandlerFactory = _ => _httpHandler) .Build(); connection.On <ExpiredMessageReceived>("ItemExpired", (parameters) => { var elemen = parameters as ExpiredMessageReceived; expiredList.Add(new InventarioItemViewModel() { Id = elemen.Id }); }); #endregion #region Act //Iniciamos la conexión con el Hub await connection.StartAsync(); //Inserto un elemento caducado request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); await _client.SendAsync(request); //Esperamos 2 minutos para asegurarnos que de tiempo al worker a ejecutarse, ya que se ejecuta cada minuto para limpiar los expirados. Thread.Sleep(120000); #endregion #region Assert Assert.Equal(1, expiredList.Count()); Assert.Equal(body.Id, expiredList[0].Id); #endregion }
public void InsertarItemMethod_Exception() { #region Arrange InventarioItemViewModel itemToInsert = null; IInventarioItemManager manager = CreateInventarioItemManager(); _inventarioItemServiceMocked.Setup(s => s.InsertarItem(It.IsAny <InventarioItem>())).Throws(new Exception("Error controlado")); #endregion #region Act manager.InsertarItem(itemToInsert).GetAwaiter().GetResult(); #endregion }
public void SacarItemMethod_Exception() { #region Arrange InventarioItemViewModel item = new InventarioItemViewModel() { Id = Guid.NewGuid(), Nombre = "item 1", Unidades = 100, FechaCaducidad = DateTime.UtcNow.AddDays(5).ToString() }; IInventarioItemManager manager = CreateInventarioItemManager(); _inventarioItemServiceMocked.Setup(s => s.SacarItem(It.IsAny <InventarioItem>())).Throws(new Exception("Error controlado")); #endregion #region Act var actual = manager.SacarItem(item).GetAwaiter().GetResult(); #endregion }
public async Task <IActionResult> InsertarItem([FromBody] InventarioItemViewModel item) { try { var result = await _inventarioManager.InsertarItem(item); if (result != null) { return(Ok(result)); } return(NotFound()); } catch (KeyNotFoundException ex) { _logger.LogError(ex, $"No se pudo insertar el elemento porque no tiene la clave primaria rellena.{item}"); return(BadRequest()); } catch (Exception ex) { _logger.LogError(ex, $"Ocurrió un error al insertar unnuevo item en el inventario {item}."); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public Task <bool> ActualizarItem(InventarioItemViewModel item) { throw new NotImplementedException(); }