Example #1
0
        public async void Get_SuccessStatus200_Test()
        {
            // 0: Remove all establishments from database
            await establishmentService.RemoveAll();

            // 1: Creating testing objects
            Establishment firstEstablishment = new Establishment()
            {
                Name = "Test 1",
                Type = "Alimentação"
            };

            Establishment secondEstablishment = new Establishment()
            {
                Name = "Test 2",
                Type = "Alimentação"
            };

            // 2: Adding to database
            await establishmentService.CreateItem(firstEstablishment);

            await establishmentService.CreateItem(secondEstablishment);

            var query = await establishmentsController.Get();

            var result = (List <Establishment>)query.Result.GetType().GetProperty("Value").GetValue(query.Result);

            // 3: Check if result is a valid Establishment
            Assert.Equal("Test 1", result[0].Name);
            Assert.Equal("Test 2", result[1].Name);
        }
Example #2
0
        public async void Post_SuccessStatus201_Test()
        {
            // 1: Add Existing establishment to validate name param on request body
            await establishmentService.CreateItem(new Establishment()
            {
                Name = "Test 1",
                Type = "Alimentação"
            });

            // 2: Request body
            ReleaseRequest requestBody = new ReleaseRequest()
            {
                Date              = "05/05/2019",
                PaymentMethod     = PaymentMethod.Credito,
                EstablishmentName = "Test 1",
                Amount            = 55.55m
            };

            // 3: Call POST Action passing body request with a new release
            var query = await releasesController.Post(requestBody);

            var resultStatusCode = query.Result.GetType().GetProperty("StatusCode").GetValue(query.Result);
            var resultValue      = (Release)query.Result.GetType().GetProperty("Value").GetValue(query.Result);

            // 4: Remove all establishments and releases from database
            await establishmentService.RemoveAll();

            await releasesService.RemoveAll();

            Assert.Equal(201, (int)resultStatusCode);
            Assert.Equal("Test 1", resultValue.EstablishmentName);
            Assert.NotNull(resultValue.Id);
        }
Example #3
0
        public async Task <ActionResult <Establishment> > Post([FromBody] EstablishmentRequest body)
        {
            Establishment resultEstablishment;

            try {
                logger.LogInformation("Trying to verify if establishment with given Name exists");
                var establishment = await establishmentService.GetByName(body.Name.FirstCharToUpper());

                if (establishment != null)
                {
                    string errorMessage = responseMessages.NotAccepted.Replace("$", "estabelecimento");
                    logger.LogInformation("Error: " + errorMessage);
                    return(httpResponseHelper.ErrorResponse(errorMessage, 406));
                }

                logger.LogInformation("Inserting establishment into database");
                var newEstablishment = new Establishment()
                {
                    Name      = body.Name.FirstCharToUpper(),
                    Type      = body.Type.FirstCharToUpper(),
                    CreatedAt = DateTime.Now
                };

                resultEstablishment = await establishmentService.CreateItem(newEstablishment);
            } catch (Exception ex) {
                logger.LogInformation("Exception: " + ex.Message);
                throw ex;
            }

            logger.LogInformation("Action POST for /api/establishments returns 201");
            return(Created("", resultEstablishment));
        }