Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support matching on variables that are collections #163

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion docs/java/priming-prepared-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,22 @@ outcomes as they will overwrite each other.

If none of the outcomes match then a RuntimeException will currently be thrown. This will be replaced with default behaviour of returning an empty successful result.

Collections are not currently supported for variable matching.
Collections can also be used for variable matching:

```
MultiPrimeRequest prime = MultiPrimeRequest.request()
.withWhen(when()
.withQuery("select * from person where nicknames = ?"))
.withThen(then()
.withVariableTypes(list(TEXT))
.withOutcomes(
outcome(match().withVariableMatchers(exactMatch().withMatcher(Lists.newArrayList("Zen", "Nez")).build()), action().withResult(Result.success))
)
)
.build();

primingClient.multiPrime(prime);
```

#### Priming errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.scassandra.cql.ListType.*;

import java.util.*;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
Expand Down Expand Up @@ -739,22 +742,25 @@ public void testMultiPrime() throws Exception {

MultiPrimeRequest prime = MultiPrimeRequest.request()
.withWhen(when()
.withQuery("select * from person where name = ?"))
.withQuery("select * from person where name = ? and nicknames = ?"))
.withThen(then()
.withVariableTypes(TEXT)
.withOutcomes(outcome(match().withVariableMatchers(exactMatch().withMatcher("Chris").build()), action()))
.withVariableTypes(TEXT, list(TEXT))
.withOutcomes(
outcome(match().withVariableMatchers(exactMatch().withMatcher("Chris").build()), action()),
outcome(match().withVariableMatchers(exactMatch().withMatcher(Lists.newArrayList("Zen", "Nez")).build()), action())
)
)
.build();

underTest.multiPrime(prime);

verify(postRequestedFor(urlEqualTo(PRIME_PREPARED_MULTI_PATH))
.withHeader("Content-Type", equalTo("application/json; charset=UTF-8"))
.withRequestBody(equalToJson("{\"when\":{\"query\":\"select * from person where name \\u003d ?\"}," +
"\"then\":{\"variable_types\":[\"text\"],\"outcomes\":[{\"criteria\":" +
"{\"variable_matcher\":[{\"matcher\":\"Chris\",\"type\":\"exact\"}]},\"action\":{}}]}}"))

);
.withRequestBody(equalToJson("{\"when\":{\"query\":\"select * from person where name \\u003d ? and nicknames \\u003d ?\"}," +
"\"then\":{\"variable_types\":[\"text\",\"list\\u003ctext\\u003e\"],\"outcomes\":[" +
"{\"criteria\":{\"variable_matcher\":[{\"matcher\":\"Chris\",\"type\":\"exact\"}]},\"action\":{}}," +
"{\"criteria\":{\"variable_matcher\":[{\"matcher\":[\"Zen\",\"Nez\"],\"type\":\"exact\"}]},\"action\":{}}]}})")
));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import common.AbstractScassandraTest;
import common.CassandraExecutor;
import common.CassandraResult;
import common.CassandraRow;
import org.junit.Test;
import org.scassandra.cql.CqlText;
import org.scassandra.cql.CqlType;
import org.scassandra.cql.ListType;
import org.scassandra.cql.PrimitiveType;
import org.scassandra.http.client.Consistency;
import org.scassandra.http.client.MultiPrimeRequest;
Expand All @@ -23,6 +26,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.scassandra.cql.ListType.*;
import static org.scassandra.cql.PrimitiveType.*;
import static org.scassandra.http.client.MultiPrimeRequest.*;

Expand Down Expand Up @@ -312,5 +316,29 @@ public void testPrimeBasedOnMatchTimestamps() throws Exception {
assertEquals(Result.read_request_timeout, result.status().getResult());
}

@Test
public void testPrimeBasedOnMatchCollection() {
String query = "select * from person where nicknames = ?";
MultiPrimeRequest prime = MultiPrimeRequest.request()
.withWhen(when()
.withQuery(query))
.withThen(then()
.withVariableTypes(list(TEXT))
.withOutcomes(
outcome(match().withVariableMatchers(exactMatch().withMatcher(Lists.newArrayList("Zen", "Nez")).build()), action().withResult(Result.success)),
outcome(match().withVariableMatchers(exactMatch().withMatcher(Lists.newArrayList("timeout")).build()), action().withResult(Result.read_request_timeout))
)
)
.build();

primingClient.multiPrime(prime);

CassandraResult successResult = cassandra().prepareAndExecute(query, Lists.newArrayList("Zen", "Nez"));
CassandraResult timeoutResult = cassandra().prepareAndExecute(query, Lists.newArrayList("timeout"));

assertEquals(Result.success, successResult.status().getResult());
assertEquals(Result.read_request_timeout, timeoutResult.status().getResult());
}

