|
| 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.xpack.enrich; |
| 7 | + |
| 8 | +import org.elasticsearch.index.reindex.ReindexPlugin; |
| 9 | +import org.elasticsearch.plugins.Plugin; |
| 10 | +import org.elasticsearch.test.ESIntegTestCase; |
| 11 | +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; |
| 12 | +import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction; |
| 13 | +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; |
| 14 | + |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.DECORATE_FIELDS; |
| 20 | +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.KEY_FIELD; |
| 21 | +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.POLICY_NAME; |
| 22 | +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.SOURCE_INDEX_NAME; |
| 23 | +import static org.hamcrest.Matchers.equalTo; |
| 24 | +import static org.hamcrest.Matchers.is; |
| 25 | + |
| 26 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) |
| 27 | +public class EnrichRestartIT extends ESIntegTestCase { |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 31 | + return List.of(EnrichPlugin.class, ReindexPlugin.class); |
| 32 | + } |
| 33 | + |
| 34 | + public void testRestart() throws Exception { |
| 35 | + final int numPolicies = randomIntBetween(2, 4); |
| 36 | + internalCluster().startNode(); |
| 37 | + |
| 38 | + EnrichPolicy enrichPolicy = |
| 39 | + new EnrichPolicy(EnrichPolicy.EXACT_MATCH_TYPE, null, List.of(SOURCE_INDEX_NAME), KEY_FIELD, List.of(DECORATE_FIELDS)); |
| 40 | + for (int i = 0; i < numPolicies; i++) { |
| 41 | + String policyName = POLICY_NAME + i; |
| 42 | + PutEnrichPolicyAction.Request request = new PutEnrichPolicyAction.Request(policyName, enrichPolicy); |
| 43 | + client().execute(PutEnrichPolicyAction.INSTANCE, request).actionGet(); |
| 44 | + } |
| 45 | + |
| 46 | + verifyPolicies(numPolicies, enrichPolicy); |
| 47 | + // After full restart the policies should still exist: |
| 48 | + internalCluster().fullRestart(); |
| 49 | + verifyPolicies(numPolicies, enrichPolicy); |
| 50 | + } |
| 51 | + |
| 52 | + private static void verifyPolicies(int numPolicies, EnrichPolicy enrichPolicy) { |
| 53 | + ListEnrichPolicyAction.Response response = |
| 54 | + client().execute(ListEnrichPolicyAction.INSTANCE, new ListEnrichPolicyAction.Request()).actionGet(); |
| 55 | + assertThat(response.getPolicies().size(), equalTo(numPolicies)); |
| 56 | + for (int i = 0; i < numPolicies; i++) { |
| 57 | + String policyName = POLICY_NAME + i; |
| 58 | + Optional<EnrichPolicy.NamedPolicy> result = response.getPolicies().stream() |
| 59 | + .filter(namedPolicy -> namedPolicy.getName().equals(policyName)) |
| 60 | + .findFirst(); |
| 61 | + assertThat(result.isPresent(), is(true)); |
| 62 | + assertThat(result.get().getPolicy(), equalTo(enrichPolicy)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments