I have this code that works fine. But I would like to create some unit test for this class. Most of the code is in private or void type methods. Please help me refactor my class to make it more testable.
namespace Insperity.HCRBlackBox.HCRServices.Biz
{
public class OrganizationChartBiz : IOrganizationChartBiz
{
private readonly OrganizationChartRepository _repository;
public OrganizationChartBiz()
{
_repository = new OrganizationChartRepository();
}
public AcknowledgeOrganizationChartType NotifyOrganizationChart(NotifyOrganizationChartType notifyOrganizationChartType)
{
foreach (var organizationChart in notifyOrganizationChartType.DataArea.OrganizationChart)
{
foreach (var unit in organizationChart.OrganizationUnit)
{
var existing = GetExistingOrganization(unit.OrganizationUnitID.Value);
if (existing.Equals(null))
{
TransformToOrganizationAndSave(unit);
}
else
{
TransformToOrganizationAndUpdate(unit, existing);
}
}
}
return new AcknowledgeOrganizationChartType();
}
public AcknowledgeOrganizationChartType SyncOrganizationChart(SyncOrganizationChartType syncOrganizationChartType)
{
foreach (var organizationChart in syncOrganizationChartType.DataArea.OrganizationChart)
{
foreach (var unit in organizationChart.OrganizationUnit)
{
var existing = GetExistingOrganization(unit.OrganizationUnitID.Value);
if (existing!=null)
{
TransformToOrganizationAndUpdate(unit, existing);
}
else
{
TransformToOrganizationAndSave(unit);
}
}
}
return new AcknowledgeOrganizationChartType();
}
private void TransformToOrganizationAndUpdate(OrgChartOrganizationUnitType org, Organization existing)
{
List<RelatedOrganization> relatedOrganizations = new List<RelatedOrganization>();
var now = DateTime.UtcNow;
var organization = existing;
if (org.ParentOrganizationUnit.OrganizationUnitID.Value!=null)
organization.ParentOrganizationId = org.ParentOrganizationUnit.OrganizationUnitID.Value;
organization.OrganizationName = org.OrganizationUnitName.Value;
organization.LastModified = now;
if (org.RelatedOrganizationUnit != null)
{
foreach (var relatedOrganizationUnitType in org.RelatedOrganizationUnit)
{
var controlGroup = new RelatedOrganization
{
ControlGroupId = relatedOrganizationUnitType.OrganizationUnitID.Value,
StartDate =
Convert.ToDateTime(relatedOrganizationUnitType.OrganizationUnitID.validFrom),
Organization = organization,
EndDate =
relatedOrganizationUnitType.OrganizationUnitID.validTo != null
? Convert.ToDateTime(relatedOrganizationUnitType.OrganizationUnitID.validTo)
: new DateTime?(),
LastModified = now
};
relatedOrganizations.Add(controlGroup);
}
}
if (org.UserArea != null)
{
var userArea = org.UserArea as OrganizationChartUserArea;
if (userArea != null)
{
organization.EducationIndicator = userArea.EducationIndicator.Equals(false) ? "F" : "T";
}
}
_repository.UpdateOrganization(organization,relatedOrganizations);
}
private void TransformToOrganizationAndSave(OrgChartOrganizationUnitType org)
{
var now = DateTime.UtcNow;
var organization = new Organization
{
OrganizationId = org.OrganizationUnitID.Value,
ParentOrganizationId = org.ParentOrganizationUnit.OrganizationUnitID.Value,
OrganizationName = org.OrganizationUnitName.Value,
LastModified = now
};
if (org.RelatedOrganizationUnit != null)
{
foreach (var relatedOrganizationUnitType in org.RelatedOrganizationUnit)
{
var controlGroup = new RelatedOrganization
{
ControlGroupId = relatedOrganizationUnitType.OrganizationUnitID.Value,
StartDate =
Convert.ToDateTime(relatedOrganizationUnitType.OrganizationUnitID.validFrom),
OrganizationId = relatedOrganizationUnitType.OrganizationUnitID.Value,
EndDate =
relatedOrganizationUnitType.OrganizationUnitID.validTo != null
? Convert.ToDateTime(relatedOrganizationUnitType.OrganizationUnitID.validTo)
: new DateTime?(),
LastModified = now
};
organization.RelatedOrganizations.Add(controlGroup);
}
}
if (!org.UserArea.Equals(null))
{
var userArea = org.UserArea as OrganizationChartUserArea;
if (userArea != null)
organization.EducationIndicator = userArea.EducationIndicator.Equals(false) ? "F" : "T";
}
_repository.SaveOrganization(organization);
}
private Organization GetExistingOrganization(string organizationUnitId)
{
return _repository.GetExistingOrganization(organizationUnitId);
}
public void Dispose()
{
_repository.Dispose();
}
}
}