Exemple #1
0
        public void TestConstructors()
        {
            // Test the empty constructor.
            var version = new XMLVersion();

            Assert.AreEqual(version.MainVersion, 0);
            Assert.AreEqual(version.SubVersion, 0);
            Assert.AreEqual(version.ToString(), "0.0");

            // Test the filled constructor.
            version = new XMLVersion(1, 2);
            Assert.AreEqual(version.MainVersion, 1);
            Assert.AreEqual(version.SubVersion, 2);
            Assert.AreEqual(version.ToString(), "1.2");

            // Test the string constructors.
            version = XMLVersion.FromString(null);
            Assert.IsNull(version);
            version = XMLVersion.FromString("12");
            Assert.AreEqual(version.MainVersion, 12);
            Assert.AreEqual(version.SubVersion, 0);
            Assert.AreEqual(version.ToString(), "12.0");
            version = XMLVersion.FromString("12.13");
            Assert.AreEqual(version.MainVersion, 12);
            Assert.AreEqual(version.SubVersion, 13);
            Assert.AreEqual(version.ToString(), "12.13");
        }
        /*
         * Validates an exception is correct.
         */
        public void ValidateException(Exception exception, XMLVersion version)
        {
            // Find the expected exception and throw an error if the type is incorrect.
            var exceptionExpected = false;

            foreach (var range in this.expectedExceptions.Keys)
            {
                if (range.IsVersionValid(version))
                {
                    var actualExceptionType   = exception.GetType();
                    var expectedExceptionType = this.expectedExceptions[range];
                    if (VersionedXMLDeserializer.IsOfType(actualExceptionType, expectedExceptionType))
                    {
                        exceptionExpected = true;
                    }
                    else
                    {
                        Assert.Fail("Exception does not match for " + version + ".\n\tExpected exception: " + expectedExceptionType.Name + "\n\tActual exception: " + actualExceptionType.Name + "\n\n" + exception);
                    }
                }
            }

            // Fail the test for an unexpected exception.
            if (!exceptionExpected)
            {
                Assert.Fail("Unexpected exception for " + version + ":\n" + exception);
            }
        }
 /*
  * Parses elements that aren't defined by properties.
  */
 public override void ParseAdditionalElements(XMLVersion version, List <string> elements)
 {
     this.parsedVersion = version;
     if (elements.Count != 0)
     {
         this.onlineResponse = elements[0];
     }
 }
 /*
  * Returns additional elements to add when serializing.
  * This method must handle all escaping of special characters.
  */
 public override List <string> GetAdditionalElements(XMLVersion version)
 {
     return(new List <string>
     {
         "<testElement>Test 3</testElement>",
         "<testElement>Test 4</testElement>",
     });
 }
Exemple #5
0
        public void TestDefaultConstructor()
        {
            // Create the config.
            var config = new ConfigManager();

            // Assert some values are correct.
            Assert.AreEqual(config.GetValue("url"), Properties.Settings.Default.url);
            Assert.AreEqual(config.GetValue("timeout"), Properties.Settings.Default.timeout);
            Assert.AreEqual(config.GetValue("responseDirectory"), Properties.Settings.Default.responseDirectory);
            Assert.AreEqual(config.GetVersion(), XMLVersion.FromString(CnpVersion.CurrentCNPXMLVersion));
        }
Exemple #6
0
        /*
         * Returns the XML version to use.
         */
        public XMLVersion GetVersion()
        {
            // Return the default if the version is not set.
            if (config["version"] == "")
            {
                Console.WriteLine("Version isn't defined in the configuration. It is recommended " +
                                  "to set it so the SDK version can be upgraded without needing " +
                                  "to change your XML version. Assuming XML version " + CnpVersion.CurrentCNPXMLVersion);

                return(XMLVersion.FromString(CnpVersion.CurrentCNPXMLVersion));
            }

            // Return the version from the string.
            return(XMLVersion.FromString(config["version"]));
        }
        /*
         * Validates a response object.
         */
        private void ValidateResponse <T>(object response, XMLVersion version)
        {
            // Go through the properties.
            var responseType = typeof(T);

            foreach (var member in responseType.GetMembers())
            {
                var property = responseType.GetProperty(member.Name);
                if (property != null)
                {
                    var name  = member.Name;
                    var value = property.GetValue(response, null);

                    // Validate the property.
                    foreach (var expectedPopulatedRange in this.expectedPopulated.Keys)
                    {
                        if (expectedPopulatedRange.IsVersionValid(version) && expectedPopulatedRange.Name == name)
                        {
                            var valueExpectedPopulated = this.expectedPopulated[expectedPopulatedRange];
                            var valueDefined           = (value != null);
                            if (valueExpectedPopulated && valueDefined)
                            {
                                if (this.expectedPopulatedObjects.ContainsKey(expectedPopulatedRange))
                                {
                                    var expectedValue = this.expectedPopulatedObjects[expectedPopulatedRange];
                                    if (!expectedValue.Equals(value))
                                    {
                                        Assert.Fail(name + " (" + version + ") is expected to be populated with:\n" + expectedValue + "\n\nbut is populated with:\n" + value);
                                    }
                                }
                            }
                            else if (valueExpectedPopulated)
                            {
                                Assert.Fail(name + " is expected to be populated in " + responseType.Name + " (" + version + ") but isn't.");
                            }
                            else if (valueDefined)
                            {
                                Assert.Fail(name + " is expected to be unpopulated in " + responseType.Name + " (" + version + ") but is with:\n" + value);
                            }
                        }
                    }
                }
            }
        }
        /*
         * Runs a test transaction for a specific version.
         */
        public void RunCnpTest <T>(cnpTransactionInterface transaction, XMLVersion version)
        {
            // Create the configuration.
            var config = new ConfigManager(new Dictionary <string, string>
            {
                { "reportGroup", "Default Report Group" },
                { "username", "DOTNET" },
                { "timeout", "15000" },
                { "merchantId", "101" },
                { "password", "TESTCASE" },
                { "printxml", "true" },
                { "neuterAccountNums", "true" },
                { "version", version.ToString() }
            });

            // Create a CNP Online object.
            var cnpOnline = new CnpOnline(config);

            // Perform the sync transaction, and validate the response.
            try
            {
                var transactionResponse = cnpOnline.SendTransaction <T>(transaction);
                this.ValidateResponse <T>(transactionResponse, version);
            }
            catch (Exception exception)
            {
                this.ValidateException(exception, version);
            }


            // Perform the async transaction, and validate the response.
            try
            {
                var transactionResponse = cnpOnline.SendTransactionAsync <T>(transaction, new CancellationToken()).Result;
                this.ValidateResponse <T>(transactionResponse, version);
            }
            catch (AggregateException exception)
            {
                this.ValidateException(exception.InnerException, version);
            }
        }
        /*
         * Returns a list of all of the versions to run.
         */
        public List <XMLVersion> GetVersionsToRun()
        {
            // Create the list.
            var versions = new List <XMLVersion>();

            // Add the versions.
            foreach (var maxMinorVersion in MAX_MINOR_VERSIONS)
            {
                for (var i = 0; i <= maxMinorVersion.SubVersion; i++)
                {
                    var version = new XMLVersion(maxMinorVersion.MainVersion, i);
                    if (version >= this.StartingVersion && version <= this.EndingVersion && !EXCLUDED_VERSIONS.Contains(version))
                    {
                        versions.Add(version);
                    }
                }
            }

            // Return the list.
            return(versions);
        }
 /*
  * Performs a test for a specific version.
  */
 public async Task PerformTest(XMLVersion version)
 {
     Console.WriteLine("Running transaction test for " + version);
     await Task.Run(() => test.RunCnpTest <T>(this.request, version));
 }
Exemple #11
0
        public void TestComparisons()
        {
            // Create 3 versions.
            var version1 = new XMLVersion(1, 2);
            var version2 = new XMLVersion(1, 5);
            var version3 = new XMLVersion(2, 2);

            // Assert that comparing equals is correct.
            Assert.AreEqual(version1, new XMLVersion(1, 2));
            Assert.AreEqual(version1, version1);
            Assert.AreNotEqual(version1, version2);
            Assert.AreNotEqual(version1, version3);
            Assert.AreNotEqual(version1, "1.2");

            // Assert the comparisons are correct.
            Assert.IsTrue(version1 == new XMLVersion(1, 2));
            Assert.IsTrue(version1 != version2);
            Assert.IsTrue(version1 != version3);
            Assert.IsTrue(version2 == new XMLVersion(1, 5));
            Assert.IsTrue(version2 != version1);
            Assert.IsTrue(version2 != version3);
            Assert.IsTrue(version3 == new XMLVersion(2, 2));
            Assert.IsTrue(version3 != version1);
            Assert.IsTrue(version3 != version2);

            Assert.IsFalse(version1 > new XMLVersion(1, 2));
            Assert.IsFalse(version1 > version2);
            Assert.IsFalse(version1 > version3);
            Assert.IsFalse(version2 > new XMLVersion(1, 5));
            Assert.IsTrue(version2 > version1);
            Assert.IsFalse(version2 > version3);
            Assert.IsFalse(version3 > new XMLVersion(2, 2));
            Assert.IsTrue(version3 > version1);
            Assert.IsTrue(version3 > version2);

            Assert.IsFalse(version1 < new XMLVersion(1, 2));
            Assert.IsTrue(version1 < version2);
            Assert.IsTrue(version1 < version3);
            Assert.IsFalse(version2 < new XMLVersion(1, 5));
            Assert.IsFalse(version2 < version1);
            Assert.IsTrue(version2 < version3);
            Assert.IsFalse(version3 < new XMLVersion(2, 2));
            Assert.IsFalse(version3 < version1);
            Assert.IsFalse(version3 < version2);

            Assert.IsTrue(version1 >= new XMLVersion(1, 2));
            Assert.IsFalse(version1 >= version2);
            Assert.IsFalse(version1 >= version3);
            Assert.IsTrue(version2 >= new XMLVersion(1, 5));
            Assert.IsTrue(version2 >= version1);
            Assert.IsFalse(version2 >= version3);
            Assert.IsTrue(version3 >= new XMLVersion(2, 2));
            Assert.IsTrue(version3 >= version1);
            Assert.IsTrue(version3 >= version2);

            Assert.IsTrue(version1 <= new XMLVersion(1, 2));
            Assert.IsTrue(version1 <= version2);
            Assert.IsTrue(version1 <= version3);
            Assert.IsTrue(version2 <= new XMLVersion(1, 5));
            Assert.IsFalse(version2 <= version1);
            Assert.IsTrue(version2 <= version3);
            Assert.IsTrue(version3 <= new XMLVersion(2, 2));
            Assert.IsFalse(version3 <= version1);
            Assert.IsFalse(version3 <= version2);
        }
 /*
  * Invoked before serializing the object to finalize
  * setting of elements.
  */
 public override void PreSerialize(XMLVersion version)
 {
     this.TestAttribute = version;
     this.TestElement   = version;
 }
Exemple #13
0
 /*
  * Parses elements that aren't defined by properties.
  */
 public override void ParseAdditionalElements(XMLVersion version, List <string> elements)
 {
     this.TestAttribute = string.Concat(elements.ToArray());
 }