Tests/Private/Format-VoiceRoutingChain.Tests.ps1

# Module: TeamsFunctions
# Function: Test
# Author: David Eberhardt
# Updated: 26-AUG-2022

#NOTE: This tests both Input and Output Cmdlets as they feed directly into each other



InModuleScope TeamsFunctions {
  BeforeAll {
    # Mocking basic connection commands to avoid connectivity related errors
    Mock Assert-AzureADConnection { return $true }
    Mock Assert-MicrosoftTeamsConnection { return $true }
    Mock Test-MicrosoftTeamsConnection { return $true }
  }

  Describe -Tags ('Unit', 'Acceptance') "Function 'Format-VoiceRoutingChain*'" {
    It "should deconstruct '<Case>' from '<Name>' and reconstruct it correctly" -TestCases @(
      #region CallRestriction Tests
      @{ Case = "CallRestriction International: Particle 'International'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-International';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-International'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'International'
        }
      }
      @{ Case = "CallRestriction National: Particle 'National'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-National';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-National'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'National'
        }
      }
      @{ Case = "CallRestriction EmergencyOnly: Particle 'EmergencyOnly'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-EmergencyOnly';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-EmergencyOnly'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'EmergencyOnly'
        }
      }
      @{ Case = "CallRestriction International: Particle 'Int'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-Int';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-Int'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'International'
        }
      }
      @{ Case = "CallRestriction National: Particle 'Nat'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-Nat';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-Nat'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'National'
        }
      }
      @{ Case = "CallRestriction EmergencyOnly: Particle 'EMS'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-EMS';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-EMS'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'EmergencyOnly'
        }
      }
      @{ Case = "CallRestriction Custom: Name 'Restricted'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-Restricted';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-Restricted'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'Restricted'
        }
      }
      @{ Case = "CallRestriction Custom: Name 'MyCustom'";
        Name  = 'OVP-EMEA-Service-GBR-GBR033-MyCustom';
        VRC   = @{
          Name                  = 'OVP-EMEA-Service-GBR-GBR033-MyCustom'
          Region                = 'EMEA'
          Country               = 'GBR'
          Site                  = 'GBR033'
          CallRestriction       = 'Custom'
          CustomRestrictionName = 'MyCustom'
        }
      }
      @{ Case = 'CallRestriction Unrestricted, if provided and part of the string';
        Name  = 'OVP-EMEA-Service-GBR-GBR033-Unrestricted';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033-Unrestricted'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'Unrestricted'
        }
      }
      @{ Case = 'CallRestriction Unrestricted, if provided and NOT part of the string';
        Name  = 'OVP-EMEA-Service-GBR-GBR033';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'GBR033'
          CallRestriction = 'Unrestricted'
        }
      }
      @{ Case = 'CallRestriction Unrestricted; CountryCode is retained if Site is not matched';
        Name  = 'OVP-EMEA-Service-GBR-GBR033';
        VRC   = @{
          Name            = 'OVP-EMEA-Service-GBR-GBR033'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'XGBR033'
          CallRestriction = 'Unrestricted'
        }
      }
      @{ Case = 'CallRestriction Unrestricted; CountryCode is added';
        Name  = 'OVP-EMEA-Service-GBR-XGBR033';
        VRC   = @{
          # Addition of CountryCode as the Site Code does not start with the CountryCode
          Name            = 'OVP-EMEA-Service-GBR-XGBR033'
          Region          = 'EMEA'
          Country         = 'GBR'
          Site            = 'XGBR033'
          CallRestriction = 'Unrestricted'
        }
      }
      @{ Case = 'CallRestriction Unrestricted, if not provided';
        Name  = 'OVP-EMEA-Service-GBR-GBR033';
        VRC   = @{
          Name    = 'OVP-EMEA-Service-GBR-GBR033'
          Region  = 'EMEA'
          Country = 'GBR'
          Site    = 'GBR033'
        }
      }
      #endregion

      #region Prefix and Name tests
      @{ Case = 'Name - OVP prefix, if not provided';
        Name  = 'EMEA-Service-GBR-GBR033';
        VRC   = @{
          Name    = 'EMEA-Service-GBR-GBR033'
          Region  = 'EMEA'
          Country = 'GBR'
          Site    = 'GBR033'
        }
      }

      @{ Case = 'Name - Basic Name provided';
        Name  = 'Service';
        VRC   = @{
          Name    = 'Service'
          Region  = 'EMEA'
          Country = 'GBR'
          Site    = 'GBR033'
        }
      }
      @{ Case = 'Name - not provided';
        Name  = '';
        VRC   = @{
          Region  = 'EMEA'
          Country = 'GBR'
          Site    = 'GBR033'
        }
      }
      #endregion

      #region Bare minimum
      @{ Case = 'Region & Country only';
        Name  = '';
        VRC   = @{
          Region  = 'EMEA'
          Country = 'GBR'
        }
      }

      @{ Case = 'Region Only';
        Name  = '';
        VRC   = @{
          Region  = 'EMEA'
        }
      }
      #endregion
    ) {
      param ($VRC)

      #region Format-VoiceRoutingChainInput
      $OutputObject = Format-VoiceRoutingChainInput @VRC

      $OutputObject | Should -BeOfType PSCustomObject
      $OutputObject.Name | Should -BeOfType System.String

      # Deconstructed Name
      if ( $Case -notmatch '^Name - ') {
        if ( $VRC.ContainsKey('Name') -and $VRC.Name -match $VRC.Site) {
          $OutputObject.Name | Should -Be 'Service'
        }
        else {
          # Edge case where Site Name was not matched so it must be retained in the name
          #cannot be tested without manual butching of the string
        }

        # Finding Region
        if ( $VRC.Name -match $VRC.Region ) {
          $OutputObject.Region | Should -Be $true
        }
      }

      # Finding Country
      if ( $VRC.ContainsKey('Country') ) {
        if ( $VRC.Name -match $VRC.Country ) {
          $OutputObject.Country | Should -Be $true
        }
      }

      # Finding Site
      if ( $VRC.ContainsKey('Site') ) {
        if ( $VRC.Name -match $VRC.Site ) {
          $OutputObject.Site | Should -Be $true
          $OutputObject.Name | Should -Not -Match $VRC.Site
        }
      }

      # Finding CallRestriction
      if ( $VRC.ContainsKey('CallRestriction') ) {
        if ( $VRC.Name -match $VRC.CallRestriction ) {
          $OutputObject.CallRestriction | Should -Be $true
        }
      }

      # Finding Prefix
      $OutputObject.Prefix | Should -Be $($VRC.Name -match 'OVP')
      #endregion


      #region Format-VoiceRoutingChainOutput
      $FormatVRCOutputParams = @{
        Object = $OutputObject
        Region = $VRC.Region
      }
      if ( $VRC.ContainsKey('Country') ) { $FormatVRCOutputParams += @{ Country = $VRC.Country } }
      if ( $VRC.ContainsKey('Site') ) { $FormatVRCOutputParams += @{ Site = $VRC.Site } }
      if ( $VRC.ContainsKey('CallRestriction') ) { $FormatVRCOutputParams += @{ CallRestriction = $VRC.CallRestriction } }
      if ( $VRC.ContainsKey('CustomRestrictionName') ) { $FormatVRCOutputParams += @{ CustomRestrictionName = $VRC.CustomRestrictionName } }

      $ProtoString = Format-VoiceRoutingChainOutput @FormatVRCOutputParams
      $ProtoString | Should -BeOfType System.String

      # Finding Name
      # not testable as the order may be different, particles removed

      # Finding Region
      $ProtoString | Should -Match $VRC.Region

      # Finding Country
      if ( $VRC.ContainsKey('Country') ) {
        if ( $VRC.ContainsKey('Site') -and $VRC.Site -notmatch "^$($VRC.Country)" ) {
          $ProtoString | Should -Match $VRC.Country
        }
      }

      # Finding Site
      if ( $VRC.ContainsKey('Site') ) {
        if ( $OutputObject.Site ) {
          $ProtoString | Should -Match $VRC.Site
        }
      }

      # Finding CallRestriction
      if ( $VRC.ContainsKey('CallRestriction') ) {
        if ( $VRC.CallRestriction -ne 'Unrestricted' ) {
          $ProtoString | Should -Match $VRC.CallRestriction
        }
        elseif ( $VRC.CallRestriction -eq 'Custom' ) {
          $ProtoString | Should -Match $VRC.CustomRestrictionName
        }
        else {
          $ProtoString | Should -Not -Match $VRC.CallRestriction
        }
      }

      # Finding Prefix
      # This cannot be tested on the Output as 'OVP' may be part of a word.
      #endregion
    }
  }
}