|
| 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 | +package org.elasticsearch.client.security; |
| 20 | + |
| 21 | +import org.elasticsearch.common.bytes.BytesReference; |
| 22 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 23 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.common.xcontent.XContentType; |
| 27 | +import org.elasticsearch.test.ESTestCase; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +public class DeleteRoleResponseTests extends ESTestCase { |
| 32 | + |
| 33 | + public void testBasicParsing() throws IOException { |
| 34 | + XContentType contentType = randomFrom(XContentType.values()); |
| 35 | + final boolean found = randomBoolean(); |
| 36 | + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() |
| 37 | + .field("found", found).endObject(); |
| 38 | + BytesReference bytes = BytesReference.bytes(builder); |
| 39 | + |
| 40 | + DeleteRoleResponse response = parse(builder.contentType(), bytes); |
| 41 | + assertEquals(found, response.isFound()); |
| 42 | + } |
| 43 | + |
| 44 | + public void testParsingWithMissingField() throws IOException { |
| 45 | + XContentType contentType = randomFrom(XContentType.values()); |
| 46 | + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); |
| 47 | + BytesReference bytes = BytesReference.bytes(builder); |
| 48 | + |
| 49 | + expectThrows(IllegalArgumentException.class, () -> parse(builder.contentType(), bytes)); |
| 50 | + } |
| 51 | + |
| 52 | + private DeleteRoleResponse parse(XContentType contentType, BytesReference bytes) throws IOException { |
| 53 | + XContentParser parser = XContentFactory.xContent(contentType) |
| 54 | + .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); |
| 55 | + parser.nextToken(); |
| 56 | + return DeleteRoleResponse.fromXContent(parser); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments