using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Realisable.Data.Interop45; using Realisable.Data.Transform; using Realisable.JobDefinition; using Realisable.Resources; using Realisable.Utils; using Realisable.Utils.DTO; using Realisable.Utils.OM; namespace Realisable.Connectors.Shopify.Test { /// /// Provides an abstract class for instantiating and processing connector datasets. /// Every test should inherit from this class. /// [TestClass] public abstract class ConnectorTest { /// /// The id of the Job Definition created as part of the unit tests. /// public const string JOBDEF_ID = "SHOPIFYTEST"; /// /// The name of the Job Definition created as part of the unit tests. /// public const string JOBDEF_NAME = "ConnectorTest"; /// /// A number used to signify the instance of the test. Leave as default as it a placeholder used in a live scenario. /// public const int INSTANCE_ID = 9977; protected JobDefinition.JobDefinition _jobDef; [TestInitialize] public void TestInitialise() { //Create the JobDefinition object so it's available to all. _jobDef = CreateJobDef(); //Setup the NHibernate session so data can be from the IMan database. SessionBuilderFactory.GetBuilder(SessionBuilderType.ThreadStatic).OpenSession(); } [TestCleanup] public void TestClose() { SessionBuilderFactory.GetBuilder(SessionBuilderType.ThreadStatic).CloseSession(); } /// /// Creates a JobDefinition so the Connector's transformDefiniton can be added. /// /// public JobDefinition.JobDefinition CreateJobDef() { var result = new JobDefinition.JobDefinition(); result.JobName = JOBDEF_NAME; return result; } /// /// Creates a TransformDefinition object containing the metadata for importing Customer for the Sample connector. /// /// protected TransformDefinition CreatePushStyleTransformDefTransposeImport(string importType) { // Create the resultant object. TransformDefinition result = CreatePushStyleTransformDef(TestConstants.CONNECTOR_SYSTEM, importType, Resources.eERPUpdateOperation.eUpdateInsert); //Need to do this through the MetaExtender otherwise it's not possible to debug the code (due to the same assembly being loaded from different paths). var metaExtender = ConnectorFactory.GetExtender(TestConstants.ASSEMBLY_NAME, false); metaExtender.ConnectorSettings = new SystemConnectorDTO(TestConstants.CONNECTOR_SYSTEM); SystemImport systemImport = metaExtender.MetaDataDocument[importType]; result.TransposeSystemImport(systemImport, TranspositionOptions.CreateMissingRecords | TranspositionOptions.CreateMissingFields); return result; } /// /// Creates a TransformDefinition object for a Push Style Connector. /// /// The id for the System Connector used to retrieve the connection properties. /// The import type. /// The update operation. /// protected TransformDefinition CreatePushStyleTransformDef(string systemConnectorId, string impType, eERPUpdateOperation updateOp) { TransformDefinition result = _jobDef.CreateTransform(); ITransformSetup tfmSetup = new CustomConnectorTransformSetup(); tfmSetup.InitialiseTransform(result); //Set the options for the connector. result.Options.SetProperty(GlobalResources.SYS_CONNECT_TRAN_TYPE_OPT, impType, false); result.Options.SetProperty(GlobalResources.SYS_CONNECT_SYSID_OPT, systemConnectorId, false); result.Options.SetProperty(GlobalResources.SYS_CONNECT_UPDATE_OP_OPT, Convert.ToInt32(updateOp).ToString(), false); result.Options.SetProperty(GlobalResources.CUS_CONNECT_ASSEMBLY_NAME_OPT, TestConstants.ASSEMBLY_NAME, false); result.AuditDefinition.ErrorAction = eTransformErrorAction.eTransformErrorActionAbort; return result; } /// /// Creates a TransformDefinition object for a Pull Style connector. /// /// The id for the System Connector used to retrieve the connection properties. /// The type of data being retrieved. /// The constructed TransformDefinition object. protected TransformDefinition CreatePullStyleTransformDef(string systemConnectorId, string importDataType) { TransformDefinition result = _jobDef.CreateTransform(); ITransformSetup tfmSetup = new CustomReaderSetup(); tfmSetup.InitialiseTransform(result); // Set the options for the Reader definition. result.Options.SetProperty(GlobalResources.SYS_CONNECT_TRAN_TYPE_OPT, importDataType, false); result.Options.SetProperty(GlobalResources.SYS_CONNECT_SYSID_OPT, systemConnectorId, false); result.Options.SetProperty(GlobalResources.CUS_CONNECT_ASSEMBLY_NAME_OPT, TestConstants.ASSEMBLY_NAME, false); result.AuditDefinition.ErrorAction = eTransformErrorAction.eTransformErrorActionAbort; return result; } protected TransformDefinition CreatePullStyleTransformDef(string systemConnectorId, string importDataType, bool transposeImport) { TransformDefinition result = _jobDef.CreateTransform(null, RandomUtils.RandomString(5, true), TransformTypes.eRead, (int)eInputReaderType.eCustomReader); ITransformSetup tfmSetup = new CustomReaderSetup(); tfmSetup.InitialiseTransform(result); // Set the options for the Reader definition. result.Options.SetProperty(GlobalResources.SYS_CONNECT_TRAN_TYPE_OPT, importDataType, false); result.Options.SetProperty(GlobalResources.SYS_CONNECT_SYSID_OPT, systemConnectorId, false); result.Options.SetProperty(GlobalResources.CUS_CONNECT_ASSEMBLY_NAME_OPT, TestConstants.ASSEMBLY_NAME, false); result.AuditDefinition.ErrorAction = eTransformErrorAction.eTransformErrorActionAbort; if (transposeImport) { //Need to do this through the MetaExtender otherwise it's not possible to debug the code (due to the same assembly being loaded from different paths). var metaExtender = ConnectorFactory.GetExtender(TestConstants.ASSEMBLY_NAME, true); metaExtender.ConnectorSettings = new SystemConnectorDTO(TestConstants.CONNECTOR_SYSTEM); SystemImport systemImport = metaExtender.MetaDataDocument[importDataType]; result.TransposeSystemImport(systemImport, TranspositionOptions.RecordAttributes | TranspositionOptions.CreateMissingRecords | TranspositionOptions.CreateMissingFields); } return result; } /// /// Processes a Push Style dataset by instantiating the ITransformInterop object and making the appropriate calls. /// /// The transaction factory containing the dataset. protected void ProcessTransactions(TransactionFactory transactionFactory) { TransformDefinition tfmDef = transactionFactory.TransformDefinition; //Obtain the system connector. SystemConnectorDTO connectorDTO = new SystemConnectorDTO(tfmDef.Options.GetProperty(GlobalResources.SYS_CONNECT_SYSID_OPT)); //Get the updateOperation. eERPUpdateOperation updateOperation = tfmDef.Options.GetProperty(GlobalResources.SYS_CONNECT_UPDATE_OP_OPT); //Create the ITransformProcess object implemented by the connector. PushDataInterop import = new PushDataInterop(); //Now initialise. import.Initialise(tfmDef, transactionFactory.CreateTransactionIterator(), false, tfmDef.AuditDefinition.ErrorAction, updateOperation, connectorDTO, new AuditProxy()); //Run the actual import. import.Process(); } /// /// Process a Push Style dataset where the data contains a known error. Used to test exceptional test cases. /// /// The transaction factory containing the dataset. /// The type of exception expected. protected void ProcessExceptionalTransactions(TransactionFactory transactionFactory) where T : Exception { //Run the actual import using AssetExternsions.Throws method. AssertExtensions.Throws(() => ProcessTransactions(transactionFactory)); } } }