|
| 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.documentation; |
| 21 | + |
| 22 | +import org.elasticsearch.action.ActionListener; |
| 23 | +import org.elasticsearch.action.delete.DeleteRequest; |
| 24 | +import org.elasticsearch.action.delete.DeleteResponse; |
| 25 | +import org.elasticsearch.action.index.IndexRequest; |
| 26 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
| 27 | +import org.elasticsearch.action.index.IndexResponse; |
| 28 | +import org.elasticsearch.client.ESRestHighLevelClientTestCase; |
| 29 | +import org.elasticsearch.client.Response; |
| 30 | +import org.elasticsearch.client.RestClient; |
| 31 | +import org.elasticsearch.client.RestHighLevelClient; |
| 32 | +import org.elasticsearch.client.http.HttpEntity; |
| 33 | +import org.elasticsearch.client.http.HttpStatus; |
| 34 | +import org.elasticsearch.client.http.entity.ContentType; |
| 35 | +import org.elasticsearch.client.http.nio.entity.NStringEntity; |
| 36 | +import org.elasticsearch.cluster.health.ClusterHealthStatus; |
| 37 | +import org.elasticsearch.common.settings.Settings; |
| 38 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 39 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 40 | +import org.elasticsearch.common.xcontent.XContentType; |
| 41 | +import org.elasticsearch.rest.RestStatus; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.InputStream; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +import static java.util.Collections.emptyMap; |
| 48 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; |
| 49 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; |
| 50 | + |
| 51 | +/** |
| 52 | + * This class is used to generate the documentation for the |
| 53 | + * docs/java-rest/high-level/migration.asciidoc page. |
| 54 | + * |
| 55 | + * You need to wrap your code between two tags like: |
| 56 | + * // tag::example[] |
| 57 | + * // end::example[] |
| 58 | + * |
| 59 | + * Where example is your tag name. |
| 60 | + * |
| 61 | + * Then in the documentation, you can extract what is between tag and end tags with |
| 62 | + * ["source","java",subs="attributes,callouts,macros"] |
| 63 | + * -------------------------------------------------- |
| 64 | + * include-tagged::{doc-tests}/MigrationDocumentationIT.java[example] |
| 65 | + * -------------------------------------------------- |
| 66 | + */ |
| 67 | +public class MigrationDocumentationIT extends ESRestHighLevelClientTestCase { |
| 68 | + |
| 69 | + public void testCreateIndex() throws IOException { |
| 70 | + RestClient restClient = client(); |
| 71 | + { |
| 72 | + //tag::migration-create-inded |
| 73 | + Settings indexSettings = Settings.builder() // <1> |
| 74 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 75 | + .put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 76 | + .build(); |
| 77 | + |
| 78 | + String payload = XContentFactory.jsonBuilder() // <2> |
| 79 | + .startObject() |
| 80 | + .startObject("settings") // <3> |
| 81 | + .value(indexSettings) |
| 82 | + .endObject() |
| 83 | + .startObject("mappings") // <4> |
| 84 | + .startObject("doc") |
| 85 | + .startObject("properties") |
| 86 | + .startObject("time") |
| 87 | + .field("type", "date") |
| 88 | + .endObject() |
| 89 | + .endObject() |
| 90 | + .endObject() |
| 91 | + .endObject() |
| 92 | + .endObject().string(); |
| 93 | + |
| 94 | + HttpEntity entity = new NStringEntity(payload, ContentType.APPLICATION_JSON); // <5> |
| 95 | + |
| 96 | + Response response = restClient.performRequest("PUT", "my-index", emptyMap(), entity); // <6> |
| 97 | + if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { |
| 98 | + // <7> |
| 99 | + } |
| 100 | + //end::migration-create-inded |
| 101 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public void testClusterHealth() throws IOException { |
| 106 | + RestClient restClient = client(); |
| 107 | + { |
| 108 | + //tag::migration-cluster-health |
| 109 | + Response response = restClient.performRequest("GET", "/_cluster/health"); // <1> |
| 110 | + |
| 111 | + ClusterHealthStatus healthStatus; |
| 112 | + try (InputStream is = response.getEntity().getContent()) { // <2> |
| 113 | + Map<String, Object> map = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); // <3> |
| 114 | + healthStatus = ClusterHealthStatus.fromString((String) map.get("status")); // <4> |
| 115 | + } |
| 116 | + |
| 117 | + if (healthStatus == ClusterHealthStatus.GREEN) { |
| 118 | + // <5> |
| 119 | + } |
| 120 | + //end::migration-cluster-health |
| 121 | + assertSame(ClusterHealthStatus.GREEN, healthStatus); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public void testRequests() throws IOException { |
| 126 | + RestHighLevelClient client = highLevelClient(); |
| 127 | + { |
| 128 | + //tag::migration-request-ctor |
| 129 | + IndexRequest request = new IndexRequest("index", "doc", "id"); // <1> |
| 130 | + request.source("{\"field\":\"value\"}", XContentType.JSON); |
| 131 | + //end::migration-request-ctor |
| 132 | + |
| 133 | + //tag::migration-request-ctor-execution |
| 134 | + IndexResponse response = client.index(request); |
| 135 | + //end::migration-request-ctor-execution |
| 136 | + assertEquals(RestStatus.CREATED, response.status()); |
| 137 | + } |
| 138 | + { |
| 139 | + //tag::migration-request-sync-execution |
| 140 | + DeleteRequest request = new DeleteRequest("index", "doc", "id"); |
| 141 | + DeleteResponse response = client.delete(request); // <1> |
| 142 | + //end::migration-request-sync-execution |
| 143 | + assertEquals(RestStatus.OK, response.status()); |
| 144 | + } |
| 145 | + { |
| 146 | + //tag::migration-request-async-execution |
| 147 | + DeleteRequest request = new DeleteRequest("index", "doc", "id"); // <1> |
| 148 | + client.deleteAsync(request, new ActionListener<DeleteResponse>() { // <2> |
| 149 | + @Override |
| 150 | + public void onResponse(DeleteResponse deleteResponse) { |
| 151 | + // <3> |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onFailure(Exception e) { |
| 156 | + // <4> |
| 157 | + } |
| 158 | + }); |
| 159 | + //end::migration-request-async-execution |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments