-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert "CountOperationTest" unit test to use native CQL driver (#672)
- Loading branch information
1 parent
a4d5254
commit b643007
Showing
4 changed files
with
348 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/java/io/stargate/sgv2/jsonapi/service/operation/model/impl/OperationTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.stargate.sgv2.jsonapi.service.operation.model.impl; | ||
|
||
import com.datastax.oss.driver.api.core.ProtocolVersion; | ||
import com.datastax.oss.driver.api.core.cql.ColumnDefinition; | ||
import com.datastax.oss.driver.api.core.cql.ColumnDefinitions; | ||
import com.datastax.oss.driver.api.core.detach.AttachmentPoint; | ||
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs; | ||
import com.datastax.oss.driver.internal.core.cql.DefaultColumnDefinition; | ||
import com.datastax.oss.driver.internal.core.cql.DefaultColumnDefinitions; | ||
import com.datastax.oss.protocol.internal.response.result.ColumnSpec; | ||
import com.datastax.oss.protocol.internal.response.result.RawType; | ||
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
|
||
public class OperationTestBase { | ||
protected final String KEYSPACE_NAME = RandomStringUtils.randomAlphanumeric(16); | ||
protected final String COLLECTION_NAME = RandomStringUtils.randomAlphanumeric(16); | ||
protected final CommandContext CONTEXT = new CommandContext(KEYSPACE_NAME, COLLECTION_NAME); | ||
|
||
protected ColumnDefinitions buildColumnDefs(List<TestColumn> columns) { | ||
return buildColumnDefs(KEYSPACE_NAME, COLLECTION_NAME, columns); | ||
} | ||
|
||
protected ColumnDefinitions buildColumnDefs( | ||
String ks, String tableName, List<TestColumn> columns) { | ||
List<ColumnDefinition> columnDefs = new ArrayList<>(); | ||
for (int ix = 0, end = columns.size(); ix < end; ++ix) { | ||
columnDefs.add( | ||
new DefaultColumnDefinition( | ||
new ColumnSpec( | ||
ks, | ||
tableName, | ||
columns.get(ix).name(), | ||
ix, | ||
RawType.PRIMITIVES.get(columns.get(ix).type())), | ||
AttachmentPoint.NONE)); | ||
} | ||
return DefaultColumnDefinitions.valueOf(columnDefs); | ||
} | ||
|
||
protected ByteBuffer byteBufferFrom(long value) { | ||
return TypeCodecs.BIGINT.encode(value, ProtocolVersion.DEFAULT); | ||
} | ||
|
||
protected record TestColumn(String name, int type) { | ||
static TestColumn of(String name, int type) { | ||
return new TestColumn(name, type); | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/test/java/io/stargate/sgv2/jsonapi/service/testutil/MockAsyncResultSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.stargate.sgv2.jsonapi.service.testutil; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import com.datastax.oss.driver.api.core.cql.AsyncResultSet; | ||
import com.datastax.oss.driver.api.core.cql.ColumnDefinitions; | ||
import com.datastax.oss.driver.api.core.cql.ExecutionInfo; | ||
import com.datastax.oss.driver.api.core.cql.Row; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
public class MockAsyncResultSet implements AsyncResultSet { | ||
|
||
private final List<Row> rows; | ||
private final Iterator<Row> iterator; | ||
private final CompletionStage<AsyncResultSet> nextPage; | ||
private final ExecutionInfo executionInfo = mock(ExecutionInfo.class); | ||
private final ColumnDefinitions columnDefs; | ||
private int remaining; | ||
|
||
public MockAsyncResultSet( | ||
ColumnDefinitions columnDefs, List<Row> rows, CompletionStage<AsyncResultSet> nextPage) { | ||
this.columnDefs = columnDefs; | ||
this.rows = rows; | ||
iterator = rows.iterator(); | ||
remaining = rows.size(); | ||
this.nextPage = nextPage; | ||
} | ||
|
||
@Override | ||
public Row one() { | ||
Row next = iterator.next(); | ||
remaining--; | ||
return next; | ||
} | ||
|
||
@Override | ||
public int remaining() { | ||
return remaining; | ||
} | ||
|
||
@Override | ||
public List<Row> currentPage() { | ||
return new ArrayList<>(rows); | ||
} | ||
|
||
@Override | ||
public boolean hasMorePages() { | ||
return nextPage != null; | ||
} | ||
|
||
@Override | ||
public CompletionStage<AsyncResultSet> fetchNextPage() throws IllegalStateException { | ||
return nextPage; | ||
} | ||
|
||
@Override | ||
public ColumnDefinitions getColumnDefinitions() { | ||
return columnDefs; | ||
} | ||
|
||
@Override | ||
public ExecutionInfo getExecutionInfo() { | ||
return executionInfo; | ||
} | ||
|
||
@Override | ||
public boolean wasApplied() { | ||
return true; | ||
} | ||
} |
Oops, something went wrong.