|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.test.eql; |
| 7 | + |
| 8 | +import org.apache.http.util.EntityUtils; |
| 9 | +import org.elasticsearch.client.Request; |
| 10 | +import org.elasticsearch.client.Response; |
| 11 | +import org.elasticsearch.client.ResponseException; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +import static org.hamcrest.Matchers.containsString; |
| 20 | +import static org.hamcrest.Matchers.is; |
| 21 | + |
| 22 | +public abstract class CommonEqlRestTestCase extends ESRestTestCase { |
| 23 | + |
| 24 | + static class SearchTestConfiguration { |
| 25 | + final String input; |
| 26 | + final int expectedStatus; |
| 27 | + final String expectedMessage; |
| 28 | + |
| 29 | + SearchTestConfiguration(String input, int status, String msg) { |
| 30 | + this.input = input; |
| 31 | + this.expectedStatus = status; |
| 32 | + this.expectedMessage = msg; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static final String defaultValidationIndexName = "eql_search_validation_test"; |
| 37 | + private static final String validRule = "process where user = 'SYSTEM'"; |
| 38 | + |
| 39 | + public static final ArrayList<SearchTestConfiguration> searchValidationTests; |
| 40 | + static { |
| 41 | + searchValidationTests = new ArrayList<>(); |
| 42 | + searchValidationTests.add(new SearchTestConfiguration(null, 400, "request body or source parameter is required")); |
| 43 | + searchValidationTests.add(new SearchTestConfiguration("{}", 400, "rule is null or empty")); |
| 44 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"\"}", 400, "rule is null or empty")); |
| 45 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"timestamp_field\": \"\"}", |
| 46 | + 400, "timestamp field is null or empty")); |
| 47 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"event_type_field\": \"\"}", |
| 48 | + 400, "event type field is null or empty")); |
| 49 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"implicit_join_key_field\": \"\"}", |
| 50 | + 400, "implicit join key field is null or empty")); |
| 51 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"size\": 0}", |
| 52 | + 400, "size must be more than 0")); |
| 53 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"size\": -1}", |
| 54 | + 400, "size must be more than 0")); |
| 55 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"search_after\": null}", |
| 56 | + 400, "search_after doesn't support values of type: VALUE_NULL")); |
| 57 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"search_after\": []}", |
| 58 | + 400, "must contains at least one value")); |
| 59 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"query\": null}", |
| 60 | + 400, "query doesn't support values of type: VALUE_NULL")); |
| 61 | + searchValidationTests.add(new SearchTestConfiguration("{\"rule\": \"" + validRule + "\", \"query\": {}}", |
| 62 | + 400, "query malformed, empty clause found")); |
| 63 | + } |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setup() throws Exception { |
| 67 | + createIndex(defaultValidationIndexName, Settings.EMPTY); |
| 68 | + } |
| 69 | + |
| 70 | + @After |
| 71 | + public void cleanup() throws Exception { |
| 72 | + deleteIndex(defaultValidationIndexName); |
| 73 | + } |
| 74 | + |
| 75 | + public void testSearchValidationFailures() throws Exception { |
| 76 | + final String contentType = "application/json"; |
| 77 | + for (SearchTestConfiguration config : searchValidationTests) { |
| 78 | + final String endpoint = "/" + defaultValidationIndexName + "/_eql/search"; |
| 79 | + Request request = new Request("GET", endpoint); |
| 80 | + request.setJsonEntity(config.input); |
| 81 | + |
| 82 | + Response response = null; |
| 83 | + if (config.expectedStatus == 400) { |
| 84 | + ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request)); |
| 85 | + response = e.getResponse(); |
| 86 | + } else { |
| 87 | + response = client().performRequest(request); |
| 88 | + } |
| 89 | + |
| 90 | + assertThat(response.getHeader("Content-Type"), containsString(contentType)); |
| 91 | + assertThat(EntityUtils.toString(response.getEntity()), containsString(config.expectedMessage)); |
| 92 | + assertThat(response.getStatusLine().getStatusCode(), is(config.expectedStatus)); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments