Skip to content

Commit 809953f

Browse files
committed
Enhance SqlAgentAlert validation logic in AssertProperties method and update tests for parameter checks
1 parent 8fceb2a commit 809953f

File tree

2 files changed

+139
-14
lines changed

2 files changed

+139
-14
lines changed

source/Classes/020.SqlAgentAlert.ps1

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,42 @@ class SqlAgentAlert : SqlResourceBase
169169

170170
hidden [void] AssertProperties([System.Collections.Hashtable] $properties)
171171
{
172-
# TODO: Waiting for issue: https://github.com/dsccommunity/DscResource.Common/issues/160
173-
if ($properties.Ensure -eq 'Present')
174-
{
175-
# Validate that at least one of Severity or MessageId is specified
176-
# TODO: Waiting for issue: https://github.com/dsccommunity/DscResource.Common/issues/161
177-
#Assert-BoundParameter -BoundParameterList $properties -AtLeastOneList @('Severity', 'MessageId')
172+
# Validate that at least one of Severity or MessageId is specified
173+
$assertAtLeastOneParams = @{
174+
BoundParameterList = $properties
175+
AtLeastOneList = @('Severity', 'MessageId')
176+
IfEqualParameterList = @{
177+
Ensure = 'Present'
178+
}
179+
}
178180

179-
# Validate that both Severity and MessageId are not specified
180-
Assert-BoundParameter -BoundParameterList $properties -MutuallyExclusiveList1 @('Severity') -MutuallyExclusiveList2 @('MessageId')
181+
Assert-BoundParameter @assertAtLeastOneParams
182+
183+
# Validate that both Severity and MessageId are not specified
184+
$assertMutuallyExclusiveParams = @{
185+
BoundParameterList = $properties
186+
MutuallyExclusiveList1 = @('Severity')
187+
MutuallyExclusiveList2 = @('MessageId')
188+
IfEqualParameterList = @{
189+
Ensure = 'Present'
190+
}
181191
}
182-
else
192+
193+
Assert-BoundParameter @assertMutuallyExclusiveParams
194+
195+
if ($properties.Ensure -eq 'Absent')
183196
{
184197
# When Ensure is 'Absent', Severity and MessageId must not be set
198+
$assertAbsentParams = @{
199+
BoundParameterList = $properties
200+
NotAllowedList = @('Severity', 'MessageId')
201+
IfEqualParameterList = @{
202+
Ensure = 'Absent'
203+
}
204+
}
205+
206+
Assert-BoundParameter @assertAbsentParams
207+
185208
if ($properties.ContainsKey('Severity') -or $properties.ContainsKey('MessageId'))
186209
{
187210
$errorMessage = $this.localizedData.SqlAgentAlert_SeverityOrMessageIdNotAllowedWhenAbsent

tests/Unit/Classes/SqlAgentAlert.Tests.ps1

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
145145
$instance = [SqlAgentAlert] @{
146146
Name = 'TestAlert'
147147
InstanceName = 'MSSQLSERVER'
148+
Severity = 17
148149
} |
149150
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
150151
return $script:mockServerObject
@@ -168,6 +169,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
168169
$instance = [SqlAgentAlert] @{
169170
Name = 'TestAlert'
170171
InstanceName = 'MSSQLSERVER'
172+
Severity = 17
171173
} |
172174
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
173175
return $script:mockServerObject
@@ -340,14 +342,14 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
340342
}
341343

342344
Context 'When Ensure is Present' {
343-
It 'Should not throw when neither Severity nor MessageId are specified' {
345+
It 'Should throw when neither Severity nor MessageId are specified' {
344346
InModuleScope -ScriptBlock {
345347
$properties = @{
346348
Name = 'TestAlert'
347349
Ensure = 'Present'
348350
}
349351

350-
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Not -Throw
352+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*(DRC0052)*'
351353
}
352354
}
353355

@@ -406,7 +408,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
406408
Severity = 16
407409
}
408410

409-
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*Cannot specify Severity or MessageId when Ensure is set to ''Absent''*'
411+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*(DRC0053)*'
410412
}
411413
}
412414

@@ -418,7 +420,7 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
418420
MessageId = 50001
419421
}
420422

421-
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*Cannot specify Severity or MessageId when Ensure is set to ''Absent''*'
423+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*(DRC0053)*'
422424
}
423425
}
424426

@@ -431,7 +433,107 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
431433
MessageId = 50001
432434
}
433435

