Monday, August 15, 2016

Writing a unit test case for a simple class in Ax 7

This is to show how to write a unit test case in Ax 7 for a simple class.

I have created a class to do simple arithmetic calculation called “MyCalculator” and created “MyCalculatorUnitTest” unit test class. In this test class shown three ways of adding the test methods.
1)      Adding [SysTestMethodAttribute] attribute 
2)      Prefix the case with “test” in the method name
3)      Adding [SysTestMethod, SysTestGranularityAttribute(SysTestGranularity::Unit)] attribute.

After creating test cases it can be viewed in Test Explorer (Test > Windows> Test Explorer) as shown below.



Main Class:
class MyCalculator
{
    real    valueA,valueB;

    public void setValueA(real _a)
    {
        valueA = _a;
    }

    public void setValueB(real _b)
    {
        valueB = _b;
    }

    public real sumOfValues(real _a = valueA,real _b = valueB)
    {
        return _a + _b;
    }

    public real differenceOfValues(real _a = valueA,real _b = valueB)
    {
        return _a - _b;
    }

    public real muliplicationOfValues(real _a = valueA,real _b = valueB)
    {
        return _a * _b;
    }

    public real divisionOfValues(real _a = valueA,real _b = valueB)
    {
        if (_b == 0)
        {
            return 0;
        }
        return _a / _b;
    }

    public real modulusOfValues(real _a = valueA,real _b = valueB)
    {
        if (_b == 0)
        {
            return 0;
        }
        return _a mod _b;
    } 
}

Unit Test Class:
class MyCalculatorUnitTest extends SysTestCase
{
    [SysTestMethodAttribute]
    public void summationTest()
    {
        MyCalculator calc = new MyCalculator();

        calc.setValueA(10);
        calc.setValueB(4);
        this.assertEquals(14, calc.sumOfValues());
        this.assertEquals(9, calc.sumOfValues(6,3));

    }

    [SysTestMethod, SysTestGranularityAttribute(SysTestGranularity::Unit)]
    public void subtractionTest()
    {
        MyCalculator calc = new MyCalculator();

        calc.setValueA(10);
        calc.setValueB(4);
        this.assertEquals(6, calc.differenceOfValues());
        this.assertEquals(3, calc.differenceOfValues(6,3));

    }

    public void testMuliplicationOfValues()
    {
        MyCalculator calc = new MyCalculator();

        calc.setValueA(10);
        calc.setValueB(4);
        this.assertEquals(40, calc.muliplicationOfValues());
        this.assertEquals(18, calc.muliplicationOfValues(6,3));

    }

    [SysTestMethod, SysTestGranularityAttribute(SysTestGranularity::Unit)]
    public void divisionTest()
    {
        MyCalculator calc = new MyCalculator();

        calc.setValueA(10);
        calc.setValueB(4);
        this.assertEquals(2.5, calc.divisionOfValues());
        this.assertEquals(2, calc.divisionOfValues(6,3));

    }

    public void testModulusOfValues()
    {
        MyCalculator calc = new MyCalculator();

        calc.setValueA(10);
        calc.setValueB(4);
        this.assertEquals(2, calc.modulusOfValues());
        this.assertEquals(0, calc.modulusOfValues(6,3));

    }
}


You can find ax wiki on unit test here.

Thursday, August 11, 2016

Usage of File::SendFileToTempStore method in Ax7

In Previous post, I have shown converting document directly to stream and using same. In this, after converting, saving that stream to temporary storage and then from there downloading data and using same. This to show how to use "File::SendFileToTempStore" method.

class Jobs
{
    public static void main(Args _args)
    {
        str                     imageToSave = "C:\\invoice.jpg";
        PurchTable              purchaseTable;
        System.Net.WebClient    webClient;
 System.IO.MemoryStream  memstream;
        System.IO.Stream        stream;

        if (imageToSave && System.IO.File::Exists(imageToSave))
        {
            using (System.IO.FileStream fileStream = new System.IO.FileStream(imageToSave, System.IO.FileMode::Open, System.IO.FileAccess::Read))
            {
                stream = fileStream;
                stream.Seek(0, System.IO.SeekOrigin::Begin);
                str downloadUrl = File::SendFileToTempStore(stream, imageToSave);
                //br.navigate(downloadUrl);

                InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);
                perm.assert();
               
                // BP Deviation Documented
                webClient = new System.Net.WebClient();
                // BP Deviation Documented
                memstream = new System.IO.MemoryStream(webClient.DownloadData(downloadUrl));

  select purchaseTable where purchaseTable.PurchId == "000006"; // select record to which you want to attach
                DocuRef docuref = DocumentManagement::attachFile(purchaseTable.TableId, purchaseTable.RecId, purchaseTable.DataAreaId,DocuType::typeFile(),memstream, System.IO.Path::GetFileName(imageToSave), System.Web.MimeMapping::GetMimeMapping(imageToSave), System.IO.Path::GetFileName(imageToSave));
                // download/view the attachment
                str displayUrl = DocumentManagement::getAttachmentPublicUrl(docuref);
                Browser br = new Browser();
                br.navigate(displayUrl);
                CodeAccessPermission::revertAssert();
            }
        }
    }

}

Attaching a document to a purchase order using x++ job (Runnable class) in Ax7

In previous post, the file attachment is done with the file selected by User from UI. Instead, if the file path is known, can directly pass same in x++ to attach to PurchTable record like below.

class Jobs
{
    public static void main(Args _args)
    {
        str                     imageToSave = "C:\\invoice.jpg";
        PurchTable              purchaseTable;
        System.Net.WebClient    webClient;
        System.IO.Stream        stream;

        if (imageToSave && System.IO.File::Exists(imageToSave))
        {
            using (System.IO.FileStream fileStream = new System.IO.FileStream(imageToSave, System.IO.FileMode::Open, System.IO.FileAccess::Read))
            {
                stream = fileStream;
                stream.Seek(0, System.IO.SeekOrigin::Begin);
               
  select purchaseTable where purchaseTable.PurchId == "000006"; // select record to which you want to attach
                DocuRef docuref = DocumentManagement::attachFile(purchaseTable.TableId, purchaseTable.RecId, purchaseTable.DataAreaId,DocuType::typeFile(),stream, System.IO.Path::GetFileName(imageToSave), System.Web.MimeMapping::GetMimeMapping(imageToSave), System.IO.Path::GetFileName(imageToSave));
                // download/view the attachment
                str displayUrl = DocumentManagement::getAttachmentPublicUrl(docuref);
                Browser br = new Browser();
                br.navigate(displayUrl);
            }
        }
    }

}

Attaching a user selected document to a purchase order using x++ job (Runnable class) in Ax7

While working in document management in Ax 7, For testing purposes I wanted to attach documents to some posted tables and written a simple program to get files from user and attach to the record. Modified same to show how it work with PurchTable.

class Jobs
{
    public static void main(Args _args)
    {
       PurchTable              purchaseTable;
       System.Net.WebClient    webClient;
       System.IO.MemoryStream  stream;

FileUploadTemporaryStorageResult fileUploadResult = File::GetFileFromUser (classstr(ImageFileUploadTemporaryStorageStrategy));
       
        if (fileUploadResult && fileUploadResult.getUploadStatus())
        {
            str imageFilePathName = fileUploadResult.getDownloadUrl();

            InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);
            perm.assert();
            
            // BP Deviation Documented
            webClient = new System.Net.WebClient();
            // BP Deviation Documented
            stream = new System.IO.MemoryStream(webClient.DownloadData(imageFilePathName));
           
select purchaseTable where purchaseTable.PurchId == "000006"; // select record to which you want to attach
DocuRef docuref = DocumentManagement::attachFile(purchaseTable.TableId,purchaseTable.RecId, purchaseTable.DataAreaId,DocuType::typeFile(),stream,fileUploadResult.getFileName(), fileUploadResult.getFileContentType(),fileUploadResult.getFileName());
            // download/view the attachment
            str displayUrl = DocumentManagement::getAttachmentPublicUrl(docuref);
            Browser br = new Browser();
            br.navigate(displayUrl);
            CodeAccessPermission::revertAssert();
        }
    }
}