|
| 1 | +using Kepware.Api.Model; |
| 2 | +using Kepware.Api.Model.Services; |
| 3 | +using Moq; |
| 4 | +using Moq.Contrib.HttpClient; |
| 5 | +using Shouldly; |
| 6 | +using System; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Xunit; |
| 13 | +using Xunit.Extensions.Ordering; |
| 14 | + |
| 15 | +namespace Kepware.Api.TestIntg.ApiClient |
| 16 | +{ |
| 17 | + public class AutomaticTagGenerationAsyncTests : TestApiClientBase |
| 18 | + { |
| 19 | + private const string UNIT_TEST_CHANNEL = "unitTestChannel"; |
| 20 | + private const string UNIT_TEST_DEVICE = "unitTestDevice"; |
| 21 | + private const string ENDPOINT_TAG_GENERATION = $"/config/v1/project/channels/{UNIT_TEST_CHANNEL}/devices/{UNIT_TEST_DEVICE}/services/TagGeneration"; |
| 22 | + private const string JOB_ENDPOINT = $"/config/v1/project/channels/{UNIT_TEST_CHANNEL}/devices/{UNIT_TEST_DEVICE}/services/TagGeneration/jobs/job123"; |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task AutomaticTagGenerationAsync_ShouldReturnKepServerJobPromise_WhenApiResponseIsInvalid() |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + // Create channel that doesn't support ATG |
| 29 | + var channel = await AddTestChannel(); |
| 30 | + var device = await AddTestDevice(channel); |
| 31 | + |
| 32 | + // Act |
| 33 | + var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(channel.Name, device.Name, TimeSpan.FromSeconds(30)); |
| 34 | + // Assert |
| 35 | + result.ShouldNotBeNull(); |
| 36 | + //result.Endpoint.ShouldBe(ENDPOINT_TAG_GENERATION); |
| 37 | + result.JobTimeToLive.ShouldBe(TimeSpan.FromSeconds(30)); |
| 38 | + |
| 39 | + // Clean up |
| 40 | + await DeleteAllChannelsAsync(); |
| 41 | + } |
| 42 | + |
| 43 | + // [Fact] |
| 44 | + // public async Task AutomaticTagGenerationAsync_ShouldThrowException_WhenHttpClientThrowsException() |
| 45 | + // { |
| 46 | + // // Arrange |
| 47 | + // _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}") |
| 48 | + // .Throws(new HttpRequestException("Network error")); |
| 49 | + |
| 50 | + // // Act & Assert |
| 51 | + // await Should.ThrowAsync<HttpRequestException>(async () => |
| 52 | + // { |
| 53 | + // await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(30)); |
| 54 | + // }); |
| 55 | + // } |
| 56 | + |
| 57 | + // [Fact] |
| 58 | + // public async Task AutomaticTagGenerationAsync_ShouldReturnSuccess_WhenJobCompletesSuccessfullyAfterFirstGet() |
| 59 | + // { |
| 60 | + // // Arrange |
| 61 | + // var jobResponse = new JobResponseMessage { ResponseStatusCode = (int)ApiResponseCode.Accepted, JobId = JOB_ENDPOINT }; |
| 62 | + // var jobStatus = new JobStatusMessage { Completed = true }; |
| 63 | + // _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}") |
| 64 | + // .ReturnsResponse(HttpStatusCode.Accepted, JsonSerializer.Serialize(jobResponse), "application/json"); |
| 65 | + // _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, $"{TEST_ENDPOINT}{JOB_ENDPOINT}") |
| 66 | + // .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatus), "application/json"); |
| 67 | + |
| 68 | + // // Act |
| 69 | + // var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(30)); |
| 70 | + // var completionResult = await result.AwaitCompletionAsync(); |
| 71 | + |
| 72 | + // // Assert |
| 73 | + // completionResult.Value.ShouldBeTrue(); |
| 74 | + // completionResult.IsSuccess.ShouldBeTrue(); |
| 75 | + // } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task AutomaticTagGenerationAsync_ShouldReturnSuccess_WhenJobCompletesSuccessfully() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + var channel = await AddTestChannel(driver: "Allen-Bradley ControlLogix Ethernet"); |
| 82 | + var device = await AddAtgTestDevice(channel); |
| 83 | + |
| 84 | + // Act |
| 85 | + var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(channel.Name, device.Name, TimeSpan.FromSeconds(30)); |
| 86 | + |
| 87 | + // Assert |
| 88 | + result.ShouldNotBeNull(); |
| 89 | + result.JobTimeToLive.ShouldBe(TimeSpan.FromSeconds(30)); |
| 90 | + |
| 91 | + // Wait for job completion |
| 92 | + var completionResult = await result.AwaitCompletionAsync(); |
| 93 | + |
| 94 | + // Assert |
| 95 | + completionResult.Value.ShouldBeTrue(); |
| 96 | + completionResult.IsSuccess.ShouldBeTrue(); |
| 97 | + |
| 98 | + // Clean up |
| 99 | + await DeleteAllChannelsAsync(); |
| 100 | + } |
| 101 | + |
| 102 | + // [Fact] |
| 103 | + // public async Task AutomaticTagGenerationAsync_ShouldReturnFailure_WhenJobFailsAfterFirstGet() |
| 104 | + // { |
| 105 | + // // Arrange |
| 106 | + // var jobResponse = new JobResponseMessage { ResponseStatusCode = (int)ApiResponseCode.Accepted, JobId = JOB_ENDPOINT }; |
| 107 | + // var jobStatus = new JobStatusMessage { Completed = false }; |
| 108 | + // _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}") |
| 109 | + // .ReturnsResponse(HttpStatusCode.Accepted, JsonSerializer.Serialize(jobResponse), "application/json"); |
| 110 | + // _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, $"{TEST_ENDPOINT}{JOB_ENDPOINT}") |
| 111 | + // .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatus), "application/json"); |
| 112 | + |
| 113 | + // // Act |
| 114 | + // var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(1)); |
| 115 | + // var completionResult = await result.AwaitCompletionAsync(TimeSpan.FromMilliseconds(100)); |
| 116 | + |
| 117 | + // // Assert |
| 118 | + // completionResult.Value.ShouldBeFalse(); |
| 119 | + // completionResult.IsSuccess.ShouldBeFalse(); |
| 120 | + // } |
| 121 | + |
| 122 | + // [Fact] |
| 123 | + // public async Task AutomaticTagGenerationAsync_ShouldReturnFailure_WhenJobFailsAfterMultipleGets() |
| 124 | + // { |
| 125 | + // // Arrange |
| 126 | + // var jobResponse = new JobResponseMessage { ResponseStatusCode = (int)ApiResponseCode.Accepted, JobId = JOB_ENDPOINT }; |
| 127 | + // var jobStatusIncomplete = new JobStatusMessage { Completed = false }; |
| 128 | + |
| 129 | + // var jobStatusFailed = new JobStatusMessage { Completed = true, Message = "Job failed" }; |
| 130 | + |
| 131 | + // _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}") |
| 132 | + // .ReturnsResponse(HttpStatusCode.Accepted, JsonSerializer.Serialize(jobResponse), "application/json"); |
| 133 | + // _httpMessageHandlerMock.SetupSequenceRequest(HttpMethod.Get, $"{TEST_ENDPOINT}{JOB_ENDPOINT}") |
| 134 | + // .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatusIncomplete), "application/json") |
| 135 | + // .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatusIncomplete), "application/json") |
| 136 | + // .ReturnsResponse(HttpStatusCode.ServiceUnavailable, JsonSerializer.Serialize(jobStatusFailed), "application/json"); |
| 137 | + |
| 138 | + // // Act |
| 139 | + // var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(5)); |
| 140 | + // var completionResult = await result.AwaitCompletionAsync(TimeSpan.FromMilliseconds(100)); |
| 141 | + |
| 142 | + // // Assert |
| 143 | + // completionResult.Value.ShouldBeFalse(); |
| 144 | + // completionResult.IsSuccess.ShouldBeFalse(); |
| 145 | + // } |
| 146 | + } |
| 147 | +} |
0 commit comments