src/functions/AdvFindConfigUtilities.Tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

# Global constants
$global:PathToConfig = "TestDrive:\config.json"
$global:config = New-Object psobject -Property @{
  JobName     = "ImportantJob";
  CrmInstance = "CRMRECRUIT";
  ViewName    = "TestView1";
  IsUserView  = $true;
  ExportPath  = "TestDrive:\TestView.json";
}
# Convenience methods for tests
function Add-OneConfig {
  Add-RegentExportConfig `
    -PathToConfig  $PathToConfig `
    -JobName $config.JobName `
    -CrmInstance $config.CrmInstance `
    -ViewName $config.ViewName `
    -IsUserView $config.IsUserView `
    -ExportPath $config.ExportPath
}

function Add-ThreeConfigs {
  for ($i = 0; $i -lt 3; $i++) {
    Add-RegentExportConfig `
      -PathToConfig  $PathToConfig `
      -JobName ($config.JobName + "$i") `
      -CrmInstance $config.CrmInstance `
      -ViewName $config.ViewName `
      -IsUserView $config.IsUserView `
      -ExportPath $config.ExportPath
  }
}

Function Assert-PropertiesMatch {
  Param ($Object)
  Process {
    foreach ($prop in ($Object.psobject.properties.name)) {
      try {
        if ($prop -eq "JobName") {
          [string]$Object."$prop".StartsWith($config."$prop") | Should -Be $true
        }
        else {
          [string]$Object."$prop" | Should -Be $config."$prop"
        }
      }
      catch {
        Write-Host -ForegroundColor Yellow "Property check failed at $prop"
      }
    }
  }
}




Describe "Add-RegentExportConfig" {
  Context "Working with a new config file" {

    It "Should Add a config file" {
      Add-OneConfig

      $result = (Get-Content $PathToConfig | ConvertFrom-Json) 
      
      Assert-PropertiesMatch -Object $result
    }

    It "Should default the config path to the current directory" {
      Push-Location "TestDrive:"
      Add-RegentExportConfig `
        -JobName $config.JobName `
        -CrmInstance $config.CrmInstance `
        -ViewName $config.ViewName `
        -IsUserView $config.IsUserView `
        -ExportPath $config.ExportPath
      Test-Path "TestDrive:\AdvancedFindExport.config.json" | Should -Be $true
      Pop-Location
    }
  }
  Context "Adding to a pre-existing config file" {
    It "Should append to an existing file if there is one (NOTE! Depends on previous test running first)" {
      Add-ThreeConfigs
      $result = (Get-Content $PathToConfig | ConvertFrom-Json)
      $result.Length | Should -Be 3
      $result | Assert-PropertiesMatch
    }
    It "Should throw an error if the specified Job name already exists" {
      Add-OneConfig
      {Add-OneConfig} | Should  -Throw
    }
  }
}

Describe "Get-RegentExportConfig" {
  Context "Given the path provided doesn't exist" {
    It "Should throw an error" {
      { Get-RegentExportConfig -PathToConfig "I:\Dont\exist.json" } `
        | Should -Throw
    }
  }

  Context "Given no JobName parameter" {
    Add-ThreeConfigs
    It "Should return all configs" {
      $result = Get-RegentExportConfig -PathToConfig $PathToConfig
      $result.Length | Should -Be 3
      $result | Assert-PropertiesMatch
    }
  }
  
  Context "Given a JobName paramter" {
    Add-ThreeConfigs
    It "Should only return that Job" {
      $result = Get-RegentExportConfig -JobName "ImportantJob2" -PathToConfig $PathToConfig
      $result | Assert-PropertiesMatch
    }
    It "Should return null when the specified Job doesn't exist" {
      $result = Get-RegentExportConfig -JobName "IDontExist" -PathToConfig $PathToConfig
      $result | Should -Be $null
    }
  }
}

Describe "Remove-RegentExportConfig" {

  Context "Given the path provided doesn't exist" {
    It "Should throw an error" {
      { Get-RegentExportConfig -PathToConfig "I:\Dont\exist.json" } `
        | Should -Throw
    }
  }
  Context "Working With a pre-existing config file" {
    Add-ThreeConfigs
    It "Should remove the specified job from the config file" {
      Remove-RegentExportConfig -JobName "ImportantJob0" -PathToConfig $PathToConfig
      Remove-RegentExportConfig "ImportantJob1" -PathToConfig $PathToConfig

      $result = Get-RegentExportConfig -PathToConfig $PathToConfig
      # There should only be one result left, since we removed the other two
      $result.JobName | Should -Be "ImportantJob2"
    }
  }
}