|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using API.Features.Customer.Validation; |
| 4 | +using API.Settings; |
| 5 | +using DLCS.Core.Types; |
| 6 | +using FluentValidation.TestHelper; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using Test.Helpers.Data; |
| 9 | + |
| 10 | +namespace API.Tests.Features.Adjuncts.Validation; |
| 11 | + |
| 12 | +public class AdjunctIdListValidatorTests |
| 13 | +{ |
| 14 | + private readonly AdjunctIdListValidator sut = new(Options.Create( |
| 15 | + new ApiSettings |
| 16 | + { |
| 17 | + MaxImageListSize = 4 |
| 18 | + })); |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void Valid_WhenSingleAdjunct() |
| 22 | + { |
| 23 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 24 | + { |
| 25 | + {AssetIdGenerator.GetAssetId(), ["first"]} |
| 26 | + }; |
| 27 | + |
| 28 | + var result = sut.TestValidate(adjuncts); |
| 29 | + result.ShouldNotHaveAnyValidationErrors(); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Valid_WhenMultipleAdjunctSingleAsset() |
| 34 | + { |
| 35 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 36 | + { |
| 37 | + {AssetIdGenerator.GetAssetId(), ["first", "second"]} |
| 38 | + }; |
| 39 | + |
| 40 | + var result = sut.TestValidate(adjuncts); |
| 41 | + result.ShouldNotHaveAnyValidationErrors(); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void Valid_WhenSingleAdjunctMultipleAsset() |
| 46 | + { |
| 47 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 48 | + { |
| 49 | + {AssetIdGenerator.GetAssetId(), ["first"]}, |
| 50 | + {AssetIdGenerator.GetAssetId(assetPostfix: "_1"), ["first"]} |
| 51 | + }; |
| 52 | + |
| 53 | + var result = sut.TestValidate(adjuncts); |
| 54 | + result.ShouldNotHaveAnyValidationErrors(); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Invalid_WhenNullValue() |
| 59 | + { |
| 60 | + Dictionary<AssetId, List<string>> adjuncts = null; |
| 61 | + |
| 62 | + var result = sut.TestValidate(adjuncts); |
| 63 | + result.ShouldHaveAnyValidationError(); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Invalid_WhenEmptyValue() |
| 68 | + { |
| 69 | + Dictionary<AssetId, List<string>> adjuncts = []; |
| 70 | + |
| 71 | + var result = sut.TestValidate(adjuncts); |
| 72 | + result.ShouldHaveAnyValidationError(); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void Invalid_WhenMultipleAdjunctRepeated() |
| 77 | + { |
| 78 | + var assetId = AssetIdGenerator.GetAssetId(); |
| 79 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 80 | + { |
| 81 | + {assetId, ["first", "first"]} |
| 82 | + }; |
| 83 | + |
| 84 | + var result = sut.TestValidate(adjuncts); |
| 85 | + result.ShouldHaveAnyValidationError().WithErrorMessage($"Members contains 1 duplicate Id(s): asset Id: {assetId} : adjunct id: first"); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Invalid_WhenMultipleAdjunctRepeatedWithAdditional() |
| 90 | + { |
| 91 | + var assetId = AssetIdGenerator.GetAssetId(); |
| 92 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 93 | + { |
| 94 | + {assetId, ["first", "first", "second"]} |
| 95 | + }; |
| 96 | + |
| 97 | + var result = sut.TestValidate(adjuncts); |
| 98 | + result.ShouldHaveAnyValidationError().WithErrorMessage($"Members contains 1 duplicate Id(s): asset Id: {assetId} : adjunct id: first"); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void Invalid_WhenMultipleAdjunctRepeatedWithSecondAdjunct() |
| 103 | + { |
| 104 | + var assetId = AssetIdGenerator.GetAssetId(); |
| 105 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 106 | + { |
| 107 | + {assetId, ["first", "first"]}, |
| 108 | + {AssetIdGenerator.GetAssetId(assetPostfix: "_1"), ["second"]} |
| 109 | + }; |
| 110 | + |
| 111 | + var stuff3 = adjuncts.Any(kvp => |
| 112 | + kvp.Value.SelectMany(a => a).Distinct().Count() == kvp.Value.SelectMany(a => a).Count()); |
| 113 | + |
| 114 | + var result = sut.TestValidate(adjuncts); |
| 115 | + result.ShouldHaveAnyValidationError().WithErrorMessage($"Members contains 1 duplicate Id(s): asset Id: {assetId} : adjunct id: first"); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void Invalid_WhenMoreAdjunctsThanBatchSize() |
| 120 | + { |
| 121 | + var assetId = AssetIdGenerator.GetAssetId(); |
| 122 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 123 | + { |
| 124 | + {assetId, ["first", "second", "third", "fourth", "fifth"]} |
| 125 | + }; |
| 126 | + |
| 127 | + var result = sut.TestValidate(adjuncts); |
| 128 | + result.ShouldHaveAnyValidationError().WithErrorMessage("Maximum adjuncts in single batch is 4"); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void Invalid_WhenMoreSingleAdjunctAssetsThanBatchSize() |
| 133 | + { |
| 134 | + var adjuncts = new Dictionary<AssetId, List<string>> |
| 135 | + { |
| 136 | + {AssetIdGenerator.GetAssetId(), ["first"]}, |
| 137 | + {AssetIdGenerator.GetAssetId(assetPostfix: "_1"), ["second"]}, |
| 138 | + {AssetIdGenerator.GetAssetId(assetPostfix: "_2"), ["third"]}, |
| 139 | + {AssetIdGenerator.GetAssetId(assetPostfix: "_3"), ["fourth"]}, |
| 140 | + {AssetIdGenerator.GetAssetId(assetPostfix: "_4"), ["fifth"]}, |
| 141 | + }; |
| 142 | + |
| 143 | + var result = sut.TestValidate(adjuncts); |
| 144 | + result.ShouldHaveAnyValidationError().WithErrorMessage("Maximum adjuncts in single batch is 4"); |
| 145 | + } |
| 146 | +} |
0 commit comments