|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Reflection; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Kepware.Api.Model; |
| 9 | +using Kepware.Api.Test.ApiClient; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Moq; |
| 12 | +using Moq.Contrib.HttpClient; |
| 13 | +using Shouldly; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Kepware.Api.Test.ApiClient |
| 17 | +{ |
| 18 | + public class GenericHandler : TestApiClientBase |
| 19 | + { |
| 20 | + [Fact] |
| 21 | + public async Task CompareAndApplyDetailedAsync_ShouldCountUpdateAsFailed_When200ContainsNotApplied() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + var channel = new Channel { Name = "Channel1" }; |
| 25 | + channel.SetDynamicProperty(Properties.Channel.DeviceDriver, "Simulator"); |
| 26 | + |
| 27 | + var sourceDevice = new Device { Name = "Device1", Description = "new description", Owner = channel }; |
| 28 | + var targetDevice = new Device { Name = "Device1", Description = "old description", Owner = channel }; |
| 29 | + |
| 30 | + var sourceCollection = new DeviceCollection { sourceDevice }; |
| 31 | + var targetCollection = new DeviceCollection { targetDevice }; |
| 32 | + |
| 33 | + _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, TEST_ENDPOINT + "/config/v1/project/channels/Channel1/devices/Device1") |
| 34 | + .ReturnsResponse(HttpStatusCode.OK, JsonSerializer.Serialize(targetDevice), "application/json"); |
| 35 | + |
| 36 | + _httpMessageHandlerMock.SetupRequest(HttpMethod.Put, TEST_ENDPOINT + "/config/v1/project/channels/Channel1/devices/Device1") |
| 37 | + .ReturnsResponse(HttpStatusCode.OK, |
| 38 | + """ |
| 39 | + { |
| 40 | + "not_applied": { |
| 41 | + "servermain.DEVICE_ID_OCTAL": 1, |
| 42 | + "servermain.DEVICE_MODEL": 0 |
| 43 | + }, |
| 44 | + "code": 200, |
| 45 | + "message": "Not all properties were applied." |
| 46 | + } |
| 47 | + """, |
| 48 | + "application/json"); |
| 49 | + |
| 50 | + // Act |
| 51 | + var result = await _kepwareApiClient.GenericConfig.CompareAndApplyDetailedAsync<DeviceCollection, Device>(sourceCollection, targetCollection, channel); |
| 52 | + |
| 53 | + // Assert |
| 54 | + result.Updates.ShouldBe(0); |
| 55 | + result.Failures.ShouldBe(1); |
| 56 | + result.FailureList.Count.ShouldBe(1); |
| 57 | + result.FailureList[0].Operation.ShouldBe(ApplyOperation.Update); |
| 58 | + (result.FailureList[0].AttemptedItem as Device)?.Name.ShouldBe("Device1"); |
| 59 | + result.FailureList[0].NotAppliedProperties.ShouldNotBeNull(); |
| 60 | + result.FailureList[0].NotAppliedProperties!.ShouldContain("servermain.DEVICE_ID_OCTAL"); |
| 61 | + result.FailureList[0].NotAppliedProperties!.ShouldContain("servermain.DEVICE_MODEL"); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task CompareAndApplyDetailedAsync_ShouldMap207InsertFeedbackToItems() |
| 66 | + { |
| 67 | + // Arrange |
| 68 | + var channel = new Channel { Name = "Channel1" }; |
| 69 | + channel.SetDynamicProperty(Properties.Channel.DeviceDriver, "Simulator"); |
| 70 | + var ownerDevice = new Device { Name = "Device1", Owner = channel }; |
| 71 | + |
| 72 | + var tag1 = new Tag { Name = "Tag1", TagAddress = "RAMP" }; |
| 73 | + var tag2 = new Tag { Name = "Tag2", TagAddress = "SINE" }; |
| 74 | + |
| 75 | + var sourceTags = new DeviceTagCollection { tag1, tag2 }; |
| 76 | + |
| 77 | + _httpMessageHandlerMock.SetupRequest(HttpMethod.Post, TEST_ENDPOINT + "/config/v1/project/channels/Channel1/devices/Device1/tags") |
| 78 | + .ReturnsResponse((HttpStatusCode)207, |
| 79 | + """ |
| 80 | + [ |
| 81 | + { |
| 82 | + "property": "common.ALLTYPES_NAME", |
| 83 | + "description": "The name 'Tag1' is already used.", |
| 84 | + "error_line": 3, |
| 85 | + "code": 400, |
| 86 | + "message": "Validation failed on property common.ALLTYPES_NAME in object definition at line 3: The name 'Tag1' is already used." |
| 87 | + }, |
| 88 | + { |
| 89 | + "code": 201, |
| 90 | + "message": "Created" |
| 91 | + } |
| 92 | + ] |
| 93 | + """, |
| 94 | + "application/json"); |
| 95 | + |
| 96 | + // Act |
| 97 | + var result = await _kepwareApiClient.GenericConfig.CompareAndApplyDetailedAsync<DeviceTagCollection, Tag>(sourceTags, null, ownerDevice); |
| 98 | + |
| 99 | + // Assert |
| 100 | + result.Inserts.ShouldBe(1); |
| 101 | + result.Failures.ShouldBe(1); |
| 102 | + result.FailureList.Count.ShouldBe(1); |
| 103 | + result.FailureList[0].Operation.ShouldBe(ApplyOperation.Insert); |
| 104 | + (result.FailureList[0].AttemptedItem as Tag)?.Name.ShouldBe("Tag1"); |
| 105 | + result.FailureList[0].ResponseCode.ShouldBe(400); |
| 106 | + result.FailureList[0].Property.ShouldBe("common.ALLTYPES_NAME"); |
| 107 | + result.FailureList[0].Description.ShouldBe("The name 'Tag1' is already used."); |
| 108 | + result.FailureList[0].ErrorLine.ShouldBe(3); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void AppendQueryString_PrivateMethod_EncodesAndSkipsNullsAndAppendsCorrectly() |
| 113 | + { |
| 114 | + // Arrange |
| 115 | + var method = typeof(Kepware.Api.ClientHandler.GenericApiHandler) |
| 116 | + .GetMethod("AppendQueryString", BindingFlags.NonPublic | BindingFlags.Static); |
| 117 | + Assert.NotNull(method); |
| 118 | + |
| 119 | + var query = new[] |
| 120 | + { |
| 121 | + new KeyValuePair<string, string?>("a", "b"), |
| 122 | + new KeyValuePair<string, string?>("space", "x y"), |
| 123 | + new KeyValuePair<string, string?>("skip", null) // should be skipped |
| 124 | + }; |
| 125 | + |
| 126 | + // Act |
| 127 | + var result1 = (string)method!.Invoke(null, new object[] { "https://api/config", query })!; |
| 128 | + var result2 = (string)method!.Invoke(null, new object[] { "https://api/config?existing=1", query })!; |
| 129 | + |
| 130 | + // Assert |
| 131 | + Assert.Equal("https://api/config?a=b&space=x%20y", result1); |
| 132 | + Assert.Equal("https://api/config?existing=1&a=b&space=x%20y", result2); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task LoadCollectionAsync_AppendsQueryAndReturnsCollection() |
| 137 | + { |
| 138 | + // Arrange |
| 139 | + var channelsJson = """ |
| 140 | + [ |
| 141 | + { |
| 142 | + "PROJECT_ID": 676550906, |
| 143 | + "common.ALLTYPES_NAME": "Channel1", |
| 144 | + "common.ALLTYPES_DESCRIPTION": "Example Simulator Channel", |
| 145 | + "servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Simulator" |
| 146 | + }, |
| 147 | + { |
| 148 | + "PROJECT_ID": 676550906, |
| 149 | + "common.ALLTYPES_NAME": "Data Type Examples", |
| 150 | + "common.ALLTYPES_DESCRIPTION": "Example Simulator Channel", |
| 151 | + "servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Simulator" |
| 152 | + } |
| 153 | + ] |
| 154 | + """; |
| 155 | + |
| 156 | + var query = new[] |
| 157 | + { |
| 158 | + new KeyValuePair<string, string?>("status", "active"), |
| 159 | + new KeyValuePair<string, string?>("name", "John Doe"), |
| 160 | + new KeyValuePair<string, string?>("skip", null) |
| 161 | + }; |
| 162 | + |
| 163 | + // Expect encoded space in "John Doe" and null entry skipped |
| 164 | + _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, TEST_ENDPOINT + "/config/v1/project/channels?status=active&name=John%20Doe") |
| 165 | + .ReturnsResponse(channelsJson, "application/json"); |
| 166 | + |
| 167 | + // Act |
| 168 | + var result = await _kepwareApiClient.GenericConfig.LoadCollectionAsync<ChannelCollection, Channel>((string?)null, query); |
| 169 | + |
| 170 | + // Assert |
| 171 | + Assert.NotNull(result); |
| 172 | + Assert.Equal(2, result.Count); |
| 173 | + Assert.Contains(result, c => c.Name == "Channel1"); |
| 174 | + Assert.Contains(result, c => c.Name == "Data Type Examples"); |
| 175 | + } |
| 176 | + |
| 177 | + [Fact] |
| 178 | + public async Task LoadEntityAsync_AppendsQueryAndReturnsEntity() |
| 179 | + { |
| 180 | + // Arrange |
| 181 | + var channelJson = """ |
| 182 | + { |
| 183 | + "PROJECT_ID": 676550906, |
| 184 | + "common.ALLTYPES_NAME": "Channel1", |
| 185 | + "common.ALLTYPES_DESCRIPTION": "Example Simulator Channel", |
| 186 | + "servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Simulator" |
| 187 | + } |
| 188 | + """; |
| 189 | + |
| 190 | + var query = new[] |
| 191 | + { |
| 192 | + new KeyValuePair<string, string?>("status", "active"), |
| 193 | + new KeyValuePair<string, string?>("name", "John Doe"), |
| 194 | + new KeyValuePair<string, string?>("skip", null) |
| 195 | + }; |
| 196 | + |
| 197 | + // Expect encoded space in "John Doe" and null entry skipped |
| 198 | + _httpMessageHandlerMock.SetupRequest(HttpMethod.Get, TEST_ENDPOINT + "/config/v1/project/channels/Channel1?status=active&name=John%20Doe") |
| 199 | + .ReturnsResponse(channelJson, "application/json"); |
| 200 | + |
| 201 | + // Act |
| 202 | + var result = await _kepwareApiClient.GenericConfig.LoadEntityAsync<Channel>("Channel1", query); |
| 203 | + |
| 204 | + // Assert |
| 205 | + Assert.NotNull(result); |
| 206 | + Assert.Equal("Channel1", result.Name); |
| 207 | + Assert.Equal("Example Simulator Channel", result.Description); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments