|
| 1 | +using LogicAppUnit.Helper; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace LogicAppUnit.Samples.LogicApps.Tests.HttpAsyncTriggerWorkflowTest |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Test cases for the <i>http-async-test-workflow</i> workflow which uses an asynchronous response for the HTTP trigger. |
| 11 | + /// </summary> |
| 12 | + [TestClass] |
| 13 | + public class HttpAsyncWorkflowTest : WorkflowTestBase |
| 14 | + { |
| 15 | + [TestInitialize] |
| 16 | + public void TestInitialize() |
| 17 | + { |
| 18 | + Initialize(Constants.LOGIC_APP_TEST_EXAMPLE_BASE_PATH, Constants.HTTP_ASYNC_WORKFLOW); |
| 19 | + } |
| 20 | + |
| 21 | + [ClassCleanup] |
| 22 | + public static void CleanResources() |
| 23 | + { |
| 24 | + Close(); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Tests that the correct response is returned when the HTTP call to the Service One API to get the customer details fails. |
| 29 | + /// </summary> |
| 30 | + [TestMethod] |
| 31 | + public void HttpAsyncWorkflowTest_When_Get_Customer_Details_Fails() |
| 32 | + { |
| 33 | + using (ITestRunner testRunner = CreateTestRunner()) |
| 34 | + { |
| 35 | + // Configure async response handling |
| 36 | + testRunner.WaitForAsynchronousResponse(30); |
| 37 | + |
| 38 | + // Mock the HTTP calls and customize responses |
| 39 | + testRunner.AddApiMocks = (request) => |
| 40 | + { |
| 41 | + HttpResponseMessage mockedResponse = new HttpResponseMessage(); |
| 42 | + if (request.RequestUri.AbsolutePath == "/api/v1/customers/12345" && request.Method == HttpMethod.Get) |
| 43 | + { |
| 44 | + mockedResponse.RequestMessage = request; |
| 45 | + mockedResponse.StatusCode = HttpStatusCode.InternalServerError; |
| 46 | + mockedResponse.Content = ContentHelper.CreatePlainStringContent("Internal server error detected in System One"); |
| 47 | + } |
| 48 | + |
| 49 | + Thread.Sleep(5000); // wait for 5 seconds to give a gap between (i) the trigger's sync response and (ii) the workflow's async response |
| 50 | + return mockedResponse; |
| 51 | + }; |
| 52 | + |
| 53 | + // Run the workflow |
| 54 | + var workflowResponse = testRunner.TriggerWorkflow( |
| 55 | + GetWebhookRequest(), |
| 56 | + HttpMethod.Post); |
| 57 | + |
| 58 | + // Check workflow run status |
| 59 | + Assert.AreEqual(WorkflowRunStatus.Succeeded, testRunner.WorkflowRunStatus); |
| 60 | + |
| 61 | + // Check workflow response |
| 62 | + testRunner.ExceptionWrapper(() => Assert.AreEqual(HttpStatusCode.InternalServerError, workflowResponse.StatusCode)); |
| 63 | + Assert.AreEqual("Unable to get customer details: Internal server error detected in System One", workflowResponse.Content.ReadAsStringAsync().Result); |
| 64 | + Assert.AreEqual("text/plain; charset=utf-8", workflowResponse.Content.Headers.ContentType.ToString()); |
| 65 | + |
| 66 | + // Check action result |
| 67 | + Assert.AreEqual(ActionStatus.Failed, testRunner.GetWorkflowActionStatus("Get_Customer_Details_from_Service_One")); |
| 68 | + Assert.AreEqual(ActionStatus.Skipped, testRunner.GetWorkflowActionStatus("Success_Response_(Async)")); |
| 69 | + Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Failed_Get_Response_(Async)")); |
| 70 | + Assert.AreEqual(ActionStatus.Skipped, testRunner.GetWorkflowActionStatus("Update_Customer_Details_in_Service_Two")); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Tests that the correct response is returned when the HTTP call to the Service Two API to update the customer details fails. |
| 76 | + /// </summary> |
| 77 | + [TestMethod] |
| 78 | + public void HttpAsyncWorkflowTest_When_Update_Customer_Fails() |
| 79 | + { |
| 80 | + using (ITestRunner testRunner = CreateTestRunner()) |
| 81 | + { |
| 82 | + // Configure async response handling |
| 83 | + testRunner.WaitForAsynchronousResponse(30); |
| 84 | + |
| 85 | + // Mock the HTTP calls and customize responses |
| 86 | + testRunner.AddApiMocks = (request) => |
| 87 | + { |
| 88 | + HttpResponseMessage mockedResponse = new HttpResponseMessage(); |
| 89 | + if (request.RequestUri.AbsolutePath == "/api/v1/customers/12345" && request.Method == HttpMethod.Get) |
| 90 | + { |
| 91 | + mockedResponse.RequestMessage = request; |
| 92 | + mockedResponse.StatusCode = HttpStatusCode.OK; |
| 93 | + mockedResponse.Content = GetCustomerResponse(); |
| 94 | + } |
| 95 | + else if (request.RequestUri.AbsolutePath == "/api/v1.1/membership/customers/12345" && request.Method == HttpMethod.Put) |
| 96 | + { |
| 97 | + mockedResponse.RequestMessage = request; |
| 98 | + mockedResponse.StatusCode = HttpStatusCode.InternalServerError; |
| 99 | + mockedResponse.Content = ContentHelper.CreatePlainStringContent("System Two is not feeling well today"); |
| 100 | + } |
| 101 | + |
| 102 | + Thread.Sleep(5000); // wait for 5 seconds to give a gap between (i) the trigger's sync response and (ii) the workflow's async response |
| 103 | + return mockedResponse; |
| 104 | + }; |
| 105 | + |
| 106 | + // Run the workflow |
| 107 | + var workflowResponse = testRunner.TriggerWorkflow( |
| 108 | + GetWebhookRequest(), |
| 109 | + HttpMethod.Post); |
| 110 | + |
| 111 | + // Check workflow run status |
| 112 | + // Workflow has failed because the last action (Update Customer) has failed |
| 113 | + Assert.AreEqual(WorkflowRunStatus.Failed, testRunner.WorkflowRunStatus); |
| 114 | + |
| 115 | + // Check workflow response |
| 116 | + // The response is OK because this is an asynchronus response that is sent before the last action (Update Customer) fails |
| 117 | + testRunner.ExceptionWrapper(() => Assert.AreEqual(HttpStatusCode.OK, workflowResponse.StatusCode)); |
| 118 | + Assert.AreEqual("Webhook processed successfully", workflowResponse.Content.ReadAsStringAsync().Result); |
| 119 | + Assert.AreEqual("text/plain; charset=utf-8", workflowResponse.Content.Headers.ContentType.ToString()); |
| 120 | + |
| 121 | + // Check action result |
| 122 | + Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Get_Customer_Details_from_Service_One")); |
| 123 | + Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Success_Response_(Async)")); |
| 124 | + Assert.AreEqual(ActionStatus.Skipped, testRunner.GetWorkflowActionStatus("Failed_Get_Response_(Async)")); |
| 125 | + Assert.AreEqual(ActionStatus.Failed, testRunner.GetWorkflowActionStatus("Update_Customer_Details_in_Service_Two")); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Tests that the correct response is returned when the HTTP call to the Service Two API to update the customer details is successful. |
| 131 | + /// </summary> |
| 132 | + [TestMethod] |
| 133 | + public void HttpAsyncWorkflowTest_When_Successful_WaitForAsyncResponse() |
| 134 | + { |
| 135 | + using (ITestRunner testRunner = CreateTestRunner()) |
| 136 | + { |
| 137 | + // Configure async response handling |
| 138 | + testRunner.WaitForAsynchronousResponse(30); |
| 139 | + |
| 140 | + // Mock the HTTP calls and customize responses |
| 141 | + testRunner.AddApiMocks = (request) => |
| 142 | + { |
| 143 | + HttpResponseMessage mockedResponse = new HttpResponseMessage(); |
| 144 | + if (request.RequestUri.AbsolutePath == "/api/v1/customers/12345" && request.Method == HttpMethod.Get) |
| 145 | + { |
| 146 | + mockedResponse.RequestMessage = request; |
| 147 | + mockedResponse.StatusCode = HttpStatusCode.OK; |
| 148 | + mockedResponse.Content = GetCustomerResponse(); |
| 149 | + } |
| 150 | + else if (request.RequestUri.AbsolutePath == "/api/v1.1/membership/customers/12345" && request.Method == HttpMethod.Put) |
| 151 | + { |
| 152 | + mockedResponse.RequestMessage = request; |
| 153 | + mockedResponse.StatusCode = HttpStatusCode.OK; |
| 154 | + mockedResponse.Content = ContentHelper.CreatePlainStringContent("success"); |
| 155 | + } |
| 156 | + |
| 157 | + Thread.Sleep(5000); // wait for 5 seconds to give a gap between (i) the trigger's sync response and (ii) the workflow's async response and then (iii) the completion of the workflow |
| 158 | + return mockedResponse; |
| 159 | + }; |
| 160 | + |
| 161 | + // Run the workflow |
| 162 | + var workflowResponse = testRunner.TriggerWorkflow( |
| 163 | + GetWebhookRequest(), |
| 164 | + HttpMethod.Post); |
| 165 | + |
| 166 | + // Check workflow run status |
| 167 | + Assert.AreEqual(WorkflowRunStatus.Succeeded, testRunner.WorkflowRunStatus); |
| 168 | + |
| 169 | + // Check workflow response |
| 170 | + testRunner.ExceptionWrapper(() => Assert.AreEqual(HttpStatusCode.OK, workflowResponse.StatusCode)); |
| 171 | + Assert.AreEqual("Webhook processed successfully", workflowResponse.Content.ReadAsStringAsync().Result); |
| 172 | + Assert.AreEqual("text/plain; charset=utf-8", workflowResponse.Content.Headers.ContentType.ToString()); |
| 173 | + |
| 174 | + // Check action result |
| 175 | + Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Get_Customer_Details_from_Service_One")); |
| 176 | + Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Success_Response_(Async)")); |
| 177 | + Assert.AreEqual(ActionStatus.Skipped, testRunner.GetWorkflowActionStatus("Failed_Get_Response_(Async)")); |
| 178 | + Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Update_Customer_Details_in_Service_Two")); |
| 179 | + |
| 180 | + // Check tracked properties |
| 181 | + var trackedProps = testRunner.GetWorkflowActionTrackedProperties("Get_Customer_Details_from_Service_One"); |
| 182 | + Assert.AreEqual("customer", trackedProps["recordType"]); |
| 183 | + Assert.AreEqual("12345", trackedProps["recordId"]); |
| 184 | + Assert.AreEqual("c2ddb2f2-7bff-4cce-b724-ac2400b12760", trackedProps["correlationId"]); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private static StringContent GetWebhookRequest() |
| 189 | + { |
| 190 | + return ContentHelper.CreateJsonStringContent(new |
| 191 | + { |
| 192 | + id = "71fbcb8e-f974-449a-bb14-ac2400b150aa", |
| 193 | + correlationId = "c2ddb2f2-7bff-4cce-b724-ac2400b12760", |
| 194 | + sourceSystem = "SystemOne", |
| 195 | + timestamp = "2022-08-27T08:45:00.1493711Z", |
| 196 | + type = "CustomerUpdated", |
| 197 | + customerId = 12345, |
| 198 | + resourceId = "12345", |
| 199 | + resourceURI = "https://external-service-one.testing.net/api/v1/customer/12345" |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + private static StringContent GetCustomerResponse() |
| 204 | + { |
| 205 | + return ContentHelper.CreateJsonStringContent(new |
| 206 | + { |
| 207 | + id = 12345, |
| 208 | + title = "Mrs", |
| 209 | + firstName = "Sarah", |
| 210 | + lastName = "Smith", |
| 211 | + dateOfBirth = "1973-11-01", |
| 212 | + languageCode = "en-GB", |
| 213 | + address = new |
| 214 | + { |
| 215 | + line1 = "8 High Street", |
| 216 | + line2 = (string)null, |
| 217 | + line3 = (string)null, |
| 218 | + town = "Luton", |
| 219 | + county = "Bedfordshire", |
| 220 | + postcode = "LT12 6TY", |
| 221 | + countryCode = "UK", |
| 222 | + countryName = "United Kingdom" |
| 223 | + } |
| 224 | + }); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments