|
@@ -3,44 +3,71 @@
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
*/
|
|
|
-package org.elasticsearch.xpack.spatial;
|
|
|
+package org.elasticsearch.xpack.spatial.action;
|
|
|
|
|
|
+import org.elasticsearch.Version;
|
|
|
+import org.elasticsearch.action.ActionListener;
|
|
|
import org.elasticsearch.action.support.ActionFilters;
|
|
|
import org.elasticsearch.action.support.PlainActionFuture;
|
|
|
+import org.elasticsearch.client.Client;
|
|
|
+import org.elasticsearch.cluster.ClusterName;
|
|
|
+import org.elasticsearch.cluster.ClusterState;
|
|
|
+import org.elasticsearch.cluster.metadata.Metadata;
|
|
|
+import org.elasticsearch.cluster.node.DiscoveryNode;
|
|
|
+import org.elasticsearch.cluster.service.ClusterService;
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
-import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.license.XPackLicenseState;
|
|
|
+import org.elasticsearch.tasks.Task;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.transport.TransportService;
|
|
|
import org.elasticsearch.xpack.core.XPackFeatureSet;
|
|
|
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse;
|
|
|
import org.elasticsearch.xpack.core.spatial.SpatialFeatureSetUsage;
|
|
|
+import org.elasticsearch.xpack.core.spatial.action.SpatialStatsAction;
|
|
|
import org.junit.Before;
|
|
|
+import org.mockito.stubbing.Answer;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
|
|
|
import static org.hamcrest.core.Is.is;
|
|
|
+import static org.mockito.Matchers.any;
|
|
|
+import static org.mockito.Matchers.eq;
|
|
|
+import static org.mockito.Mockito.doAnswer;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
public class SpatialInfoTransportActionTests extends ESTestCase {
|
|
|
|
|
|
private XPackLicenseState licenseState;
|
|
|
+ private ClusterService clusterService;
|
|
|
|
|
|
@Before
|
|
|
public void init() {
|
|
|
licenseState = mock(XPackLicenseState.class);
|
|
|
+ clusterService = mock(ClusterService.class);
|
|
|
+
|
|
|
+ DiscoveryNode discoveryNode = new DiscoveryNode("nodeId", buildNewFakeTransportAddress(), Version.CURRENT);
|
|
|
+ when(clusterService.localNode()).thenReturn(discoveryNode);
|
|
|
+ ClusterName clusterName = new ClusterName("cluster_name");
|
|
|
+ when(clusterService.getClusterName()).thenReturn(clusterName);
|
|
|
+ ClusterState clusterState = mock(ClusterState.class);
|
|
|
+ when(clusterState.getMetadata()).thenReturn(Metadata.EMPTY_METADATA);
|
|
|
+ when(clusterState.getClusterName()).thenReturn(clusterName);
|
|
|
+ when(clusterService.state()).thenReturn(clusterState);
|
|
|
}
|
|
|
|
|
|
public void testAvailable() throws Exception {
|
|
|
SpatialInfoTransportAction featureSet = new SpatialInfoTransportAction(
|
|
|
- mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, licenseState);
|
|
|
+ mock(TransportService.class), mock(ActionFilters.class), licenseState);
|
|
|
boolean available = randomBoolean();
|
|
|
when(licenseState.isAllowed(XPackLicenseState.Feature.SPATIAL)).thenReturn(available);
|
|
|
assertThat(featureSet.available(), is(available));
|
|
|
|
|
|
- var usageAction = new SpatialUsageTransportAction(mock(TransportService.class), null, null,
|
|
|
- mock(ActionFilters.class), null, Settings.EMPTY, licenseState);
|
|
|
+ var usageAction = new SpatialUsageTransportAction(mock(TransportService.class), clusterService, null,
|
|
|
+ mock(ActionFilters.class), null, licenseState, mockClient());
|
|
|
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
|
|
|
- usageAction.masterOperation(null, null, null, future);
|
|
|
+ Task task = new Task(1L, "_type", "_action", "_description", null, Collections.emptyMap());
|
|
|
+ usageAction.masterOperation(task, null, clusterService.state(), future);
|
|
|
XPackFeatureSet.Usage usage = future.get().getUsage();
|
|
|
assertThat(usage.available(), is(available));
|
|
|
|
|
@@ -51,16 +78,16 @@ public class SpatialInfoTransportActionTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testEnabled() throws Exception {
|
|
|
- Settings.Builder settings = Settings.builder();
|
|
|
SpatialInfoTransportAction featureSet = new SpatialInfoTransportAction(
|
|
|
- mock(TransportService.class), mock(ActionFilters.class), settings.build(), licenseState);
|
|
|
+ mock(TransportService.class), mock(ActionFilters.class), licenseState);
|
|
|
assertThat(featureSet.enabled(), is(true));
|
|
|
assertTrue(featureSet.enabled());
|
|
|
|
|
|
SpatialUsageTransportAction usageAction = new SpatialUsageTransportAction(mock(TransportService.class),
|
|
|
- null, null, mock(ActionFilters.class), null, settings.build(), licenseState);
|
|
|
+ clusterService, null, mock(ActionFilters.class), null,
|
|
|
+ licenseState, mockClient());
|
|
|
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
|
|
|
- usageAction.masterOperation(null, null, null, future);
|
|
|
+ usageAction.masterOperation(null, null, clusterService.state(), future);
|
|
|
XPackFeatureSet.Usage usage = future.get().getUsage();
|
|
|
assertTrue(usage.enabled());
|
|
|
|
|
@@ -70,4 +97,16 @@ public class SpatialInfoTransportActionTests extends ESTestCase {
|
|
|
assertTrue(serializedUsage.enabled());
|
|
|
}
|
|
|
|
|
|
+ private Client mockClient() {
|
|
|
+ Client client = mock(Client.class);
|
|
|
+ doAnswer((Answer<Void>) invocation -> {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ ActionListener<SpatialStatsAction.Response> listener =
|
|
|
+ (ActionListener<SpatialStatsAction.Response>) invocation.getArguments()[2];
|
|
|
+ listener.onResponse(new SpatialStatsAction.Response(clusterService.getClusterName(),
|
|
|
+ Collections.emptyList(), Collections.emptyList()));
|
|
|
+ return null;
|
|
|
+ }).when(client).execute(eq(SpatialStatsAction.INSTANCE), any(), any());
|
|
|
+ return client;
|
|
|
+ }
|
|
|
}
|