-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutomaticTagGenerationAsyncTests.cs
More file actions
147 lines (124 loc) · 7.37 KB
/
Copy pathAutomaticTagGenerationAsyncTests.cs
File metadata and controls
147 lines (124 loc) · 7.37 KB
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
using Kepware.Api.Model;
using Kepware.Api.Model.Services;
using Moq;
using Moq.Contrib.HttpClient;
using Shouldly;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Extensions.Ordering;
namespace Kepware.Api.TestIntg.ApiClient
{
public class AutomaticTagGenerationAsyncTests : TestApiClientBase
{
private const string UNIT_TEST_CHANNEL = "unitTestChannel";
private const string UNIT_TEST_DEVICE = "unitTestDevice";
private const string ENDPOINT_TAG_GENERATION = $"/config/v1/project/channels/{UNIT_TEST_CHANNEL}/devices/{UNIT_TEST_DEVICE}/services/TagGeneration";
private const string JOB_ENDPOINT = $"/config/v1/project/channels/{UNIT_TEST_CHANNEL}/devices/{UNIT_TEST_DEVICE}/services/TagGeneration/jobs/job123";
[Fact]
public async Task AutomaticTagGenerationAsync_ShouldReturnKepServerJobPromise_WhenApiResponseIsInvalid()
{
// Arrange
// Create channel that doesn't support ATG
var channel = await AddTestChannel();
var device = await AddTestDevice(channel);
// Act
var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(channel.Name, device.Name, TimeSpan.FromSeconds(30));
// Assert
result.ShouldNotBeNull();
//result.Endpoint.ShouldBe(ENDPOINT_TAG_GENERATION);
result.JobTimeToLive.ShouldBe(TimeSpan.FromSeconds(30));
// Clean up
await DeleteAllChannelsAsync();
}
// [Fact]
// public async Task AutomaticTagGenerationAsync_ShouldThrowException_WhenHttpClientThrowsException()
// {
// // Arrange
// _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}")
// .Throws(new HttpRequestException("Network error"));
// // Act & Assert
// await Should.ThrowAsync<HttpRequestException>(async () =>
// {
// await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(30));
// });
// }
// [Fact]
// public async Task AutomaticTagGenerationAsync_ShouldReturnSuccess_WhenJobCompletesSuccessfullyAfterFirstGet()
// {
// // Arrange
// var jobResponse = new JobResponseMessage { ResponseStatusCode = (int)ApiResponseCode.Accepted, JobId = JOB_ENDPOINT };
// var jobStatus = new JobStatusMessage { Completed = true };
// _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}")
// .ReturnsResponse(HttpStatusCode.Accepted, JsonSerializer.Serialize(jobResponse), "application/json");
// _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, $"{TEST_ENDPOINT}{JOB_ENDPOINT}")
// .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatus), "application/json");
// // Act
// var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(30));
// var completionResult = await result.AwaitCompletionAsync();
// // Assert
// completionResult.Value.ShouldBeTrue();
// completionResult.IsSuccess.ShouldBeTrue();
// }
[Fact]
public async Task AutomaticTagGenerationAsync_ShouldReturnSuccess_WhenJobCompletesSuccessfully()
{
// Arrange
var channel = await AddTestChannel(driver: "Allen-Bradley ControlLogix Ethernet");
var device = await AddAtgTestDevice(channel);
// Act
var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(channel.Name, device.Name, TimeSpan.FromSeconds(30));
// Assert
result.ShouldNotBeNull();
result.JobTimeToLive.ShouldBe(TimeSpan.FromSeconds(30));
// Wait for job completion
var completionResult = await result.AwaitCompletionAsync();
// Assert
completionResult.Value.ShouldBeTrue();
completionResult.IsSuccess.ShouldBeTrue();
// Clean up
await DeleteAllChannelsAsync();
}
// [Fact]
// public async Task AutomaticTagGenerationAsync_ShouldReturnFailure_WhenJobFailsAfterFirstGet()
// {
// // Arrange
// var jobResponse = new JobResponseMessage { ResponseStatusCode = (int)ApiResponseCode.Accepted, JobId = JOB_ENDPOINT };
// var jobStatus = new JobStatusMessage { Completed = false };
// _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}")
// .ReturnsResponse(HttpStatusCode.Accepted, JsonSerializer.Serialize(jobResponse), "application/json");
// _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, $"{TEST_ENDPOINT}{JOB_ENDPOINT}")
// .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatus), "application/json");
// // Act
// var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(1));
// var completionResult = await result.AwaitCompletionAsync(TimeSpan.FromMilliseconds(100));
// // Assert
// completionResult.Value.ShouldBeFalse();
// completionResult.IsSuccess.ShouldBeFalse();
// }
// [Fact]
// public async Task AutomaticTagGenerationAsync_ShouldReturnFailure_WhenJobFailsAfterMultipleGets()
// {
// // Arrange
// var jobResponse = new JobResponseMessage { ResponseStatusCode = (int)ApiResponseCode.Accepted, JobId = JOB_ENDPOINT };
// var jobStatusIncomplete = new JobStatusMessage { Completed = false };
// var jobStatusFailed = new JobStatusMessage { Completed = true, Message = "Job failed" };
// _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, $"{TEST_ENDPOINT}{ENDPOINT_TAG_GENERATION}")
// .ReturnsResponse(HttpStatusCode.Accepted, JsonSerializer.Serialize(jobResponse), "application/json");
// _httpMessageHandlerMock.SetupSequenceRequest(HttpMethod.Get, $"{TEST_ENDPOINT}{JOB_ENDPOINT}")
// .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatusIncomplete), "application/json")
// .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(jobStatusIncomplete), "application/json")
// .ReturnsResponse(HttpStatusCode.ServiceUnavailable, JsonSerializer.Serialize(jobStatusFailed), "application/json");
// // Act
// var result = await _kepwareApiClient.ApiServices.AutomaticTagGenerationAsync(UNIT_TEST_CHANNEL, UNIT_TEST_DEVICE, TimeSpan.FromSeconds(5));
// var completionResult = await result.AwaitCompletionAsync(TimeSpan.FromMilliseconds(100));
// // Assert
// completionResult.Value.ShouldBeFalse();
// completionResult.IsSuccess.ShouldBeFalse();
// }
}
}