public async Task TestGetCardSetJsonWithEscapeCharacters() { // Arrange CardSetFile cardSetFile = new CardSetFile() { cdn_root = @"https:\/\/steamcdn-a.akamaihd.net\/", expire_time = 1541954560, url = @"\/apps\/583950\/resource\/card_set_1.0E871AFDD63D1CBD0FB52D924DF1923C4A6D443A.json" }; const string testContent = @"{ ""card_set"":{ ""version"":1,""set_info"":{ ""set_id"":0,""pack_item_def"":0,""name"":{ ""english"":""Base Set""} },""card_list"":[{""card_id"":1000,""base_card_id"":1000,""card_type"":""Stronghold"",""card_name"":{""english"":""Ancient Tower""},""card_text"":{},""mini_image"":{""default"":""https://steamcdn-a.akamaihd.net/apps/583950/icons/set00/1000.91b2ed80da07ef5cf343540b09687fbf875168c8.png""},""large_image"":{""default"":""https://steamcdn-a.akamaihd.net/apps/583950/icons/set00/1000_large_english.3dea67025da70c778d014dc3aae80c0c0a7008a6.png""},""ingame_image"":{},""hit_points"":80,""references"":[]}]}}"; Mock <ILoggingAdapter <HttpClientService> > myLogger = new Mock <ILoggingAdapter <HttpClientService> >(); myLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); Mock <HttpMessageHandler> mockMessageHandler = new Mock <HttpMessageHandler>(); mockMessageHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(testContent) }); HttpClientAccessor httpClientAccessor = new HttpClientAccessor(mockMessageHandler.Object); HttpClientService clientService = new HttpClientService(myLogger.Object, httpClientAccessor); // Act string result = await clientService.GetCardSetJson(cardSetFile); // Assert }
public async Task <string> GetCardSetJson(CardSetFile file) { if (file == null || string.IsNullOrEmpty(file.cdn_root) || string.IsNullOrEmpty(file.url)) { ArgumentNullException ex = new ArgumentNullException("file"); _logger.LogError(ex, "CardSetFile cannot be null or empty."); throw ex; } if (file.cdn_root.Contains(@"\/")) { FormatException ex = new FormatException("Invalid url format, escape characters are still in the string."); _logger.LogError(ex, "Exception thrown in HttpClientService.GetCardSetJson method."); throw ex; } string result = null; HttpResponseMessage response = await _client.GetAsync(file.cdn_root + file.url); if (response.IsSuccessStatusCode) { result = await response.Content.ReadAsStringAsync(); } return(result); }
public async Task TestCardSetFileInvalidId() { // Arrange const string testContent = "test content"; Mock <ILoggingAdapter <CardSetManager> > mockCardSetLogger = new Mock <ILoggingAdapter <CardSetManager> >(); mockCardSetLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); Mock <ILoggingAdapter <HttpClientService> > mockHttpLogger = new Mock <ILoggingAdapter <HttpClientService> >(); Mock <ILoggingAdapter <JsonParsingManager> > mockJsonLogger = new Mock <ILoggingAdapter <JsonParsingManager> >(); Mock <HttpMessageHandler> mockMessage = new Mock <HttpMessageHandler>(); mockMessage.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(testContent) }); HttpClientAccessor httpAccessor = new HttpClientAccessor(mockMessage.Object); Mock <IHttpClientService> mockClientService = new Mock <IHttpClientService>(); Mock <IJsonParsingManager> mockJsonManager = new Mock <IJsonParsingManager>(); CardSetManager manager = new CardSetManager(mockCardSetLogger.Object, mockClientService.Object, mockJsonManager.Object); // Act CardSetFile setFile = await manager.GetCardSetFile("lkjhekljhdjkfgksghdf"); // Assert // Invalid Set Id seems to return the base set, but we want to throw a format exception }
public CardSetFile ParseRawJsonFileLocation(string locationUrl) { if (string.IsNullOrEmpty(locationUrl)) { throw new ArgumentNullException("locationUrl"); } JsonValue jsonLocationUrl = null; try { jsonLocationUrl = JsonValue.Parse(locationUrl); } catch (ArgumentException ex) { _logger.LogError(ex, "Exception thrown in JsonParsingManager.ParseRawJsonFileLocation method."); throw new FormatException("locationUrl was not in JSON format.", ex); } CardSetFile cardSetFile = JsonConvert.DeserializeObject <CardSetFile>(locationUrl); string cdnRootKey = "cdn_root"; string dateKey = "expire_time"; string urlKey = "url"; if (!jsonLocationUrl.ContainsKey(cdnRootKey) || !jsonLocationUrl.ContainsKey(dateKey) || !jsonLocationUrl.ContainsKey(urlKey)) { FormatException ex = new FormatException("locationUrl was in JSON format, but did not have the correct keys."); _logger.LogError(ex, "Exception thrown in JsonParsingManager.ParseRawJsonFileLocation method."); throw ex; } return(cardSetFile); }
public void FileExpirationDatePropTest() { // Arrange CardSetFile file = new CardSetFile(); // Act file.expire_time = 1541860748; // Assert Assert.AreEqual(1541860748, file.expire_time); }
public void UrlPropTest() { // Arrange CardSetFile file = new CardSetFile(); const string testUrl = "http://website.com"; // Act file.url = testUrl; // Assert Assert.AreEqual(testUrl, file.url); }
public void ContentDeliveryRootPropTest() { // Arrange CardSetFile file = new CardSetFile(); const string test = "testWebsite"; // Act file.cdn_root = test; // Assert Assert.AreEqual(test, file.cdn_root); }
public void TestFileLocationInvalidJsonKeys() { // Arrange Mock <ILoggingAdapter <JsonParsingManager> > mockLogger = new Mock <ILoggingAdapter <JsonParsingManager> >(); mockLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); JsonParsingManager manager = new JsonParsingManager(mockLogger.Object); // Act const string validJsonDataWithWrongKeys = @"{ ""key_A"":""https:\/\/steamcdn-a.akamaihd.net\/"",""key_B"":""\/apps\/583950\/resource\/card_set_0.BB8732855C64ACE2696DCF5E25DEDD98D134DD2A.json"",""key_C"":1541860748}"; CardSetFile cardSetFile = manager.ParseRawJsonFileLocation(validJsonDataWithWrongKeys); // Assert }
public void TestFileLocationValidFormatUtcDate() { // Arrange double unixTimeStamp = 1541860748; const string validJsonData = @"{ ""cdn_root"":""https:\/\/steamcdn-a.akamaihd.net\/"",""url"":""\/apps\/583950\/resource\/card_set_0.BB8732855C64ACE2696DCF5E25DEDD98D134DD2A.json"",""expire_time"":1541860748}"; Mock <ILoggingAdapter <JsonParsingManager> > mockLogger = new Mock <ILoggingAdapter <JsonParsingManager> >(); mockLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); JsonParsingManager manager = new JsonParsingManager(mockLogger.Object); // Act CardSetFile cardSetFile = manager.ParseRawJsonFileLocation(validJsonData); // Assert Assert.AreEqual(unixTimeStamp, cardSetFile.expire_time); }
public void TestFileLocationValidFormatWithAdditionalKeys() { // Arrange Mock <ILoggingAdapter <JsonParsingManager> > mockLogger = new Mock <ILoggingAdapter <JsonParsingManager> >(); mockLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); JsonParsingManager manager = new JsonParsingManager(mockLogger.Object); // Act const string validJsonData = @"{ ""cdn_root"":""https:\/\/steamcdn-a.akamaihd.net\/"",""url"":""\/apps\/583950\/resource\/card_set_0.BB8732855C64ACE2696DCF5E25DEDD98D134DD2A.json"",""expire_time"":1541860748,""key_D"":""123""}"; CardSetFile cardSetFile = manager.ParseRawJsonFileLocation(validJsonData); // Assert Assert.IsNotNull(cardSetFile); Assert.IsNotNull(cardSetFile.cdn_root); Assert.IsNotNull(cardSetFile.expire_time); Assert.IsNotNull(cardSetFile.url); }
public async Task TestCardSetFileValidId() { // Arrange const string testContent = "test content"; string testSetId = "00"; string testFileLocation = @"{""cdn_root"":""https:\/\/ steamcdn - a.akamaihd.net\/ "",""url"":""\/ apps\/ 583950\/ resource\/ card_set_0.BB8732855C64ACE2696DCF5E25DEDD98D134DD2A.json"",""expire_time"":1541859144}"; Mock <ILoggingAdapter <CardSetManager> > mockCardSetLogger = new Mock <ILoggingAdapter <CardSetManager> >(); mockCardSetLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); Mock <ILoggingAdapter <HttpClientService> > mockHttpLogger = new Mock <ILoggingAdapter <HttpClientService> >(); Mock <ILoggingAdapter <JsonParsingManager> > mockJsonLogger = new Mock <ILoggingAdapter <JsonParsingManager> >(); Mock <HttpMessageHandler> mockMessage = new Mock <HttpMessageHandler>(); mockMessage.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(testContent) }); HttpClientAccessor httpAccessor = new HttpClientAccessor(mockMessage.Object); Mock <IHttpClientService> mockClientService = new Mock <IHttpClientService>(); mockClientService.Setup(mockClient => mockClient.GetRawJsonFileLocation(testSetId)).Returns(Task.FromResult(testFileLocation)); Mock <IJsonParsingManager> mockJsonManager = new Mock <IJsonParsingManager>(); mockJsonManager.Setup(jsonManager => jsonManager.ParseRawJsonFileLocation(testFileLocation)) .Returns(new CardSetFile { cdn_root = @"https:\/\/ steamcdn - a.akamaihd.net\/", expire_time = 1541859144, url = @"\/apps\/583950\/resource\/card_set_0.BB8732855C64ACE2696DCF5E25DEDD98D134DD2A.json" }); CardSetManager manager = new CardSetManager(mockCardSetLogger.Object, mockClientService.Object, mockJsonManager.Object); // Act CardSetFile setFile = await manager.GetCardSetFile(testSetId); // Assert Assert.IsNotNull(setFile); Assert.IsNotNull(setFile.cdn_root); Assert.IsNotNull(setFile.expire_time); Assert.IsNotNull(setFile.url); }
public void ToStringTest() { // Arrange CardSetFile file = new CardSetFile(); const string testUrl = "http://website.com"; int expireTime = 1111111; const string cdn = "http://cdn.com"; file.cdn_root = cdn; file.expire_time = expireTime; file.url = testUrl; // Act string toString = file.ToString(); // Assert Assert.IsTrue(toString.Contains("cdn_root")); Assert.IsTrue(toString.Contains("expire_time")); Assert.IsTrue(toString.Contains("url")); }
public void TestFileLocationDefaultFormat() { // Arrange const string escapeChars = @"\/"; Mock <ILoggingAdapter <JsonParsingManager> > mockLogger = new Mock <ILoggingAdapter <JsonParsingManager> >(); mockLogger.Setup(logger => logger.LogError(It.IsAny <Exception>(), It.IsAny <string>())).Verifiable(); JsonParsingManager manager = new JsonParsingManager(mockLogger.Object); // Act const string jsonData = "{ \"cdn_root\":\"https:\\/\\/steamcdn-a.akamaihd.net\\/\",\"url\":\"\\/apps\\/583950\\/resource\\/card_set_1.0E871AFDD63D1CBD0FB52D924DF1923C4A6D443A.json\",\"expire_time\":1541954560}"; CardSetFile cardSetFile = manager.ParseRawJsonFileLocation(jsonData); // Assert Assert.IsNotNull(cardSetFile); Assert.IsNotNull(cardSetFile.cdn_root); Assert.IsNotNull(cardSetFile.expire_time); Assert.IsNotNull(cardSetFile.url); Assert.IsFalse(cardSetFile.cdn_root.Contains(escapeChars)); Assert.IsFalse(cardSetFile.url.Contains(escapeChars)); }
public async Task Start(string[] args) { _serviceProvider.GetService <ILoggerFactory>() .AddConsole(LogLevel.Debug); var loggerFactory = _serviceProvider.GetService <ILoggerFactory>(); var logger = loggerFactory.CreateLogger <Program>(); logger.LogDebug("Starting application"); IHttpClientService service = _serviceProvider.GetService <IHttpClientService>(); string locationJson = await service.GetRawJsonFileLocation("01"); IJsonParsingManager jsonParser = _serviceProvider.GetService <IJsonParsingManager>(); CardSetFile cardSetFile = jsonParser.ParseRawJsonFileLocation(locationJson); logger.LogDebug(cardSetFile.ToString()); string cardSetJson = await service.GetCardSetJson(cardSetFile); CardSet cards = jsonParser.ParseRawJsonFile(cardSetJson); logger.LogDebug(cards.ToString()); }