/// <summary>
        /// GetFakeListTerminalAssignmentDtoForDropTerm - return list of fake TerminalAssignmentDto objects
        /// </summary>
        /// <param name="numberOfDropTermItems"></param>
        /// <param name="previousTerminalNames"></param>
        /// <param name="currentTerminalNames"></param>
        /// <returns></returns>
        public List<TerminalAssignmentDto> GetFakeListTerminalAssignmentDtoForDropTerm(int numberOfDropTermItems, string[] previousTerminalNames, string[] currentTerminalNames)
        {
            var fakeListTerminalAssignmentDtoForDropTerm = new List<TerminalAssignmentDto>();

            for (var i = 1; i <= numberOfDropTermItems; i++)
            {
                var item = new TerminalAssignmentDto
                {
                    PreviousTerminal = string.Format(previousTerminalNames[i - 1], (i - 1)),
                    TerminalType = "BU",
                    TerminalName = string.Format(currentTerminalNames[i - 1], i),
                    CableName = string.Format("TerminalAssignmentDtoDropTerm0{0}_IncomingPairValue", i),
                    PairNumber = string.Format("TerminalAssignmentDtoDropTerm0{0}_OutgoingPairValue", i),
                    TerminalAddress = string.Format("TerminalAssignmentDtoDropTerm0{0}_TerminalAddress", i)
                };

                fakeListTerminalAssignmentDtoForDropTerm.Add(item);
            };

            return fakeListTerminalAssignmentDtoForDropTerm;
        }
        /// <summary>
        /// GetFakeListTerminalAssignmentDtoForXBox - return list of fake TerminalAssignmentDto objects
        /// </summary>
        /// <param name="numberOfXBoxItems"></param>
        /// <param name="previousTerminalNames"></param>
        /// <param name="currentTerminalNames"></param>
        /// <returns></returns>
        public IEnumerable<TerminalAssignmentDto> GetFakeListTerminalAssignmentDtoForXBox(int numberOfXBoxItems, string[] previousTerminalNames, string[] currentTerminalNames)
        {
            var fakeListTerminalAssignmentDtoForXBox = new List<TerminalAssignmentDto>();

            for (var i = 1; i <= numberOfXBoxItems; i++)
            {
                var item = new TerminalAssignmentDto
                {
                    PreviousTerminal = string.Format(previousTerminalNames[i - 1], (i - 1)),
                    TerminalType = "XT",
                    TerminalName = string.Format(currentTerminalNames[i - 1], i),
                    CableName = string.Format("TerminalAssignmentDtoXbox0{0}_IncomingPairValue", i),
                    PairNumber = string.Format("TerminalAssignmentDtoXbox0{0}_OutgoingPairValue", i),
                    TerminalAddress = string.Format("TerminalAssignmentDtoXbox0{0}_TerminalAddress", i)
                };

                fakeListTerminalAssignmentDtoForXBox.Add(item);
            };

            return fakeListTerminalAssignmentDtoForXBox;
        }
        private GetFacilitiesResponseDto MapResponse(retrieveServiceDesignResponse1 esbResponse)
        {
            var getFacilityResponse = new GetFacilitiesResponseDto {PlantData = new PlantDataDto()};
            var copperPlantData = new CopperPlantDataDto();
            var fiberPlantData = new FiberPlantDataDto();
            try
            {
                //If any null objects throw...
                var facility =
                    esbResponse.retrieveServiceDesignResponse.retrieveServiceDesignOutput.Payload.First()
                        .BusinessInteraction.InteractionItems.First()
                        .Service;

                if (facility == null)
                    throw new EnterpriseServiceException("A null response was returned from ESB Get Facility");

                var copperResourceFacingService =
                    facility.ResourceFacingServices.FirstOrDefault(
                        rfs => rfs.ResourceFacingServiceId.type.Equals("copper", StringComparison.OrdinalIgnoreCase));
                var fiberResourceFacingService =
                    facility.ResourceFacingServices.FirstOrDefault(
                        rfs => rfs.ResourceFacingServiceId.type.Equals("fiber", StringComparison.OrdinalIgnoreCase));

                if (fiberResourceFacingService != null && fiberResourceFacingService.CharacteristicSpecification != null)
                {
                    fiberPlantData.Status =
                        (fiberResourceFacingService.CharacteristicSpecification.Any(
                            sd =>
                                sd.name.Equals("status", StringComparison.OrdinalIgnoreCase) && sd.Value != null &&
                                !string.IsNullOrEmpty(sd.Value.value)))
                            ? fiberResourceFacingService.CharacteristicSpecification.First(
                                sdb =>
                                    sdb.name.Equals("status", StringComparison.OrdinalIgnoreCase) && sdb.Value != null &&
                                    !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;
                    getFacilityResponse.PlantData.FiberPlantData = fiberPlantData;
                }

                if (fiberResourceFacingService != null && copperResourceFacingService == null)
                {
                    return getFacilityResponse;
                }

                var serviceDescriptions = facility.ServiceDescribedBy.ToList();
                if (serviceDescriptions.Any())
                {
                    getFacilityResponse.Environment =
                        (serviceDescriptions.Any(
                            sd =>
                                sd.name.Equals("environment", StringComparison.OrdinalIgnoreCase) && sd.Value != null &&
                                !string.IsNullOrEmpty(sd.Value.value)))
                            ? serviceDescriptions.First(
                                sdb =>
                                    sdb.name.Equals("environment", StringComparison.OrdinalIgnoreCase) &&
                                    sdb.Value != null && !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;

                    getFacilityResponse.USI =
                        (serviceDescriptions.Any(
                            sd =>
                                sd.name.Equals("uniqueServiceIdentifier", StringComparison.OrdinalIgnoreCase) &&
                                sd.Value != null && !string.IsNullOrEmpty(sd.Value.value)))
                            ? serviceDescriptions.First(
                                sdb =>
                                    sdb.name.Equals("uniqueServiceIdentifier", StringComparison.OrdinalIgnoreCase) &&
                                    sdb.Value != null && !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;

                    getFacilityResponse.TN =
                        (serviceDescriptions.Any(
                            sd =>
                                sd.name.Equals("telephoneNumber", StringComparison.OrdinalIgnoreCase) &&
                                sd.Value != null && !string.IsNullOrEmpty(sd.Value.value)))
                            ? serviceDescriptions.First(
                                sdb =>
                                    sdb.name.Equals("telephoneNumber", StringComparison.OrdinalIgnoreCase) &&
                                    sdb.Value != null && !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;

                    getFacilityResponse.ProvisionedSpeed =
                        (serviceDescriptions.Any(
                            sd =>
                                sd.name.Equals("provisionedSpeed", StringComparison.OrdinalIgnoreCase) &&
                                sd.Value != null && !string.IsNullOrEmpty(sd.Value.value)))
                            ? serviceDescriptions.First(
                                sdb =>
                                    sdb.name.Equals("provisionedSpeed", StringComparison.OrdinalIgnoreCase) &&
                                    sdb.Value != null && !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;

                }

                #region address

                getFacilityResponse.ServiceAddress = new ServiceAddressDto();

                getFacilityResponse.LocationId = (facility.ServiceLocation != null &&
                                                  !string.IsNullOrEmpty(facility.ServiceLocation.ID))
                    ? facility.ServiceLocation.ID
                    : string.Empty;

                var geoAddress = (facility.ServiceLocation != null && facility.ServiceLocation.GeographicAddress != null)
                    ? facility.ServiceLocation.GeographicAddress
                    : null;

                var addressExtension = (geoAddress != null) ? geoAddress.GeographicAddressExtension : null;

                if (addressExtension != null && addressExtension.AddressText != null &&
                    addressExtension.AddressText.Any())
                {
                    var address1 = (addressExtension.AddressText.FirstOrDefault(et => et.sequenceNumber == 1)
                                    ?? new AddressText()).addressText
                                    ?? string.Empty;
                    getFacilityResponse.ServiceAddress.Address1 = address1.Trim();

                    var address2 = (addressExtension.AddressText.FirstOrDefault(et => et.sequenceNumber == 2)
                                    ?? new AddressText()).addressText
                                    ?? string.Empty;
                    getFacilityResponse.ServiceAddress.Address2 = address2.Trim();
                }

                #endregion

                if (copperResourceFacingService != null)
                {

                    var physicalResources = copperResourceFacingService.physicalResource.ToList();
                    var logicalResources = copperResourceFacingService.logicalResource.ToList();

                    copperPlantData.Status =
                        (copperResourceFacingService.CharacteristicSpecification.Any(
                            sd =>
                                sd.name.Equals("status", StringComparison.OrdinalIgnoreCase) && sd.Value != null &&
                                !string.IsNullOrEmpty(sd.Value.value)))
                            ? copperResourceFacingService.CharacteristicSpecification.First(
                                sdb =>
                                    sdb.name.Equals("status", StringComparison.OrdinalIgnoreCase) && sdb.Value != null &&
                                    !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;

                    copperPlantData.MaxAttainableSpeed =
                        (copperResourceFacingService.CharacteristicSpecification.Any(
                            sd =>
                                sd.name.Equals("maxAttainableSpeed", StringComparison.OrdinalIgnoreCase) &&
                                sd.Value != null && !string.IsNullOrEmpty(sd.Value.value)))
                            ? copperResourceFacingService.CharacteristicSpecification.First(
                                sdb =>
                                    sdb.name.Equals("maxAttainableSpeed", StringComparison.OrdinalIgnoreCase) &&
                                    sdb.Value != null && !string.IsNullOrEmpty(sdb.Value.value)).Value.value
                            : string.Empty;

                    #region physicalresources
                    // TODO: refactor below code to remove numerous Possible NullReference Exceptions
                    if (physicalResources.Any())
                    {

                        var switchData = physicalResources.Where(pr =>
                                    pr.PhysicalResourceExtension.type.Equals("switch", StringComparison.OrdinalIgnoreCase)).ToList();
                        var dslamData = physicalResources.Where(pr =>
                                    pr.PhysicalResourceExtension.type.Equals("dslam", StringComparison.OrdinalIgnoreCase)).ToList();
                        var routerData = physicalResources.Where(pr =>
                                    pr.PhysicalResourceExtension.type.Equals("router", StringComparison.OrdinalIgnoreCase)).ToList();
                        if (dslamData != null)
                        {
                            copperPlantData.DslamClli = (dslamData.FirstOrDefault().PhysicalResourceExtension != null
                                                         && !string.IsNullOrEmpty(dslamData.FirstOrDefault()
                                                                 .PhysicalResourceExtension
                                                                 .commonLanguageLocationIdentifier))
                                ? dslamData.FirstOrDefault().PhysicalResourceExtension.commonLanguageLocationIdentifier
                                : string.Empty;

                            copperPlantData.DslamOfficeCode = (dslamData.FirstOrDefault().PhysicalResourceExtension != null
                                                               && !string.IsNullOrEmpty(dslamData.FirstOrDefault()
                                                                       .PhysicalResourceExtension.officeId))
                                ? dslamData.FirstOrDefault().PhysicalResourceExtension.officeId
                                : string.Empty;

                        }

                        if (switchData != null)
                        {
                            copperPlantData.SwitchClli = (switchData.FirstOrDefault().PhysicalResourceExtension != null &&
                                                          !string.IsNullOrEmpty(switchData.FirstOrDefault()
                                                                  .PhysicalResourceExtension
                                                                  .commonLanguageLocationIdentifier))
                                ? switchData.FirstOrDefault().PhysicalResourceExtension.commonLanguageLocationIdentifier
                                : string.Empty;

                            copperPlantData.SwitchOfficeCode = (switchData.FirstOrDefault().PhysicalResourceExtension != null
                                                                && !string.IsNullOrEmpty(switchData.FirstOrDefault()
                                                                        .PhysicalResourceExtension.officeId))
                                ? switchData.FirstOrDefault().PhysicalResourceExtension.officeId
                                : string.Empty;

                            if (switchData.FirstOrDefault().PhysicalResourceCharacteristicValues != null &&
                                switchData.FirstOrDefault().PhysicalResourceCharacteristicValues.Any())
                            {
                                var switchLrn = switchData.Select(sd => sd.PhysicalResourceCharacteristicValues
                                    .FirstOrDefault(prcv =>
                                            prcv.name.Equals("localRoutingNumber", StringComparison.OrdinalIgnoreCase) &&
                                            !string.IsNullOrEmpty(prcv.Value.value))).ToList();

                                copperPlantData.SwitchLrn = (switchLrn != null && switchLrn.FirstOrDefault() != null &&
                                                             switchLrn.FirstOrDefault().Value != null &&
                                                             !string.IsNullOrEmpty(
                                                                 switchLrn.FirstOrDefault().Value.value))
                                    ? switchLrn.FirstOrDefault().Value.value
                                    : string.Empty;

                                var switchType = switchData.Select(sd => sd.PhysicalResourceCharacteristicValues
                                    .FirstOrDefault(prcv =>
                                            prcv.name.Equals("switchType", StringComparison.OrdinalIgnoreCase) &&
                                            !string.IsNullOrEmpty(prcv.Value.value))).ToList();

                                copperPlantData.SwitchType = (switchType != null && switchType.FirstOrDefault() != null &&
                                                              switchType.FirstOrDefault().Value != null &&
                                                              !string.IsNullOrEmpty(
                                                                  switchType.FirstOrDefault().Value.value))
                                    ? switchType.FirstOrDefault().Value.value
                                    : string.Empty;
                            }
                        }

                        if (routerData != null)
                        {
                            copperPlantData.ServiceRouterClli = (routerData.FirstOrDefault().PhysicalResourceExtension != null
                                 && !string.IsNullOrEmpty(routerData.FirstOrDefault()
                                         .PhysicalResourceExtension.commonLanguageLocationIdentifier))
                                    ? routerData.FirstOrDefault()
                                        .PhysicalResourceExtension.commonLanguageLocationIdentifier
                                    : string.Empty;

                            if (routerData.FirstOrDefault().PhysicalResourceCharacteristicValues != null &&
                                routerData.FirstOrDefault().PhysicalResourceCharacteristicValues.Any())
                            {
                                var serviceRouterIp = switchData.Select(sd => sd.PhysicalResourceCharacteristicValues
                                    .FirstOrDefault(prcv =>
                                            prcv.name.Equals("routerIpAddress", StringComparison.OrdinalIgnoreCase) &&
                                            prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value))).ToList();

                                copperPlantData.ServiceRouterIpAddress = (serviceRouterIp != null &&
                                                                          serviceRouterIp.FirstOrDefault() != null &&
                                                                          serviceRouterIp.FirstOrDefault().Value != null &&
                                                                          !string.IsNullOrEmpty(
                                                                              serviceRouterIp.FirstOrDefault()
                                                                                  .Value.value))
                                    ? serviceRouterIp.FirstOrDefault().Value.value
                                    : string.Empty;
                            }

                        }

                    }
                    #endregion

                    #region logicalresource

                    if (logicalResources.Any())
                    {
                        copperPlantData.PairAssignment = new List<PairAssignmentDto>();
                        foreach (var logicalResource in logicalResources)
                        {
                            var pairAssignment = new PairAssignmentDto();

                            pairAssignment.TerminalAssignment = new List<TerminalAssignmentDto>();
                            pairAssignment.VCI = !string.IsNullOrEmpty(logicalResource.subscriberVCI)
                                ? logicalResource.subscriberVCI
                                : null;
                            pairAssignment.VPI = !string.IsNullOrEmpty(logicalResource.subscriberVPI)
                                ? logicalResource.subscriberVPI
                                : null;
                            var pairValues = (logicalResource.LogicalResourceCharacteristicValues != null &&
                                              logicalResource.LogicalResourceCharacteristicValues.Any())
                                ? logicalResource.LogicalResourceCharacteristicValues.ToList()
                                : new List<CharacteristicSpecification>();
                            if (pairValues.Any())
                            {
                                pairAssignment.Pair =
                                    (pairValues.Any(
                                        pv =>
                                            pv.name.Equals("pairName", StringComparison.OrdinalIgnoreCase) &&
                                            pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)))
                                        ? pairValues.First(
                                            pv =>
                                                pv.name.Equals("pairName", StringComparison.OrdinalIgnoreCase) &&
                                                pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)).Value.value
                                        : string.Empty;

                                pairAssignment.HsiPort =
                                    (pairValues.Any(
                                        pv =>
                                            pv.name.Equals("hsiPort", StringComparison.OrdinalIgnoreCase) &&
                                            pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)))
                                        ? pairValues.First(
                                            pv =>
                                                pv.name.Equals("hsiPort", StringComparison.OrdinalIgnoreCase) &&
                                                pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)).Value.value
                                        : string.Empty;

                                pairAssignment.DropType =
                                    (pairValues.Any(
                                        pv =>
                                            pv.name.Equals("dropType", StringComparison.OrdinalIgnoreCase) &&
                                            pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)))
                                        ? pairValues.First(
                                            pv =>
                                                pv.name.Equals("dropType", StringComparison.OrdinalIgnoreCase) &&
                                                pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)).Value.value
                                        : string.Empty;

                                pairAssignment.LineCardSequenceNumber =
                                    (pairValues.Any(
                                        pv =>
                                            pv.name.Equals("lineCardSequenceNumber", StringComparison.OrdinalIgnoreCase) &&
                                            pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)))
                                        ? pairValues.First(
                                            pv =>
                                                pv.name.Equals("lineCardSequenceNumber",
                                                    StringComparison.OrdinalIgnoreCase) && pv.Value != null &&
                                                !string.IsNullOrEmpty(pv.Value.value)).Value.value
                                        : string.Empty;

                                pairAssignment.LineCardType =
                                    (pairValues.Any(
                                        pv =>
                                            pv.name.Equals("lineCardType", StringComparison.OrdinalIgnoreCase) &&
                                            pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)))
                                        ? pairValues.First(
                                            pv =>
                                                pv.name.Equals("lineCardType", StringComparison.OrdinalIgnoreCase) &&
                                                pv.Value != null && !string.IsNullOrEmpty(pv.Value.value)).Value.value
                                        : string.Empty;
                            }

                            pairAssignment.LineCardTN =
                                logicalResource.LogicalResourceExtension.PhoneNumber.telephoneNumber;

                            var logicalPhysicalResources = (logicalResource.PhysicalResources != null)
                                ? logicalResource.PhysicalResources.ToList()
                                : new List<PhysicalResource>();

                            foreach (var physicalResource in logicalPhysicalResources)
                            {
                                var terminalAssignment = new TerminalAssignmentDto();
                                if (physicalResource.PhysicalResourceCharacteristicValues != null &&
                                    physicalResource.PhysicalResourceCharacteristicValues.Any())
                                {

                                    var cableName =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("cableName", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.CableName = (cableName != null &&
                                                                    cableName.Value != null &&
                                                                    !string.IsNullOrEmpty(cableName.Value.value))
                                        ? cableName.Value.value.Trim()
                                        : string.Empty;

                                    var pairNumber =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("pairNumber", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.PairNumber = (pairNumber != null &&
                                                                     pairNumber.Value != null &&
                                                                     !string.IsNullOrEmpty(pairNumber.Value.value))
                                        ? pairNumber.Value.value.Trim()
                                        : string.Empty;

                                    var terminalAddress =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("terminalAddress", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.TerminalAddress = (terminalAddress != null &&
                                                                          terminalAddress.Value != null &&
                                                                          !string.IsNullOrEmpty(
                                                                              terminalAddress.Value.value))
                                        ? terminalAddress.Value.value.Trim()
                                        : string.Empty;

                                    var previousTerminal =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("previousTerminal", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.PreviousTerminal = (previousTerminal != null &&
                                                                           previousTerminal.Value != null &&
                                                                           !string.IsNullOrEmpty(
                                                                               previousTerminal.Value.value))
                                        ? previousTerminal.Value.value.Trim()
                                        : string.Empty;

                                    var fromBindingPost =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("fromBindingPost", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.FromBindingPost = (fromBindingPost != null &&
                                                                          fromBindingPost.Value != null &&
                                                                          !string.IsNullOrEmpty(
                                                                              fromBindingPost.Value.value))
                                        ? fromBindingPost.Value.value.Trim()
                                        : string.Empty;

                                    var toBindingPost =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("toBindingPost", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.ToBindingPost = (toBindingPost != null &&
                                                                        toBindingPost.Value != null &&
                                                                        !string.IsNullOrEmpty(
                                                                            toBindingPost.Value.value))
                                        ? toBindingPost.Value.value.Trim()
                                        : string.Empty;

                                    var cableSegment =
                                        physicalResource.PhysicalResourceCharacteristicValues.FirstOrDefault(
                                            prcv =>
                                                prcv.name.Equals("cableSegment", StringComparison.OrdinalIgnoreCase) &&
                                                prcv.Value != null && !string.IsNullOrEmpty(prcv.Value.value));

                                    terminalAssignment.CableSegment = (cableSegment != null &&
                                                                       cableSegment.Value != null &&
                                                                       !string.IsNullOrEmpty(
                                                                           cableSegment.Value.value))
                                        ? cableSegment.Value.value.Trim()
                                        : string.Empty;
                                }

                                terminalAssignment.TerminalType = (physicalResource.PhysicalResourceExtension != null &&
                                                                   !string.IsNullOrEmpty(
                                                                       physicalResource.PhysicalResourceExtension.type))
                                    ? physicalResource.PhysicalResourceExtension.type
                                    : null;

                                terminalAssignment.TerminalName = (!string.IsNullOrEmpty(physicalResource.name))
                                    ? physicalResource.name
                                    : string.Empty;

                                pairAssignment.TerminalAssignment.Add(terminalAssignment);

                            }

                            copperPlantData.PairAssignment.Add(pairAssignment);
                        }

                    }

                    #endregion

                    getFacilityResponse.PlantData.CopperPlantData = copperPlantData;
                }

            }
            catch (System.Exception ex)
            {
                throw new EnterpriseServiceException("Error Mapping Facility Data from ESB Response", ex);
            }

            return getFacilityResponse;
        }