Search in sources :

Example 71 with Transaction

use of redis.clients.jedis.Transaction in project jedis by redis.

the class TransactionCommandsTest method transactionResponseWithErrorWithGeneralCommand.

@Test
public void transactionResponseWithErrorWithGeneralCommand() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    t.sendCommand(SET, "x", "1");
    Response<Set<String>> error = t.smembers("foo");
    Response<String> r = t.get("foo");
    Response<Object> x = t.sendCommand(GET, "x");
    t.sendCommand(INCR, "x");
    List<Object> l = t.exec();
    assertSame(JedisDataException.class, l.get(2).getClass());
    try {
        error.get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals("bar", r.get());
    assertEquals("1", SafeEncoder.encode((byte[]) x.get()));
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 72 with Transaction

use of redis.clients.jedis.Transaction in project jedis by redis.

the class TransactionCommandsTest method unwatch.

@Test
public void unwatch() {
    jedis.watch("mykey");
    jedis.get("mykey");
    String val = "foo";
    assertEquals("OK", jedis.unwatch());
    Transaction t = jedis.multi();
    nj.set("mykey", "bar");
    t.set("mykey", val);
    List<Object> resp = t.exec();
    assertEquals(1, resp.size());
    assertEquals("OK", resp.get(0));
    // Binary
    jedis.watch(bmykey);
    jedis.get(bmykey);
    byte[] bval = bfoo;
    assertEquals("OK", jedis.unwatch());
    t = jedis.multi();
    nj.set(bmykey, bbar);
    t.set(bmykey, bval);
    resp = t.exec();
    assertEquals(1, resp.size());
    assertEquals("OK", resp.get(0));
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 73 with Transaction

use of redis.clients.jedis.Transaction in project jedis by redis.

the class TransactionCommandsTest method transactionResponseWithError.

@Test
public void transactionResponseWithError() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    Response<Set<String>> error = t.smembers("foo");
    Response<String> r = t.get("foo");
    List<Object> l = t.exec();
    assertSame(JedisDataException.class, l.get(1).getClass());
    try {
        error.get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals("bar", r.get());
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 74 with Transaction

use of redis.clients.jedis.Transaction in project jedis by redis.

the class GraphTransactionTest method testMultiExecWithReadOnlyQueries.

// 
// @Test
// public void testWriteTransactionWatch() {
// 
// RedisGraphContext c1 = api.getContext();
// RedisGraphContext c2 = api.getContext();
// 
// c1.watch("social");
// RedisGraphTransaction t1 = c1.multi();
// 
// t1.graphQuery("social", "CREATE (:Person {name:'a'})");
// c2.graphQuery("social", "CREATE (:Person {name:'b'})");
// List<Object> returnValue = t1.exec();
// assertNull(returnValue);
// c1.close();
// c2.close();
// }
// 
// @Test
// public void testReadTransactionWatch() {
// 
// RedisGraphContext c1 = api.getContext();
// RedisGraphContext c2 = api.getContext();
// assertNotEquals(c1.getConnectionContext(), c2.getConnectionContext());
// c1.graphQuery("social", "CREATE (:Person {name:'a'})");
// c1.watch("social");
// RedisGraphTransaction t1 = c1.multi();
// 
// Map<String, Object> params = new HashMap<>();
// params.put("name", 'b');
// t1.graphQuery("social", "CREATE (:Person {name:$name})", params);
// c2.graphQuery("social", "MATCH (n) return n");
// List<Object> returnValue = t1.exec();
// 
// assertNotNull(returnValue);
// c1.close();
// c2.close();
// }
@Test
public void testMultiExecWithReadOnlyQueries() {
    Transaction transaction = new Transaction(c);
    transaction.set("x", "1");
    transaction.graphQuery("social", "CREATE (:Person {name:'a'})");
    transaction.graphQuery("g", "CREATE (:Person {name:'a'})");
    transaction.graphReadonlyQuery("social", "MATCH (n:Person) RETURN n");
    transaction.graphDelete("g");
    // transaction.callProcedure("social", "db.labels");
    transaction.graphQuery("social", "CALL db.labels()");
    List<Object> results = transaction.exec();
    // Redis set command
    assertEquals(String.class, results.get(0).getClass());
    assertEquals("OK", results.get(0));
    // Redis graph command
    // assertEquals(ResultSetImpl.class, results.get(1).getClass());
    ResultSet resultSet = (ResultSet) results.get(1);
    assertEquals(1, resultSet.getStatistics().nodesCreated());
    assertEquals(1, resultSet.getStatistics().propertiesSet());
    // assertEquals(ResultSetImpl.class, results.get(2).getClass());
    resultSet = (ResultSet) results.get(2);
    assertEquals(1, resultSet.getStatistics().nodesCreated());
    assertEquals(1, resultSet.getStatistics().propertiesSet());
    // Graph read-only query result
    // assertEquals(ResultSetImpl.class, results.get(5).getClass());
    resultSet = (ResultSet) results.get(3);
    assertNotNull(resultSet.getHeader());
    Header header = resultSet.getHeader();
    List<String> schemaNames = header.getSchemaNames();
    assertNotNull(schemaNames);
    assertEquals(1, schemaNames.size());
    assertEquals("n", schemaNames.get(0));
    Property<String> nameProperty = new Property<>("name", "a");
    Node expectedNode = new Node();
    expectedNode.setId(0);
    expectedNode.addLabel("Person");
    expectedNode.addProperty(nameProperty);
    // see that the result were pulled from the right graph
    assertEquals(1, resultSet.size());
    Iterator<Record> iterator = resultSet.iterator();
    assertTrue(iterator.hasNext());
    Record record = iterator.next();
    assertFalse(iterator.hasNext());
    assertEquals(Arrays.asList("n"), record.keys());
    assertEquals(expectedNode, record.getValue("n"));
    // assertEquals(ResultSetImpl.class, results.get(5).getClass());
    resultSet = (ResultSet) results.get(5);
    assertNotNull(resultSet.getHeader());
    header = resultSet.getHeader();
    schemaNames = header.getSchemaNames();
    assertNotNull(schemaNames);
    assertEquals(1, schemaNames.size());
    assertEquals("label", schemaNames.get(0));
    assertEquals(1, resultSet.size());
    iterator = resultSet.iterator();
    assertTrue(iterator.hasNext());
    record = iterator.next();
    assertFalse(iterator.hasNext());
    assertEquals(Arrays.asList("label"), record.keys());
    assertEquals("Person", record.getValue("label"));
}
Also used : Transaction(redis.clients.jedis.Transaction) Header(redis.clients.jedis.graph.Header) Node(redis.clients.jedis.graph.entities.Node) ResultSet(redis.clients.jedis.graph.ResultSet) Record(redis.clients.jedis.graph.Record) Property(redis.clients.jedis.graph.entities.Property) Test(org.junit.Test)

Example 75 with Transaction

use of redis.clients.jedis.Transaction in project jedis by redis.

the class AccessControlListCommandsTest method aclLogTest.

@Test
public void aclLogTest() {
    jedis.aclLogReset();
    assertTrue(jedis.aclLog().isEmpty());
    // create new user and cconnect
    jedis.aclSetUser("antirez", ">foo", "on", "+set", "~object:1234");
    jedis.aclSetUser("antirez", "+eval", "+multi", "+exec");
    jedis.auth("antirez", "foo");
    // generate an error (antirez user does not have the permission to access foo)
    try {
        jedis.get("foo");
        fail("Should have thrown an JedisAccessControlException: user does not have the permission to get(\"foo\")");
    } catch (JedisAccessControlException e) {
    }
    // test the ACL Log
    jedis.auth("default", "foobared");
    assertEquals("Number of log messages ", 1, jedis.aclLog().size());
    assertEquals(1, jedis.aclLog().get(0).getCount());
    assertEquals("antirez", jedis.aclLog().get(0).getUsername());
    assertEquals("toplevel", jedis.aclLog().get(0).getContext());
    assertEquals("command", jedis.aclLog().get(0).getReason());
    assertEquals("get", jedis.aclLog().get(0).getObject());
    // Capture similar event
    jedis.aclLogReset();
    assertTrue(jedis.aclLog().isEmpty());
    jedis.auth("antirez", "foo");
    for (int i = 0; i < 10; i++) {
        // generate an error (antirez user does not have the permission to access foo)
        try {
            jedis.get("foo");
            fail("Should have thrown an JedisAccessControlException: user does not have the permission to get(\"foo\")");
        } catch (JedisAccessControlException e) {
        }
    }
    // test the ACL Log
    jedis.auth("default", "foobared");
    assertEquals("Number of log messages ", 1, jedis.aclLog().size());
    assertEquals(10, jedis.aclLog().get(0).getCount());
    assertEquals("get", jedis.aclLog().get(0).getObject());
    // Generate another type of error
    jedis.auth("antirez", "foo");
    try {
        jedis.set("somekeynotallowed", "1234");
        fail("Should have thrown an JedisAccessControlException: user does not have the permission to set(\"somekeynotallowed\", \"1234\")");
    } catch (JedisAccessControlException e) {
    }
    // test the ACL Log
    jedis.auth("default", "foobared");
    assertEquals("Number of log messages ", 2, jedis.aclLog().size());
    assertEquals(1, jedis.aclLog().get(0).getCount());
    assertEquals("somekeynotallowed", jedis.aclLog().get(0).getObject());
    assertEquals("key", jedis.aclLog().get(0).getReason());
    jedis.aclLogReset();
    assertTrue(jedis.aclLog().isEmpty());
    jedis.auth("antirez", "foo");
    Transaction t = jedis.multi();
    t.incr("foo");
    try {
        t.exec();
        fail("Should have thrown an JedisAccessControlException: user does not have the permission to incr(\"foo\")");
    } catch (Exception e) {
    }
    t.close();
    jedis.auth("default", "foobared");
    assertEquals("Number of log messages ", 1, jedis.aclLog().size());
    assertEquals(1, jedis.aclLog().get(0).getCount());
    assertEquals("multi", jedis.aclLog().get(0).getContext());
    assertEquals("incr", jedis.aclLog().get(0).getObject());
    // ACL LOG can accept a numerical argument to show less entries
    jedis.auth("antirez", "foo");
    for (int i = 0; i < 5; i++) {
        try {
            jedis.incr("foo");
            fail("Should have thrown an JedisAccessControlException: user does not have the permission to incr(\"foo\")");
        } catch (JedisAccessControlException e) {
        }
    }
    try {
        jedis.set("foo-2", "bar");
        fail("Should have thrown an JedisAccessControlException: user does not have the permission to set(\"foo-2\", \"bar\")");
    } catch (JedisAccessControlException e) {
    }
    jedis.auth("default", "foobared");
    assertEquals("Number of log messages ", 3, jedis.aclLog().size());
    assertEquals("Number of log messages ", 2, jedis.aclLog(2).size());
    // Binary tests
    assertEquals("Number of log messages ", 3, jedis.aclLogBinary().size());
    assertEquals("Number of log messages ", 2, jedis.aclLogBinary(2).size());
    // RESET
    String status = jedis.aclLogReset();
    assertEquals(status, "OK");
    jedis.aclDelUser("antirez");
}
Also used : Transaction(redis.clients.jedis.Transaction) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JedisAccessControlException(redis.clients.jedis.exceptions.JedisAccessControlException) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) JedisAccessControlException(redis.clients.jedis.exceptions.JedisAccessControlException) Test(org.junit.Test)

Aggregations

Transaction (redis.clients.jedis.Transaction)149 Test (org.junit.Test)90 Jedis (redis.clients.jedis.Jedis)57 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)15 Set (java.util.Set)14 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)8 List (java.util.List)8 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)7 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)6 Protocol (redis.clients.jedis.Protocol)4 Response (redis.clients.jedis.Response)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 Header (redis.clients.jedis.graph.Header)4 Record (redis.clients.jedis.graph.Record)4 ResultSet (redis.clients.jedis.graph.ResultSet)4 Node (redis.clients.jedis.graph.entities.Node)4 Property (redis.clients.jedis.graph.entities.Property)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3