. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" Describe "Get-NewRelicObjects" { $apiKey = "this key is garbage" Mock -ModuleName $moduleForMock Get-LogLeadName -MockWith { return "[UUT]" } Mock -ModuleName $moduleForMock Write-Warning -MockWith { } Mock -ModuleName $moduleForMock Get-NewRelicNextLink -MockWith { } Context "Gets an empty response from NR if no data returned" { Mock -ModuleName $moduleForMock Invoke-WebRequest -MockWith { $response = New-MockObject -Type ([Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject]) $content = '{"applications": []}' $statusCode = 200 $response | Add-Member -NotePropertyName Content -NotePropertyValue $content -Force $response | Add-Member -NotePropertyName StatusCode -NotePropertyValue $statusCode -Force return $response } It "is empty" { $applications = (Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json" -ObjectKey "applications" -FilterKey "name" -FilterValue "Some.Package.Id").applications $applications | Should -BeNullOrEmpty } } Context "Gets a response with values from NR if some data returned" { Mock -ModuleName $moduleForMock Invoke-WebRequest -MockWith { $response = New-MockObject -Type ([Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject]) $content = @" {"applications": [ {"id":1234,"name":"Unit Test Environment sample application"}, {"id":5678,"name":"Unit Test Environment Alkami.Microservice Test 1"}, {"id":1010,"name":"Unit Test Environment Alkami.Microservice Test 2"}, {"id":9876,"name":"Fake Pod 12 Alkami.Microservice Test 3"}, {"id":8765,"name":"Fake Pod 12.1 Alkami.Microservice Test 4"} ]} "@ $statusCode = 200 $response | Add-Member -NotePropertyName Content -NotePropertyValue $content -Force $response | Add-Member -NotePropertyName StatusCode -NotePropertyValue $statusCode -Force return $response } $applications = (Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json" -ObjectKey "applications").applications It "is not empty" { $applications | Should -Not -BeNullOrEmpty } It "has 5 elements" { $applications | Should -HaveCount 5 } } Context "Gets a response with values from NR if some data returned" { Mock -ModuleName $moduleForMock Invoke-WebRequest -MockWith { $response = New-MockObject -Type ([Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject]) $content = @" {"applications": [ {"id":1234,"name":"Unit Test Environment 1 sample application"}, {"id":5678,"name":"Unit Test Environment 1 Alkami.Microservice Test 1"}, {"id":1010,"name":"Unit Test Environment 1 Alkami.Microservice Test 2"}, {"id":1234,"name":"Unit Test Environment 12 sample application"}, {"id":5678,"name":"Unit Test Environment 12 Alkami.Microservice Test 1"}, {"id":1010,"name":"Unit Test Environment 12 Alkami.Microservice Test 2"} ]} "@ $statusCode = 200 $response | Add-Member -NotePropertyName Content -NotePropertyValue $content -Force $response | Add-Member -NotePropertyName StatusCode -NotePropertyValue $statusCode -Force return $response } $applications = (Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json" -ObjectKey "applications" -FilterKey "name" -FilterValue "Unit Test Environment 1").applications It "is not empty" { $applications | Should -Not -BeNullOrEmpty } It "has 3 filtered elements" { $applications | Should -HaveCount 3 } } }