/* 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; using System.Linq; using OpenSearch.Net; using FluentAssertions; using OpenSearch.Client; using OpenSearch.Client.Specification.IngestApi; using Tests.Core.Extensions; using Tests.Core.ManagedOpenSearch.Clusters; using Tests.Domain; using Tests.Framework.EndpointTests; using Tests.Framework.EndpointTests.TestState; namespace Tests.Ingest.SimulatePipeline { public class SimulatePipelineApiTests : ApiIntegrationTestBase { public SimulatePipelineApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } protected override bool ExpectIsValid => true; protected override object ExpectJson { get; } = new { pipeline = new { description = "pipeline simulation", processors = new object[] { new { set = new { field = "name", value = "buzz" } }, new { append = new { field = "colors", value = new[] { "blue", "black" } } }, new { uppercase = new { field = "name" } } } }, docs = new object[] { new { _index = "project", _id = Project.Instance.Name, _source = Project.InstanceAnonymous }, new { _index = "otherindex", _id = "otherid", _source = Project.InstanceAnonymous }, new { _index = "otherindex", _id = "2", _source = new { id = "2", colors = new[] { "red" } } } } }; protected override int ExpectStatusCode => 200; protected override Func Fluent => d => d .Pipeline(pl => pl .Description("pipeline simulation") .Processors(ps => ps .Set(s => s .Field(p => p.Name) .Value("buzz") ) .Append(a => a .Field(p => p.Colors) .Value("blue", "black") ) .Uppercase(u => u .Field(p => p.Name) ) ) ) .Documents(ds => ds .Document(doc => doc .Source(Project.Instance) ) .Document(doc => doc .Source(Project.Instance) .Index("otherindex") .Id("otherid") ) .Document(doc => doc .Index("otherindex") .Source(new AnotherType { Id = "2", Colors = new[] { "red" } }) ) ); protected override HttpMethod HttpMethod => HttpMethod.POST; protected override SimulatePipelineRequest Initializer => new SimulatePipelineRequest { Pipeline = new Pipeline { Description = "pipeline simulation", Processors = new IProcessor[] { new SetProcessor { Field = "name", Value = "buzz" }, new AppendProcessor { Field = "colors", Value = new[] { "blue", "black" } }, new UppercaseProcessor { Field = "name" } } }, Documents = new[] { new SimulatePipelineDocument { Source = Project.Instance }, new SimulatePipelineDocument { Source = Project.Instance, Index = "otherindex", Id = "otherid" }, new SimulatePipelineDocument { Index = "otherindex", Source = new AnotherType { Id = "2", Colors = new[] { "red" } } } } }; protected override bool SupportsDeserialization => false; protected override string UrlPath => $"/_ingest/pipeline/_simulate"; protected override LazyResponses ClientUsage() => Calls( (client, f) => client.Ingest.SimulatePipeline(f), (client, f) => client.Ingest.SimulatePipelineAsync(f), (client, r) => client.Ingest.SimulatePipeline(r), (client, r) => client.Ingest.SimulatePipelineAsync(r) ); protected override void ExpectResponse(SimulatePipelineResponse response) { response.ShouldBeValid(); response.Documents.Should().NotBeNull().And.HaveCount(3); var simulation = response.Documents.FirstOrDefault(d => d.Document.Id == Project.Instance.Name); simulation.Should().NotBeNull(); simulation.Document.Ingest.Should().NotBeNull(); simulation.Document.Ingest.Timestamp.Should().NotBe(default(DateTime)); var project = simulation.Document.Source.As(); project.Should().NotBeNull(); project.Name.Should().Be("BUZZ"); project.ShouldAdhereToSourceSerializerWhenSet(); simulation = response.Documents.FirstOrDefault(d => d.Document.Id == "otherid"); simulation.Should().NotBeNull(); simulation.Document.Ingest.Should().NotBeNull(); simulation.Document.Ingest.Timestamp.Should().NotBe(default(DateTime)); project = simulation.Document.Source.As(); project.Name.Should().Be("BUZZ"); simulation = response.Documents.FirstOrDefault(d => d.Document.Id == "2"); simulation.Document.Ingest.Should().NotBeNull(); simulation.Document.Ingest.Timestamp.Should().NotBe(default(DateTime)); var anotherType = simulation.Document.Source.As(); anotherType.Should().NotBeNull(); anotherType.Colors.Should().BeEquivalentTo(new string[] { "red", "blue", "black" }); } public class AnotherType { public string[] Colors { get; set; } public string Id { get; set; } } } public class SimulatePipelineVerboseApiTests : SimulatePipelineApiTests { public SimulatePipelineVerboseApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } protected override Func Fluent => f => base.Fluent(f.Verbose()); protected override SimulatePipelineRequest Initializer { get { var initializer = base.Initializer; initializer.Verbose = true; return initializer; } } protected override string UrlPath => $"/_ingest/pipeline/_simulate?verbose=true"; protected override void ExpectResponse(SimulatePipelineResponse response) { response.ShouldBeValid(); response.Documents.Count.Should().Be(3); foreach (var doc in response.Documents) { doc.ProcessorResults.Should().NotBeNull(); foreach (var result in doc.ProcessorResults) { result.Document.Should().NotBeNull(); result.Document.Ingest.Should().NotBeNull(); } } } } }