public async Task ScriptsClient_List_With_Filter() { var connection = new Mock <IConnection>(); connection.Setup(c => c.Get <IList <Script> >(ApiUrls.ScriptsList(), It.IsAny <Dictionary <string, string> >())) .ReturnsAsync(() => { var json = System.IO.File.ReadAllText("./Fixtures/Scripts_GetScripts.json"); return(JsonConvert.DeserializeObject <IList <Script> >(json)); }); var scriptsClient = new ScriptsClient(connection.Object); var result = await scriptsClient.List(new ScriptFilter() { Id = "1234", Description = "asdf", DTCreated = DateTime.Now, IsEnabled = true, Name = "asdf", OwnerId = "12345asdf", OwnerType = ScriptOwnerType.Team, RunOnce = true }); Assert.AreEqual(1, result.Count); }
public void Scripts_client_retrieves_last_known_good_content_from_local_cache_if_service_throws_exception() { // Arrange var service = new Mock<IScriptsService>(); service.Setup(x => x.Get(ContentTypes.Intranet_FatFooter)).Throws(new Exception("Unit test exception")); var cache = new Mock<ILocalCache<List<ContentScript>>>(); cache.Setup(x => x.WriteToCache(ContentTypes.Intranet_FatFooter, It.IsAny<List<ContentScript>>(), It.IsAny<DateTime>())); var scripts = new List<ContentScript> { new ContentScript { Url = new Uri("http://scripts.com/1.js") } }; cache.Setup(x => x.ReadFromCache(ContentTypes.Intranet_FatFooter)).Returns(scripts); var client = new ScriptsClient(service.Object, cache.Object); // Act client.Get(ContentTypes.Intranet_FatFooter); // Assert service.Verify(x => x.Get(ContentTypes.Intranet_FatFooter), Times.Once); cache.Verify(x => x.ReadFromCache(ContentTypes.Intranet_FatFooter), Times.Once); cache.Verify(x => x.WriteToCache(ContentTypes.Intranet_FatFooter, It.IsAny<List<ContentScript>>(), It.IsAny<DateTime>()), Times.Never); }
public async Task ScriptsClient_Destroy_HappyPath() { var connection = new Mock <IConnection>(); connection.Setup(c => c.Post <Script>(ApiUrls.ScriptsDestroy("test1234"), null, It.IsAny <object>(), null, null)) .ReturnsAsync(() => { return(null); }); var scriptsClient = new ScriptsClient(connection.Object); await scriptsClient.Destroy("test1234"); }
public async Task ScriptsClient_Create_Throws_On_Invalid_Args() { var connection = new Mock <IConnection>(); connection.Setup(c => c.Post <Script>(ApiUrls.ScriptsCreate(), null, It.IsAny <object>(), null, "test123")) .ReturnsAsync(() => { var json = System.IO.File.ReadAllText("./Fixtures/Scripts_GetScript.json"); return(JsonConvert.DeserializeObject <Script>(json)); }); var scriptsClient = new ScriptsClient(connection.Object); await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() => { await scriptsClient.Create(new CreateScriptRequest() { ScriptName = "foo", MachineId = "test12345", ScriptFile = "test123", ScriptText = "asdf" }); }); await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() => { await scriptsClient.Create(new CreateScriptRequest() { ScriptName = "foo", MachineId = "test12345", ScriptFile = null, ScriptText = null }); }); await scriptsClient.Create(new CreateScriptRequest() { ScriptName = "foo", MachineId = "test12345", ScriptFile = "test123", ScriptText = null }); await scriptsClient.Create(new CreateScriptRequest() { ScriptName = "foo", MachineId = "test12345", ScriptFile = null, ScriptText = "test123" }); }
public async Task ScriptsClient_Text_HappyPath() { var connection = new Mock <IConnection>(); connection.Setup(c => c.Get <string>(ApiUrls.ScriptsText("test12345"), It.IsAny <Dictionary <string, string> >())) .ReturnsAsync(() => { var json = System.IO.File.ReadAllText("./Fixtures/Scripts_GetScriptText.json"); return(JsonConvert.DeserializeObject <string>(json)); }); var scriptsClient = new ScriptsClient(connection.Object); var script = await scriptsClient.Text("test12345"); Assert.IsNotNull(script); }
public async Task ScriptsClient_List_HappyPath() { var connection = new Mock <IConnection>(); connection.Setup(c => c.Get <IList <Script> >(ApiUrls.ScriptsList(), null)) .ReturnsAsync(() => { var json = System.IO.File.ReadAllText("./Fixtures/Scripts_GetScripts.json"); return(JsonConvert.DeserializeObject <IList <Script> >(json)); }); var scriptsClient = new ScriptsClient(connection.Object); var result = await scriptsClient.List(); Assert.AreEqual(1, result.Count); }
public void Scripts_client_retrieves_last_known_good_content_from_local_cache_if_service_returns_no_data() { // Arrange var service = new Mock<IScriptsService>(); service.Setup(x => x.Get(ContentTypes.Intranet_FatFooter)).Returns((List<ContentScript>)null); var cache = new Mock<ILocalCache<List<ContentScript>>>(); cache.Setup(x => x.WriteToCache(ContentTypes.Intranet_FatFooter, It.IsAny<List<ContentScript>>(), It.IsAny<DateTime>())); cache.Setup(x => x.ReadFromCache(ContentTypes.Intranet_FatFooter)).Returns((List<ContentScript>)null); var client = new ScriptsClient(service.Object, cache.Object); // Act client.Get(ContentTypes.Intranet_FatFooter); // Assert service.Verify(x => x.Get(ContentTypes.Intranet_FatFooter), Times.Once); cache.Verify(x => x.ReadFromCache(ContentTypes.Intranet_FatFooter), Times.Once); cache.Verify(x => x.WriteToCache(ContentTypes.Intranet_FatFooter, It.IsAny<List<ContentScript>>(), It.IsAny<DateTime>()), Times.Never); }
public async Task ScriptsClient_Create_HappyPath() { var connection = new Mock <IConnection>(); connection.Setup(c => c.Post <Script>(ApiUrls.ScriptsCreate(), null, It.IsAny <object>(), null, "test123")) .ReturnsAsync(() => { var json = System.IO.File.ReadAllText("./Fixtures/Scripts_GetScript.json"); return(JsonConvert.DeserializeObject <Script>(json)); }); var scriptsClient = new ScriptsClient(connection.Object); var result = await scriptsClient.Create(new CreateScriptRequest() { ScriptName = "foo", MachineId = "test12345", ScriptFile = "test123" }); Assert.IsNotNull(result); }
public void Scripts_client_stores_last_known_good_response_to_local_cache() { // Arrange var service = new Mock<IScriptsService>(); var scripts = new List<ContentScript> { new ContentScript { Url = new Uri("http://scripts.com/1.js") } }; service.Setup(x => x.Get(ContentTypes.Intranet_FatFooter)).Returns(scripts); var cache = new Mock<ILocalCache<List<ContentScript>>>(); cache.Setup(x => x.WriteToCache(ContentTypes.Intranet_FatFooter, It.IsAny<List<ContentScript>>(), It.IsAny<DateTime>())); var client = new ScriptsClient(service.Object, cache.Object); // Act client.Get(ContentTypes.Intranet_FatFooter); // Assert service.Verify(x => x.Get(ContentTypes.Intranet_FatFooter), Times.Once); cache.Verify(x => x.WriteToCache(ContentTypes.Intranet_FatFooter, It.IsAny<List<ContentScript>>(), It.IsAny<DateTime>()), Times.Once); }
public void SmokeTest() { // Arrange ConfigurationManager.AppSettings["ScriptsServiceUrl"] = "http://localhost:2222/api/Scripts/"; var client = new ScriptsClient(); // Act var content = client.Get(ContentTypes.Intranet_FatFooter); // Assert Assert.That(content, Is.Not.Null); Assert.That(content, Is.InstanceOf<List<ContentScript>>()); }