|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client; |
| 21 | + |
| 22 | +import org.apache.http.client.methods.HttpDelete; |
| 23 | +import org.apache.http.client.methods.HttpGet; |
| 24 | +import org.apache.http.client.methods.HttpPost; |
| 25 | +import org.apache.http.client.methods.HttpPut; |
| 26 | +import org.elasticsearch.action.ingest.DeletePipelineRequest; |
| 27 | +import org.elasticsearch.action.ingest.GetPipelineRequest; |
| 28 | +import org.elasticsearch.action.ingest.PutPipelineRequest; |
| 29 | +import org.elasticsearch.action.ingest.SimulatePipelineRequest; |
| 30 | +import org.elasticsearch.action.support.master.AcknowledgedRequest; |
| 31 | +import org.elasticsearch.common.bytes.BytesArray; |
| 32 | +import org.elasticsearch.common.xcontent.XContentType; |
| 33 | +import org.elasticsearch.test.ESTestCase; |
| 34 | +import org.junit.Assert; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.StringJoiner; |
| 41 | + |
| 42 | +public class IngestRequestConvertersTests extends ESTestCase { |
| 43 | + |
| 44 | + public void testPutPipeline() throws IOException { |
| 45 | + String pipelineId = "some_pipeline_id"; |
| 46 | + PutPipelineRequest request = new PutPipelineRequest( |
| 47 | + "some_pipeline_id", |
| 48 | + new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), |
| 49 | + XContentType.JSON |
| 50 | + ); |
| 51 | + Map<String, String> expectedParams = new HashMap<>(); |
| 52 | + RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); |
| 53 | + RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); |
| 54 | + |
| 55 | + Request expectedRequest = IngestRequestConverters.putPipeline(request); |
| 56 | + StringJoiner endpoint = new StringJoiner("/", "/", ""); |
| 57 | + endpoint.add("_ingest/pipeline"); |
| 58 | + endpoint.add(pipelineId); |
| 59 | + Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); |
| 60 | + Assert.assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod()); |
| 61 | + Assert.assertEquals(expectedParams, expectedRequest.getParameters()); |
| 62 | + } |
| 63 | + |
| 64 | + public void testGetPipeline() { |
| 65 | + String pipelineId = "some_pipeline_id"; |
| 66 | + Map<String, String> expectedParams = new HashMap<>(); |
| 67 | + GetPipelineRequest request = new GetPipelineRequest("some_pipeline_id"); |
| 68 | + RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); |
| 69 | + Request expectedRequest = IngestRequestConverters.getPipeline(request); |
| 70 | + StringJoiner endpoint = new StringJoiner("/", "/", ""); |
| 71 | + endpoint.add("_ingest/pipeline"); |
| 72 | + endpoint.add(pipelineId); |
| 73 | + Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); |
| 74 | + Assert.assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod()); |
| 75 | + Assert.assertEquals(expectedParams, expectedRequest.getParameters()); |
| 76 | + } |
| 77 | + |
| 78 | + public void testDeletePipeline() { |
| 79 | + String pipelineId = "some_pipeline_id"; |
| 80 | + Map<String, String> expectedParams = new HashMap<>(); |
| 81 | + DeletePipelineRequest request = new DeletePipelineRequest(pipelineId); |
| 82 | + RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); |
| 83 | + RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); |
| 84 | + Request expectedRequest = IngestRequestConverters.deletePipeline(request); |
| 85 | + StringJoiner endpoint = new StringJoiner("/", "/", ""); |
| 86 | + endpoint.add("_ingest/pipeline"); |
| 87 | + endpoint.add(pipelineId); |
| 88 | + Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); |
| 89 | + Assert.assertEquals(HttpDelete.METHOD_NAME, expectedRequest.getMethod()); |
| 90 | + Assert.assertEquals(expectedParams, expectedRequest.getParameters()); |
| 91 | + } |
| 92 | + |
| 93 | + public void testSimulatePipeline() throws IOException { |
| 94 | + String pipelineId = ESTestCase.randomBoolean() ? "some_pipeline_id" : null; |
| 95 | + boolean verbose = ESTestCase.randomBoolean(); |
| 96 | + String json = "{\"pipeline\":{" + |
| 97 | + "\"description\":\"_description\"," + |
| 98 | + "\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]}," + |
| 99 | + "\"docs\":[{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}]}"; |
| 100 | + SimulatePipelineRequest request = new SimulatePipelineRequest( |
| 101 | + new BytesArray(json.getBytes(StandardCharsets.UTF_8)), |
| 102 | + XContentType.JSON |
| 103 | + ); |
| 104 | + request.setId(pipelineId); |
| 105 | + request.setVerbose(verbose); |
| 106 | + Map<String, String> expectedParams = new HashMap<>(); |
| 107 | + expectedParams.put("verbose", Boolean.toString(verbose)); |
| 108 | + |
| 109 | + Request expectedRequest = IngestRequestConverters.simulatePipeline(request); |
| 110 | + StringJoiner endpoint = new StringJoiner("/", "/", ""); |
| 111 | + endpoint.add("_ingest/pipeline"); |
| 112 | + if (pipelineId != null && !pipelineId.isEmpty()) |
| 113 | + endpoint.add(pipelineId); |
| 114 | + endpoint.add("_simulate"); |
| 115 | + Assert.assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); |
| 116 | + Assert.assertEquals(HttpPost.METHOD_NAME, expectedRequest.getMethod()); |
| 117 | + Assert.assertEquals(expectedParams, expectedRequest.getParameters()); |
| 118 | + RequestConvertersTests.assertToXContentBody(request, expectedRequest.getEntity()); |
| 119 | + } |
| 120 | +} |
0 commit comments