|
| 1 | +using CodePush.ReactNative; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | + |
| 6 | +namespace CodePush.Net46.Test |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Some tests for telemetry manager |
| 10 | + /// As implementation of TelemetryManager was ported from android version, we do not test logic here. |
| 11 | + /// Here are tests for some tricky parts of implementation, or check some data transformation that |
| 12 | + /// has no full equvalent in C# |
| 13 | + /// </summary> |
| 14 | + [TestClass] |
| 15 | + public class TelemetryManagerTest |
| 16 | + { |
| 17 | + #region Constants from TelemetryManager |
| 18 | + //private static readonly string APP_VERSION_KEY = "appVersion"; |
| 19 | + private static readonly string DEPLOYMENT_FAILED_STATUS = "DeploymentFailed"; |
| 20 | + private static readonly string DEPLOYMENT_KEY_KEY = "deploymentKey"; |
| 21 | + private static readonly string DEPLOYMENT_SUCCEEDED_STATUS = "DeploymentSucceeded"; |
| 22 | + private static readonly string LABEL_KEY = "label"; |
| 23 | + private static readonly string LAST_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_LAST_DEPLOYMENT_REPORT"; |
| 24 | + //private static readonly string PACKAGE_KEY = "package"; |
| 25 | + //private static readonly string PREVIOUS_DEPLOYMENT_KEY_KEY = "previousDeploymentKey"; |
| 26 | + //private static readonly string PREVIOUS_LABEL_OR_APP_VERSION_KEY = "previousLabelOrAppVersion"; |
| 27 | + private static readonly string RETRY_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_RETRY_DEPLOYMENT_REPORT"; |
| 28 | + private static readonly string STATUS_KEY = "status"; |
| 29 | + #endregion |
| 30 | + |
| 31 | + [TestMethod] |
| 32 | + public void TestGetUpdateReportNoPreviousUpdate() |
| 33 | + { |
| 34 | + var input = new JObject(); |
| 35 | + input.Add(DEPLOYMENT_KEY_KEY, "depKeyParam"); |
| 36 | + input.Add(LABEL_KEY, "labelParam"); |
| 37 | + |
| 38 | + var output = TelemetryManager.GetUpdateReport(input); |
| 39 | + Assert.IsNotNull(output); |
| 40 | + Assert.IsTrue(output.ToString(Formatting.None).Contains("\"status\":\"DeploymentSucceeded\"")); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public void TestGetUpdateReportWithPreviousUpdate() |
| 45 | + { |
| 46 | + SettingsManager.SetString(LAST_DEPLOYMENT_REPORT_KEY, "prevKey:prevLabel"); |
| 47 | + var input = new JObject(); |
| 48 | + input.Add(DEPLOYMENT_KEY_KEY, "depKeyParam"); |
| 49 | + input.Add(LABEL_KEY, "labelParam"); |
| 50 | + |
| 51 | + var output = TelemetryManager.GetUpdateReport(input); |
| 52 | + Assert.IsNotNull(output); |
| 53 | + Assert.IsTrue(output.ToString(Formatting.None).Contains("\"status\":\"DeploymentSucceeded\"")); |
| 54 | + Assert.IsTrue(output.ToString(Formatting.None).Contains("\"previousDeploymentKey\":\"prevKey\",\"previousLabelOrAppVersion\":\"prevLabel\"")); |
| 55 | + |
| 56 | + //Clean Up |
| 57 | + SettingsManager.RemoveString(LAST_DEPLOYMENT_REPORT_KEY); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void TestGetUpdateReportNegative() |
| 62 | + { |
| 63 | + var inputNoLabel = new JObject(); |
| 64 | + inputNoLabel.Add(DEPLOYMENT_KEY_KEY, "depKeyParam"); |
| 65 | + Assert.IsNull(TelemetryManager.GetUpdateReport(inputNoLabel)); |
| 66 | + |
| 67 | + var inputNoKey = new JObject(); |
| 68 | + inputNoKey.Add(LABEL_KEY, "labelParam"); |
| 69 | + Assert.IsNull(TelemetryManager.GetUpdateReport(inputNoKey)); |
| 70 | + } |
| 71 | + |
| 72 | + [TestMethod] |
| 73 | + public void TestRecordStatusReportWithRollback() |
| 74 | + { |
| 75 | + var report = new JObject(); |
| 76 | + report.Add(STATUS_KEY, DEPLOYMENT_FAILED_STATUS); |
| 77 | + |
| 78 | + TelemetryManager.RecordStatusReported(report); |
| 79 | + Assert.IsTrue(true); |
| 80 | + } |
| 81 | + |
| 82 | + [TestMethod] |
| 83 | + public void TestRecordStatusReportWithoutRollback() |
| 84 | + { |
| 85 | + var reportSuccess = new JObject(); |
| 86 | + reportSuccess.Add(STATUS_KEY, DEPLOYMENT_SUCCEEDED_STATUS); |
| 87 | + TelemetryManager.RecordStatusReported(reportSuccess); |
| 88 | + |
| 89 | + var reportNoStatus = new JObject(); |
| 90 | + TelemetryManager.RecordStatusReported(reportNoStatus); |
| 91 | + |
| 92 | + Assert.IsTrue(true); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void TestStatusReportForRetrySerialization() |
| 97 | + { |
| 98 | + SettingsManager.RemoveString(RETRY_DEPLOYMENT_REPORT_KEY); |
| 99 | + var original = new JObject(); |
| 100 | + original.Add("keyString", "stringValue"); |
| 101 | + original.Add("keyInt", 42); |
| 102 | + original.Add("keyBool", true); |
| 103 | + |
| 104 | + TelemetryManager.SaveStatusReportForRetry(original); |
| 105 | + |
| 106 | + var stringified = SettingsManager.GetString(RETRY_DEPLOYMENT_REPORT_KEY); |
| 107 | + SettingsManager.RemoveString(RETRY_DEPLOYMENT_REPORT_KEY); |
| 108 | + |
| 109 | + Assert.IsNotNull(stringified); |
| 110 | + var result = JObject.Parse(stringified); |
| 111 | + |
| 112 | + Assert.IsTrue((bool)result.GetValue("keyBool")); |
| 113 | + Assert.AreEqual(42, (int)result.GetValue("keyInt")); |
| 114 | + Assert.AreEqual("stringValue", (string)result.GetValue("keyString")); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments