예제 #1
0
        public async Task ShouldInitializeMovies()
        {
            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieServer, TestMovieServer>();
                    services.AddSingleton <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddSingleton <ICacheMovieRepository>(s => new TestMovieMemoryRepository(false));
                    services.AddSingleton <IDataUpdaterService>(s => new DataUpdaterService(DateTimeOffset.UtcNow.AddDays(-1), TimeSpan.FromSeconds(20), "https://localhost:44363/api/action/update", s.GetRequiredService <ILogger <DataUpdaterService> >()));
                });
            })
                         .CreateClient();

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <List <Movie> > > >();
            var converter  = new JsonConverter.JsonConverterService <List <Movie> >(fakeLogger.Object);
            //Act
            var response = await client.GetStringAsync("/api/movies");

            var dataBeforeInit = converter.Deserialize(response);
            await client.PostAsync("/api/action/initialize", null);

            client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
            {
                NoCache = true
            };
            var response2 = await client.GetStringAsync("/api/movies");

            var dataAfterInit = converter.Deserialize(response2);

            Assert.NotEqual(dataBeforeInit.Count(), dataAfterInit.Count());
        }
예제 #2
0
        public async Task ShouldReturnACachedImageThatIsTheSameAsTheExternalGiven(string id, string originalImage)
        {
            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieServer, TestMovieServer>();
                    services.AddScoped <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddScoped <ICacheMovieRepository>(s => new TestMovieMemoryRepository());
                });
            })
                         .CreateClient();

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <Movie> > >();
            var converter  = new JsonConverter.JsonConverterService <Movie>(fakeLogger.Object);

            //Act
            var response = await client.GetStringAsync($"{_baseAddress}/{Guid.Parse(id)}");

            var data     = converter.Deserialize(response);
            var xyzImage = await client.GetByteArrayAsync("/" + data.cardImages[0].url);

            HttpClient externalClient   = new HttpClient();
            var        xyzExternalImage = await externalClient.GetByteArrayAsync(originalImage);

            // Assert
            Assert.Equal(xyzImage, xyzExternalImage);
        }
예제 #3
0
        public async Task ShouldUpdateRepositoryAndReturnUpdatedMovies(string id, int position)
        {
            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieServer, TestMovieServer>();
                    services.AddSingleton <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddSingleton <ICacheMovieRepository>(s => new TestMovieMemoryRepository(false));
                    services.AddSingleton <IDataUpdaterService>(s => new DataUpdaterService(DateTimeOffset.UtcNow.AddDays(-1), TimeSpan.FromSeconds(20), "https://localhost:44363/api/action/update", s.GetRequiredService <ILogger <DataUpdaterService> >()));
                });
            })
                         .CreateClient();
            await client.PostAsync("/api/action/update", null);

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <List <Movie> > > >();
            var converter  = new JsonConverter.JsonConverterService <List <Movie> >(fakeLogger.Object);

            //Act
            var response = await client.GetStringAsync("/api/movies");

            var data = converter.Deserialize(response);

            // Assert
            Assert.Equal(Guid.Parse(id), data[position].id);
        }
예제 #4
0
        public async Task ShouldReturnMoreThanOneEntriesWhenGetMoviesCalledGivenNotUpToDateRepo()
        {
            var client = _factory.WithWebHostBuilder(builder =>
            {
                var s = builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieServer, TestMovieServer>();
                    services.AddSingleton <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddSingleton <ICacheMovieRepository>(s => new TestMovieMemoryRepository(false));
                    services.AddSingleton <IDataUpdaterService>(s => new DataUpdaterService(DateTimeOffset.UtcNow.AddDays(-1), TimeSpan.FromSeconds(20), "https://localhost:44363/api/action/update", s.GetRequiredService <ILogger <DataUpdaterService> >()));
                });
            })
                         .CreateClient();
            await client.PostAsync("/api/action/update", null);

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <List <Movie> > > >();
            var converter  = new JsonConverter.JsonConverterService <List <Movie> >(fakeLogger.Object);

            //Act
            var response = await client.GetStringAsync("/api/movies");

            var data = converter.Deserialize(response);

            // Assert
            Assert.True(data.Count() > 1);
        }
