Created
October 3, 2017 05:14
-
-
Save DanielLoth/1eaf53259d9b45f4fa90c413eaeb59d0 to your computer and use it in GitHub Desktop.
ServiceNow attachment retrieval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Text; | |
namespace UnitTests | |
{ | |
[TestClass] | |
public class ServiceNowAttachmentRetrievalTests | |
{ | |
private static readonly string Chunk0AsBase64 = "H4sIAAAAAAAAAA=="; // Length = 16, position = 0 | |
// Length = 1,416, position = 1 | |
private static readonly string Chunk1AsBase64 = | |
"7VVBcuQ2DPwKHjDxA5LDVu3Nl+SSD1AUZsi1RGoJcsba16cBSh77kHOqUrzYGhIAgUYD/b1VeqW1SSV+3xYXE9V" + | |
"Me24U8oPcslANUWiNUt0bJ4ozO8pXmjnllnxMN9oWdtIKk0szbcVFsVMN9XBCUy7Jrl7pERHvFu9sDzjyeYVzha" + | |
"f3iFY1cA1Mskvl9WJeyApXs507X5tDRux8wBty2t8Ku55/LlzO01paDRf7XB0Clt+mFpe5G4S2ukTBbVtMLPJCf" + | |
"2bKianwD/ZVLjRHWeIb4ysXcvccZ3lWGqvwcr3QxN410d8Un9c4B6rnXQ0Zfx8h05wp5UpvCcgqugB6a0UaPwMX" + | |
"V2NOgH0nToYJ0vU5Cf9sOGBBOJTqYMrvtfDKsFSor23RGpDqTYGPasjWk13L0ueXfIc/TPqj9jmzxKJRUf1U1dP" + | |
"6BoD+pUTcfi0ve+/kzNnH4tsKqliquGqFlAYh+oA34tI5ok94wL+V7LXqEFeSvJ6NPNF4ob8zKe1AlVriPaL3/O" + | |
"6UM5cjJhJtQnwHSiAJF7UWWhxIFzNutrBL9ObHxUeBI7973uqniu1lN99dqu7GdC15RbXf6DuqVNwCSAwYqcRbM" + | |
"L9rRBFX1xbcxxqQnXJJTX3IaLbByelH3nH10VprnMZKGeFS3nVKPrfWmHY262Cc62CZL9Cam+KKAOgZnkfGH/G/" + | |
"0V/JuJ618XgozQCJz0HlnqrVwIoMaoi3ZHSzphx8p5V7KUoxyWjzDUPDc7fhNRe3xF/4Pe32mg+urDaIH4Ue47c" + | |
"C1lQvFmTBa92nE+7SK4LZrkTQqbhidIXZXK1mfbCW3KaFn6Sf+i5QeEHhP/qG+KlLYVoc2jjxknUxwOI5d1cH4h" + | |
"mKHEH5VjV1RL4FAOTedP41ad1OJ6/6/JBoTLRMnHXr9JJQYnrTEyPLF2KDtAGFoCwlgua8cblip2A8JCp3e9JOd" + | |
"s0SsFcEalHCC72iaoQECiG3ormgGfiiLT/62gLBInqJ9LAailsx/2dzzBhIBtu+BTORqpj3xHrkDEjbQg+FE9yw" + | |
"hk8s6JKO0P5pwRmEE/DjBVv6eOIwsu4oP3l+sSnRcdbZ0/9fNoA65YdBlztdoDCdLtYGEN4oC4LcjIp2NTWxpaw" + | |
"7xyQDkOiMKIR9p/R5OJIVkP3OR76FtzZHV4+MbdCOXLzOvaaMBiGusI2trUklH8Tu4XYEy8ssB13MpmIHH4zSDY" + | |
"jW+6htRKZYkegs8v6dwod2fEoMTsK24vpU2n7D/w8Lm3le8A78Oc3mpQCbr4FMj1ykz4R0uIdeD70eej30euj10" + | |
"Ouh10Ovh14PvR56PfR66PXQ66HXQ6+HXg+9Hno99Hro9dDroddDr4deD70eej30euj10Ov/WK+HXA+5HnI95HrI" + | |
"9ZDr/5dc/wODfyWHSS0AAA=="; | |
/// <summary> | |
/// The list of chunks. In practice, there are many more chunks when attachments are sized | |
/// in Mebibytes. | |
/// | |
/// Note that the order is important. | |
/// </summary> | |
private static readonly IList<string> Base64ChunksOrdered = new List<string> | |
{ | |
Chunk0AsBase64, | |
Chunk1AsBase64 | |
}.AsReadOnly(); | |
/// <summary> | |
/// This method does everything. | |
/// </summary> | |
[TestMethod] | |
public void RunServiceNowAttachmentRetrieval() | |
{ | |
Assert.AreEqual(16, Chunk0AsBase64.Length); | |
Assert.AreEqual(1416, Chunk1AsBase64.Length); | |
var chunk0AsBinary = Convert.FromBase64String(Chunk0AsBase64); | |
var chunk1AsBinary = Convert.FromBase64String(Chunk1AsBase64); | |
var gzippedData = new byte[chunk0AsBinary.Length + chunk1AsBinary.Length]; | |
Array.Copy(chunk0AsBinary, 0, gzippedData, 0, chunk0AsBinary.Length); | |
Array.Copy(chunk1AsBinary, 0, gzippedData, chunk0AsBinary.Length, chunk1AsBinary.Length); | |
//const int blockSize = 4096; | |
var buffer = new byte[4096]; | |
using (var inputStream = new GZipStream(new MemoryStream(gzippedData), CompressionMode.Decompress)) | |
using (var outputStream = new MemoryStream()) | |
{ | |
var bytesRead = 0; | |
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
outputStream.Write(buffer, 0, bytesRead); | |
} | |
var uncompressedData = outputStream.ToArray(); | |
var outputText = Encoding.UTF8.GetString(uncompressedData); | |
Assert.AreEqual(CiceroText, outputText); | |
} | |
} | |
/// <summary> | |
/// And this method makes use of refactored code. | |
/// </summary> | |
[TestMethod] | |
public void ServiceNow2() | |
{ | |
var gzippedData = ConvertBase64ChunksToGzippedData(Base64ChunksOrdered); | |
var decompressedData = DecompressGzippedData(gzippedData); | |
var outputText = Encoding.UTF8.GetString(decompressedData); | |
Assert.AreEqual(CiceroText, outputText); | |
} | |
private byte[] DecompressGzippedData(byte[] gzippedData) | |
{ | |
var buffer = new byte[4096]; | |
using (var inputStream = new GZipStream(new MemoryStream(gzippedData), CompressionMode.Decompress)) | |
using (var decompressedData = new MemoryStream()) | |
{ | |
var bytesRead = 0; | |
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
decompressedData.Write(buffer, 0, bytesRead); | |
} | |
return decompressedData.ToArray(); | |
} | |
} | |
private byte[] ConvertBase64ChunksToGzippedData(IList<string> attachmentBase64Chunks) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
foreach (var chunk in attachmentBase64Chunks) | |
{ | |
var chunkBinary = Convert.FromBase64String(chunk); | |
ms.Write(chunkBinary, 0, chunkBinary.Length); | |
} | |
return ms.ToArray(); | |
} | |
} | |
#region Test string | |
/// <summary> | |
/// Our testing string. Sufficiently large that ServiceNow used two chunks to store it | |
/// as an attachment. | |
/// </summary> | |
private readonly string CiceroText = | |
"But I must explain to you how all this mistaken idea of deno" + | |
"uncing pleasure and praising pain was born and I will give y" + | |
"ou a complete account of the system, and expound the actual " + | |
"teachings of the great explorer of the truth, the master-bui" + | |
"lder of human happiness. No one rejects, dislikes, or avoids" + | |
" pleasure itself, because it is pleasure, but because those " + | |
"who do not know how to pursue pleasure rationally encounter " + | |
"consequences that are extremely painful. Nor again is there " + | |
"anyone who loves or pursues or desires to obtain pain of its" + | |
"elf, because it is pain, but because occasionally circumstan" + | |
"ces occur in which toil and pain can procure him some great " + | |
"pleasure. To take a trivial example, which of us ever undert" + | |
"akes laborious physical exercise, except to obtain some adva" + | |
"ntage from it? But who has any right to find fault with a ma" + | |
"n who chooses to enjoy a pleasure that has no annoying conse" + | |
"quences, or one who avoids a pain that produces no resultant" + | |
" pleasure? On the other hand, we denounce with righteous ind" + | |
"ignation and dislike men who are so beguiled and demoralized" + | |
" by the charms of pleasure of the moment, so blinded by desi" + | |
"re, that they cannot foresee the pain and trouble that are b" + | |
"ound to ensue; and equal blame belongs to those who fail in " + | |
"their duty through weakness of will, which is the same as sa" + | |
"ying through shrinking from toil and pain. These cases are p" + | |
"erfectly simple and easy to distinguish. In a free hour, whe" + | |
"n our power of choice is untrammelled and when nothing preve" + | |
"nts our being able to do what we like best, every pleasure i" + | |
"s to be welcomed and every pain avoided. But in certain circ" + | |
"umstances and owing to the claims of duty or the obligations" + | |
" of business it will frequently occur that pleasures have to" + | |
" be repudiated and annoyances accepted. The wise man therefo" + | |
"re always holds in these matters to this principle of select" + | |
"ion: he rejects pleasures to secure other greater pleasures," + | |
" or else he endures pains to avoid worse pains. But I must e" + | |
"xplain to you how all this mistaken idea of denouncing pleas" + | |
"ure and praising pain was born and I will give you a complet" + | |
"e account of the system, and expound the actual teachings of" + | |
" the great explorer of the truth, the master-builder of huma" + | |
"n happiness. No one rejects, dislikes, or avoids pleasure it" + | |
"self, because it is pleasure, but because those who do not k" + | |
"now how to pursue pleasure rationally encounter consequences" + | |
" that are extremely painful. Nor again is there anyone who l" + | |
"oves or pursues or desires to obtain pain of itself, because" + | |
" it is pain, but because occasionally circumstances occur in" + | |
" which toil and pain can procure him some great pleasure. To" + | |
" take a trivial example, which of us ever undertakes laborio" + | |
"us physical exercise, except to obtain some advantage from i" + | |
"t? But who has any right to find fault with a man who choose" + | |
"s to enjoy a pleasure that has no annoying consequences, or " + | |
"one who avoids a pain that produces no resultant pleasure? O" + | |
"n the other hand, we denounce with righteous indignation and" + | |
" dislike men who are so beguiled and demoralized by the char" + | |
"ms of pleasure of the moment, so blinded by desire, that the" + | |
"y cannot foresee the pain and trouble that are bound to ensu" + | |
"e; and equal blame belongs to those who fail in their duty t" + | |
"hrough weakness of will, which is the same as saying through" + | |
" shrinking from toil and pain. These cases are perfectly sim" + | |
"ple and easy to distinguish. In a free hour, when our power " + | |
"of choice is untrammelled and when nothing prevents our bein" + | |
"g able to do what we like best, every pleasure is to be welc" + | |
"omed and every pain avoided. But in certain circumstances an" + | |
"d owing to the claims of duty or the obligations of business" + | |
" it will frequently occur that pleasures have to be repudiat" + | |
"ed and annoyances accepted. The wise man therefore always ho" + | |
"lds in these matters to this principle of selection: he reje" + | |
"cts pleasures to secure other greater pleasures, or else he " + | |
"endures pains to avoid worse pains. But I must explain to yo" + | |
"u how all this mistaken idea of denouncing pleasure and prai" + | |
"sing pain was born and I will give you a complete account of" + | |
" the system, and expound the actual teachings of the great e" + | |
"xplorer of the truth, the master-builder of human happiness." + | |
" No one rejects, dislikes, or avoids pleasure itself, becaus" + | |
"e it is pleasure, but because those who do not know how to p" + | |
"ursue pleasure rationally encounter consequences that are ex" + | |
"tremely painful. Nor again is there anyone who loves or purs" + | |
"ues or desires to obtain pain of itself, because it is pain," + | |
" but because occasionally circumstances occur in which toil " + | |
"and pain can procure him some great pleasure. To take a triv" + | |
"ial example, which of us ever undertakes laborious physical " + | |
"exercise, except to obtain some advantage from it? But who h" + | |
"as any right to find fault with a man who chooses to enjoy a" + | |
" pleasure that has no annoying consequences, or one who avoi" + | |
"ds a pain that produces no resultant pleasure? On the other " + | |
"hand, we denounce with righteous indignation and dislike men" + | |
" who are so beguiled and demoralized by the charms of pleasu" + | |
"re of the moment, so blinded by desire, that they cannot for" + | |
"esee the pain and trouble that are bound to ensue; and equal" + | |
" blame belongs to those who fail in their duty through weakn" + | |
"ess of will, which is the same as saying through shrinking f" + | |
"rom toil and pain. These cases are perfectly simple and easy" + | |
" to distinguish. In a free hour, when our power of choice is" + | |
" untrammelled and when nothing prevents our being able to do" + | |
" what we like best, every pleasure is to be welcomed and eve" + | |
"ry pain avoided. But in certain circumstances and owing to t" + | |
"he claims of duty or the obligations of business it will fre" + | |
"quently occur that pleasures have to be repudiated and annoy" + | |
"ances accepted. The wise man therefore always holds in these" + | |
" matters to this principle of selection: he rejects pleasure" + | |
"s to secure other greater pleasures, or else he endures pain" + | |
"s to avoid worse pains. But I must explain to you how all th" + | |
"is mistaken idea of denouncing pleasure and praising pain wa" + | |
"s born and I will give you a complete account of the system," + | |
" and expound the actual teachings of the great explorer of t" + | |
"he truth, the master-builder of human happiness. No one reje" + | |
"cts, dislikes, or avoids pleasure itself, because it is plea" + | |
"sure, but because those who do not know how to pursue pleasu" + | |
"re rationally encounter consequences that are extremely pain" + | |
"ful. Nor again is there anyone who loves or pursues or desir" + | |
"es to obtain pain of itself, because it is pain, but because" + | |
" occasionally circumstances occur in which toil and pain can" + | |
" procure him some great pleasure. To take a trivial example," + | |
" which of us ever undertakes laborious physical exercise, ex" + | |
"cept to obtain some advantage from it? But who has any right" + | |
" to find fault with a man who chooses to enjoy a pleasure th" + | |
"at has no annoying consequences, or one who avoids a pain th" + | |
"at produces no resultant pleasure? On the other hand, we den" + | |
"ounce with righteous indignation and dislike men who are so " + | |
"beguiled and demoralized by the charms of pleasure of the mo" + | |
"ment, so blinded by desire, that they cannot foresee the pai" + | |
"n and trouble that are bound to ensue; and equal blame belon" + | |
"gs to those who fail in their duty through weakness of will," + | |
" which is the same as saying through shrinking from toil and" + | |
" pain. These cases are perfectly simple and easy to distingu" + | |
"ish. In a free hour, when our power of choice is untrammelle" + | |
"d and when nothing prevents our being able to do what we lik" + | |
"e best, every pleasure is to be welcomed and every pain avoi" + | |
"ded. But in certain circumstances and owing to the claims of" + | |
" duty or the obligations of business it will frequently occu" + | |
"r that pleasures have to be repudiated and annoyances accept" + | |
"ed. The wise man therefore always holds in these matters to " + | |
"this principle of selection: he rejects pleasures to secure " + | |
"other greater pleasures, or else he endures pains to avoid w" + | |
"orse pains. But I must explain to you how all this mistaken " + | |
"idea of denouncing pleasure and praising pain was born and I" + | |
" will give you a complete account of the system, and expound" + | |
" the actual teachings of the great explorer of the truth, th" + | |
"e master-builder of human happiness. No one rejects, dislike" + | |
"s, or avoids pleasure itself, because it is pleasure, but be" + | |
"cause those who do not know how to pursue pleasure rationall" + | |
"y encounter consequences that are extremely painful. Nor aga" + | |
"in is there anyone who loves or pursues or desires to obtain" + | |
" pain of itself, because it is pain, but because occasionall" + | |
"y circumstances occur in which toil and pain can procure him" + | |
" some great pleasure. To take a trivial example, which of us" + | |
" ever undertakes laborious physical exercise, except to obta" + | |
"in some advantage from it? But who has any right to find fau" + | |
"lt with a man who chooses to enjoy a pleasure that has no an" + | |
"noying consequences, or one who avoids a pain that produces " + | |
"no resultant pleasure? On the other hand, we denounce with r" + | |
"ighteous indignation and dislike men who are so beguiled and" + | |
" demoralized by the charms of pleasure of the moment, so bli" + | |
"nded by desire, that they cannot foresee the pain and troubl" + | |
"e that are bound to ensue; and equal blame belongs to those " + | |
"who fail in their duty through weakness of will, which is th" + | |
"e same as saying through shrinking from toil and pain. These" + | |
" cases are perfectly simple and easy to distinguish. In a fr" + | |
"ee hour, when our power of choice is untrammelled and when n" + | |
"othing prevents our being able to do what we like best, ever" + | |
"y pleasure is to be welcomed and every pain avoided. But in " + | |
"certain circumstances and owing to the claims of duty or the" + | |
" obligations of business it will frequently occur that pleas" + | |
"ures have to be repudiated and annoyances accepted. The wise" + | |
" man therefore always holds in these matters to this princip" + | |
"le of selection: he rejects pleasures to secure other greate" + | |
"r pleasures, or else he endures pains to avoid worse pains.B" + | |
"ut I must explain to you how all this mistaken idea of denou" + | |
"ncing pleasure and praising pain was born and I will give yo" + | |
"u a complete account of the system, and expound the actual t" + | |
"eachings of the great explorer of the truth, the master-buil" + | |
"der of human happiness. No one rejects, dislikes, or avoids " + | |
"pleasure itself, because it is pleasure, but because those w" + | |
"ho do not know how to pursue pleasure rationally encounter c" + | |
"onsequences that are extremely painful. Nor again is there a" + | |
"nyone who loves or pursues or desires to obtain pain of itse" + | |
"lf, because it is pain, but because occasionally circumstanc" + | |
"es occur in which toil and pain can procure him some great p" + | |
"leasure. To take a trivial example, which of us ever underta" + | |
"kes laborious physical exercise, except to obtain some advan" + | |
"tage from it? But who has any right to find fault with a man" + | |
" who chooses to enjoy a pleasure that has no annoying conseq" + | |
"uences, or one who avoids a pain that produces no resultant " + | |
"pleasure? On the other hand, we denounce with righteous indi" + | |
"gnation and dislike men who are so beguiled and demoralized " + | |
"by the charms of pleasure of the moment, so blinded by desir" + | |
"e, that they cannot foresee the pain and trouble that are bo" + | |
"und to ensue; and equal blame belongs to those who fail in t" + | |
"heir duty through weakness of will, which is the same as say" + | |
"ing through shrinking from toil and pain. These cases are pe" + | |
"rfectly simple and easy to distinguish. In a free hour, when" + | |
" our power of choice is untrammelled and when nothing preven" + | |
"ts our being able to do what we like best, every pleasure is" + | |
" to be welcomed and every pain avoided. But in certain circu" + | |
"mstances and owing to the claims of duty or the obligations " + | |
"of business it will frequently occur that pleasures have to " + | |
"be repudiated"; | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment