Example #1
0
        public void TestSearch()
        {
            // Arrange
            var mockRuntime = new Mock <Runtime>(
                QueueMode.Server | QueueMode.Client,
                180, Enviorenment.Development
                )
            {
                CallBase = true
            };

            var serviceProvider = new ServiceCollection()
                                  .AddLogging()
                                  .BuildServiceProvider();

            var factory = serviceProvider.GetService <ILoggerFactory>();

            var logger = factory.CreateLogger <CacheController>();

            QueryRepository repo = new QueryRepository(
                QueryContextFactory.Create(mockRuntime.Object));

            CacheController cache = new CacheController(
                repo, mockRuntime.Object, logger);

            // Act
            SearchQuery query = new SearchQuery {
                Text       = "docker",
                MaxResults = 100
            };

            byte[] rawQuery = cache.AddQuery(Encoding.UTF8.GetBytes(
                                                 JsonConvert.SerializeObject(query)));

            SearchQuery cachedQuery = JsonConvert.DeserializeObject <SearchQuery>(
                Encoding.UTF8.GetString(rawQuery));

            SearchResult result = new SearchResult();

            result.SearchQueryId = cachedQuery.Id;
            result.Service       = "bitbucket";
            result.HandledBy     = "cacheUnitTest";
            result.Data          = "{'result': 'Hello World'}";

            byte[] rawQueryResult = cache.CacheResult(Encoding.UTF8.GetBytes(
                                                          JsonConvert.SerializeObject(result)));

            SearchResult cachedResult = JsonConvert.DeserializeObject <SearchResult>(
                Encoding.UTF8.GetString(rawQueryResult));

            // Assert
            Assert.NotNull(cachedResult);
            Assert.True(cachedResult.Data == result.Data);
        }