|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using Elastic.Clients.Elasticsearch.IndexManagement; |
| 7 | +using Tests.Core.ManagedElasticsearch.Clusters; |
| 8 | +using Tests.Core.ManagedElasticsearch.NodeSeeders; |
| 9 | +using Tests.Domain; |
| 10 | +using Tests.Framework.EndpointTests; |
| 11 | +using Tests.Framework.EndpointTests.TestState; |
| 12 | + |
| 13 | +namespace Tests.IndexManagement.AliasManagement; |
| 14 | + |
| 15 | +public class GetAliasApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetAliasResponse, GetAliasRequestDescriptor, GetAliasRequest> |
| 16 | +{ |
| 17 | + private static readonly Names Names = Infer.Names(DefaultSeeder.ProjectsAliasName); |
| 18 | + |
| 19 | + public GetAliasApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } |
| 20 | + |
| 21 | + protected override bool ExpectIsValid => true; |
| 22 | + protected override int ExpectStatusCode => 200; |
| 23 | + protected override HttpMethod ExpectHttpMethod => HttpMethod.GET; |
| 24 | + protected override bool SupportsDeserialization => false; |
| 25 | + protected override string ExpectedUrlPathAndQuery => $"_all/_alias/{DefaultSeeder.ProjectsAliasName}"; |
| 26 | + |
| 27 | + protected override GetAliasRequest Initializer => new(Indices.All, Names); |
| 28 | + protected override Action<GetAliasRequestDescriptor> Fluent => d => d.Name(Names); |
| 29 | + |
| 30 | + protected override LazyResponses ClientUsage() => Calls( |
| 31 | + (client, f) => client.Indices.GetAlias(Indices.All, f), |
| 32 | + (client, f) => client.Indices.GetAliasAsync(Indices.All, f), |
| 33 | + (client, r) => client.Indices.GetAlias(r), |
| 34 | + (client, r) => client.Indices.GetAliasAsync(r) |
| 35 | + ); |
| 36 | + |
| 37 | + protected override void ExpectResponse(GetAliasResponse response) |
| 38 | + { |
| 39 | + response.Aliases.Should().NotBeEmpty($"expect to find indices pointing to {DefaultSeeder.ProjectsAliasName}"); |
| 40 | + var indexAliases = response.Aliases[Infer.Index<Project>()]; |
| 41 | + indexAliases.Should().NotBeNull("expect to find alias for project"); |
| 42 | + indexAliases.Aliases.Should().NotBeEmpty("expect to find aliases dictionary definitions for project"); |
| 43 | + var alias = indexAliases.Aliases[DefaultSeeder.ProjectsAliasName]; |
| 44 | + alias.Should().NotBeNull(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// TODO: Support exception information from specification and avoid default error response deserialization in transport |
| 49 | + |
| 50 | +//public class GetAliasPartialMatchApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetAliasResponse, GetAliasRequestDescriptor, GetAliasRequest> |
| 51 | +//{ |
| 52 | +// private static readonly Names Names = Infer.Names(DefaultSeeder.ProjectsAliasName, "x", "y"); |
| 53 | + |
| 54 | +// public GetAliasPartialMatchApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } |
| 55 | + |
| 56 | +// protected override bool ExpectIsValid => true; |
| 57 | +// protected override int ExpectStatusCode => 404; |
| 58 | +// protected override HttpMethod ExpectHttpMethod => HttpMethod.GET; |
| 59 | +// protected override bool SupportsDeserialization => false; |
| 60 | +// protected override string ExpectedUrlPathAndQuery => $"_all/_alias/{DefaultSeeder.ProjectsAliasName}%2Cx%2Cy"; |
| 61 | + |
| 62 | +// protected override GetAliasRequest Initializer => new(Indices.All, Names); |
| 63 | +// protected override Action<GetAliasRequestDescriptor> Fluent => d => d.Name(Names); |
| 64 | + |
| 65 | +// protected override LazyResponses ClientUsage() => Calls( |
| 66 | +// (client, f) => client.Indices.GetAlias(Indices.All, f), |
| 67 | +// (client, f) => client.Indices.GetAliasAsync(Indices.All, f), |
| 68 | +// (client, r) => client.Indices.GetAlias(r), |
| 69 | +// (client, r) => client.Indices.GetAliasAsync(r) |
| 70 | +// ); |
| 71 | + |
| 72 | +// protected override void ExpectResponse(GetAliasResponse response) |
| 73 | +// { |
| 74 | +// response.Aliases.Should().NotBeNull(); |
| 75 | +// response.Aliases.Count.Should().BeGreaterThan(0); |
| 76 | +// } |
| 77 | +//} |
| 78 | + |
| 79 | +public class GetAliasNotFoundApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetAliasResponse, GetAliasRequestDescriptor, GetAliasRequest> |
| 80 | +{ |
| 81 | + private static readonly Names Names = Infer.Names("bad-alias"); |
| 82 | + |
| 83 | + public GetAliasNotFoundApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } |
| 84 | + |
| 85 | + protected override bool ExpectIsValid => false; |
| 86 | + protected override int ExpectStatusCode => 404; |
| 87 | + protected override HttpMethod ExpectHttpMethod => HttpMethod.GET; |
| 88 | + protected override bool SupportsDeserialization => false; |
| 89 | + protected override string ExpectedUrlPathAndQuery => $"_all/_alias/bad-alias"; |
| 90 | + |
| 91 | + protected override GetAliasRequest Initializer => new(Indices.All, Names); |
| 92 | + protected override Action<GetAliasRequestDescriptor> Fluent => d => d.Name(Names); |
| 93 | + |
| 94 | + protected override LazyResponses ClientUsage() => Calls( |
| 95 | + (client, f) => client.Indices.GetAlias(Indices.All, f), |
| 96 | + (client, f) => client.Indices.GetAliasAsync(Indices.All, f), |
| 97 | + (client, r) => client.Indices.GetAlias(r), |
| 98 | + (client, r) => client.Indices.GetAliasAsync(r) |
| 99 | + ); |
| 100 | + |
| 101 | + protected override void ExpectResponse(GetAliasResponse response) |
| 102 | + { |
| 103 | + response.ElasticsearchServerError.Should().NotBeNull(); |
| 104 | + response.ElasticsearchServerError.Error.Reason.Should().Contain("missing"); |
| 105 | + |
| 106 | + response.Aliases.Should().NotBeNull(); |
| 107 | + response.Aliases.Should().BeEmpty(); |
| 108 | + } |
| 109 | +} |
0 commit comments