Tags:  .NET API C#
C# is not supported any more, this page is deprecated.

Using the CodeBeamer Remote API from .NET (C#) Clients

codeBeamer 5.0 provides .Net binding for WebServices API. One can use .Net binding to communicate with codeBeamer using e.g. C# and MS Visual Studio. codeBeamer .Net API provides the same functionality as codeBeamer Java WebServices API.

Proof of concept: Integration with Sparx EA You must login to see this link. Register now, if you have no user account yet..

Prerequisites

Dynamic libraries necessary to use codeBeamer .Net WebServices API are located at You must login to see this link. Register now, if you have no user account yet.

MS Visual Studio Project

Download complete MS Visual Studio Project from You must login to see this link. Register now, if you have no user account yet.

TBD - link to the SVN location of the MS VS project

C# Example

Download C# example from You must login to see this link. Register now, if you have no user account yet.

TBD - link to the SVN location of the C# example

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using hessiancsharp.client;
using IRemoting.com.intland.codebeamer.remoting;
using System.Diagnostics;
using com.intland.codebeamer.persistence.dto;
using Intland.CodeBeamer.Test.Properties;
using System.Drawing;
using System.IO;

namespace Intland.CodeBeamer.Test
{
    /// <summary>
    /// Summary description for UnitTest_RemoteApi
    /// </summary>
    [TestClass]
    public class UnitTest_RemoteApi
    {
        protected static CHessianProxyFactory factory = new CHessianProxyFactory();
        protected static RemoteApi api = null;

        // SET YOUR USERNAME AND PASSWORD HERE!!
        protected static string apiUser = "REPLACE_IT_WITH_USERNAME";
        protected static string apiPassword = "REPLACE_IT_WITH_PASSWORD";

        //protected static string url = "http://172.20.41.170:8080/cb/remote-api";
        protected static string url = "http://localhost:8080/cb/remote-api";
        protected static string token = string.Empty;
        protected static ProjectDto project;
        protected static TrackerDto tracker;

        public UnitTest_RemoteApi()
        {
            
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Init & CleanUP

        [ClassInitialize()]
        public static void Login(TestContext testContext)
        {            
            
            api = (RemoteApi)factory.Create(typeof(RemoteApi), url);
            
            if (string.IsNullOrEmpty(token))
            {
                token = api.login(apiUser, apiPassword);
            }
            Assert.IsNotNull(api);
            Assert.IsNotNull(token);
        }

        [ClassCleanup()]
        public static void Logout()
        {
            if (!object.Equals(api, null))
            {
                api.logout(token);
            }
        } 

        #endregion        

        #region TestMethods


        [TestMethod]
        public void TestCreateUser()
        {
            CreateUser();
        }        


        [TestMethod]
        public void TestCreateProjectFromTemlate()
        {
            ProjectDto templateProject = GetProject();
            RoleDto[] roles = api.findRolesByProject(token, templateProject.Id.Value);
            Assert.IsTrue(roles.Length > 0);

            UserDto newUser = CreateUser();
            bool found = false;
            foreach (RoleDto role in roles)
            {
                if (role.Description.Equals("Customer", StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    api.addUserToRole(token, newUser.Id.Value, templateProject.Id.Value, role.Id.Value);
                    break;
                }
            }
            Assert.IsTrue(found);
            
            ProjectDto newTemplateProject = CreateProject(Resources.Project+DateTime.Now.ToString(Resources.DateFormat), templateProject.Id);
            Assert.IsTrue(newTemplateProject.Id > 0);

            UserDto[] users = api.findUsersByProject(token, newTemplateProject.Id.Value, false);
            found = false;
            foreach (UserDto user in users)
            {
                if (user.Id.Equals(newUser.Id))
                {
                    found = true;
                    break;
                }
            }
            Assert.IsTrue(found);
        }

        [TestMethod]
        public void TestFindAllUsers()
        {
            UserDto[] users = api.findAllUsers(token);
            Assert.IsTrue(users.Length > 0);
            bool found = false;
            foreach (UserDto user in users)
            {
                if (user.Name.Equals(apiUser, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    break;
                }
            }
            Assert.IsTrue(found);
        }

        [TestMethod]
        public void TestFindTrackerItemById()
        {
            TrackerItemDto item = CreateTrackerItem();
            TrackerItemDto itemById = api.findTrackerItemById(token, item.Id.Value);
            Assert.Equals(item.Id, itemById.Id);
        }       

        [TestMethod]
        public void TestCreateTrackerItem()
        {
            Assert.IsNotNull(CreateTrackerItem());

            TrackerItemDto[] items = api.findTrackerItemsByTrackerIdAndUserId(token, GetTracker().Id.Value, getUser().Id.Value);
            Assert.IsTrue(items.Length > 0);

            items = api.findAllUserTrackerItems(token);
            Assert.IsTrue(items.Length > 0);
        }

        [TestMethod]
        public void TestGetTracker()
        {
            GetTracker();
            TrackerDto[] trackers = api.findAllTrackers(token);
            Assert.IsTrue(trackers.Length > 0);
           
            trackers = api.findTrackersByProject(token, GetProject().Id.Value);
            Assert.IsTrue(trackers.Length > 0);
        }

        [TestMethod]
        public void TestCreateProject()
        {
            GetProject();
            ProjectDto[] projects = api.findAllProjects(token);
            Assert.IsTrue(projects.Length > 0);
        }

        [TestMethod]
        public void TestCreateTrackerItemComment()
        {
            TrackerItemDto item = CreateTrackerItem();

            Assert.IsNotNull(item);

            TrackerItemCommentDto comment = new TrackerItemCommentDto();
            comment.Description = TestString(Resources.Description);
            comment.TrackerItem = item;

            TrackerItemCommentDto newComment = api.createTrackerItemComment(token, comment);

            Assert.IsTrue(newComment.Id > 0);
            CheckDateTime(newComment.SubmittedAt);

            Assert.IsNotNull(newComment.Submitter.Id);
            Assert.Equals(comment.Description, newComment.Description);

            TrackerItemCommentDto foundItem = api.findTrackerItemCommentById(token, newComment.Id.Value);

            Assert.Equals(newComment.Id, foundItem.Id);
            
        }

        [TestMethod]
        public void TestCreateTrackerItemAttachment()
        {
            TrackerItemDto item = CreateTrackerItem();

            Assert.IsNotNull(item);

            TrackerItemAttachmentDto attach = new TrackerItemAttachmentDto();
            attach.Description = TestString(Resources.Description);
            attach.TrackerItem = item;
            attach.Filename = Resources.FileName;


            TrackerItemAttachmentDto newAttach = api.createTrackerItemAttachment(token, attach);

            Assert.IsTrue(newAttach.Id > 0);
            CheckDateTime(newAttach.SubmittedAt);

            Assert.IsNotNull(newAttach.Submitter.Id);
            Assert.Equals(attach.Description, newAttach.Description);

            TrackerItemAttachmentDto foundAttach = api.findTrackerItemAttachmentById(token, newAttach.Id.Value);

            Assert.Equals(newAttach.Id, foundAttach.Id);

            
            BinaryStreamDto binaryStream = new BinaryStreamDto();

            //String content = TestString(Resources.FileContent);
            //binaryStream.Bytes = System.Text.Encoding.Default.GetBytes(content);
            
            binaryStream.FileName = Resources.FileName;
            byte[] fileContents = GetBitmapBytes(Resources.ImgDilbert);
            binaryStream.Bytes = fileContents;
            api.saveTrackerItemAttachmentBlob(token, newAttach.Id.Value, binaryStream);
            BinaryStreamDto returnbinaryStream = api.loadTrackerItemAttachmentBlob(token, newAttach.Id.Value);

            Assert.Equals(fileContents.Length, returnbinaryStream.Bytes);
        }

        [TestMethod]
        public void TestFindUserByID()
        {
            UserDto user = api.findUserById(token, 1);
            Assert.IsNotNull(user);
            Assert.Equals(1, user.Id);

        }

        [TestMethod]
        public void TestCreateArtifactWithParent()
        {
            ArtifactDto parent = new ArtifactDto();
            parent.Project = GetProject();
            parent.Name = TestString(Resources.Document);
            parent.Description = TestString(Resources.Description);
            parent.TypeId = ArtifactType.DIR;
            parent.Owner = getUser();

            ArtifactDto newParent = api.createArtifact(token, parent);

            Assert.Equals(parent.Name, newParent.Name);
            Assert.IsTrue(newParent.Id > 0);
            Assert.IsTrue(newParent.isA(ArtifactType.DIR));

            ArtifactDto artifact = CreateArtifactFile(newParent);

            Assert.Equals(newParent.Id, artifact.Parent.Id);
            Assert.IsNotNull(artifact);
        }

        [TestMethod]
        public void TestCreateArtifactAttachment()
        {
            ArtifactDto artifact = CreateArtifactFile(null);

            Assert.IsNotNull(artifact);            

            ArtifactCommentDto aComment = new ArtifactCommentDto();
            aComment.Description = TestString(Resources.Description);
            aComment.Artifact= artifact;
            aComment.Filename = Resources.FileName;
            aComment.Submitter = getUser();            

            ArtifactCommentDto newaComment = api.createArtifactComment(token, aComment);

            Assert.IsTrue(newaComment.Id > 0);
            CheckDateTime(newaComment.SubmittedAt);

            Assert.IsNotNull(newaComment.Submitter.Id);
            Assert.Equals(aComment.Description, newaComment.Description);

            ArtifactCommentDto foundaComment = api.findArtifactCommentById(token, newaComment.Id.Value);

            Assert.Equals(newaComment.Id, foundaComment.Id);


            BinaryStreamDto binaryStream = new BinaryStreamDto();

            //String content = TestString(Resources.FileContent);
            //binaryStream.Bytes = System.Text.Encoding.Default.GetBytes(content);

            binaryStream.FileName = Resources.FileName;
            byte[] fileContents = GetBitmapBytes(Resources.ImgDilbert);
            binaryStream.Bytes = fileContents;
            api.saveArtifactCommentBlob(token, newaComment.Id.Value, binaryStream);
            BinaryStreamDto returnbinaryStream = api.loadArtifactCommentBlob(token, newaComment.Id.Value);

            Assert.Equals(fileContents.Length, returnbinaryStream.Bytes);
        }

        [TestMethod]
        public void TestCreateAssociation()
        {
            TrackerItemDto source = CreateTrackerItem();
            TrackerItemDto target = CreateTrackerItem();

            AssociationDto association = new AssociationDto();
            association.Description = TestString(Resources.Description);
            association.FromTypeId = GroupType.TRACKER;
            association.ToTypeId = GroupType.TRACKER;
            association.FromId = source.Id.Value;
            association.ToId = target.Id.Value;
            association.Submitter = getUser();
            association.TypeId = AssociationTypeDto.ASSOCIATION_TYPE_DEFAULT_TO;

            AssociationDto newAssociation = api.createAssociation(token, association);

            Assert.Equals(AssociationTypeDto.ASSOCIATION_TYPE_DEFAULT_TO, newAssociation.TypeId);
            Assert.IsNotNull(newAssociation);
            Assert.IsTrue(newAssociation.Id > 0);
            CheckDateTime(newAssociation.SubmittedAt);

            Assert.Equals(target.Id, newAssociation.ToId);
            Assert.Equals(source.Id, newAssociation.FromId);

            AssociationDto foundAssociation = api.findAssociationById(token, newAssociation.Id.Value);
            Assert.Equals(newAssociation.Id, foundAssociation.Id);

            AssociationDto[] associations = api.findAllAssociations(token);
            Assert.IsTrue(associations.Length > 0);
        }

        [TestMethod]
        public void TestCreateWikiPage()
        {
            CreateWikiPage();
        }

        #endregion

        #region Static Methods

        private static string TestString(string prefix)
        {
            return string.Format(Resources.TestString, prefix, Guid.NewGuid().ToString());
        }

        private static byte[] GetBitmapBytes(Bitmap Bitmap)
        {
            MemoryStream memStream = new MemoryStream();
            byte[] bytes;
            try
            {
                Bitmap.Save(memStream, Bitmap.RawFormat);
                bytes = new byte[memStream.Length];
                memStream.Seek(0, SeekOrigin.Begin);
                memStream.Read(bytes, 0, bytes.Length);                
            }
            finally
            {
                memStream.Close();
            }
            return bytes;
        }

        protected static void CheckDateTime(DateTime? date)
        {
            Assert.Equals(DateTime.Now.Year, date.Value.Year);
            Assert.Equals(DateTime.Now.Month, date.Value.Month);
            Assert.Equals(DateTime.Now.Day, date.Value.Day);
            Assert.Equals(DateTime.Now.Hour, date.Value.Hour);
        }

        protected static TrackerItemDto CreateTrackerItem()
        {
            TrackerItemDto item = new TrackerItemDto();
            item.Tracker = GetTracker();
            item.Summary = TestString(Resources.Summary);
            item.Description = TestString(Resources.Description);

            TrackerItemDto newItem = api.createTrackerItem(token, item);

            Assert.Equals(item.Summary, newItem.Summary);
            Assert.Equals(item.Description, newItem.Description);
            Assert.IsTrue(newItem.Id > 0);
            CheckDateTime(newItem.SubmittedAt);
            return newItem;
        }

        protected static WikiPageDto CreateWikiPage()
        {
            WikiPageDto wikiPage = new WikiPageDto();
            wikiPage.Name = TestString("Wiki");
            wikiPage.Project = GetProject();
            wikiPage.Description = TestString(Resources.Description);
            wikiPage.Owner = getUser();

            WikiPageDto newWikiPage = api.createWikiPage(token, wikiPage, Resources.Wiki, TestString(Resources.Comment), CommentFormat.Text);

            Assert.Equals(wikiPage.Name, newWikiPage.Name);
            Assert.Equals(wikiPage.Description, newWikiPage.Description);            
            Assert.IsTrue(newWikiPage.Id > 0);
            CheckDateTime(newWikiPage.CreatedAt);

            WikiPageDto foundWikiPage = api.findWikiPageById(token, newWikiPage.Id.Value);
            Assert.IsNotNull(foundWikiPage);
            Assert.Equals(foundWikiPage.Id.Value, newWikiPage.Id.Value);

            Assert.Equals(Resources.Wiki, api.downloadWikiPage(token, newWikiPage.Id.Value, false));
            Assert.IsTrue(api.downloadWikiPage(token, newWikiPage.Id.Value, true).IndexOf("<b>", StringComparison.CurrentCultureIgnoreCase) >-1);

            return newWikiPage;
        }

        protected static ArtifactDto CreateArtifactFile(ArtifactDto parent)
        {
            ArtifactDto artifact = new ArtifactDto();
            artifact.Project = GetProject();
            artifact.Name = TestString(Resources.Document)+ ".gif";
            artifact.Description = TestString(Resources.Description);
            artifact.TypeId = ArtifactType.FILE;
            artifact.Owner = getUser();
            artifact.Parent = parent;

            byte[] fileContents = GetBitmapBytes(Resources.ImgDilbert);
            ArtifactDto newArtifact = api.createAndUploadArtifact(token, artifact, fileContents, TestString(Resources.Comment), CommentFormat.Text);

            byte[] downloadedFile = api.downloadArtifact(token, newArtifact.Id.Value);

            Assert.Equals(artifact.Name, newArtifact.Name);
            Assert.Equals(artifact.Description, newArtifact.Description);
            Assert.IsTrue(newArtifact.Id > 0);
            CheckDateTime(newArtifact.CreatedAt);



            Assert.Equals(fileContents.Length, downloadedFile.Length);

            return newArtifact;
        }

        protected static TrackerDto GetTracker()
        {
            string trackerName = Resources.Tracker;
            if (object.Equals(tracker, null))
            {
                GetProject();
                TrackerDto[] trackers = api.findTrackersByProject(token, project.Id.Value);
                foreach (TrackerDto trk in trackers)
                {
                    if (trk.Name.Equals(trackerName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        tracker = trk;
                    }
                }
            }
            Assert.IsNotNull(tracker);
            return tracker;
        }

        protected static ProjectDto GetProject()
        {
            if (object.Equals(project, null))
            {
                Random rnd = new Random();
                string projName = Resources.Project; // - "+rnd.Next().ToString();
                project = api.findProjectByName(token, projName);
                //Assert.IsNull(project, projName);
                if (object.Equals(project, null))
                {
                    project = CreateProject(string.Empty, null);              
                }
            }
            Assert.IsNotNull(project);
            return project;
        }

        protected static ProjectDto CreateProject(string projName, int? templateID)
        {
            ProjectDto project = new ProjectDto();
            project.CreatedBy = getUser();            
            project.Name = projName;
            if (string.IsNullOrEmpty(projName))
            {
                project.Name = Resources.Project;
            }

            project.Description = TestString(Resources.Description);
            ProjectDto newProject = null;
            if (object.Equals(templateID, null))
            {
                newProject = api.createProject(token, project);
            }else
            {
                newProject = api.createProjectFromTemplate(token, project, templateID.Value);
            }
            
            
            Assert.Equals(project.Name, newProject.Name);
            Assert.IsTrue(newProject.Id > 0);

            CheckDateTime(newProject.CreatedAt);
            Assert.IsNull(newProject.StartDate);

            return newProject;      
        }

        protected static UserDto getUser()
        {
            UserDto user = new UserDto();
            user.Name = apiUser;
            user.Id = 1;
            return user;
        }

        protected static UserDto CreateUser()
        {
            UserDto user = new UserDto();
            user.FirstName = "James";
            user.LastName = "Moneypenny";
            user.Name = "james" + DateTime.Now.Millisecond;
            user.Password = "008";
            
            UserDto newUser = api.createUser(token, user);
            Assert.IsNotNull(newUser);
            return newUser;
        }

        #endregion      
    }
}