public void testParseRestSpecIndexApi() throws Exception { parser = createParser(YamlXContent.yamlXContent, REST_SPEC_INDEX_API); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser); assertThat(restApi, notNullValue()); assertThat(restApi.getName(), equalTo("index")); assertThat(restApi.getMethods().size(), equalTo(2)); assertThat(restApi.getMethods().get(0), equalTo("POST")); assertThat(restApi.getMethods().get(1), equalTo("PUT")); assertThat(restApi.getPaths().size(), equalTo(2)); assertThat(restApi.getPaths().get(0), equalTo("/{index}/{type}")); assertThat(restApi.getPaths().get(1), equalTo("/{index}/{type}/{id}")); assertThat(restApi.getPathParts().size(), equalTo(3)); assertThat(restApi.getPathParts().get(0), equalTo("id")); assertThat(restApi.getPathParts().get(1), equalTo("index")); assertThat(restApi.getPathParts().get(2), equalTo("type")); assertThat(restApi.getParams().size(), equalTo(4)); assertThat(restApi.getParams(), contains("wait_for_active_shards", "op_type", "parent", "refresh")); assertThat(restApi.isBodySupported(), equalTo(true)); assertThat(restApi.isBodyRequired(), equalTo(true)); }
public void testParseRestSpecGetTemplateApi() throws Exception { parser = createParser(YamlXContent.yamlXContent, REST_SPEC_GET_TEMPLATE_API); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser); assertThat(restApi, notNullValue()); assertThat(restApi.getName(), equalTo("indices.get_template")); assertThat(restApi.getMethods().size(), equalTo(1)); assertThat(restApi.getMethods().get(0), equalTo("GET")); assertThat(restApi.getPaths().size(), equalTo(2)); assertThat(restApi.getPaths().get(0), equalTo("/_template")); assertThat(restApi.getPaths().get(1), equalTo("/_template/{name}")); assertThat(restApi.getPathParts().size(), equalTo(1)); assertThat(restApi.getPathParts().get(0), equalTo("name")); assertThat(restApi.getParams().size(), equalTo(0)); assertThat(restApi.isBodySupported(), equalTo(false)); assertThat(restApi.isBodyRequired(), equalTo(false)); }
public void testParseRestSpecCountApi() throws Exception { parser = createParser(YamlXContent.yamlXContent, REST_SPEC_COUNT_API); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser); assertThat(restApi, notNullValue()); assertThat(restApi.getName(), equalTo("count")); assertThat(restApi.getMethods().size(), equalTo(2)); assertThat(restApi.getMethods().get(0), equalTo("POST")); assertThat(restApi.getMethods().get(1), equalTo("GET")); assertThat(restApi.getPaths().size(), equalTo(3)); assertThat(restApi.getPaths().get(0), equalTo("/_count")); assertThat(restApi.getPaths().get(1), equalTo("/{index}/_count")); assertThat(restApi.getPaths().get(2), equalTo("/{index}/{type}/_count")); assertThat(restApi.getPathParts().size(), equalTo(2)); assertThat(restApi.getPathParts().get(0), equalTo("index")); assertThat(restApi.getPathParts().get(1), equalTo("type")); assertThat(restApi.getParams().size(), equalTo(1)); assertThat(restApi.getParams().get(0), equalTo("ignore_unavailable")); assertThat(restApi.isBodySupported(), equalTo(true)); assertThat(restApi.isBodyRequired(), equalTo(false)); }
public void testParseDoSectionNoBody() throws Exception { parser = createParser(YamlXContent.yamlXContent, "get:\n" + " index: test_index\n" + " type: test_type\n" + " id: 1" ); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("get")); assertThat(apiCallSection.getParams().size(), equalTo(3)); assertThat(apiCallSection.getParams().get("index"), equalTo("test_index")); assertThat(apiCallSection.getParams().get("type"), equalTo("test_type")); assertThat(apiCallSection.getParams().get("id"), equalTo("1")); assertThat(apiCallSection.hasBody(), equalTo(false)); }
public void testParseDoSectionWithJsonBody() throws Exception { String body = "{ \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }"; parser = createParser(YamlXContent.yamlXContent, "index:\n" + " index: test_1\n" + " type: test\n" + " id: 1\n" + " body: " + body ); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("index")); assertThat(apiCallSection.getParams().size(), equalTo(3)); assertThat(apiCallSection.getParams().get("index"), equalTo("test_1")); assertThat(apiCallSection.getParams().get("type"), equalTo("test")); assertThat(apiCallSection.getParams().get("id"), equalTo("1")); assertThat(apiCallSection.hasBody(), equalTo(true)); assertJsonEquals(apiCallSection.getBodies().get(0), body); }
public void testParseDoSectionWithYamlBody() throws Exception { parser = createParser(YamlXContent.yamlXContent, "search:\n" + " body:\n" + " \"_source\": [ include.field1, include.field2 ]\n" + " \"query\": { \"match_all\": {} }" ); String body = "{ \"_source\": [ \"include.field1\", \"include.field2\" ], \"query\": { \"match_all\": {} }}"; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("search")); assertThat(apiCallSection.getParams().size(), equalTo(0)); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(1)); assertJsonEquals(apiCallSection.getBodies().get(0), body); }
public void testParseDoSectionWithYamlBodyMultiGet() throws Exception { parser = createParser(YamlXContent.yamlXContent, "mget:\n" + " body:\n" + " docs:\n" + " - { _index: test_2, _type: test, _id: 1}\n" + " - { _index: test_1, _type: none, _id: 1}" ); String body = "{ \"docs\": [ " + "{\"_index\": \"test_2\", \"_type\":\"test\", \"_id\":1}, " + "{\"_index\": \"test_1\", \"_type\":\"none\", \"_id\":1} " + "]}"; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("mget")); assertThat(apiCallSection.getParams().size(), equalTo(0)); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(1)); assertJsonEquals(apiCallSection.getBodies().get(0), body); }
public void testParseDoSectionWithBodyStringified() throws Exception { parser = createParser(YamlXContent.yamlXContent, "index:\n" + " index: test_1\n" + " type: test\n" + " id: 1\n" + " body: \"{ \\\"_source\\\": true, \\\"query\\\": { \\\"match_all\\\": {} } }\"" ); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("index")); assertThat(apiCallSection.getParams().size(), equalTo(3)); assertThat(apiCallSection.getParams().get("index"), equalTo("test_1")); assertThat(apiCallSection.getParams().get("type"), equalTo("test")); assertThat(apiCallSection.getParams().get("id"), equalTo("1")); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(1)); //stringified body is taken as is assertJsonEquals(apiCallSection.getBodies().get(0), "{ \"_source\": true, \"query\": { \"match_all\": {} } }"); }
public void testParseDoSectionWithBodiesStringifiedAndNot() throws Exception { parser = createParser(YamlXContent.yamlXContent, "index:\n" + " body:\n" + " - \"{ \\\"_source\\\": true, \\\"query\\\": { \\\"match_all\\\": {} } }\"\n" + " - { size: 100, query: { match_all: {} } }" ); String body = "{ \"size\": 100, \"query\": { \"match_all\": {} } }"; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection.getApi(), equalTo("index")); assertThat(apiCallSection.getParams().size(), equalTo(0)); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(2)); //stringified body is taken as is assertJsonEquals(apiCallSection.getBodies().get(0), "{ \"_source\": true, \"query\": { \"match_all\": {} } }"); assertJsonEquals(apiCallSection.getBodies().get(1), body); }
public void testParseDoSectionWithHeaders() throws Exception { parser = createParser(YamlXContent.yamlXContent, "headers:\n" + " Authorization: \"thing one\"\n" + " Content-Type: \"application/json\"\n" + "indices.get_warmer:\n" + " index: test_index\n" + " name: test_warmer" ); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getApiCallSection().getApi(), equalTo("indices.get_warmer")); assertThat(doSection.getApiCallSection().getParams().size(), equalTo(2)); assertThat(doSection.getApiCallSection().hasBody(), equalTo(false)); assertThat(doSection.getApiCallSection().getHeaders(), notNullValue()); assertThat(doSection.getApiCallSection().getHeaders().size(), equalTo(2)); assertThat(doSection.getApiCallSection().getHeaders().get("Authorization"), equalTo("thing one")); assertThat(doSection.getApiCallSection().getHeaders().get("Content-Type"), equalTo("application/json")); }
public void testParseDoSectionMultivaluedField() throws Exception { parser = createParser(YamlXContent.yamlXContent, "indices.get_field_mapping:\n" + " index: test_index\n" + " type: test_type\n" + " field: [ text , text1 ]" ); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getApiCallSection().getApi(), equalTo("indices.get_field_mapping")); assertThat(doSection.getApiCallSection().getParams().size(), equalTo(3)); assertThat(doSection.getApiCallSection().getParams().get("index"), equalTo("test_index")); assertThat(doSection.getApiCallSection().getParams().get("type"), equalTo("test_type")); assertThat(doSection.getApiCallSection().getParams().get("field"), equalTo("text,text1")); assertThat(doSection.getApiCallSection().hasBody(), equalTo(false)); assertThat(doSection.getApiCallSection().getBodies().size(), equalTo(0)); }
@SuppressWarnings("unchecked") public void testParseMatchSourceValues() throws Exception { parser = createParser(YamlXContent.yamlXContent, "{ _source: { responses.0.hits.total: 3, foo: bar }}" ); MatchAssertion matchAssertion = MatchAssertion.parse(parser); assertThat(matchAssertion, notNullValue()); assertThat(matchAssertion.getField(), equalTo("_source")); assertThat(matchAssertion.getExpectedValue(), instanceOf(Map.class)); Map<String, Object> expectedValue = (Map<String, Object>) matchAssertion.getExpectedValue(); assertThat(expectedValue.size(), equalTo(2)); Object o = expectedValue.get("responses.0.hits.total"); assertThat(o, instanceOf(Integer.class)); assertThat((Integer)o, equalTo(3)); o = expectedValue.get("foo"); assertThat(o, instanceOf(String.class)); assertThat(o.toString(), equalTo("bar")); }
public void testParseTeardownSection() throws Exception { parser = createParser(YamlXContent.yamlXContent, " - do:\n" + " delete:\n" + " index: foo\n" + " type: doc\n" + " id: 1\n" + " ignore: 404\n" + " - do:\n" + " delete2:\n" + " index: foo\n" + " type: doc\n" + " id: 1\n" + " ignore: 404" ); TeardownSection section = TeardownSection.parse(parser); assertThat(section, notNullValue()); assertThat(section.getSkipSection().isEmpty(), equalTo(true)); assertThat(section.getDoSections().size(), equalTo(2)); assertThat(section.getDoSections().get(0).getApiCallSection().getApi(), equalTo("delete")); assertThat(section.getDoSections().get(1).getApiCallSection().getApi(), equalTo("delete2")); }
public void testParseTestSectionWithDoSection() throws Exception { parser = createParser(YamlXContent.yamlXContent, "\"First test section\": \n" + " - do :\n" + " catch: missing\n" + " indices.get_warmer:\n" + " index: test_index\n" + " name: test_warmer" ); ClientYamlTestSection testSection = ClientYamlTestSection.parse(parser); assertThat(testSection, notNullValue()); assertThat(testSection.getName(), equalTo("First test section")); assertThat(testSection.getSkipSection(), equalTo(SkipSection.EMPTY)); assertThat(testSection.getExecutableSections().size(), equalTo(1)); DoSection doSection = (DoSection)testSection.getExecutableSections().get(0); assertThat(doSection.getCatch(), equalTo("missing")); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getApiCallSection().getApi(), equalTo("indices.get_warmer")); assertThat(doSection.getApiCallSection().getParams().size(), equalTo(2)); assertThat(doSection.getApiCallSection().hasBody(), equalTo(false)); }
public void testParseSetupSection() throws Exception { parser = createParser(YamlXContent.yamlXContent, " - do:\n" + " index1:\n" + " index: test_1\n" + " type: test\n" + " id: 1\n" + " body: { \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }\n" + " - do:\n" + " index2:\n" + " index: test_1\n" + " type: test\n" + " id: 2\n" + " body: { \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }\n" ); SetupSection setupSection = SetupSection.parse(parser); assertThat(setupSection, notNullValue()); assertThat(setupSection.getSkipSection().isEmpty(), equalTo(true)); assertThat(setupSection.getDoSections().size(), equalTo(2)); assertThat(setupSection.getDoSections().get(0).getApiCallSection().getApi(), equalTo("index1")); assertThat(setupSection.getDoSections().get(1).getApiCallSection().getApi(), equalTo("index2")); }
public void testPutIndexTemplateRequestSerializationXContent() throws IOException { PutIndexTemplateRequest request = new PutIndexTemplateRequest("foo"); String mapping = YamlXContent.contentBuilder().startObject().field("foo", "bar").endObject().string(); request.patterns(Collections.singletonList("foo")); request.mapping("bar", mapping, XContentType.YAML); assertNotEquals(mapping, request.mappings().get("bar")); assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), request.mappings().get("bar")); BytesStreamOutput out = new BytesStreamOutput(); request.writeTo(out); StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes); PutIndexTemplateRequest serialized = new PutIndexTemplateRequest(); serialized.readFrom(in); assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), serialized.mappings().get("bar")); }
public static ClientYamlTestSuite parse(String api, Path file) throws IOException { if (!Files.isRegularFile(file)) { throw new IllegalArgumentException(file.toAbsolutePath() + " is not a file"); } String filename = file.getFileName().toString(); //remove the file extension int i = filename.lastIndexOf('.'); if (i > 0) { filename = filename.substring(0, i); } //our yaml parser seems to be too tolerant. Each yaml suite must end with \n, otherwise clients tests might break. try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) { ByteBuffer bb = ByteBuffer.wrap(new byte[1]); channel.read(bb, channel.size() - 1); if (bb.get(0) != 10) { throw new IOException("test suite [" + api + "/" + filename + "] doesn't end with line feed (\\n)"); } } try (XContentParser parser = YamlXContent.yamlXContent.createParser(ExecutableSection.XCONTENT_REGISTRY, Files.newInputStream(file))) { return parse(api, filename, parser); } catch(Exception e) { throw new IOException("Error parsing " + api + "/" + filename, e); } }
private void parseAndExpectFailure(String brokenJson, String expectedErrorMessage) throws Exception { XContentParser parser = createParser(YamlXContent.yamlXContent, brokenJson); ClientYamlSuiteRestApiParser restApiParser = new ClientYamlSuiteRestApiParser(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> restApiParser.parse("location", parser)); assertThat(e.getMessage(), containsString(expectedErrorMessage)); }
public void testParseSkipSectionVersionNoFeature() throws Exception { parser = createParser(YamlXContent.yamlXContent, "version: \" - 2.1.0\"\n" + "reason: Delete ignores the parent param" ); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); assertThat(skipSection.getLowerVersion(), equalTo(VersionUtils.getFirstVersion())); assertThat(skipSection.getUpperVersion(), equalTo(Version.V_2_1_0)); assertThat(skipSection.getFeatures().size(), equalTo(0)); assertThat(skipSection.getReason(), equalTo("Delete ignores the parent param")); }
public void testParseSkipSectionAllVersions() throws Exception { parser = createParser(YamlXContent.yamlXContent, "version: \" all \"\n" + "reason: Delete ignores the parent param" ); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); assertThat(skipSection.getLowerVersion(), equalTo(VersionUtils.getFirstVersion())); assertThat(skipSection.getUpperVersion(), equalTo(Version.CURRENT)); assertThat(skipSection.getFeatures().size(), equalTo(0)); assertThat(skipSection.getReason(), equalTo("Delete ignores the parent param")); }
public void testParseSkipSectionFeatureNoVersion() throws Exception { parser = createParser(YamlXContent.yamlXContent, "features: regex" ); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); assertThat(skipSection.isVersionCheck(), equalTo(false)); assertThat(skipSection.getFeatures().size(), equalTo(1)); assertThat(skipSection.getFeatures().get(0), equalTo("regex")); assertThat(skipSection.getReason(), nullValue()); }
public void testParseSkipSectionFeaturesNoVersion() throws Exception { parser = createParser(YamlXContent.yamlXContent, "features: [regex1,regex2,regex3]" ); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); assertThat(skipSection.isVersionCheck(), equalTo(false)); assertThat(skipSection.getFeatures().size(), equalTo(3)); assertThat(skipSection.getFeatures().get(0), equalTo("regex1")); assertThat(skipSection.getFeatures().get(1), equalTo("regex2")); assertThat(skipSection.getFeatures().get(2), equalTo("regex3")); assertThat(skipSection.getReason(), nullValue()); }
public void testParseSkipSectionBothFeatureAndVersion() throws Exception { parser = createParser(YamlXContent.yamlXContent, "version: \" - 0.90.2\"\n" + "features: regex\n" + "reason: Delete ignores the parent param" ); SkipSection skipSection = SkipSection.parse(parser); assertEquals(VersionUtils.getFirstVersion(), skipSection.getLowerVersion()); assertEquals(Version.fromString("0.90.2"), skipSection.getUpperVersion()); assertEquals(Collections.singletonList("regex"), skipSection.getFeatures()); assertEquals("Delete ignores the parent param", skipSection.getReason()); }
public void testParseSkipSectionNoReason() throws Exception { parser = createParser(YamlXContent.yamlXContent, "version: \" - 0.90.2\"\n" ); Exception e = expectThrows(ParsingException.class, () -> SkipSection.parse(parser)); assertThat(e.getMessage(), is("reason is mandatory within skip version section")); }
public void testParseSkipSectionNoVersionNorFeature() throws Exception { parser = createParser(YamlXContent.yamlXContent, "reason: Delete ignores the parent param\n" ); Exception e = expectThrows(ParsingException.class, () -> SkipSection.parse(parser)); assertThat(e.getMessage(), is("version or features is mandatory within skip section")); }
public void testParseDoSectionNoParamsNoBody() throws Exception { parser = createParser(YamlXContent.yamlXContent, "cluster.node_info: {}" ); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("cluster.node_info")); assertThat(apiCallSection.getParams().size(), equalTo(0)); assertThat(apiCallSection.hasBody(), equalTo(false)); }
public void testParseDoSectionWithJsonMultipleBodiesAsLongString() throws Exception { String bodies[] = new String[]{ "{ \"index\": { \"_index\":\"test_index\", \"_type\":\"test_type\", \"_id\":\"test_id\" } }\n", "{ \"f1\":\"v1\", \"f2\":42 }\n", "{ \"index\": { \"_index\":\"test_index2\", \"_type\":\"test_type2\", \"_id\":\"test_id2\" } }\n", "{ \"f1\":\"v2\", \"f2\":47 }\n" }; parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body: |\n" + " " + bodies[0] + " " + bodies[1] + " " + bodies[2] + " " + bodies[3] ); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("bulk")); assertThat(apiCallSection.getParams().size(), equalTo(1)); assertThat(apiCallSection.getParams().get("refresh"), equalTo("true")); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(4)); }
public void testParseDoSectionWithJsonMultipleBodiesRepeatedProperty() throws Exception { assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled", XContent.isStrictDuplicateDetectionEnabled()); String[] bodies = new String[] { "{ \"index\": { \"_index\":\"test_index\", \"_type\":\"test_type\", \"_id\":\"test_id\" } }", "{ \"f1\":\"v1\", \"f2\":42 }", }; parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body: \n" + " " + bodies[0] + "\n" + " body: \n" + " " + bodies[1] ); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("bulk")); assertThat(apiCallSection.getParams().size(), equalTo(1)); assertThat(apiCallSection.getParams().get("refresh"), equalTo("true")); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(bodies.length)); for (int i = 0; i < bodies.length; i++) { assertJsonEquals(apiCallSection.getBodies().get(i), bodies[i]); } }
public void testParseDoSectionWithYamlMultipleBodies() throws Exception { parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body:\n" + " - index:\n" + " _index: test_index\n" + " _type: test_type\n" + " _id: test_id\n" + " - f1: v1\n" + " f2: 42\n" + " - index:\n" + " _index: test_index2\n" + " _type: test_type2\n" + " _id: test_id2\n" + " - f1: v2\n" + " f2: 47" ); String[] bodies = new String[4]; bodies[0] = "{\"index\": {\"_index\": \"test_index\", \"_type\": \"test_type\", \"_id\": \"test_id\"}}"; bodies[1] = "{ \"f1\":\"v1\", \"f2\": 42 }"; bodies[2] = "{\"index\": {\"_index\": \"test_index2\", \"_type\": \"test_type2\", \"_id\": \"test_id2\"}}"; bodies[3] = "{ \"f1\":\"v2\", \"f2\": 47 }"; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("bulk")); assertThat(apiCallSection.getParams().size(), equalTo(1)); assertThat(apiCallSection.getParams().get("refresh"), equalTo("true")); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(bodies.length)); for (int i = 0; i < bodies.length; i++) { assertJsonEquals(apiCallSection.getBodies().get(i), bodies[i]); } }
public void testParseDoSectionWithYamlMultipleBodiesRepeatedProperty() throws Exception { assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled", XContent.isStrictDuplicateDetectionEnabled()); parser = createParser(YamlXContent.yamlXContent, "bulk:\n" + " refresh: true\n" + " body:\n" + " index:\n" + " _index: test_index\n" + " _type: test_type\n" + " _id: test_id\n" + " body:\n" + " f1: v1\n" + " f2: 42\n" ); String[] bodies = new String[2]; bodies[0] = "{\"index\": {\"_index\": \"test_index\", \"_type\": \"test_type\", \"_id\": \"test_id\"}}"; bodies[1] = "{ \"f1\":\"v1\", \"f2\": 42 }"; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); assertThat(apiCallSection, notNullValue()); assertThat(apiCallSection.getApi(), equalTo("bulk")); assertThat(apiCallSection.getParams().size(), equalTo(1)); assertThat(apiCallSection.getParams().get("refresh"), equalTo("true")); assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(bodies.length)); for (int i = 0; i < bodies.length; i++) { assertJsonEquals(apiCallSection.getBodies().get(i), bodies[i]); } }
public void testParseDoSectionWithCatch() throws Exception { parser = createParser(YamlXContent.yamlXContent, "catch: missing\n" + "indices.get_warmer:\n" + " index: test_index\n" + " name: test_warmer" ); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), equalTo("missing")); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getApiCallSection().getApi(), equalTo("indices.get_warmer")); assertThat(doSection.getApiCallSection().getParams().size(), equalTo(2)); assertThat(doSection.getApiCallSection().hasBody(), equalTo(false)); }
public void testParseDoSectionWithoutClientCallSection() throws Exception { parser = createParser(YamlXContent.yamlXContent, "catch: missing\n" ); Exception e = expectThrows(IllegalArgumentException.class, () -> DoSection.parse(parser)); assertThat(e.getMessage(), is("client call section is mandatory within a do section")); }
public void testParseDoSectionExpectedWarnings() throws Exception { parser = createParser(YamlXContent.yamlXContent, "indices.get_field_mapping:\n" + " index: test_index\n" + " type: test_type\n" + "warnings:\n" + " - some test warning they are typically pretty long\n" + " - some other test warning somtimes they have [in] them" ); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getApiCallSection().getApi(), equalTo("indices.get_field_mapping")); assertThat(doSection.getApiCallSection().getParams().size(), equalTo(2)); assertThat(doSection.getApiCallSection().getParams().get("index"), equalTo("test_index")); assertThat(doSection.getApiCallSection().getParams().get("type"), equalTo("test_type")); assertThat(doSection.getApiCallSection().hasBody(), equalTo(false)); assertThat(doSection.getApiCallSection().getBodies().size(), equalTo(0)); assertThat(doSection.getExpectedWarningHeaders(), equalTo(Arrays.asList( "some test warning they are typically pretty long", "some other test warning somtimes they have [in] them"))); parser = createParser(YamlXContent.yamlXContent, "indices.get_field_mapping:\n" + " index: test_index\n" + "warnings:\n" + " - just one entry this time" ); doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getExpectedWarningHeaders(), equalTo(singletonList( "just one entry this time"))); }
private void assertJsonEquals(Map<String, Object> actual, String expected) throws IOException { Map<String,Object> expectedMap; try (XContentParser parser = createParser(YamlXContent.yamlXContent, expected)) { expectedMap = parser.mapOrdered(); } MatcherAssert.assertThat(actual, equalTo(expectedMap)); }
public void testParseSetSectionSingleValue() throws Exception { parser = createParser(YamlXContent.yamlXContent, "{ _id: id }" ); SetSection setSection = SetSection.parse(parser); assertThat(setSection, notNullValue()); assertThat(setSection.getStash(), notNullValue()); assertThat(setSection.getStash().size(), equalTo(1)); assertThat(setSection.getStash().get("_id"), equalTo("id")); }
public void testParseSetSectionMultipleValues() throws Exception { parser = createParser(YamlXContent.yamlXContent, "{ _id: id, _type: type, _index: index }" ); SetSection setSection = SetSection.parse(parser); assertThat(setSection, notNullValue()); assertThat(setSection.getStash(), notNullValue()); assertThat(setSection.getStash().size(), equalTo(3)); assertThat(setSection.getStash().get("_id"), equalTo("id")); assertThat(setSection.getStash().get("_type"), equalTo("type")); assertThat(setSection.getStash().get("_index"), equalTo("index")); }
public void testParseSetSectionNoValues() throws Exception { parser = createParser(YamlXContent.yamlXContent, "{ }" ); Exception e = expectThrows(ParsingException.class, () -> SetSection.parse(parser)); assertThat(e.getMessage(), is("set section must set at least a value")); }
public void testParseIsTrue() throws Exception { parser = createParser(YamlXContent.yamlXContent, "get.fields._timestamp" ); IsTrueAssertion trueAssertion = IsTrueAssertion.parse(parser); assertThat(trueAssertion, notNullValue()); assertThat(trueAssertion.getField(), equalTo("get.fields._timestamp")); }
public void testParseIsFalse() throws Exception { parser = createParser(YamlXContent.yamlXContent, "docs.1._source" ); IsFalseAssertion falseAssertion = IsFalseAssertion.parse(parser); assertThat(falseAssertion, notNullValue()); assertThat(falseAssertion.getField(), equalTo("docs.1._source")); }
public void testParseGreaterThan() throws Exception { parser = createParser(YamlXContent.yamlXContent, "{ field: 3}" ); GreaterThanAssertion greaterThanAssertion = GreaterThanAssertion.parse(parser); assertThat(greaterThanAssertion, notNullValue()); assertThat(greaterThanAssertion.getField(), equalTo("field")); assertThat(greaterThanAssertion.getExpectedValue(), instanceOf(Integer.class)); assertThat((Integer) greaterThanAssertion.getExpectedValue(), equalTo(3)); }