|
6 | 6 | package com.datastax.mgmtapi;
|
7 | 7 |
|
8 | 8 | import javax.ws.rs.core.MediaType;
|
| 9 | +import java.io.IOException; |
9 | 10 | import java.net.URISyntaxException;
|
10 | 11 | import java.util.ArrayList;
|
11 | 12 | import java.util.Arrays;
|
| 13 | +import java.util.Collections; |
12 | 14 | import java.util.List;
|
13 | 15 | import java.util.Map;
|
14 | 16 |
|
|
21 | 23 | import com.datastax.mgmtapi.resources.NodeOpsResources;
|
22 | 24 | import com.datastax.mgmtapi.resources.TableOpsResources;
|
23 | 25 | import com.datastax.mgmtapi.resources.models.CompactRequest;
|
| 26 | +import com.datastax.mgmtapi.resources.models.CreateKeyspaceRequest; |
24 | 27 | import com.datastax.mgmtapi.resources.models.KeyspaceRequest;
|
| 28 | +import com.datastax.mgmtapi.resources.models.ReplicationSetting; |
25 | 29 | import com.datastax.mgmtapi.resources.models.ScrubRequest;
|
| 30 | +import org.apache.http.ConnectionClosedException; |
26 | 31 | import org.apache.http.HttpStatus;
|
| 32 | +import org.assertj.core.api.Assertions; |
27 | 33 | import org.jboss.resteasy.core.messagebody.WriterUtility;
|
28 | 34 | import org.jboss.resteasy.mock.MockDispatcherFactory;
|
29 | 35 | import org.jboss.resteasy.mock.MockHttpRequest;
|
|
37 | 43 | import com.datastax.oss.driver.api.core.cql.Row;
|
38 | 44 |
|
39 | 45 | import static org.apache.commons.lang3.StringUtils.EMPTY;
|
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
40 | 47 | import static org.mockito.ArgumentMatchers.any;
|
41 | 48 | import static org.mockito.ArgumentMatchers.anyString;
|
42 | 49 | import static org.mockito.ArgumentMatchers.eq;
|
@@ -990,6 +997,59 @@ public void testGetStreamInfo() throws Exception
|
990 | 997 | verify(context.cqlService).executeCql(any(), eq("CALL NodeOps.getStreamInfo()"));
|
991 | 998 | }
|
992 | 999 |
|
| 1000 | + @Test |
| 1001 | + public void testCreatingKeyspace() throws IOException, URISyntaxException |
| 1002 | + { |
| 1003 | + CreateKeyspaceRequest keyspaceRequest = new CreateKeyspaceRequest("myKeyspace", Arrays.asList(new ReplicationSetting("dc1", 3), new ReplicationSetting("dc2", 3))); |
| 1004 | + |
| 1005 | + Context context = setup(); |
| 1006 | + |
| 1007 | + when(context.cqlService.executePreparedStatement(any(), anyString())) |
| 1008 | + .thenReturn(null); |
| 1009 | + |
| 1010 | + String keyspaceRequestAsJSON = WriterUtility.asString(keyspaceRequest, MediaType.APPLICATION_JSON); |
| 1011 | + MockHttpResponse response = postWithBody("/ops/keyspace/create", keyspaceRequestAsJSON, context); |
| 1012 | + |
| 1013 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); |
| 1014 | + assertThat(response.getContentAsString()).contains("OK"); |
| 1015 | + |
| 1016 | + verify(context.cqlService).executePreparedStatement(any(), eq("CALL NodeOps.createKeyspace(?, ?)"), any()); |
| 1017 | + } |
| 1018 | + |
| 1019 | + @Test |
| 1020 | + public void testCreatingEmptyKeyspaceShouldFail() throws IOException, URISyntaxException |
| 1021 | + { |
| 1022 | + CreateKeyspaceRequest keyspaceRequest = new CreateKeyspaceRequest("", Arrays.asList(new ReplicationSetting("dc1", 3), new ReplicationSetting("dc2", 3))); |
| 1023 | + |
| 1024 | + Context context = setup(); |
| 1025 | + |
| 1026 | + when(context.cqlService.executePreparedStatement(any(), anyString())) |
| 1027 | + .thenReturn(null); |
| 1028 | + |
| 1029 | + String keyspaceRequestAsJSON = WriterUtility.asString(keyspaceRequest, MediaType.APPLICATION_JSON); |
| 1030 | + MockHttpResponse response = postWithBody("/ops/keyspace/create", keyspaceRequestAsJSON, context); |
| 1031 | + |
| 1032 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_BAD_REQUEST); |
| 1033 | + assertThat(response.getContentAsString()).contains("Keyspace creation failed. Non-empty 'keyspace_name' must be provided"); |
| 1034 | + } |
| 1035 | + |
| 1036 | + @Test |
| 1037 | + public void testCreatingEmptyReplicationSettingsShouldFail() throws IOException, URISyntaxException |
| 1038 | + { |
| 1039 | + CreateKeyspaceRequest keyspaceRequest = new CreateKeyspaceRequest("TestKeyspace", Collections.emptyList()); |
| 1040 | + |
| 1041 | + Context context = setup(); |
| 1042 | + |
| 1043 | + when(context.cqlService.executePreparedStatement(any(), anyString())) |
| 1044 | + .thenReturn(null); |
| 1045 | + |
| 1046 | + String keyspaceRequestAsJSON = WriterUtility.asString(keyspaceRequest, MediaType.APPLICATION_JSON); |
| 1047 | + MockHttpResponse response = postWithBody("/ops/keyspace/create", keyspaceRequestAsJSON, context); |
| 1048 | + |
| 1049 | + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_BAD_REQUEST); |
| 1050 | + assertThat(response.getContentAsString()).contains("Keyspace creation failed. 'replication_settings' must be provided"); |
| 1051 | + } |
| 1052 | + |
993 | 1053 | private MockHttpResponse postWithBody(String path, String body, Context context) throws URISyntaxException {
|
994 | 1054 | MockHttpRequest request = MockHttpRequest
|
995 | 1055 | .post(ROOT_PATH + path)
|
|
0 commit comments