//todo blobs
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.concurrent.TimeUnit

import org.scalatest.{BeforeAndAfter, Matchers, FunSuite}
import org.scassandra.server.cqlmessages.{Consistency, TWO, ONE}
import org.scassandra.server.cqlmessages.types.CqlText
import org.scassandra.server.cqlmessages.types._
import org.scassandra.server.priming.{WriteRequestTimeoutResult, ReadRequestTimeoutResult, SuccessResult}
import org.scassandra.server.priming.json.{WriteTimeout, ReadTimeout, Success}
import org.scassandra.server.priming.query.{PrimeCriteria, Prime, PrimeMatch}
Expand Down Expand Up @@ -35,6 +35,23 @@ class PrimePreparedMultiStoreTest extends FunSuite with Matchers with BeforeAndA
preparedPrime.value.getPrime(List(Some("Chris"))) should equal(Prime(rows = List(), result = SuccessResult))
}

test("Match on collection variable type - success") {
val variableTypes = List(CqlList(CqlText), CqlMap(CqlText, CqlInt), CqlSet(CqlText))
val outcomeWithListMatch = Outcome(Criteria(List(ExactMatch(Some(List("Zen", "Nez"))))), Action(Some(List()), result = Some(Success)))
val outcomeWithMapMatch = Outcome(Criteria(List(ExactMatch(Some(Map("key" -> 1))))), Action(Some(List()), result = Some(Success)))
val outcomeWithSetMatch = Outcome(Criteria(List(ExactMatch(Some(Set("Zen", "Nez"))))), Action(Some(List()), result = Some(Success)))
val thenDo: ThenPreparedMulti = ThenPreparedMulti(Some(variableTypes), List(outcomeWithListMatch, outcomeWithMapMatch, outcomeWithSetMatch))
val queryText = "Some query"
underTest.record(PrimePreparedMulti(WhenPrepared(Some(queryText)), thenDo))

val preparedPrime = underTest.findPrime(PrimeMatch(queryText, ONE))

preparedPrime.value.variableTypes should equal(variableTypes)
preparedPrime.value.getPrime(List(Some(List("Zen", "Nez")))) should equal(Prime(rows = List(), result = SuccessResult))
preparedPrime.value.getPrime(List(Some(Map("key" -> 1)))) should equal(Prime(rows = List(), result = SuccessResult))
preparedPrime.value.getPrime(List(Some(Set("Zen", "Nez")))) should equal(Prime(rows = List(), result = SuccessResult))
}

test("Match on variable type - failure") {
val variableTypes = List(CqlText)
val thenDo: ThenPreparedMulti = ThenPreparedMulti(Some(variableTypes), List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
import org.scassandra.server.cqlmessages.types.{CqlText, CqlInt, CqlVarchar}
import org.scassandra.server.cqlmessages.types.{CqlList, CqlText, CqlInt, CqlVarchar}
import org.scassandra.server.cqlmessages.{ONE, TWO}
import org.scassandra.server.priming._
import org.scassandra.server.priming.json._
Expand Down Expand Up @@ -67,8 +67,12 @@ class PrimingPreparedRouteTest extends FunSpec with Matchers with ScalatestRoute

describe("Priming multiple responses") {
it("Should record it with the multi prime store") {
val when: WhenPrepared = WhenPrepared(Some("select * from people where name = ?"))
val thenDo = ThenPreparedMulti(Some(List(CqlText)), List(Outcome(Criteria(List(ExactMatch(Some("Chris")))), Action(None))))
val when: WhenPrepared = WhenPrepared(Some("select * from people where name = ? and nicknames = ?"))
val outcomes = List(
Outcome(Criteria(List(ExactMatch(Some("Chris")))), Action(None)),
Outcome(Criteria(List(ExactMatch(Some(List("Zen","Nez"))))), Action(None))
)
val thenDo = ThenPreparedMulti(Some(List(CqlText, CqlList(CqlText))), outcomes)
val prime = PrimePreparedMulti(when, thenDo)
Post(primePreparedMultiPath, prime) ~> routeForPreparedPriming ~> check {
status should equal(StatusCodes.OK)
Expand Down