/graylog2-server/src/test/java/org/graylog/testing/elasticsearch/ContainerMatrixElasticsearchBaseTest.java

https://github.com/Graylog2/graylog2-server · Java · 97 lines · 48 code · 14 blank · 35 comment · 2 complexity · df2474276d0b70e69e62df3d048a1044 MD5 · raw file

  1. /*
  2. * Copyright (C) 2020 Graylog, Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the Server Side Public License, version 1,
  6. * as published by MongoDB, Inc.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * Server Side Public License for more details.
  12. *
  13. * You should have received a copy of the Server Side Public License
  14. * along with this program. If not, see
  15. * <http://www.mongodb.com/licensing/server-side-public-license>.
  16. */
  17. package org.graylog.testing.elasticsearch;
  18. import org.graylog2.indexer.MessageIndexTemplateProvider;
  19. import org.graylog2.storage.SearchVersion;
  20. import org.junit.jupiter.api.BeforeAll;
  21. import java.util.Collections;
  22. import java.util.Map;
  23. /**
  24. * This class can be used as base class for Elasticsearch integration tests.
  25. * <p>
  26. * Check the {@link #importFixture(String)} method if you need to load fixture data from JSON files.
  27. */
  28. public abstract class ContainerMatrixElasticsearchBaseTest {
  29. private final SearchServerInstance elasticsearch;
  30. public ContainerMatrixElasticsearchBaseTest(SearchServerInstance elasticsearch) {
  31. this.elasticsearch = elasticsearch;
  32. }
  33. protected SearchServerInstance elasticsearch() {
  34. return this.elasticsearch;
  35. }
  36. // override this in derived classes to skip import if the default template. See old ElasticsearchBaseTest + IndicesGetAllMessageFieldsIT
  37. protected boolean skipTemplates() {
  38. return false;
  39. }
  40. @BeforeAll
  41. public void before() {
  42. if (!skipTemplates()) {
  43. addGraylogDefaultIndexTemplate();
  44. }
  45. }
  46. private void addGraylogDefaultIndexTemplate() {
  47. addIndexTemplates(getGraylogDefaultMessageTemplates(elasticsearch().version()));
  48. }
  49. private static Map<String, Map<String, Object>> getGraylogDefaultMessageTemplates(SearchVersion version) {
  50. final Map<String, Object> template =
  51. new MessageIndexTemplateProvider().create(version, null)
  52. .messageTemplate("*", "standard", -1);
  53. return Collections.singletonMap("graylog-test-internal", template);
  54. }
  55. private void addIndexTemplates(Map<String, Map<String, Object>> templates) {
  56. for (Map.Entry<String, Map<String, Object>> template : templates.entrySet()) {
  57. final String templateName = template.getKey();
  58. elasticsearch().client().putTemplate(templateName, template.getValue());
  59. }
  60. }
  61. /**
  62. * Returns a custom Elasticsearch client with a bunch of utility methods.
  63. *
  64. * @return the client
  65. */
  66. protected Client client() {
  67. return elasticsearch().client();
  68. }
  69. /**
  70. * Import the given fixture resource path. The given path can be either a single file name or a full
  71. * resource path to a JSON fixture file. (e.g. "TheTest.json" or "org/graylog/test/TheTest.json")
  72. * If the resource path is a single filename, the method tries to find the resource in the resource path of
  73. * the test class.
  74. *
  75. * @param resourcePath the fixture resource path
  76. */
  77. protected void importFixture(String resourcePath) {
  78. elasticsearch().importFixtureResource(resourcePath, getClass());
  79. }
  80. protected SearchVersion elasticsearchVersion() {
  81. return elasticsearch().version();
  82. }
  83. }