<commit_before>﻿using artm.MvxPlugins.Fetcher.Entities;
using artm.MvxPlugins.Fetcher.Services;
using artm.MvxPlugins.Fetcher.Tests.Common;
using artm.MvxPlugins.Fetcher.Tests.Services.Calculator;
using artm.MvxPlugins.Logger.Services;
using Moq;
using MvvmCross.Platform;
using NUnit.Framework;
using System;
using System.Threading.Tasks;

namespace artm.MvxPlugins.Fetcher.Tests.Services.DreamsFetcher
{
    [TestFixture]
    public class DreamsFetcherServiceTests : MvxTestFixtureBase
    {
        private const string URL = "https://jsonplaceholder.typicode.com/users";

        [Test]
        [Category(TestCategory.Slow)]
        [Category(TestCategory.Internet)]
        public async Task Fetch_Sunshine_TriesToFetchFromWeb()
        {
            var sut = new FetcherServiceMock(Mock.Of<ILoggerService>(), new FetcherRepositoryServiceMock());
            sut.FetchFromWebResponse = FetcherResponseValidFactory();

            var response = await sut.Fetch(new Uri(URL));

            Assert.NotNull(response.Response);
            Assert.IsTrue(response.Response.Equals(FetcherResponseValidFactory()));
        }

        #region Mock factories
        private static string FetcherResponseValidFactory()
        {
            return @"
            
[
  {
    ""id"": 1,
    ""name"": ""<NAME>"",
    ""username"": ""Bret"",
    ""email"": ""<EMAIL>"",
    ""address"": {
      ""street"": ""Kulas Light"",
      ""suite"": ""Apt. 556"",
      ""city"": ""Gwenborough"",
      ""zipcode"": ""92998-3874"",
      ""geo"": {
        ""lat"": ""-37.3159"",
        ""lng"": ""81.1496""
      }
    },
    ""phone"": ""1-770-736-8031 x56442"",
    ""website"": ""hildegard.org"",
    ""company"": {
      ""name"": ""Romaguera-Crona"",
      ""catchPhrase"": ""Multi-layered client-server neural-net"",
      ""bs"": ""harness real-time e-markets""<commit_msg>add tests<commit_after>﻿using artm.MvxPlugins.Fetcher.Entities;
using artm.MvxPlugins.Fetcher.Services;
using artm.MvxPlugins.Fetcher.Tests.Common;
using artm.MvxPlugins.Fetcher.Tests.Services.Calculator;
using artm.MvxPlugins.Logger.Services;
using Moq;
using MvvmCross.Platform;
using NUnit.Framework;
using System;
using System.Threading.Tasks;

namespace artm.MvxPlugins.Fetcher.Tests.Services.DreamsFetcher
{
    [TestFixture]
    public class DreamsFetcherServiceTests : MvxTestFixtureBase
    {
        private const string URL = "https://jsonplaceholder.typicode.com/users";

        [Test]
        public async Task Fetch_Sunshine_TriesToFetchFromWeb()
        {
            var sut = new FetcherServiceMock(Mock.Of<ILoggerService>(), new FetcherRepositoryServiceMock());
            sut.FetchFromWebResponse = FetcherResponseValidFactory();

            var response = await sut.Fetch(new Uri(URL));

            Assert.NotNull(response.Response);
            Assert.IsTrue(response.Response.Equals(FetcherResponseValidFactory()));
        }

        [Test]
        public async Task Fetch_NoEntries_RepositoryInsertUrlIsCalled()
        {
            var repository = new Mock<IFetcherRepositoryService>();
            var sut = new FetcherServiceMock(Mock.Of<ILoggerService>(), repository.Object);

            var response = await sut.Fetch(new Uri(URL));

            repository.Verify(x => x.InsertUrl(It.IsAny<Uri>(), It.IsAny<string>()), Times.Once);
        }

        [Test]
        public async Task Fetch_WithEntries_RepositoryInsertUrlIsNotCalled()
        {
            var repository = new Mock<IFetcherRepositoryService>();
            repository.Setup(x => x.GetEntryForUrl(It.IsAny<Uri>())).Returns(() => new UrlCacheInfoMock() { Url = URL, Created = DateTimeOffset.UtcNow, LastAccessed = DateTimeOffset.UtcNow, LastUpdated = DateTimeOffset.UtcNow, Response = "myResponse" });

            var sut = new FetcherServiceMock(Mock.Of<ILoggerService>(), repository.Object);

            var response = await sut.Fetch(new Uri(URL));

            repository.Verify(x => x.InsertUrl(It.IsAny<Uri>(), It.IsAny<string>()), Times.Never);
        }

        [Test]
        public async Task Fetch_WithEntries_RepositoryUpdateLastAccessedIsCalled()
        {
            var repository = new Mock<IFetcherRepositoryService>();
            repository.Setup(x => x.GetEntryForUrl(It.IsAny<Uri>())).Returns(() => new UrlCacheInfoMock() { Url = URL, Created = DateTimeOffset.UtcNow, LastAccessed = DateTimeOffset.UtcNow, LastUpdated = DateTimeOffset.UtcNow, Response = "myResponse" });

            var sut = new FetcherServiceMock(Mock.Of<ILoggerService>(), repository.Object);

            var response = await sut.Fetch(new Uri(URL));

            repository.Verify(x => x.UpdateLastAccessed(It.IsAny<IUrlCacheInfo>()), Times.Once);
        }

        #region Mock factories
        private static string FetcherResponseValidFactory()
        {
            return @"
            
[
  {
    ""id"": 1,
    ""name"": ""<NAME>"",
    ""username"": ""Bret"",
    ""email"": ""<EMAIL>"",
    ""address"": {
      ""street"": ""Kulas Light"",
      ""suite"": ""Apt. 556"",
      ""city"": ""Gwenborough"",
      ""zipcode"": ""92998-3874"",
      ""geo"": {
        ""lat"": ""-37.3159"",
        ""lng"": ""81.1496""
      }
    },
    ""phone"": ""1-770-736-8031 x56442"",
    ""website"": ""hildegard.org"",
    ""company"": {
      ""name"": ""Romaguera-Crona"",
      ""catchPhrase"": ""Multi-layered client-server neural-net"",
      ""bs"": ""harness real-time e-markets""
{'fraction_non_alphanumeric': 0.15102639296187684, 'fraction_numerical': 0.015029325513196481, 'mean_word_length': 3.0184094256259204, 'pattern_counts': {'":': 36, '<': 22, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '419084', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1707, 'n_words': 298}