예제 #5
0
        public void ShouldDeserializeANotValidFormatGuidGivenAByteArray()
        {
            var fakeLogger = new Mock <ILogger <JsonConverterService <TestGuidDeserialization> > >();

            JsonConverter.JsonConverterService <TestGuidDeserialization> srv = new JsonConverter.JsonConverterService <TestGuidDeserialization>(fakeLogger.Object);

            var deserialize = "{\"Guid\":\"81a130d2502f4cf1a37663edeb000e9f\"}";
            var ss          = srv.Deserialize(Encoding.Default.GetBytes(deserialize));

            Assert.Equal(Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f"), ss.Guid);
        }
예제 #6
0
        public void ShouldDeserializeAValidGuidGivenAstring()
        {
            var fakeLogger = new Mock <ILogger <JsonConverterService <TestGuidDeserialization> > >();

            JsonConverter.JsonConverterService <TestGuidDeserialization> srv = new JsonConverter.JsonConverterService <TestGuidDeserialization>(fakeLogger.Object);

            var deserialize = "{\"Guid\":\"81a130d2-502f-4cf1-a376-63edeb000e9f\"}";
            var ss          = srv.Deserialize(deserialize);

            Assert.Equal(Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f"), ss.Guid);
        }
예제 #7
0
        public async Task ShouldDownloadAnImageAndSaveItToDiscAndToChache(string id, string serverLocation)
        {
            var fileLocation = Path.Combine(Directory.GetCurrentDirectory(), serverLocation);
            var fileSaved    = File.Exists(fileLocation);

            if (fileSaved)
            {
                File.Delete(fileLocation);
            }

            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieServer, TestMovieServer>();
                    services.AddScoped <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddScoped <ICacheMovieRepository>(s => new TestMovieMemoryRepository());
                });
            })
                         .CreateClient();

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <Movie> > >();
            var converter  = new JsonConverter.JsonConverterService <Movie>(fakeLogger.Object);

            //Act
            var response = await client.GetStringAsync($"{_baseAddress}/{Guid.Parse(id)}");

            var data = converter.Deserialize(response);

            fileSaved = File.Exists(fileLocation);
            if (fileSaved)
            {
                File.Delete(fileLocation);
            }
            // Assert
            Assert.True(fileSaved);
        }
예제 #8
0
        public async Task ShouldReturnASingleMovieWithTheGiveId(string id, string url)
        {
            // Arrange
            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddScoped <ICacheMovieRepository, TestMovieMemoryRepository>();
                });
            })
                         .CreateClient();

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <Movie> > >();
            var converter  = new JsonConverter.JsonConverterService <Movie>(fakeLogger.Object);

            //Act
            var response = await client.GetStringAsync(url + $"/{Guid.Parse(id)}");

            var data = converter.Deserialize(response);

            // Assert
            Assert.Equal(Guid.Parse(id), data.id);
        }
예제 #9
0
        public async Task ShouldUpdateDataAndReturnTheNewUpdatedHeadLine(string id, string newHeadLine)
        {
            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddScoped <IMovieServer, TestMovieServer>();
                    services.AddScoped <IMovieRepository, TestMovieMemoryRepository>();
                    services.AddScoped <ICacheMovieRepository>(s => new TestMovieMemoryRepository(false));
                });
            })
                         .CreateClient();

            var fakeLogger = new Mock <ILogger <JsonConverter.JsonConverterService <Movie> > >();
            var converter  = new JsonConverter.JsonConverterService <Movie>(fakeLogger.Object);

            //Act
            var response = await client.GetStringAsync($"{_baseAddress}/{Guid.Parse(id)}");

            var data = converter.Deserialize(response);

            // Assert
            Assert.Equal(newHeadLine, data.headline);
        }