public async Task Get_ValidCountryCode_HttpOkAndReturnsSingleCountry()
        {
            // Arrange
            var randomCountryId = PreDefinedData.GetRandomCountryId();
            var path            = GetRelativePath(nameof(ReferenceCountriesController), randomCountryId.ToString());

            // Act
            var response = await Client.GetAsync(path);

            var responseString = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.DoesNotThrow(
                () => response.EnsureSuccessStatusCode(),
                string.Format(HttpExceptionFormattedMessage, responseString));
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));

            var apiReturnedObject =
                JsonConvert.DeserializeObject <ReferenceCountryDto>(responseString);

            var preDefinedObject =
                PreDefinedData.ReferenceCountries
                .SingleOrDefault(c => c.Id == randomCountryId);

            AssertHelper.AreObjectsEqual(apiReturnedObject,
                                         Mapper.Map <ReferenceCountry, ReferenceCountryDto>(preDefinedObject));
        }
        public async Task Update_ValidCountry_HttpNoContent()
        {
            // Arrange
            var randomCountryId = PreDefinedData.GetRandomCountryId();

            var apiUpdatingCountry = UnitOfWork.ReferenceCountries.Get(randomCountryId);

            apiUpdatingCountry.LongName = "Update Test";
            var path = GetRelativePath(nameof(ReferenceCountriesController), randomCountryId.ToString());

            // Act
            var response = await Client.PutAsync(path, new StringContent(
                                                     JsonConvert.SerializeObject(Mapper.Map <ReferenceCountry, ReferenceCountryDto>(apiUpdatingCountry)),
                                                     Encoding.UTF8,
                                                     JsonMediaType));

            var responseString = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.DoesNotThrow(
                () => response.EnsureSuccessStatusCode(),
                string.Format(HttpExceptionFormattedMessage, responseString));
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));

            var dbUpdatedCountry = UnitOfWork.ReferenceCountries.Get(apiUpdatingCountry.Id);

            AssertHelper.AreObjectsEqual(apiUpdatingCountry, dbUpdatedCountry);
        }
        public void SingleOrDefault_PredicateUsedToFindMoreOneCountry_ThrowsInvalidOperationException()
        {
            var randomCountryId = PreDefinedData.GetRandomCountryId();

            Assert.That(() =>
                        UnitOfWork.ReferenceCountries.SingleOrDefault(p => p.Id != randomCountryId),
                        Throws.InvalidOperationException);
        }
        public void SingleOrDefault_PredicateUsedToFindOneCountry_ReturnsOneCountry()
        {
            var randomCountryId = PreDefinedData.GetRandomCountryId();

            var result = UnitOfWork.ReferenceCountries.SingleOrDefault(p => p.Id == randomCountryId);

            Assert.That(result, Is.Not.Null);
        }
        public void Find_PredicateUsedToFindOneCountry_ReturnsCollection()
        {
            var randomCountryId = PreDefinedData.GetRandomCountryId();

            var result = UnitOfWork.ReferenceCountries.Find(p => p.Id == randomCountryId);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result.First().Id == randomCountryId);
        }
        public void Get_ValidCountryCode_ReturnsSingleCountry()
        {
            var randomCountryId = PreDefinedData.GetRandomCountryId();

            var result = UnitOfWork.ReferenceCountries.Get(randomCountryId);

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Id, Is.EqualTo(randomCountryId));
        }
        public void Remove_ValidCountryExists_CountryCannotBeFetched()
        {
            var randomCountryId        = PreDefinedData.GetRandomCountryId();
            var removeReferenceCountry = UnitOfWork.ReferenceCountries.Get(randomCountryId);

            UnitOfWork.ReferenceCountries.Remove(removeReferenceCountry);
            UnitOfWork.Complete();

            var result = UnitOfWork.ReferenceCountries.Get(removeReferenceCountry.Id);

            Assert.That(result, Is.Null);
        }
        public void Add_ValidCountryExists_ThrowsInvalidOperationException()
        {
            var randomCountryId = PreDefinedData.GetRandomCountryId();
            var randomCountry   = UnitOfWork.ReferenceCountries.Get(randomCountryId);

            Assert.That(() => UnitOfWork.ReferenceCountries.Add(
                            new ReferenceCountry
            {
                Id   = randomCountry.Id,
                Code = randomCountry.Code
            }),
                        Throws.InvalidOperationException);
        }
        public async Task Update_EmptyPayload_HttpBadRequest()
        {
            // Arrange
            var randomCountryId = PreDefinedData.GetRandomCountryId();
            var path            = GetRelativePath(nameof(ReferenceCountriesController), randomCountryId.ToString());

            // Act
            var response = await Client.PutAsync(path,
                                                 new StringContent(string.Empty, Encoding.UTF8));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
        }
        public async Task Delete_ValidCountry_HttpNoContent()
        {
            // Arrange
            var randomCountryId = PreDefinedData.GetRandomCountryId();
            var path            = GetRelativePath(nameof(ReferenceCountriesController), randomCountryId.ToString());

            // Act
            var response = await Client.DeleteAsync(path);

            var responseString = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.DoesNotThrow(
                () => response.EnsureSuccessStatusCode(),
                string.Format(HttpExceptionFormattedMessage, responseString));
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
        }
        public void RemoveRange_TwoValidCountriesDuplicated_ThrowsInvalidOperationException()
        {
            var randomCountryId = PreDefinedData.GetRandomCountryId();
            var randomCountry   = UnitOfWork.ReferenceCountries.Get(randomCountryId);

            var existingCountries = new Collection <ReferenceCountry>
            {
                new ReferenceCountry {
                    Id = randomCountry.Id, Code = randomCountry.Code
                },
                new ReferenceCountry {
                    Id = randomCountry.Id, Code = randomCountry.Code
                }
            };

            Assert.That(() => UnitOfWork.ReferenceCountries.RemoveRange(existingCountries),
                        Throws.InvalidOperationException);
        }
        public async Task Update_MalformedPayload_HttpBadRequest()
        {
            // Arrange
            var randomCountryId    = PreDefinedData.GetRandomCountryId();
            var path               = GetRelativePath(nameof(ReferenceCountriesController), randomCountryId.ToString());
            var apiUpdatingCountry = new ReferenceCountryDto
            {
                Id = randomCountryId
                     // Code is required, keep it missing
            };

            // Act
            var response = await Client.PutAsync(path, new StringContent(
                                                     JsonConvert.SerializeObject(apiUpdatingCountry),
                                                     Encoding.UTF8,
                                                     JsonMediaType));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
        }
        public async Task Create_ExistingCountry_HttpConflict()
        {
            // Arrange
            var path            = GetRelativePath(nameof(ReferenceCountriesController));
            var randomCountryId = PreDefinedData.GetRandomCountryId();
            var randomCountry   = PreDefinedData.ReferenceCountries[randomCountryId - 1];

            var newCountryDto = new ReferenceCountryDto
            {
                Code        = randomCountry.Code,
                LongName    = "Create Test",
                CreatedDate = DateTime.UtcNow
            };

            // Act
            var response = await Client.PostAsync(path, new StringContent(
                                                      JsonConvert.SerializeObject(newCountryDto),
                                                      Encoding.UTF8,
                                                      JsonMediaType));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Conflict));
        }