|
| 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.LatchedActionListener; |
| 24 | +import org.elasticsearch.client.ESRestHighLevelClientTestCase; |
| 25 | +import org.elasticsearch.client.RequestOptions; |
| 26 | +import org.elasticsearch.client.RestHighLevelClient; |
| 27 | +import org.elasticsearch.protocol.xpack.license.LicensesStatus; |
| 28 | +import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; |
| 29 | +import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; |
| 30 | + |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import static org.hamcrest.Matchers.hasSize; |
| 36 | +import static org.hamcrest.Matchers.not; |
| 37 | +import static org.hamcrest.Matchers.startsWith; |
| 38 | + |
| 39 | +/** |
| 40 | + * Documentation for Licensing APIs in the high level java client. |
| 41 | + * Code wrapped in {@code tag} and {@code end} tags is included in the docs. |
| 42 | + */ |
| 43 | +public class LicensingDocumentationIT extends ESRestHighLevelClientTestCase { |
| 44 | + |
| 45 | + public void testPutLicense() throws Exception { |
| 46 | + RestHighLevelClient client = highLevelClient(); |
| 47 | + String license = "{\"license\": {\"uid\":\"893361dc-9749-4997-93cb-802e3d7fa4a8\",\"type\":\"gold\"," + |
| 48 | + "\"issue_date_in_millis\":1411948800000,\"expiry_date_in_millis\":1914278399999,\"max_nodes\":1,\"issued_to\":\"issued_to\"," + |
| 49 | + "\"issuer\":\"issuer\",\"signature\":\"AAAAAgAAAA3U8+YmnvwC+CWsV/mRAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSm" + |
| 50 | + "kxakxZdW5IMlhlTHNoN1N2MXMvRFk4d3JTZEx3R3RRZ0pzU3lobWJKZnQvSEFva0ppTHBkWkprZWZSQi9iNmRQNkw1SlpLN0lDalZCS095MXRGN1lIZlpYcVVTTn" + |
| 51 | + "FrcTE2dzhJZmZrdFQrN3JQeGwxb0U0MXZ0dDJHSERiZTVLOHNzSDByWnpoZEphZHBEZjUrTVBxRENNSXNsWWJjZllaODdzVmEzUjNiWktNWGM5TUhQV2plaUo4Q1" + |
| 52 | + "JOUml4MXNuL0pSOEhQaVB2azhmUk9QVzhFeTFoM1Q0RnJXSG53MWk2K055c28zSmRnVkF1b2JSQkFLV2VXUmVHNDZ2R3o2VE1qbVNQS2lxOHN5bUErZlNIWkZSVm" + |
| 53 | + "ZIWEtaSU9wTTJENDVvT1NCYklacUYyK2FwRW9xa0t6dldMbmMzSGtQc3FWOTgzZ3ZUcXMvQkt2RUZwMFJnZzlvL2d2bDRWUzh6UG5pdENGWFRreXNKNkE9PQAAAQ" + |
| 54 | + "Be8GfzDm6T537Iuuvjetb3xK5dvg0K5NQapv+rczWcQFxgCuzbF8plkgetP1aAGZP4uRESDQPMlOCsx4d0UqqAm9f7GbBQ3l93P+PogInPFeEH9NvOmaAQovmxVM" + |
| 55 | + "9SE6DsDqlX4cXSO+bgWpXPTd2LmpoQc1fXd6BZ8GeuyYpVHVKp9hVU0tAYjw6HzYOE7+zuO1oJYOxElqy66AnIfkvHrvni+flym3tE7tDTgsDRaz7W3iBhaqiSnt" + |
| 56 | + "EqabEkvHdPHQdSR99XGaEvnHO1paK01/35iZF6OXHsF7CCj+558GRXiVxzueOe7TsGSSt8g7YjZwV9bRCyU7oB4B/nidgI\"}}"; |
| 57 | + { |
| 58 | + //tag::put-license-execute |
| 59 | + PutLicenseRequest request = new PutLicenseRequest(); |
| 60 | + request.setLicenseDefinition(license); // <1> |
| 61 | + request.setAcknowledge(false); // <2> |
| 62 | + |
| 63 | + PutLicenseResponse response = client.xpack().license().putLicense(request, RequestOptions.DEFAULT); |
| 64 | + //end::put-license-execute |
| 65 | + |
| 66 | + //tag::put-license-response |
| 67 | + LicensesStatus status = response.status(); // <1> |
| 68 | + assertEquals(status, LicensesStatus.VALID); // <2> |
| 69 | + boolean acknowledged = response.isAcknowledged(); // <3> |
| 70 | + String acknowledgeHeader = response.acknowledgeHeader(); // <4> |
| 71 | + Map<String, String[]> acknowledgeMessages = response.acknowledgeMessages(); // <5> |
| 72 | + //end::put-license-response |
| 73 | + |
| 74 | + assertFalse(acknowledged); // Should fail because we are trying to downgrade from platinum trial to gold |
| 75 | + assertThat(acknowledgeHeader, startsWith("This license update requires acknowledgement.")); |
| 76 | + assertThat(acknowledgeMessages.keySet(), not(hasSize(0))); |
| 77 | + } |
| 78 | + { |
| 79 | + PutLicenseRequest request = new PutLicenseRequest(); |
| 80 | + // tag::put-license-execute-listener |
| 81 | + ActionListener<PutLicenseResponse> listener = new ActionListener<PutLicenseResponse>() { |
| 82 | + @Override |
| 83 | + public void onResponse(PutLicenseResponse indexResponse) { |
| 84 | + // <1> |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onFailure(Exception e) { |
| 89 | + // <2> |
| 90 | + } |
| 91 | + }; |
| 92 | + // end::put-license-execute-listener |
| 93 | + |
| 94 | + // Replace the empty listener by a blocking listener in test |
| 95 | + final CountDownLatch latch = new CountDownLatch(1); |
| 96 | + listener = new LatchedActionListener<>(listener, latch); |
| 97 | + |
| 98 | + // tag::put-license-execute-async |
| 99 | + client.xpack().license().putLicenseAsync( |
| 100 | + request, RequestOptions.DEFAULT, listener); // <1> |
| 101 | + // end::put-license-execute-async |
| 102 | + |
| 103 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments