/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using System.Threading.Tasks; using OpenSearch.OpenSearch.Xunit.XunitPlumbing; using FluentAssertions; using OpenSearch.Client; using OpenSearch.Client.Specification.IndicesApi; using Tests.Core.Extensions; using Tests.Core.ManagedOpenSearch.Clusters; using Tests.Domain; using Tests.Framework.EndpointTests; using Tests.Framework.EndpointTests.TestState; namespace Tests.Document.Single { public class DocumentCrudTests : CrudTestBase, UpdateResponse, DeleteResponse, ExistsResponse> { public DocumentCrudTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { } protected override bool SupportsDeletes => true; protected override LazyResponses Exists() => Calls, DocumentExistsRequest, IDocumentExistsRequest, ExistsResponse>( id => new DocumentExistsRequest(id){ Routing = Project.Instance.Name }, (id, d) => d.Routing(Project.Instance.Name), (s, c, f) => c.DocumentExists(s, f), (s, c, f) => c.DocumentExistsAsync(s, f), (s, c, r) => c.DocumentExists(r), (s, c, r) => c.DocumentExistsAsync(r) ); protected override LazyResponses Create() => Calls, IndexRequest, IIndexRequest, IndexResponse>( id => new IndexRequest(Project.Instance, id: id){ Routing = Project.Instance.Name }, (id, d) => d.Id(id).Routing(Project.Instance.Name), (s, c, f) => c.Index(Project.Instance, f), (s, c, f) => c.IndexAsync(Project.Instance, f), (s, c, r) => c.Index(r), (s, c, r) => c.IndexAsync(r) ); protected override LazyResponses Read() => Calls, GetRequest, IGetRequest, GetResponse>( id => new GetRequest(id) { Routing = Project.Instance.Name }, (id, d) => d.Routing(Project.Instance.Name), (s, c, f) => c.Get(s, f), (s, c, f) => c.GetAsync(s, f), (s, c, r) => c.Get(r), (s, c, r) => c.GetAsync(r) ); protected override LazyResponses Update() => Calls< UpdateDescriptor, UpdateRequest, IUpdateRequest, UpdateResponse >( id => new UpdateRequest(id) { Routing = Project.Instance.Name, Doc = new Project { Description = id + " updated" } }, (id, d) => d .Routing(Project.Instance.Name) .Doc(new Project { Description = id + " updated" }), (s, c, f) => c.Update(s, f), (s, c, f) => c.UpdateAsync(s, f), (s, c, r) => c.Update(r), (s, c, r) => c.UpdateAsync(r) ); protected override LazyResponses Delete() => Calls, DeleteRequest, IDeleteRequest, DeleteResponse>( id => new DeleteRequest(id) { Routing = Project.Instance.Name }, (id, d) => d.Routing(Project.Instance.Name), (s, c, f) => c.Delete(s, f), (s, c, f) => c.DeleteAsync(s, f), (s, c, r) => c.Delete(r), (s, c, r) => c.DeleteAsync(r) ); [I] protected async Task DocumentIsUpdated() => await AssertOnGetAfterUpdate(r => { r.Source.Should().NotBeNull(); r.Version.Should().BeGreaterThan(1); r.SequenceNumber.Should().BeGreaterOrEqualTo(1); r.PrimaryTerm.Should().BeGreaterThan(0); r.Source.Description.Should().EndWith("updated"); }); [I] protected async Task DocumentIsDeleted() => await AssertOnGetAfterDelete(r => r.Found.Should().BeFalse() ); [I] protected override async Task GetAfterDeleteIsValid() => await AssertOnGetAfterDelete(r => { r.ShouldNotBeValid(); r.Index.Should().NotBeNullOrEmpty(); if (Cluster.ClusterConfiguration.Version < "2.0.0") r.Type.Should().NotBeNullOrEmpty(); r.Id.Should().NotBeNullOrEmpty(); }); protected override void ExpectDeleteNotFoundResponse(DeleteResponse response) { response.Index.Should().NotBeNullOrEmpty(); if (Cluster.ClusterConfiguration.Version < "2.0.0") response.Type.Should().NotBeNullOrEmpty(); response.Id.Should().NotBeNullOrEmpty(); response.Version.Should().BeGreaterThan(0); response.SequenceNumber.Should().BeGreaterThan(0); response.Result.Should().Be(Result.NotFound); } } }