/* * Retrieves a response. */ public cnpResponse ReceiveFromCnp(string batchFileName) { var useEncryption = config.GetValue("useEncryption"); var pgpPassphrase = config.GetValue("pgpPassphrase"); cnpFile.CreateDirectory(this.responseDirectory); var responseFilePath = Path.Combine(this.responseDirectory, batchFileName); var batchResponseDir = this.responseDirectory; var finalResponseFilePath = responseFilePath; // Decrypt the file. if ("true".Equals(useEncryption)) { batchResponseDir = Path.Combine(responseDirectory, "encrypted"); finalResponseFilePath = Path.Combine(batchResponseDir, batchFileName); cnpFile.CreateDirectory(finalResponseFilePath); } communication.FtpPickUp(finalResponseFilePath, config, batchFileName); if ("true".Equals(useEncryption)) { responseFilePath = responseFilePath.Replace(".encrypted", ""); PgpHelper.DecryptFile(finalResponseFilePath, responseFilePath, pgpPassphrase); } // Deserialize and return the response. var cnpResponse = VersionedXMLDeserializer.Deserialize <cnpResponse>(File.ReadAllText(responseFilePath), this.config.GetVersion()); return(cnpResponse); }
public void DeserializeNullChildElements() { // Deserialize an object. var xmlObject = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement TestAttribute1= \"1\" TestAttribute2 ='Test \"1\"' TestAttribute3= \"true\"><TestElement1>2</TestElement1> <TestElement3>false</TestElement3><TestElement4/></TestXMLElement>", new XMLVersion()); // Assert the element was deserialized correctly. Assert.AreEqual(xmlObject.TestAttribute1, 1, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute2, "Test \"1\"", "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute3, true, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement1, 2, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement2, null, "Element was initialized (should be null)."); Assert.AreEqual(xmlObject.TestElement3, false, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement4, null, "Element was initialized (should be null)."); // Deserialize an object with attributes. xmlObject = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement TestAttribute1= \"1\" TestAttribute2 ='Test \"1\"' TestAttribute3= \"true\"><TestElement1>2</TestElement1> <TestElement3>false</TestElement3><TestElement4 TestAttribute3=\"true\"></TestElement4></TestXMLElement>", new XMLVersion()); // Assert the element was deserialized correctly. Assert.AreEqual(xmlObject.TestAttribute1, 1, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute2, "Test \"1\"", "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute3, true, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement1, 2, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement2, null, "Element was initialized (should be null)."); Assert.AreEqual(xmlObject.TestElement3, false, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement4.TestAttribute3, true, "Element wasn't deserialized correctly."); }
public void TestDeserializeUnknownElements() { // Deserialize two objects. var xmlObject1 = VersionedXMLDeserializer.Deserialize <TestXMLElementUnknownElements>("<TestXMLElement></TestXMLElement>", new XMLVersion()); var xmlObject2 = VersionedXMLDeserializer.Deserialize <TestXMLElementUnknownElements>("<TestXMLElement><Element1>Test1</Element1><Element2>Test1</Element2></TestXMLElement>", new XMLVersion()); // Assert the attributes are set correctly. Assert.AreEqual(xmlObject1.TestAttribute, ""); Assert.AreEqual(xmlObject2.TestAttribute, "<Element1>Test1</Element1><Element2>Test1</Element2>"); }
public void TestDeserializeSwitchingType() { // Deserialize 2 objects. var xmlObject1 = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement switchingItem=\"Test\"></TestXMLElement>", new XMLVersion(1, 0)); var xmlObject2 = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement><switchingItem>Test</switchingItem></TestXMLElement>", new XMLVersion(2, 0)); // Assert the elements were deserialized correctly. Assert.AreEqual(xmlObject1.switchingItem, "Test", "Item wasn't deserialized correctly."); Assert.AreEqual(xmlObject2.switchingItem, "Test", "Item wasn't deserialized correctly."); }
public void TestDeserializeNoElements() { // Deserialize an object. var xmlObject = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement TestAttribute1= \"1\" TestAttribute2 ='Test \"1\"' TestAttribute3= \"true\"/> ", new XMLVersion()); // Assert the element was deserialized correctly. Assert.AreEqual(xmlObject.TestAttribute1, 1, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute2, "Test \"1\"", "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute3, true, "Attribute wasn't deserialized correctly."); }
public void TestDeserializeNoAttributes() { // Deserialize an object. var xmlObject = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement><CustomElement1>2</CustomElement1> <CustomElement2>false</CustomElement2><CustomElement3>Test 2</CustomElement3></TestXMLElement> ", new XMLVersion(1, 0)); // Assert the element was deserialized correctly. Assert.AreEqual(xmlObject.TestElement1, 2, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement2, "Test 2", "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement3, false, "Element wasn't deserialized correctly."); }
public void TestDeserializeChildXML() { // Deserialize an object. var xmlObject = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement><TestElement4 TestAttribute1= \"1\" TestAttribute2 ='Test \"1\"' TestAttribute3= \"true\"><TestElement1>2</TestElement1> <TestElement3>false</TestElement3></TestElement4></TestXMLElement>", new XMLVersion()); // Assert the element was deserialized correctly. Assert.AreEqual(xmlObject.TestElement4.TestAttribute1, 1, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement4.TestAttribute2, "Test \"1\"", "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement4.TestAttribute3, true, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement4.TestElement1, 2, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement4.TestElement3, false, "Element wasn't deserialized correctly."); }
public void TestDeserializeWithCustomNames() { // Deserialize an object. var xmlObject = VersionedXMLDeserializer.Deserialize <TestXMLElement>("<TestXMLElement CustomAttribute1= \"1\" CustomAttribute2 ='Test \"1\"' CustomAttribute3= \"true\"><CustomElement1>2</CustomElement1> <CustomElement2>false</CustomElement2><CustomElement3>Test 2</CustomElement3></TestXMLElement> ", new XMLVersion(1, 0)); // Assert the element was deserialized correctly. Assert.AreEqual(xmlObject.TestAttribute1, 1, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute2, "Test \"1\"", "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestAttribute3, true, "Attribute wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement1, 2, "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement2, "Test 2", "Element wasn't deserialized correctly."); Assert.AreEqual(xmlObject.TestElement3, false, "Element wasn't deserialized correctly."); }
public void TestDeserializeToList() { // Deserialize two objects. var xmlObject1 = VersionedXMLDeserializer.Deserialize <TestXMLElementListElements>("<TestXMLElement><testElement>String1</testElement></TestXMLElement>", new XMLVersion()); var xmlObject2 = VersionedXMLDeserializer.Deserialize <TestXMLElementListElements>("<TestXMLElement><testElement>String1</testElement><testElement>String2</testElement><testElement>String3</testElement></TestXMLElement>", new XMLVersion()); // Assert the attributes are set correctly. Assert.AreEqual(xmlObject1.TestElement, new List <string>() { "String1" }); Assert.AreEqual(xmlObject2.TestElement, new List <string>() { "String1", "String2", "String3" }); }
public void TestDeserializeEnums() { // Deserialize two objects. var xmlObject1 = VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value1\"><testElement>Value1</testElement></TestXMLElement>", new XMLVersion()); var xmlObject2 = VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"TestValue\"><testElement>TestValue</testElement></TestXMLElement>", new XMLVersion()); var xmlObject3 = VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value3\"><testElement>Value3</testElement></TestXMLElement>", new XMLVersion()); var xmlObject4 = VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value3\"><testElement>Value3</testElement></TestXMLElement>", new XMLVersion(3, 0)); var xmlObject5 = VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value6\"><testElement>Value6</testElement></TestXMLElement>", new XMLVersion()); var xmlObject6 = VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value7\"><testElement>Value7</testElement></TestXMLElement>", new XMLVersion(3, 0)); // Assert the attributes are set correctly. Assert.AreEqual(xmlObject1.TestAttribute, TestEnum.Value1); Assert.AreEqual(xmlObject1.TestElement, TestEnum.Value1); Assert.AreEqual(xmlObject2.TestAttribute, TestEnum.Value2); Assert.AreEqual(xmlObject2.TestElement, TestEnum.Value2); Assert.AreEqual(xmlObject3.TestAttribute, TestEnum.Value3); Assert.AreEqual(xmlObject3.TestElement, TestEnum.Value3); Assert.AreEqual(xmlObject4.TestAttribute, TestEnum.Value4); Assert.AreEqual(xmlObject4.TestElement, TestEnum.Value4); Assert.AreEqual(xmlObject5.TestAttribute, TestEnum.Value5); Assert.AreEqual(xmlObject5.TestElement, TestEnum.Value5); Assert.AreEqual(xmlObject6.TestAttribute, TestEnum.Value5); Assert.AreEqual(xmlObject6.TestElement, TestEnum.Value5); // Assert that exceptions are thrown for invalid versions. Assert.Catch <InvalidVersionException>(() => { VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value6\"><testElement>Value3</testElement></TestXMLElement>", new XMLVersion(-1, 0)); }); Assert.Catch <InvalidVersionException>(() => { VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value3\"><testElement>Value6</testElement></TestXMLElement>", new XMLVersion(-1, 0)); }); // Assert that exceptions are thrown for unusable enums. Assert.Catch <InvalidVersionException>(() => { VersionedXMLDeserializer.Deserialize <XMLWithEnums>("<TestXMLElement testAttribute=\"Value5\"><testElement>Value5</testElement></TestXMLElement>", new XMLVersion()); }); }
/* * Parses a CNP response. */ private cnpOnlineResponse DeserializeResponse(string xmlResponse) { try { // Deserialize the response and output the response. var cnpOnlineResponse = VersionedXMLDeserializer.Deserialize <cnpOnlineResponse>(xmlResponse, this.config.GetVersion()); if (Convert.ToBoolean(this.config.GetValue("printxml"))) { Console.WriteLine(cnpOnlineResponse.response); } // If the response isn't "0" (success), throw an exception if an error is valid. if (!"0".Equals(cnpOnlineResponse.response)) { if ("2".Equals(cnpOnlineResponse.response) || "3".Equals(cnpOnlineResponse.response)) { throw new CnpInvalidCredentialException(cnpOnlineResponse.message); } else if ("4".Equals(cnpOnlineResponse.response)) { throw new CnpConnectionLimitExceededException(cnpOnlineResponse.message); } else if ("5".Equals(cnpOnlineResponse.response)) { throw new CnpObjectionableContentException(cnpOnlineResponse.message); } else { throw new CnpOnlineException(cnpOnlineResponse.message); } } return(cnpOnlineResponse); } catch (InvalidOperationException invalidOperationException) { throw new CnpOnlineException("Error validating xml data against the schema", invalidOperationException); } }
/* * Returns the response object. */ public T GetOnlineResponse <T>() { return(VersionedXMLDeserializer.Deserialize <T>(this.onlineResponse, this.parsedVersion)); }