Tests/Unit/MSFT_MsiPackage.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'

Describe 'MsiPackage Unit Tests' {
    BeforeAll {
        # Import CommonTestHelper
        $testsFolderFilePath = Split-Path $PSScriptRoot -Parent
        $testHelperFolderFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'TestHelpers'
        $commonTestHelperFilePath = Join-Path -Path $testHelperFolderFilePath -ChildPath 'CommonTestHelper.psm1'
        Import-Module -Name $commonTestHelperFilePath

        $script:testEnvironment = Enter-DscResourceTestEnvironment `
            -DscResourceModuleName 'PSDscResources' `
            -DscResourceName 'MSFT_MsiPackage' `
            -TestType 'Unit'
    }

    AfterAll {
        Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
    }

    InModuleScope 'MSFT_MsiPackage' {
        $script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
        $testHelperFolderFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'TestHelpers'
        $script:commonTestHelperFilePath = Join-Path -Path $testHelperFolderFilePath -ChildPath 'CommonTestHelper.psm1'

        # This must be imported again within the InModuleScope so that the helper functions can be accessed
        Import-Module -Name $commonTestHelperFilePath

        $testUsername = 'TestUsername'
        $testPassword = 'TestPassword'
        $secureTestPassword = ConvertTo-SecureString -String $testPassword -AsPlainText -Force

        $script:testCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @( $testUsername, $secureTestPassword )
        $script:testProductId = '{deadbeef-80c6-41e6-a1b9-8bdb8a05027f}'
        $script:testIdentifyingNumber = '{DEADBEEF-80C6-41E6-A1B9-8BDB8A05027F}'
        $script:testWrongProductId = 'wrongId'
        $script:testPath = 'file://test.msi'
        $script:destinationPath = Join-Path -Path $script:packageCacheLocation -ChildPath 'C:\'
        $script:testUriHttp = [Uri] 'http://test.msi'
        $script:testUriHttps = [Uri] 'https://test.msi'
        $script:testUriFile = [Uri] 'file://test.msi'
        $script:testUriNonUnc = [Uri] 'file:///C:/test.msi'
        $script:testUriQuery = [Uri] 'http://C:/directory/test/test.msi?sv=2017-01-31&spr=https'
        $script:testUriOnlyFile = [Uri] 'test.msi'

        $script:mockStream = New-MockObject -Type 'System.IO.FileStream'
        $script:mockWebRequest = New-MockObject -Type 'System.Net.HttpWebRequest'
        $script:mockStartInfo = New-MockObject -Type 'System.Diagnostics.ProcessStartInfo'
        $script:mockProcess = New-MockObject -Type 'System.Diagnostics.Process'
        $script:mockProductEntry = New-MockObject -Type 'Microsoft.Win32.RegistryKey'

        $script:mockPSDrive = @{
            Root = 'mockRoot'
        }

        $script:mockProductEntryInfo = @{
            Name = 'TestDisplayName'
            InstallSource = 'TestInstallSource'
            InstalledOn = ([DateTime]::new(2017, 4, 24).ToShortDateString())
            Size = 2048
            Version = '1.2.3.4'
            PackageDescription = 'Test Description'
            Publisher = 'Test Publisher'
            Ensure = 'Present'
        }

        # Used to create the names of the tests that check to ensure the correct error is thrown.
        $script:errorMessageTitles = @{
            CouldNotOpenLog = 'not being able to open the log path'
            InvalidId = 'the specified product ID not matching the actual product ID'
            CouldNotOpenDestFile = 'not being able to open the destination file to write to'
            PathDoesNotExist = 'not being able to find the path'
            CouldNotStartProcess = 'not being able to start the process'
            PostValidationError = 'not being able to find the package after installation'
        }

        Describe 'Get-TargetResource' {
            Mock -CommandName 'Convert-ProductIdToIdentifyingNumber' -MockWith { return $script:testIdentifyingNumber }
            Mock -CommandName 'Get-ProductEntry' -MockWith { return $null }
            Mock -CommandName 'Get-ProductEntryInfo' -MockWith { return $script:mockProductEntryInfo }

            Context 'MSI package does not exist' {
                $getTargetResourceParameters = @{
                    ProductId = $script:testProductId
                    Path = $script:testPath
                }

                $mocksCalled = @(
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Get-ProductEntryInfo'; Times = 0 }
                )

                $expectedReturnValue = @{
                    Ensure = 'Absent'
                    ProductId = $script:testIdentifyingNumber
                }

                Invoke-GetTargetResourceUnitTest -GetTargetResourceParameters $getTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ExpectedReturnValue $expectedReturnValue
            }

            Mock -CommandName 'Get-ProductEntry' -MockWith { return $script:mockProductEntry }

            Context 'MSI package does exist' {
                $getTargetResourceParameters = @{
                    ProductId = $script:testProductId
                    Path = $script:testPath
                }

                $mocksCalled = @(
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Get-ProductEntryInfo'; Times = 1 }
                )

                $expectedReturnValue = $script:mockProductEntryInfo

                Invoke-GetTargetResourceUnitTest -GetTargetResourceParameters $getTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ExpectedReturnValue $expectedReturnValue
            }
        }
           
        Describe 'Set-TargetResource' {
            $setTargetResourceParameters = @{
                ProductId = 'TestProductId'
                Path = $script:testPath
                Ensure = 'Present'
                Arguments = 'TestArguments'
                LogPath = 'TestLogPath'
                FileHash = 'TestFileHash'
                HashAlgorithm = 'Sha256'
                SignerSubject = 'TestSignerSubject'
                SignerThumbprint = 'TestSignerThumbprint'
                ServerCertificateValidationCallback = 'TestValidationCallback'
                RunAsCredential = $script:testCredential
            }
            
            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriNonUnc }
            Mock -CommandName 'Convert-ProductIdToIdentifyingNumber' -MockWith { return $script:testIdentifyingNumber }
            Mock -CommandName 'Assert-PathExtensionValid' -MockWith {}
            Mock -CommandName 'New-LogFile' -MockWith {}
            Mock -CommandName 'New-PSDrive' -MockWith { return $script:mockPSDrive }
            Mock -CommandName 'Test-Path' -MockWith { return $true }
            Mock -CommandName 'New-Item' -MockWith {}
            Mock -CommandName 'New-Object' -MockWith { return $script:mockStream } #-ParameterFilter { $TypeName -eq 'System.IO.FileStream' }
            Mock -CommandName 'Get-WebRequestResponse' -MockWith { return $script:mockStream }
            Mock -CommandName 'Copy-ResponseStreamToFileStream' -MockWith {}
            Mock -CommandName 'Close-Stream' -MockWith {}
            Mock -CommandName 'Assert-FileValid' -MockWith {}
            Mock -CommandName 'Get-MsiProductCode' -MockWith { return $script:testIdentifyingNumber }
            Mock -CommandName 'Start-MsiProcess' -MockWith { return 0 } # returns the exit code
            Mock -CommandName 'Remove-PSDrive' -MockWith {}
            Mock -CommandName 'Remove-Item' -MockWith {}
            Mock -CommandName 'Invoke-CimMethod' -MockWith {}
            Mock -CommandName 'Get-ItemProperty' -MockWith { return $null }
            Mock -CommandName 'Get-ProductEntry' -MockWith { return $script:mockProductEntry }
            
            Context 'Uri scheme is non-UNC file and installation succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Convert-PathToUri'; Times = 1 }
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Assert-PathExtensionValid'; Times = 1 }
                    @{ Command = 'New-LogFile'; Times = 1 }
                    @{ Command = 'New-PSDrive'; Times = 0 }
                    @{ Command = 'Get-WebRequestResponse'; Times = 0 }
                    @{ Command = 'Test-Path'; Times = 1; Custom = 'to the MSI file' }
                    @{ Command = 'Assert-FileValid'; Times = 1 }
                    @{ Command = 'Get-MsiProductCode'; Times = 1 }
                    @{ Command = 'Start-MsiProcess'; Times = 1 }
                    @{ Command = 'Remove-PSDrive'; Times = 0 }
                    @{ Command = 'Remove-Item'; Times = 0; Custom = 'the downloaded file from the http server' }
                    @{ Command = 'Invoke-CimMethod'; Times = 1 }
                    @{ Command = 'Get-ItemProperty'; Times = 1; Custom = 'the registry data for pending file rename operations' }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                )

                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ShouldThrow $false
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriFile }
            $setTargetResourceParameters.Ensure = 'Present'

            Context 'Uri scheme is UNC file and installation succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Convert-PathToUri'; Times = 1 }
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Assert-PathExtensionValid'; Times = 1 }
                    @{ Command = 'New-LogFile'; Times = 1 }
                    @{ Command = 'New-PSDrive'; Times = 1 }
                    @{ Command = 'Get-WebRequestResponse'; Times = 0 }
                    @{ Command = 'Test-Path'; Times = 1; Custom = 'to the MSI file' }
                    @{ Command = 'Assert-FileValid'; Times = 1 }
                    @{ Command = 'Get-MsiProductCode'; Times = 1 }
                    @{ Command = 'Start-MsiProcess'; Times = 1 }
                    @{ Command = 'Remove-PSDrive'; Times = 1 }
                    @{ Command = 'Remove-Item'; Times = 0; Custom = 'the downloaded file from the http server' }
                    @{ Command = 'Invoke-CimMethod'; Times = 1 }
                    @{ Command = 'Get-ItemProperty'; Times = 1; Custom = 'the registry data for pending file rename operations' }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                )

                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ShouldThrow $false
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriHttp }

            Context 'Uri scheme is Http and installation succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Convert-PathToUri'; Times = 1 }
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Assert-PathExtensionValid'; Times = 1 }
                    @{ Command = 'New-LogFile'; Times = 1 }
                    @{ Command = 'New-PSDrive'; Times = 0 }
                    @{ Command = 'Test-Path'; Times = 2; Custom = 'to the package cache' }
                    @{ Command = 'New-Item'; Times = 0; Custom = 'directory for the package cache' }
                    @{ Command = 'New-Object'; Times = 1; Custom = 'file stream to copy the response to' }
                    @{ Command = 'Get-WebRequestResponse'; Times = 1 }
                    @{ Command = 'Copy-ResponseStreamToFileStream'; Times = 1 }
                    @{ Command = 'Close-Stream'; Times = 2 }
                    @{ Command = 'Test-Path'; Times = 2; Custom = 'to the MSI file' }
                    @{ Command = 'Assert-FileValid'; Times = 1 }
                    @{ Command = 'Get-MsiProductCode'; Times = 1 }
                    @{ Command = 'Start-MsiProcess'; Times = 1 }
                    @{ Command = 'Remove-PSDrive'; Times = 0 }
                    @{ Command = 'Remove-Item'; Times = 1; Custom = 'the directory used for the package cache' }
                    @{ Command = 'Invoke-CimMethod'; Times = 1 }
                    @{ Command = 'Get-ItemProperty'; Times = 1; Custom = 'the registry data for pending file rename operations' }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                )

                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ShouldThrow $false
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriHttps }

            Context 'Uri scheme is Https and installation succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Convert-PathToUri'; Times = 1 }
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Assert-PathExtensionValid'; Times = 1 }
                    @{ Command = 'New-LogFile'; Times = 1 }
                    @{ Command = 'New-PSDrive'; Times = 0 }
                    @{ Command = 'Test-Path'; Times = 2; Custom = 'to the package cache' }
                    @{ Command = 'New-Item'; Times = 0; Custom = 'directory for the package cache' }
                    @{ Command = 'New-Object'; Times = 1; Custom = 'file stream to copy the response to' }
                    @{ Command = 'Get-WebRequestResponse'; Times = 1 }
                    @{ Command = 'Copy-ResponseStreamToFileStream'; Times = 1 }
                    @{ Command = 'Close-Stream'; Times = 2 }
                    @{ Command = 'Test-Path'; Times = 2; Custom = 'to the MSI file' }
                    @{ Command = 'Assert-FileValid'; Times = 1 }
                    @{ Command = 'Get-MsiProductCode'; Times = 1 }
                    @{ Command = 'Start-MsiProcess'; Times = 1 }
                    @{ Command = 'Remove-PSDrive'; Times = 0 }
                    @{ Command = 'Remove-Item'; Times = 1; Custom = 'the directory used for the package cache' }
                    @{ Command = 'Invoke-CimMethod'; Times = 1 }
                    @{ Command = 'Get-ItemProperty'; Times = 1; Custom = 'the registry data for pending file rename operations' }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                )

                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ShouldThrow $false
            }

            $setTargetResourceParameters.Ensure = 'Absent'

            # The URI scheme doesn't matter for uninstallation - it will always do the same thing
            Context 'Uninstallation succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Convert-PathToUri'; Times = 1 }
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Assert-PathExtensionValid'; Times = 1 }
                    @{ Command = 'New-LogFile'; Times = 1 }
                    @{ Command = 'New-PSDrive'; Times = 0 }
                    @{ Command = 'Get-WebRequestResponse'; Times = 0 }
                    @{ Command = 'Test-Path'; Times = 0; Custom = 'to the MSI file' }
                    @{ Command = 'Assert-FileValid'; Times = 0 }
                    @{ Command = 'Get-MsiProductCode'; Times = 0 }
                    @{ Command = 'Start-MsiProcess'; Times = 1 }
                    @{ Command = 'Remove-PSDrive'; Times = 0 }
                    @{ Command = 'Remove-Item'; Times = 0; Custom = 'the downloaded file from the http server' }
                    @{ Command = 'Invoke-CimMethod'; Times = 1 }
                    @{ Command = 'Get-ItemProperty'; Times = 1; Custom = 'the registry data for pending file rename operations' }
                    @{ Command = 'Get-ProductEntry'; Times = 0 }
                )

                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled $mocksCalled `
                                             -ShouldThrow $false
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriQuery }

            Context 'Path is a query path' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -ShouldThrow $false

                It 'Should assert that the file without the query string has a valid extension' {
                    Assert-MockCalled -CommandName 'Assert-PathExtensionValid' -Exactly 1 -Scope 'Context' -ParameterFilter { $Path -eq (Split-Path -Path $script:testuriQuery.LocalPath -Leaf) }
                }
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriOnlyFile }

            Context 'Converted URI does not have a local path' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -ShouldThrow $false

                It 'Should assert that the original path has a valid extension' {
                    Assert-MockCalled -CommandName 'Assert-PathExtensionValid' -Exactly 1 -Scope 'Context' -ParameterFilter { $Path -eq $script:testPath }
                }
            }

            $setTargetResourceParameters.Remove('LogPath')

            Context 'Converted URI does not have a local path' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled @(@{ Command = 'New-LogFile'; Times = 0 }) `
                                             -ShouldThrow $false
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriHttp }
            Mock -CommandName 'Test-Path' -MockWith { return $false } -ParameterFilter { $Path -eq $script:packageCacheLocation }
            $setTargetResourceParameters.Ensure = 'Present'

            Context 'URI scheme is Http and package cache location does not exist yet' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -MocksCalled @(@{ Command = 'New-Item'; Times = 1; Custom = 'directory for the package cache' }) `
                                             -ShouldThrow $false
            }

            # Error Tests

            Mock -CommandName 'Get-ProductEntry' -MockWith { return $null }

            Context 'Package could not be found after installation' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -ShouldThrow $true `
                                             -ErrorMessage ($script:localizedData.PostValidationError -f $setTargetResourceParameters.Path) `
                                             -ErrorTestName $script:errorMessageTitles.PostValidationError
            }
            
            Mock -CommandName 'Get-MsiProductCode' -MockWith { return $script:testWrongProductId }

            Context 'Product code from downloaded MSI package does not match specified ID' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -ShouldThrow $true `
                                             -ErrorMessage ($script:localizedData.InvalidId -f $script:testIdentifyingNumber, $script:testWrongProductId) `
                                             -ErrorTestName $script:errorMessageTitles.InvalidId
            }

            Mock -CommandName 'New-Object' -MockWith { Throw }

            Context 'Failure while creating the file stream object to download to' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -ShouldThrow $true `
                                             -ErrorMessage ($script:localizedData.CouldNotOpenDestFile -f $script:destinationPath) `
                                             -ErrorTestName $script:errorMessageTitles.CouldNotOpenDestFile
            }

            Mock -CommandName 'Convert-PathToUri' -MockWith { return $script:testUriNonUnc }
            Mock -CommandName 'Test-Path' -MockWith { return $false } -ParameterFilter { $Path -eq $script:testPath }

            Context 'Invalid path was passed in' {
                Invoke-SetTargetResourceUnitTest -SetTargetResourceParameters $setTargetResourceParameters `
                                             -ShouldThrow $true `
                                             -ErrorMessage ($script:localizedData.PathDoesNotExist -f $script:testPath) `
                                             -ErrorTestName $script:errorMessageTitles.PathDoesNotExist
            }
        }

        Describe 'Test-TargetResource' {
            Mock -CommandName 'Convert-ProductIdToIdentifyingNumber' -MockWith { return $script:testIdentifyingNumber }
            Mock -CommandName 'Get-ProductEntry' -MockWith { return $script:mockProductEntry }
            Mock -CommandName 'Get-ProductEntryValue' -MockWith { return $script:mockProductEntryInfo.Name }

            Context 'Specified package is present and should be' {
                $testTargetResourceParameters = @{
                    ProductId = $script:testProductId
                    Path = $script:testPath
                    Ensure = 'Present'
                }

                $mocksCalled = @(
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Get-ProductEntryValue'; Times = 1 }
                )

                Invoke-TestTargetResourceUnitTest -TestTargetResourceParameters $testTargetResourceParameters `
                                              -MocksCalled $mocksCalled `
                                              -ExpectedReturnValue $true
            }

            Context 'Specified package is present but should not be' {
                $testTargetResourceParameters = @{
                    ProductId = $script:testProductId
                    Path = $script:testPath
                    Ensure = 'Absent'
                }

                $mocksCalled = @(
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Get-ProductEntryValue'; Times = 1 }
                )

                Invoke-TestTargetResourceUnitTest -TestTargetResourceParameters $testTargetResourceParameters `
                                              -MocksCalled $mocksCalled `
                                              -ExpectedReturnValue $false
            }

            Mock -CommandName 'Get-ProductEntry' -MockWith { return $null }

            Context 'Specified package is Absent but should not be' {
                $testTargetResourceParameters = @{
                    ProductId = $script:testProductId
                    Path = $script:testPath
                    Ensure = 'Present'
                }

                $mocksCalled = @(
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Get-ProductEntryValue'; Times = 0 }
                )

                Invoke-TestTargetResourceUnitTest -TestTargetResourceParameters $testTargetResourceParameters `
                                              -MocksCalled $mocksCalled `
                                              -ExpectedReturnValue $false
            }

            Context 'Specified package is Absent and should be' {
                $testTargetResourceParameters = @{
                    ProductId = $script:testProductId
                    Path = $script:testPath
                    Ensure = 'Absent'
                }

                $mocksCalled = @(
                    @{ Command = 'Convert-ProductIdToIdentifyingNumber'; Times = 1 }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Get-ProductEntryValue'; Times = 0 }
                )

                Invoke-TestTargetResourceUnitTest -TestTargetResourceParameters $testTargetResourceParameters `
                                              -MocksCalled $mocksCalled `
                                              -ExpectedReturnValue $true
            }
        }

        Describe 'Assert-PathExtensionValid' {
            Context 'Path is a valid .msi path' {
                It 'Should not throw' {
                    { Assert-PathExtensionValid -Path 'testMsiFile.msi' } | Should Not Throw
                }
            }

            Context 'Path is not a valid .msi path' {
                It 'Should throw an invalid argument exception when an EXE file is passed in' {
                    $invalidPath = 'testMsiFile.exe'
                    $expectedErrorMessage = ($script:localizedData.InvalidBinaryType -f $invalidPath)

                    { Assert-PathExtensionValid -Path $invalidPath } | Should Throw $expectedErrorMessage
                }

                It 'Should throw an invalid argument exception when an invalid file type is passed in' {
                    $invalidPath = 'testMsiFilemsi'
                    $expectedErrorMessage = ($script:localizedData.InvalidBinaryType -f $invalidPath)

                    { Assert-PathExtensionValid -Path $invalidPath } | Should Throw $expectedErrorMessage
                }
            }
        }

        Describe 'Convert-PathToUri' {
            Context 'Path has a valid URI scheme' {
                It 'Should return the expected URI when scheme is a file' {
                    $filePath = (Join-Path -Path $PSScriptRoot -ChildPath 'testMsi.msi')
                    $expectedReturnValue = [Uri] $filePath

                    Convert-PathToUri -Path $filePath | Should Be $expectedReturnValue
                }
                
                It 'Should return the expected URI when scheme is http' {
                    $filePath = 'http://localhost:1242/testMsi.msi'
                    $expectedReturnValue = [Uri] $filePath

                    Convert-PathToUri -Path $filePath | Should Be $expectedReturnValue
                }

                It 'Should return the expected URI when scheme is https' {
                    $filePath = 'https://localhost:1243/testMsi.msi'
                    $expectedReturnValue = [Uri] $filePath

                    Convert-PathToUri -Path $filePath | Should Be $expectedReturnValue
                }
            }

            Context 'Invalid path passed in' {
                It 'Should throw an error when uri scheme is invalid' {
                    $filePath = 'ht://localhost:1243/testMsi.msi'
                    $expectedErrorMessage = ($script:localizedData.InvalidPath -f $filePath)

                    { Convert-PathToUri -Path $filePath } | Should Throw $expectedErrorMessage
                }

                It 'Should throw an error when path is not in valid format' {
                    $filePath = 'mri'
                    $expectedErrorMessage = ($script:localizedData.InvalidPath -f $filePath)

                    { Convert-PathToUri -Path $filePath } | Should Throw $expectedErrorMessage
                }
            }
        }

        Describe 'Convert-ProductIdToIdentifyingNumber' {
            Context 'Valid Product ID is passed in' {
                It 'Should return the same value that is passed in when the Product ID is already in the correct format' {
                    Convert-ProductIdToIdentifyingNumber -ProductId $script:testIdentifyingNumber | Should Be $script:testIdentifyingNumber
                }

                It 'Should convert a valid poduct ID to the identifying number format' {
                    Convert-ProductIdToIdentifyingNumber -ProductId $script:testProductId | Should Be $script:testIdentifyingNumber
                }
            }

            Context 'Invalid Product ID is passed in' {
                It 'Should throw an exception when an invalid product ID is passed in' {
                    $expectedErrorMessage = ($script:localizedData.InvalidIdentifyingNumber -f $script:testWrongProductId)
                    { Convert-ProductIdToIdentifyingNumber -ProductId $script:testWrongProductId } | Should Throw $expectedErrorMessage
                }
            }
        }

        Describe 'Get-ProductEntry' {
            $uninstallRegistryKeyLocation = (Join-Path -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall' -ChildPath $script:testIdentifyingNumber)
            $uninstallRegistryKeyWow64Location = (Join-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' -ChildPath $script:testIdentifyingNumber)

            Mock -CommandName 'Get-Item' -MockWith { return $script:mockProductEntry } -ParameterFilter { $Path -eq $uninstallRegistryKeyLocation }
            Mock -CommandName 'Get-Item' -MockWith { return $script:mockProductEntry } -ParameterFilter { $Path -eq $uninstallRegistryKeyWow64Location }

            Context 'Product entry is found in the expected location' {
                It 'Should return the expected product entry' {
                    Get-ProductEntry -IdentifyingNumber $script:testIdentifyingNumber | Should Be $script:mockProductEntry
                }

                It 'Should retrieve the item' {
                    Assert-MockCalled -CommandName 'Get-Item' -Exactly 1 -Scope 'Context'
                }
            }

            Mock -CommandName 'Get-Item' -MockWith { return $null } -ParameterFilter { $Path -eq $uninstallRegistryKeyLocation }

            Context 'Product entry is found under Wow6432Node' {
                It 'Should return the expected product entry' {
                    Get-ProductEntry -IdentifyingNumber $script:testIdentifyingNumber | Should Be $script:mockProductEntry
                }

                It 'Should attempt to retrieve the item twice' {
                    Assert-MockCalled -CommandName 'Get-Item' -Exactly 2 -Scope 'Context'
                }
            }

            Mock -CommandName 'Get-Item' -MockWith { return $null } -ParameterFilter { $Path -eq $uninstallRegistryKeyWow64Location }

            Context 'Product entry is not found' {
                It 'Should return $null' {
                    Get-ProductEntry -IdentifyingNumber $script:testIdentifyingNumber | Should Be $null
                }

                It 'Should attempt to retrieve the item twice' {
                    Assert-MockCalled -CommandName 'Get-Item' -Exactly 2 -Scope 'Context'
                }
            }
        }

        Describe 'Get-ProductEntryInfo' {
            Mock -CommandName Get-ProductEntryValue -MockWith { return '20170424' } -ParameterFilter { $Property -eq 'InstallDate' }
            Mock -CommandName Get-ProductEntryValue -MockWith { return $script:mockProductEntryInfo.Publisher } -ParameterFilter { $Property -eq 'Publisher' }
            Mock -CommandName Get-ProductEntryValue -MockWith { return $script:mockProductEntryInfo.Size } -ParameterFilter { $Property -eq 'EstimatedSize' }
            Mock -CommandName Get-ProductEntryValue -MockWith { return $script:mockProductEntryInfo.Version } -ParameterFilter { $Property -eq 'DisplayVersion' }
            Mock -CommandName Get-ProductEntryValue -MockWith { return $script:mockProductEntryInfo.PackageDescription } -ParameterFilter { $Property -eq 'Comments' }
            Mock -CommandName Get-ProductEntryValue -MockWith { return $script:mockProductEntryInfo.Name } -ParameterFilter { $Property -eq 'DisplayName' }
            Mock -CommandName Get-ProductEntryValue -MockWith { return $script:mockProductEntryInfo.InstallSource } -ParameterFilter { $Property -eq 'InstallSource' }

            Context 'All properties are retrieved successfully' {

                $getProductEntryInfoResult = Get-ProductEntryInfo -ProductEntry $script:mockProductEntry

                It 'Should return the expected installed date' {
                     $getProductEntryInfoResult.InstalledOn | Should Be $script:mockProductEntryInfo.InstalledOn
                }

                It 'Should return the expected publisher' {
                     $getProductEntryInfoResult.Publisher | Should Be $script:mockProductEntryInfo.Publisher
                }

                It 'Should return the expected size' {
                     $getProductEntryInfoResult.Size | Should Be ($script:mockProductEntryInfo.Size / 1024)
                }

                It 'Should return the expected Version' {
                     $getProductEntryInfoResult.Version | Should Be $script:mockProductEntryInfo.Version
                }

                It 'Should return the expected package description' {
                     $getProductEntryInfoResult.PackageDescription | Should Be $script:mockProductEntryInfo.PackageDescription
                }

                It 'Should return the expected name' {
                     $getProductEntryInfoResult.Name | Should Be $script:mockProductEntryInfo.Name
                }

                It 'Should return the expected install source' {
                     $getProductEntryInfoResult.InstallSource | Should Be $script:mockProductEntryInfo.InstallSource
                }

                It 'Should retrieve 7 product entry values' {
                    Assert-MockCalled -CommandName 'Get-ProductEntryValue' -Exactly 7 -Scope 'Context'
                }
            }

            Mock -CommandName Get-ProductEntryValue -MockWith { return '4/4/2017' } -ParameterFilter { $Property -eq 'InstallDate' }

            Context 'Install date is in incorrect format' {

                $getProductEntryInfoResult = Get-ProductEntryInfo -ProductEntry $script:mockProductEntry

                It 'Should return $null for InstalledOn' {
                    $getProductEntryInfoResult.InstalledOn | Should Be $null
                }
            }
        }

        Describe 'New-LogFile' {
            Mock -CommandName 'Test-Path' -MockWith { return $true }
            Mock -CommandName 'Remove-Item' -MockWith {}
            Mock -CommandName 'New-Item' -MockWith {}

            Context 'File with name of given log file already exists and creation of new log file succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Test-Path'; Times = 1; Custom = 'to the log file' }
                    @{ Command = 'Remove-Item'; Times = 1; Custom = 'any file with the same name as the log file' }
                    @{ Command = 'New-Item'; Times = 1; Custom = 'log file' }
                )

                Invoke-GenericUnitTest -Function { Param($script:testPath) New-LogFile $script:testPath } `
                                       -FunctionParameters @{ LogPath = $script:testPath } `
                                       -MocksCalled $mocksCalled `
                                       -ShouldThrow $false
            }

            Mock -CommandName 'Test-Path' -MockWith { return $false }

            Context 'File with name of given log file does not exist and creation of new log file succeeds' {
                $mocksCalled = @(
                    @{ Command = 'Test-Path'; Times = 1; Custom = 'to the log file' }
                    @{ Command = 'Remove-Item'; Times = 0; Custom = 'any file with the same name as the log file' }
                    @{ Command = 'New-Item'; Times = 1; Custom = 'log file' }
                )

                Invoke-GenericUnitTest -Function { Param($script:testPath) New-LogFile $script:testPath } `
                                       -FunctionParameters @{ LogPath = $script:testPath } `
                                       -MocksCalled $mocksCalled `
                                       -ShouldThrow $false
            }

            Mock -CommandName 'New-Item' -MockWith { Throw }

            Context 'Creation of new log file fails' {
                Invoke-GenericUnitTest -Function { Param($script:testPath) New-LogFile $script:testPath } `
                                       -FunctionParameters @{ LogPath = $script:testPath } `
                                       -ShouldThrow $true `
                                       -ErrorMessage ($script:localizedData.CouldNotOpenLog -f $script:testPath) `
                                       -ErrorTestName $script:errorMessageTitles.CouldNotOpenLog
            }
        }
        
        Describe 'Get-WebRequestResponse' {
            Mock -CommandName 'Get-WebRequest' -MockWith { return $script:mockWebRequest }
            Mock -CommandName 'Get-ScriptBlock' -MockWith { return { Write-Verbose 'Hello World' } }
            Mock -CommandName 'Get-WebRequestResponseStream' -MockWith { return $script:mockStream }

            Context 'URI scheme is Http and response is successfully retrieved' {
                $mocksCalled = @(
                    @{ Command = 'Get-WebRequest'; Times = 1 }
                    @{ Command = 'Get-ScriptBlock'; Times = 0 }
                    @{ Command = 'Get-WebRequestResponseStream'; Times = 1 }
                )

                It 'Should return the expected response stream' {
                    Get-WebRequestResponse -Uri $script:testUriHttp | Should Be $script:mockStream
                }
                
                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'URI scheme is Https with no callback and response is successfully retrieved' {
                $mocksCalled = @(
                    @{ Command = 'Get-WebRequest'; Times = 1 }
                    @{ Command = 'Get-ScriptBlock'; Times = 0 }
                    @{ Command = 'Get-WebRequestResponseStream'; Times = 1 }
                )

                It 'Should return the expected response stream' {
                    Get-WebRequestResponse -Uri $script:testUriHttps | Should Be $script:mockStream
                }
                
                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'URI scheme is Https with callback and response is successfully retrieved' {
                $mocksCalled = @(
                    @{ Command = 'Get-WebRequest'; Times = 1 }
                    @{ Command = 'Get-ScriptBlock'; Times = 1 }
                    @{ Command = 'Get-WebRequestResponseStream'; Times = 1 }
                )

                It 'Should return the expected response stream' {
                    Get-WebRequestResponse -Uri $script:testUriHttps -ServerCertificateValidationCallback 'TestCallbackFunction' | Should Be $script:mockStream
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Mock -CommandName 'Get-WebRequestResponseStream' -MockWith { Throw }

            Context 'Error occurred during while retrieving the response' {
                It 'Should throw the expected exception' {
                    $expectedErrorMessage = ($script:localizedData.CouldNotGetResponseFromWebRequest -f $script:testUriHttp.Scheme, $script:testUriHttp.OriginalString)
                    { Get-WebRequestResponse -Uri $script:testUriHttp } | Should Throw $expectedErrorMessage
                }
            }
        }

        Describe 'Assert-FileValid' {
            Mock -CommandName 'Assert-FileHashValid' -MockWith {}
            Mock -CommandName 'Assert-FileSignatureValid' -MockWith {}

            Context 'FileHash is passed in and SignerThumbprint and SignerSubject are not' {
                $mocksCalled = @(
                    @{ Command = 'Assert-FileHashValid'; Times = 1 }
                    @{ Command = 'Assert-FileSignatureValid'; Times = 0 }
                )

                It 'Should not throw' {
                    { Assert-FileValid -Path $script:testPath -FileHash 'mockFileHash' } | Should Not Throw
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'FileHash and SignerThumbprint are passed in but SignerSubject is not' {
                $mocksCalled = @(
                    @{ Command = 'Assert-FileHashValid'; Times = 1 }
                    @{ Command = 'Assert-FileSignatureValid'; Times = 1 }
                )

                It 'Should not throw' {
                    { Assert-FileValid -Path $script:testPath -FileHash 'mockFileHash' -SignerThumbprint 'mockSignerThumbprint' } | Should Not Throw
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'Only Path and SignerSubject are passed in' {
                $mocksCalled = @(
                    @{ Command = 'Assert-FileHashValid'; Times = 0 }
                    @{ Command = 'Assert-FileSignatureValid'; Times = 1 }
                )

                It 'Should not throw' {
                    { Assert-FileValid -Path $script:testPath -SignerSubject 'mockSignerSubject' } | Should Not Throw
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'FileHash, SignerThumbprint, and SignerSubject are passed in' {
                $mocksCalled = @(
                    @{ Command = 'Assert-FileHashValid'; Times = 1 }
                    @{ Command = 'Assert-FileSignatureValid'; Times = 1 }
                )

                It 'Should not throw' {
                    { Assert-FileValid -Path $script:testPath -FileHash 'mockFileHash' `
                                                              -SignerThumbprint 'mockSignerThumbprint' `
                                                              -SignerSubject 'mockSignerSubject'
                    } | Should Not Throw
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'SignerThumbprint and SignerSubject are passed in but FileHash is not' {
                $mocksCalled = @(
                    @{ Command = 'Assert-FileHashValid'; Times = 0 }
                    @{ Command = 'Assert-FileSignatureValid'; Times = 1 }
                )

                It 'Should not throw' {
                    { Assert-FileValid -Path $script:testPath -SignerThumbprint 'mockSignerThumbprint' `
                                                              -SignerSubject 'mockSignerSubject'
                    } | Should Not Throw
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }

            Context 'Only path is passed in' {
                $mocksCalled = @(
                    @{ Command = 'Assert-FileHashValid'; Times = 0 }
                    @{ Command = 'Assert-FileSignatureValid'; Times = 0 }
                )

                It 'Should not throw' {
                    { Assert-FileValid -Path $script:testPath } | Should Not Throw
                }

                Invoke-ExpectedMocksAreCalledTest -MocksCalled $mocksCalled
            }
        }

        Describe 'Assert-FileHashValid' {
            $mockHash = @{ Hash = 'testHash' }
            Mock -CommandName 'Get-FileHash' -MockWith { return $mockHash }

            Context 'File hash is valid' {
                It 'Should not throw when hashes match' {
                    { Assert-FileHashValid -Path $script:testPath -Hash $mockHash.Hash -Algorithm 'SHA256' } | Should Not Throw
                }

                It 'Should fetch the file hash' {
                    Assert-MockCalled -CommandName 'Get-FileHash' -Exactly 1 -Scope 'Context'
                }
            }

            Context 'File hash is invalid' {
                $badHash = 'BadHash'
                $expectedErrorMessage = ($script:localizedData.InvalidFileHash -f $script:testPath, $badHash, 'SHA256')

                It 'Should throw when hashes do not match' {
                    { Assert-FileHashValid -Path $script:testPath -Hash $badHash -Algorithm 'SHA256' } | Should Throw $expectedErrorMessage
                }
            }
        }

        Describe 'Assert-FileSignatureValid' {
            $mockThumbprint = 'mockThumbprint'
            $mockSubject = 'mockSubject'
            $mockSignature = @{ 
                Status = [System.Management.Automation.SignatureStatus]::Valid
                SignerCertificate = @{ Thumbprint = $mockThumbprint; Subject = $mockSubject }
            }

            Mock -CommandName 'Get-AuthenticodeSignature' -MockWith { return $mockSignature }

            Context 'File signature status, thumbprint and subject are valid' {
                It 'Should not throw' {
                    { Assert-FileSignatureValid -Path $script:testPath -Thumbprint $mockThumbprint -Subject $mockSubject } | Should Not Throw
                }
            }

            Context 'File signature status and thumbprint are valid and Subject not passed in' {
                It 'Should not throw' {
                    { Assert-FileSignatureValid -Path $script:testPath -Thumbprint $mockThumbprint } | Should Not Throw
                }
            }

            Context 'File signature status and subject are valid and Thumbprint not passed in' {
                It 'Should not throw' {
                    { Assert-FileSignatureValid -Path $script:testPath -Subject $mockSubject } | Should Not Throw
                }
            }

            Context 'Only Path is passed in' {
                It 'Should not throw' {
                    { Assert-FileSignatureValid -Path $script:testPath } | Should Not Throw
                }
            }

            Context 'File signature status and thumbprint are valid and subject is invalid' {
                $badSubject = 'BadSubject'
                $expectedErrorMessage = ($script:localizedData.WrongSignerSubject -f $script:testPath, $badSubject)

                It 'Should throw expected error message' {
                    { Assert-FileSignatureValid -Path $script:testPath -Thumbprint $mockThumbprint -Subject $badSubject } | Should Throw $expectedErrorMessage
                }
            }

            Context 'File signature status and subject are valid and thumbprint is invalid' {
                $badThumbprint = 'BadThumbprint'
                $expectedErrorMessage = ($script:localizedData.WrongSignerThumbprint -f $script:testPath, $badThumbprint)

                It 'Should throw expected error message' {
                    { Assert-FileSignatureValid -Path $script:testPath -Thumbprint $badThumbprint -Subject $mockSubject } | Should Throw $expectedErrorMessage
                }
            }

            Context 'File signature status is invalid and subject and thumbprint are valid' {
                $mockSignature.Status = 'Invalid'
                $expectedErrorMessage = ($script:localizedData.InvalidFileSignature -f $script:testPath, $mockSignature.Status)

                It 'Should throw expected error message' {
                    { Assert-FileSignatureValid -Path $script:testPath -Thumbprint $mockThumbprint -Subject $mockSubject } | Should Throw $expectedErrorMessage
                }
            }
        }

        Describe 'Start-MsiProcess' {
            Mock -CommandName 'New-Object' -MockWith { return $script:mockStartInfo } -ParameterFilter { $TypeName -eq 'System.Diagnostics.ProcessStartInfo' }
            Mock -CommandName 'New-Object' -MockWith { return $script:mockProcess } -ParameterFilter { $TypeName -eq 'System.Diagnostics.Process' }
            Mock -CommandName 'Get-ProductEntry' -MockWith { return $script:mockProductEntryInfo }
            Mock -CommandName 'Invoke-PInvoke' -MockWith { return 0 }
            Mock -CommandName 'Invoke-Process' -MockWith { return 0 }

            $startMsiProcessParameters = @{
                IdentifyingNumber = $script:testIdentifyingNumber
                Path = $script:testPath
                Ensure = 'Present'
                Arguments = 'TestArguments'
                LogPath = 'TestLogPath'
                RunAsCredential = $script:testCredential
            }

            Context 'Install MSI package with RunAsCredential specified' {
                $mocksCalled = @(
                    @{ Command = 'New-Object'; Times = 1; Custom = 'process start info object' }
                    @{ Command = 'Get-ProductEntry'; Times = 0 }
                    @{ Command = 'Invoke-PInvoke'; Times = 1; Custom = 'install' }
                    @{ Command = 'Invoke-Process'; Times = 0; Custom = 'install' }
                )

                Invoke-GenericUnitTest -Function { Param($startMsiProcessParameters) Start-MsiProcess @startMsiProcessParameters } `
                                       -FunctionParameters $startMsiProcessParameters `
                                       -MocksCalled $mocksCalled `
                                       -ShouldThrow $false
            }

            $startMsiProcessParameters.Ensure = 'Absent'

            Context 'Uninstall MSI package with RunAsCredential specified' {
                $mocksCalled = @(
                    @{ Command = 'New-Object'; Times = 1; Custom = 'process start info object' }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Invoke-PInvoke'; Times = 1; Custom = 'uninstall' }
                    @{ Command = 'Invoke-Process'; Times = 0; Custom = 'uninstall' }
                )

                Invoke-GenericUnitTest -Function { Param($startMsiProcessParameters) Start-MsiProcess @startMsiProcessParameters } `
                                       -FunctionParameters $startMsiProcessParameters `
                                       -MocksCalled $mocksCalled `
                                       -ShouldThrow $false
            }

            $startMsiProcessParameters.Ensure = 'Present'
            $startMsiProcessParameters.Remove('RunAsCredential')

            Context 'Install MSI package without RunAsCredential' {
                $mocksCalled = @(
                    @{ Command = 'New-Object'; Times = 2; Custom = 'process start info object and process' }
                    @{ Command = 'Get-ProductEntry'; Times = 0 }
                    @{ Command = 'Invoke-PInvoke'; Times = 0; Custom = 'install' }
                    @{ Command = 'Invoke-Process'; Times = 1; Custom = 'install' }
                )

                Invoke-GenericUnitTest -Function { Param($startMsiProcessParameters) Start-MsiProcess @startMsiProcessParameters } `
                                       -FunctionParameters $startMsiProcessParameters `
                                       -MocksCalled $mocksCalled `
                                       -ShouldThrow $false
            }

            $startMsiProcessParameters.Ensure = 'Absent'

            Context 'Uninstall MSI package without RunAsCredential' {
                $mocksCalled = @(
                    @{ Command = 'New-Object'; Times = 2; Custom = 'process start info object and process object' }
                    @{ Command = 'Get-ProductEntry'; Times = 1 }
                    @{ Command = 'Invoke-PInvoke'; Times = 0; Custom = 'uninstall' }
                    @{ Command = 'Invoke-Process'; Times = 1; Custom = 'uninstall' }
                )

                Invoke-GenericUnitTest -Function { Param($startMsiProcessParameters) Start-MsiProcess @startMsiProcessParameters } `
                                       -FunctionParameters $startMsiProcessParameters `
                                       -MocksCalled $mocksCalled `
                                       -ShouldThrow $false
            }

            Mock -CommandName 'Invoke-Process' -MockWith { Throw }

            Context 'Error occurred while trying to invoke the process' {
                Invoke-GenericUnitTest -Function { Param($startMsiProcessParameters) Start-MsiProcess @startMsiProcessParameters } `
                                       -FunctionParameters $startMsiProcessParameters `
                                       -ShouldThrow $true `
                                       -ErrorMessage ($script:localizedData.CouldNotStartProcess -f $script:testPath) `
                                       -ErrorTestName $script:errorMessageTitles.CouldNotStartProcess
            }
        }
    }
}