Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbf5792

Browse files
harisomergify[bot]
authored andcommitted
Add AlterRepositoryPlanTest
1 parent b9aa7cb commit bbf5792

4 files changed

Lines changed: 174 additions & 8 deletions

File tree

server/src/main/java/io/crate/planner/node/ddl/AlterRepositoryPlan.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import io.crate.analyze.AnalyzedAlterRepository;
3535
import io.crate.analyze.SymbolEvaluator;
3636
import io.crate.analyze.repositories.RepositoryParamValidator;
37-
import io.crate.common.annotations.VisibleForTesting;
3837
import io.crate.data.Row;
3938
import io.crate.data.Row1;
4039
import io.crate.data.RowConsumer;
@@ -95,8 +94,7 @@ public void executeOrFail(DependencyCarrier dependencies,
9594
.whenComplete(new OneRowActionListener<>(consumer, rCount -> new Row1(rCount == null ? -1 : rCount)));
9695
}
9796

98-
@VisibleForTesting
99-
public static AlterRepositoryRequest createRequest(AnalyzedAlterRepository alterRepository,
97+
private AlterRepositoryRequest createRequest(AnalyzedAlterRepository alterRepository,
10098
CoordinatorTxnCtx txnCtx,
10199
NodeContext nodeCtx,
102100
Row parameters,
@@ -113,17 +111,14 @@ public static AlterRepositoryRequest createRequest(AnalyzedAlterRepository alter
113111
subQueryResults
114112
);
115113

114+
// make sure that only supported keys are used
116115
var genericProperties = alterRepository.properties().map(eval);
117116
var repository = repositoryService.getRepository(alterRepository.name());
118117
if (repository == null) {
119118
throw new RepositoryMissingException(alterRepository.name());
120119
}
121120

122-
// make sure that only supported keys are used
123-
var supportedKeys = repositoryParamValidator.settingsForType(repository.type())
124-
.all()
125-
.keySet();
126-
genericProperties.ensureContainsOnly(supportedKeys);
121+
repositoryParamValidator.validateSupportedOnly(repository.type(), genericProperties);
127122

128123
// build and return request
129124
return new AlterRepositoryRequest(

server/src/main/java/org/elasticsearch/action/ActionType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ public boolean equals(Object o) {
5959
public int hashCode() {
6060
return name.hashCode();
6161
}
62+
63+
@Override
64+
public String toString() {
65+
return name;
66+
}
6267
}

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/AlterRepositoryRequest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
2424

2525
import java.io.IOException;
26+
import java.util.Objects;
2627

2728
import org.elasticsearch.action.support.master.AcknowledgedRequest;
2829
import org.elasticsearch.common.io.stream.StreamInput;
@@ -67,4 +68,23 @@ public String name() {
6768
public Settings settings() {
6869
return this.settings;
6970
}
71+
72+
@Override
73+
public String toString() {
74+
return "AlterRepositoryRequest{" +
75+
"name=" + name + ";" +
76+
"settings=" + settings.toString() +
77+
'}';
78+
}
79+
80+
@Override
81+
public boolean equals(Object o) {
82+
if (!(o instanceof AlterRepositoryRequest that)) return false;
83+
return Objects.equals(name, that.name) && Objects.equals(settings, that.settings);
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
return Objects.hash(name, settings);
89+
}
7090
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Licensed to Crate.io GmbH ("Crate") under one or more contributor
3+
* license agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership. Crate licenses
5+
* this file to you under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License. You may
7+
* obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*
17+
* However, if you have executed another commercial license agreement
18+
* with Crate these terms will supersede the license and you may use the
19+
* software solely pursuant to the terms of the relevant commercial agreement.
20+
*/
21+
22+
package io.crate.planner.node.ddl;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.Mockito.doThrow;
28+
import static org.mockito.Mockito.when;
29+
30+
import java.util.Map;
31+
import java.util.concurrent.CompletableFuture;
32+
33+
import org.elasticsearch.Version;
34+
import org.elasticsearch.action.admin.cluster.repositories.put.AlterRepositoryRequest;
35+
import org.elasticsearch.action.admin.cluster.repositories.put.TransportAlterRepository;
36+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
37+
import org.elasticsearch.client.Client;
38+
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
39+
import org.elasticsearch.common.settings.Settings;
40+
import org.elasticsearch.repositories.RepositoryMissingException;
41+
import org.junit.Before;
42+
import org.junit.Test;
43+
import org.junit.runner.RunWith;
44+
import org.mockito.Answers;
45+
import org.mockito.Mock;
46+
import org.mockito.junit.MockitoJUnitRunner;
47+
48+
import io.crate.analyze.AnalyzedAlterRepository;
49+
import io.crate.analyze.repositories.RepositoryParamValidator;
50+
import io.crate.data.Row;
51+
import io.crate.data.testing.TestingRowConsumer;
52+
import io.crate.execution.ddl.RepositoryService;
53+
import io.crate.expression.symbol.Literal;
54+
import io.crate.planner.DependencyCarrier;
55+
import io.crate.planner.PlannerContext;
56+
import io.crate.planner.operators.SubQueryResults;
57+
import io.crate.sql.tree.GenericProperties;
58+
59+
@RunWith(MockitoJUnitRunner.class)
60+
public class AlterRepositoryPlanTest {
61+
@Mock
62+
RepositoryService repoService;
63+
@Mock
64+
RepositoryParamValidator repoParamValidator;
65+
@Mock
66+
Client client;
67+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
68+
PlannerContext plannerCtx;
69+
TestingRowConsumer rowConsumer;
70+
@Mock
71+
DependencyCarrier dependencyCarrier;
72+
73+
@Before
74+
public void setUp() {
75+
rowConsumer = new TestingRowConsumer();
76+
when(dependencyCarrier.repositoryService()).thenReturn(repoService);
77+
when(dependencyCarrier.repositoryParamValidator()).thenReturn(repoParamValidator);
78+
when(dependencyCarrier.client()).thenReturn(client);
79+
}
80+
81+
@Test
82+
public void test_execute_or_fail() throws Exception {
83+
var underTest = new AlterRepositoryPlan(
84+
new AnalyzedAlterRepository(
85+
"repo-name",
86+
new GenericProperties<>(Map.of("location", Literal.of("/tmp/data")))
87+
)
88+
);
89+
90+
when(repoService.getRepository("repo-name"))
91+
.thenReturn(new RepositoryMetadata("repo-name", "dummy-type", Settings.EMPTY));
92+
when(client.execute(
93+
TransportAlterRepository.ACTION, new AlterRepositoryRequest("repo-name", Settings.builder().put("location", "/tmp/data").build())
94+
)).thenReturn(CompletableFuture.completedFuture(new AcknowledgedResponse(true)));
95+
96+
underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY);
97+
98+
assertThat(rowConsumer.getResult()).hasSize(1);
99+
assertThat(rowConsumer.getResult().getFirst()).hasSize(1);
100+
// the number of rows changed
101+
assertThat(rowConsumer.getResult().getFirst()[0]).isEqualTo(1L);
102+
}
103+
104+
@Test
105+
public void test_execute_or_fail_missing_repository() {
106+
var underTest = new AlterRepositoryPlan(
107+
new AnalyzedAlterRepository("repo-name", new GenericProperties<>(Map.of()))
108+
);
109+
110+
assertThatThrownBy(() ->
111+
underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY)
112+
).isExactlyInstanceOf(RepositoryMissingException.class)
113+
.hasMessageContaining("[repo-name]");
114+
}
115+
116+
@Test
117+
public void test_execute_or_fail_unsupported_property() {
118+
var underTest = new AlterRepositoryPlan(
119+
new AnalyzedAlterRepository("repo-name", new GenericProperties<>(Map.of()))
120+
);
121+
122+
when(repoService.getRepository("repo-name"))
123+
.thenReturn(new RepositoryMetadata("repo-name", "dummy-type", Settings.EMPTY));
124+
doThrow(new IllegalArgumentException("invalid property"))
125+
.when(repoParamValidator).validateSupportedOnly(any(), any());
126+
127+
assertThatThrownBy(() ->
128+
underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY)
129+
).isExactlyInstanceOf(IllegalArgumentException.class)
130+
.hasMessageContaining("invalid property");
131+
}
132+
133+
@Test
134+
public void test_execute_or_fail_min_node_version() {
135+
var underTest = new AlterRepositoryPlan(
136+
new AnalyzedAlterRepository("repo-name", new GenericProperties<>(Map.of()))
137+
);
138+
139+
when(plannerCtx.clusterState().nodes().getMinNodeVersion()).thenReturn(Version.V_6_3_2);
140+
141+
assertThatThrownBy(() ->
142+
underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY)
143+
).isExactlyInstanceOf(IllegalStateException.class)
144+
.hasMessageContaining("Cannot execute ALTER REPOSITORY while there are <6.4.0 nodes in the cluster");
145+
}
146+
}

0 commit comments

Comments
 (0)