public void test_usagelog_prepandpushitem() { //Arrange //It will generate the exception related to large AppName and by consequence we reach the expection section in UsageLog constructor var logger = new UsageLog("AppName1", UserId, SessionId); //Using reflection we set to true the private property EnableDiagnosticsOutput PropertyInfo propertyInfo = typeof(UsageLog).GetProperty("EnableDiagnosticsOutput", BindingFlags.NonPublic | BindingFlags.Instance); propertyInfo.SetValue(logger, true, null); //Act largeAppName.Clear(); for (int i = 0; i < TEST_MAX_DATA_LENGTH + 1; i++) { largeAppName.Append(i.ToString()); } logger.Debug("notificationD", largeAppName.ToString()); //Using reflection we get the private field items inside UsageLog class FieldInfo varField = typeof(UsageLog).GetField("items", BindingFlags.NonPublic | BindingFlags.Instance); var itemsList = (Queue <Dictionary <string, string> >)varField.GetValue(logger); //Assert //We inserted the Debug //Because when using the UploaderExec method is using threads we can have 3 or 4 elements in the list. logger.Dispose(); Assert.IsNotNull(itemsList); //Check that itemsList is not null Assert.IsTrue(itemsList.Any()); //Check that itemsList has at least one item Assert.IsTrue(itemsList.FirstOrDefault().FirstOrDefault().Value.StartsWith("notificationD")); //Check that the value contains the value logged in the Debug method }
public void test_usagelog_log_methods() { //Arrange //It will generate the exception related to large AppName and by consequence we reach the expection section in UsageLog constructor var logger = new UsageLog("AppName1", UserId, SessionId); //Act logger.Debug("notificationD", "Debug Message"); logger.Error("notificationE", "Error Message"); logger.Log("notificationL", "Log Message"); logger.Verbose("notificationV", "Verbose Message"); //Using reflection we get the private field items inside UsageLog class FieldInfo varField = typeof(UsageLog).GetField("items", BindingFlags.NonPublic | BindingFlags.Instance); var itemsList = (Queue <Dictionary <string, string> >)varField.GetValue(logger); //Assert //We inserted the Debug, Error, Log and Verbose so we should have only four elements in the queue //Because when using the UploaderExec method is using threads we can have 3 or 4 elements in the list. logger.Dispose(); Assert.IsNotNull(itemsList); //Check that itemsList is not null Assert.IsTrue(itemsList.Any()); //Check that itemsList has at least one item Assert.LessOrEqual(itemsList.Count, 4); //Check that itemsList has less than 4 or equals to 4 }
public void test_usagelog_validate_inputs_exceptions() { //Arrange var logger = new UsageLog("AppName1", UserId, SessionId); //Act //Create a large string so the validations will fail when calling the logger functions largeAppName.Clear(); for (int i = 0; i < TEST_MAX_DATA_LENGTH + 1; i++) { largeAppName.Append(i.ToString()); } try { //Exception when the first parameter of Debug method is null logger.Debug(null, "Debug Message"); } catch (ArgumentNullException ex) { Assert.IsTrue(ex.Message.Contains("Tag must not be null")); } catch (Exception ex) { Assert.Fail(ex.Message); } try { //Exception when the second parameter of Error method is null logger.Error("notificationE", null); } catch (ArgumentNullException ex) { Assert.IsTrue(ex.Message.Contains("Text must not be null")); } catch (Exception ex) { Assert.Fail(ex.Message); } try { //Exception when the second parameter of Log method is null logger.Log(largeAppName.ToString(), "Log Message"); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message.Contains("Tag must be 256 chars or less")); } catch (Exception ex) { Assert.Fail(ex.Message); } try { //Exception when the second parameter of Verbose method has special characters logger.Verbose(specialCharsStr, "Verbose Message"); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message.Contains("Tag must only be letters, numbers or '-', '.'")); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { logger.Dispose(); } }
public void test_usagelog_push_item() { //Arrange //It will generate the exception related to large AppName and by consequence we reach the expection section in UsageLog constructor var logger = new UsageLog("AppName1", UserId, SessionId); //Using reflection we set to true the private property EnableDiagnosticsOutput PropertyInfo propertyInfo = typeof(UsageLog).GetProperty("EnableDiagnosticsOutput", BindingFlags.NonPublic | BindingFlags.Instance); propertyInfo.SetValue(logger, true, null); StringBuilder debugMessage = new StringBuilder("Debug Message "); //Act for (int i = 0; i < TEST_MAX_DATA_LENGTH + 1; i++) { debugMessage.Append(i.ToString()); } logger.Debug("notificationD", debugMessage.ToString()); //Using reflection we get the private field items inside UsageLog class FieldInfo varField = typeof(UsageLog).GetField("items", BindingFlags.NonPublic | BindingFlags.Instance); Queue <Dictionary <string, string> > itemsLarge = new Queue <Dictionary <string, string> >(); for (int i = 0; i < TEST_MAX_DATA_LENGTH + 1; i++) { var item = new Dictionary <string, string> { { "Tag", i.ToString() }, { "Priority", "PR" }, { "AppIdent", "AN" }, { "UserID", "UserID" }, { "SessionID", "Session" }, { "DateTime", DateTime.Now.ToString() }, { "MicroTime", "MT" }, { "Data", "Test" + i.ToString() } }; itemsLarge.Enqueue(item); } //This will set the items field with 5000000 items varField.SetValue(logger, itemsLarge); var itemFinal = new Dictionary <string, string> { { "Tag", "Test1" }, { "Priority", "PR" }, { "AppIdent", "AN" }, { "UserID", "UserID" }, { "SessionID", "Session" }, { "DateTime", DateTime.Now.ToString() }, { "MicroTime", "MT" }, { "Data", "Test1" } }; //Using reflection we execute the PushItem method, the second parameter has to be a Dictionary<string, string> //This will rearch the validation inside PushItem about MAX_ITEMS MethodInfo dynMethod = typeof(UsageLog).GetMethod("PushItem", BindingFlags.NonPublic | BindingFlags.Instance); dynMethod.Invoke(logger, new object[] { itemFinal }); var itemsList = (Queue <Dictionary <string, string> >)varField.GetValue(logger); //Assert //We inserted the Debug, Error, Log and Verbose so we should have only four elements in the queue //Because when using the UploaderExec method is using threads we can have 3 or 4 elements in the list. logger.Dispose(); Assert.IsNotNull(itemsList); //Check that itemsList is not null Assert.IsTrue(itemsList.Any()); //Check that itemsList has at least one item Assert.AreEqual(itemsList.Count, TEST_MAX_DATA_LENGTH + 1); //Check that itemsList has 500 000 + 1 items }