Skip to content

Commit

Permalink
Adds batch execution matcher
Browse files Browse the repository at this point in the history
Also adds ability to retrieve variable types of prepared statements
executed within batches.

Fixes #107.
  • Loading branch information
Adam Dougal committed Jan 16, 2017
1 parent da75c5e commit b3265f9
Show file tree
Hide file tree
Showing 12 changed files with 978 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Arrays;
import java.util.List;

import static org.scassandra.http.client.BatchType.LOGGED;

public final class BatchExecution {

private final List<BatchQuery> batchQueries;
Expand All @@ -39,6 +41,10 @@ public String getConsistency() {
return consistency;
}

public BatchType getBatchType() {
return batchType;
}

@Override
public boolean equals(Object o) {

Expand Down Expand Up @@ -78,8 +84,8 @@ public static BatchExecutionBuilder builder() {

public static class BatchExecutionBuilder {
private List<BatchQuery> batchQueries;
private String consistency;
private BatchType batchType;
private String consistency = "ONE";
private BatchType batchType = LOGGED;

private BatchExecutionBuilder() {
}
Expand All @@ -102,11 +108,23 @@ public BatchExecutionBuilder withBatchQueries(BatchQuery.BatchQueryBuilder... ba
return this;
}

/**
* Defaults to ONE if not set.
*
* @param consistency Query consistency
* @return this builder
*/
public BatchExecutionBuilder withConsistency(String consistency) {
this.consistency = consistency;
return this;
}

/**
* Defaults to LOGGED if not set.
*
* @param batchType Batch type
* @return this builder
*/
public BatchExecutionBuilder withBatchType(BatchType batchType) {
this.batchType = batchType;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.scassandra.cql.CqlType;

public final class BatchQuery {

private final String query;
private final BatchQueryKind batchQueryKind;
private final List<Object> variables;
private final List<CqlType> variableTypes;

private BatchQuery(String query, BatchQueryKind batchQueryKind, List<Object> variables) {
private BatchQuery(String query, BatchQueryKind batchQueryKind, List<Object> variables, List<CqlType> variableTypes) {
this.query = query;
this.batchQueryKind = batchQueryKind;
this.variables = variables;
this.variableTypes = variableTypes;
}

public String getQuery() {
Expand All @@ -43,12 +48,17 @@ public List<Object> getVariables() {
return variables;
}

public List<CqlType> getVariableTypes() {
return variableTypes;
}

@Override
public String toString() {
return "BatchQuery{" +
"query='" + query + '\'' +
", batchQueryKind=" + batchQueryKind +
", variables=" + variables +
", variableTypes=" + variableTypes +
'}';
}

Expand All @@ -61,15 +71,16 @@ public boolean equals(Object o) {

if (query != null ? !query.equals(that.query) : that.query != null) return false;
if (batchQueryKind != that.batchQueryKind) return false;
return !(variables != null ? !variables.equals(that.variables) : that.variables != null);

if (variables != null ? !variables.equals(that.variables) : that.variables != null) return false;
return variableTypes != null ? variableTypes.equals(that.variableTypes) : that.variableTypes == null;
}

@Override
public int hashCode() {
int result = query != null ? query.hashCode() : 0;
result = 31 * result + (batchQueryKind != null ? batchQueryKind.hashCode() : 0);
result = 31 * result + (variables != null ? variables.hashCode() : 0);
result = 31 * result + (variableTypes != null ? variableTypes.hashCode() : 0);
return result;
}

Expand All @@ -81,6 +92,7 @@ public static class BatchQueryBuilder {
private String query;
private BatchQueryKind batchQueryKind = BatchQueryKind.query;
private List<Object> variables = new ArrayList<Object>();
private List<CqlType> variableTypes = Collections.emptyList();

private BatchQueryBuilder() {
}
Expand All @@ -100,8 +112,13 @@ public BatchQueryBuilder withVariables(Object... variables) {
return this;
}

public BatchQueryBuilder withVariableTypes(CqlType... variableTypes) {
this.variableTypes = Arrays.asList(variableTypes);
return this;
}

public BatchQuery build() {
return new BatchQuery(query, batchQueryKind, variables);
return new BatchQuery(query, batchQueryKind, variables, variableTypes);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2016 Christopher Batey and Dogan Narinc
*
* Licensed 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 org.scassandra.matchers;

import org.hamcrest.Description;
import org.scassandra.cql.CqlType;
import org.scassandra.http.client.BatchExecution;
import org.scassandra.http.client.BatchQuery;

import java.util.List;

public class BatchExecutionMatcher extends ScassandraMatcher<List<BatchExecution>, BatchExecution> {

private final BatchExecution expectedBatchExecution;

BatchExecutionMatcher(BatchExecution expectedBatchExecution) {
if (expectedBatchExecution == null)
throw new IllegalArgumentException("null expectedBatchExecution");
this.expectedBatchExecution = expectedBatchExecution;
}

@Override
public void describeMismatchSafely(List<BatchExecution> batchExecutions, Description description) {
description.appendText("the following batches were executed: ");
for (BatchExecution batchExecution : batchExecutions) {
description.appendText("\n" + batchExecution);
}
}

@Override
public void describeTo(Description description) {
description.appendText("Expected batch " + expectedBatchExecution + " to be executed");
}

@Override
protected boolean match(BatchExecution actualBatchExecution) {
if (!actualBatchExecution.getConsistency().equals(expectedBatchExecution.getConsistency())) {
return false;
}

if (!actualBatchExecution.getBatchType().equals(expectedBatchExecution.getBatchType())) {
return false;
}

List<BatchQuery> expectedBatchQueries = expectedBatchExecution.getBatchQueries();
List<BatchQuery> actualBatchQueries = actualBatchExecution.getBatchQueries();

for (int i = 0; i < expectedBatchExecution.getBatchQueries().size(); i++) {
BatchQuery expectedBatchQuery = expectedBatchQueries.get(0);
BatchQuery actualBatchQuery = actualBatchQueries.get(0);

if (!expectedBatchQuery.getQuery().equals(actualBatchQuery.getQuery())) {
return false;
}

if (!expectedBatchQuery.getBatchQueryKind().equals(actualBatchQuery.getBatchQueryKind())) {
return false;
}

List<Object> expectedVariables = expectedBatchQuery.getVariables();
List<CqlType> actualVariableTypes = actualBatchQuery.getVariableTypes();
List<Object> actualVariables = actualBatchQuery.getVariables();

if (!checkVariables(expectedVariables, actualVariableTypes, actualVariables)) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.scassandra.matchers;

import org.scassandra.http.client.BatchExecution;
import org.scassandra.http.client.PreparedStatementExecution;
import org.scassandra.http.client.Query;

Expand All @@ -28,4 +29,8 @@ public static PreparedStatementMatcher preparedStatementRecorded(PreparedStateme
return new PreparedStatementMatcher(query);
}

public static BatchExecutionMatcher batchExecutionRecorded(BatchExecution batchExecution) {
return new BatchExecutionMatcher(batchExecution);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void testClearAllActivityHistory() {
public void testRetrievalOfBatchExecutions() {
//given
stubFor(get(urlEqualTo(batchUrl)).willReturn(aResponse()
.withBody("[{\"batchQueries\":[{\"query\":\"select * from people\", \"batchQueryKind\":\"query\", \"variables\": []}],\"consistency\":\"TWO\", \"batchType\":\"COUNTER\"}]")));
.withBody("[{\"batchQueries\":[{\"query\":\"select * from people\", \"batchQueryKind\":\"query\", \"variables\": [], \"variableTypes\": []}],\"consistency\":\"TWO\", \"batchType\":\"COUNTER\"}]")));
//when
List<BatchExecution> batchExecutions = underTest.retrieveBatches();
//then
Expand Down
Loading

0 comments on commit b3265f9

Please sign in to comment.