434-
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*Cannot specify Severity or MessageId when Ensure is set to ''Absent''*'
436+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw -ExpectedMessage '*(DRC0053)*'
437+
}
438+
}
439+
}
440+
}
441+
442+
Context 'When validating Assert-BoundParameter calls' {
443+
BeforeAll {
444+
InModuleScope -ScriptBlock {
445+
$script:mockSqlAgentAlertInstance = [SqlAgentAlert]::new()
446+
}
447+
}
448+
449+
Context 'When Ensure is Present' {
450+
It 'Should call Assert-BoundParameter to validate at least one of Severity or MessageId is specified' {
451+
InModuleScope -ScriptBlock {
452+
Mock -CommandName 'Assert-BoundParameter' -MockWith { }
453+
454+
$properties = @{
455+
Name = 'TestAlert'
456+
Ensure = 'Present'
457+
Severity = 16
458+
}
459+
460+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Not -Throw
461+
462+
Should -Invoke -CommandName 'Assert-BoundParameter' -ParameterFilter {
463+
$BoundParameterList -is [hashtable] -and
464+
$AtLeastOneList -contains 'Severity' -and
465+
$AtLeastOneList -contains 'MessageId' -and
466+
$IfEqualParameterList.Ensure -eq 'Present'
467+
} -Exactly -Times 1 -Scope It
468+
}
469+
}
470+
471+
It 'Should call Assert-BoundParameter to validate Severity and MessageId are mutually exclusive' {
472+
InModuleScope -ScriptBlock {
473+
Mock -CommandName 'Assert-BoundParameter' -MockWith { }
474+
475+
$properties = @{
476+
Name = 'TestAlert'
477+
Ensure = 'Present'
478+
Severity = 16
479+
}
480+
481+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Not -Throw
482+
483+
Should -Invoke -CommandName 'Assert-BoundParameter' -ParameterFilter {
484+
$BoundParameterList -is [hashtable] -and
485+
$MutuallyExclusiveList1 -contains 'Severity' -and
486+
$MutuallyExclusiveList2 -contains 'MessageId' -and
487+
$IfEqualParameterList.Ensure -eq 'Present'
488+
} -Exactly -Times 1 -Scope It
489+
}
490+
}
491+
}
492+
493+
Context 'When Ensure is Absent' {
494+
It 'Should call Assert-BoundParameter to validate Severity and MessageId are not allowed' {
495+
InModuleScope -ScriptBlock {
496+
Mock -CommandName 'Assert-BoundParameter' -MockWith { }
497+
498+
$properties = @{
499+
Name = 'TestAlert'
500+
Ensure = 'Absent'
501+
}
502+
503+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Not -Throw
504+
505+
Should -Invoke -CommandName 'Assert-BoundParameter' -ParameterFilter {
506+
$BoundParameterList -is [hashtable] -and
507+
$NotAllowedList -contains 'Severity' -and
508+
$NotAllowedList -contains 'MessageId' -and
509+
$IfEqualParameterList.Ensure -eq 'Absent'
510+
} -Exactly -Times 1 -Scope It
511+
}
512+
}
513+
514+
It 'Should call Assert-BoundParameter with correct parameters when Severity is specified' {
515+
InModuleScope -ScriptBlock {
516+
Mock -CommandName 'Assert-BoundParameter' -MockWith {
517+
# Simulate the Assert-BoundParameter throwing an exception for NotAllowed parameters
518+
if ($NotAllowedList -and ($BoundParameterList.ContainsKey('Severity') -or $BoundParameterList.ContainsKey('MessageId'))) {
519+
throw 'Parameter validation failed'
520+
}
521+
}
522+
523+
$properties = @{
524+
Name = 'TestAlert'
525+
Ensure = 'Absent'
526+
Severity = 16
527+
}
528+
529+
{ $script:mockSqlAgentAlertInstance.AssertProperties($properties) } | Should -Throw
530+
531+
Should -Invoke -CommandName 'Assert-BoundParameter' -ParameterFilter {
532+
$BoundParameterList -is [hashtable] -and
533+
$NotAllowedList -contains 'Severity' -and
534+
$NotAllowedList -contains 'MessageId' -and
535+
$IfEqualParameterList.Ensure -eq 'Absent'
536+
} -Exactly -Times 1 -Scope It
435537
}
436538
}
437539
}

0 commit comments

Comments
 (0)