|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using Xunit; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +using Amazon.Lambda.TestUtilities; |
| 10 | +using Amazon.Lambda.PowerShellHost; |
| 11 | + |
| 12 | +using static Amazon.Lambda.PowerShellTests.TestUtilites; |
| 13 | + |
| 14 | +namespace Amazon.Lambda.PowerShellTests |
| 15 | +{ |
| 16 | + public class ExceptionHandlingTests |
| 17 | + { |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void CheckIfErrorCode() |
| 21 | + { |
| 22 | + Assert.True(ExceptionManager.IsErrorCode("ErrorCode1")); |
| 23 | + Assert.True(ExceptionManager.IsErrorCode("Error_Code")); |
| 24 | + Assert.True(ExceptionManager.IsErrorCode("ErrorϴCode")); |
| 25 | + |
| 26 | + Assert.False(ExceptionManager.IsErrorCode("1ErrorCode")); |
| 27 | + Assert.False(ExceptionManager.IsErrorCode("Error Code")); |
| 28 | + Assert.False(ExceptionManager.IsErrorCode("Error@Code")); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void ValidateSystemException() |
| 33 | + { |
| 34 | + ExceptionValidator("ThrowSystemException", "FileNotFoundException", null); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void ValidateCustomException() |
| 39 | + { |
| 40 | + ExceptionValidator("CustomException", "AccountNotFound", "The Account is not found"); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void CustomExceptionNoMessage() |
| 45 | + { |
| 46 | + ExceptionValidator("CustomExceptionNoMessage", "CustomExceptionNoMessage", "CustomExceptionNoMessage"); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void TestMultipleInvokesWithSameCustomException() |
| 51 | + { |
| 52 | + PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1"); |
| 53 | + |
| 54 | + ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found"); |
| 55 | + ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found"); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void TestMultipleInvokesWithDifferentCustomException() |
| 60 | + { |
| 61 | + PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1"); |
| 62 | + |
| 63 | + ExceptionValidator(function, "CustomException", "AccountNotFound", "The Account is not found"); |
| 64 | + ExceptionValidator(function, "CustomExceptionNoMessage", "CustomExceptionNoMessage", null); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void ThrowWithStringMessage() |
| 69 | + { |
| 70 | + ExceptionValidator("ThrowWithStringMessage", "RuntimeException", "Here is your error"); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void ThrowWithStringErrorCode() |
| 75 | + { |
| 76 | + ExceptionValidator("ThrowWithStringErrorCode", "ErrorCode42", "ErrorCode42"); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void WriteErrorWithMessageTest() |
| 81 | + { |
| 82 | + ExceptionValidator("WriteErrorWithMessageTest", "WriteErrorException", "Testing out Write-Error"); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void WriteErrorWithExceptionTest() |
| 87 | + { |
| 88 | + ExceptionValidator("WriteErrorWithExceptionTest", "FileNotFoundException", null); |
| 89 | + } |
| 90 | + |
| 91 | + private void ExceptionValidator(string psFunction, string exceptionType, string message) |
| 92 | + { |
| 93 | + PowerShellScriptsAsFunctions.Function function = new PowerShellScriptsAsFunctions.Function("ErrorExamples.ps1"); |
| 94 | + ExceptionValidator(function, psFunction, exceptionType, message); |
| 95 | + } |
| 96 | + |
| 97 | + private void ExceptionValidator(PowerShellScriptsAsFunctions.Function function, string psFunction, string exceptionType, string message) |
| 98 | + { |
| 99 | + var logger = new TestLambdaLogger(); |
| 100 | + var context = new TestLambdaContext |
| 101 | + { |
| 102 | + Logger = logger |
| 103 | + }; |
| 104 | + |
| 105 | + function.PowerShellFunctionName = psFunction; |
| 106 | + |
| 107 | + Exception foundException = null; |
| 108 | + try |
| 109 | + { |
| 110 | + function.ExecuteFunction(new MemoryStream(), context); |
| 111 | + } |
| 112 | + catch (Exception e) |
| 113 | + { |
| 114 | + foundException = e; |
| 115 | + } |
| 116 | + |
| 117 | + Assert.NotNull(foundException); |
| 118 | + Assert.True(foundException.GetType().Name.EndsWith(exceptionType)); |
| 119 | + |
| 120 | + if(message != null) |
| 121 | + { |
| 122 | + Assert.Equal(message, foundException.Message); |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments