group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
@@ -0,0 +1,212 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.*;
|
||||
import redis.clients.jedis.*;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class JedisIntegrationTest {
|
||||
|
||||
private Jedis jedis;
|
||||
private static RedisServer redisServer;
|
||||
|
||||
public JedisIntegrationTest() {
|
||||
jedis = new Jedis();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
redisServer = new RedisServer(6379);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@After
|
||||
public void flush() {
|
||||
jedis.flushAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_thenSaveItAsRedisStrings() {
|
||||
String key = "key";
|
||||
String value = "value";
|
||||
|
||||
jedis.set(key, value);
|
||||
String value2 = jedis.get(key);
|
||||
|
||||
Assert.assertEquals(value, value2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListElements_thenSaveThemInRedisList() {
|
||||
String queue = "queue#tasks";
|
||||
|
||||
String taskOne = "firstTask";
|
||||
String taskTwo = "secondTask";
|
||||
String taskThree = "thirdTask";
|
||||
|
||||
jedis.lpush(queue, taskOne, taskTwo);
|
||||
|
||||
String taskReturnedOne = jedis.rpop(queue);
|
||||
|
||||
jedis.lpush(queue, taskThree);
|
||||
Assert.assertEquals(taskOne, taskReturnedOne);
|
||||
|
||||
String taskReturnedTwo = jedis.rpop(queue);
|
||||
String taskReturnedThree = jedis.rpop(queue);
|
||||
|
||||
Assert.assertEquals(taskTwo, taskReturnedTwo);
|
||||
Assert.assertEquals(taskThree, taskReturnedThree);
|
||||
|
||||
String taskReturnedFour = jedis.rpop(queue);
|
||||
Assert.assertNull(taskReturnedFour);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSetElements_thenSaveThemInRedisSet() {
|
||||
String countries = "countries";
|
||||
|
||||
String countryOne = "Spain";
|
||||
String countryTwo = "Ireland";
|
||||
String countryThree = "Ireland";
|
||||
|
||||
jedis.sadd(countries, countryOne);
|
||||
|
||||
Set<String> countriesSet = jedis.smembers(countries);
|
||||
Assert.assertEquals(1, countriesSet.size());
|
||||
|
||||
jedis.sadd(countries, countryTwo);
|
||||
countriesSet = jedis.smembers(countries);
|
||||
Assert.assertEquals(2, countriesSet.size());
|
||||
|
||||
jedis.sadd(countries, countryThree);
|
||||
countriesSet = jedis.smembers(countries);
|
||||
Assert.assertEquals(2, countriesSet.size());
|
||||
|
||||
boolean exists = jedis.sismember(countries, countryThree);
|
||||
Assert.assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectFields_thenSaveThemInRedisHash() {
|
||||
String key = "user#1";
|
||||
|
||||
String field = "name";
|
||||
String value = "William";
|
||||
|
||||
String field2 = "job";
|
||||
String value2 = "politician";
|
||||
|
||||
jedis.hset(key, field, value);
|
||||
jedis.hset(key, field2, value2);
|
||||
|
||||
String value3 = jedis.hget(key, field);
|
||||
Assert.assertEquals(value, value3);
|
||||
|
||||
Map<String, String> fields = jedis.hgetAll(key);
|
||||
String value4 = fields.get(field2);
|
||||
Assert.assertEquals(value2, value4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenARanking_thenSaveItInRedisSortedSet() {
|
||||
String key = "ranking";
|
||||
|
||||
Map<String, Double> scores = new HashMap<>();
|
||||
|
||||
scores.put("PlayerOne", 3000.0);
|
||||
scores.put("PlayerTwo", 1500.0);
|
||||
scores.put("PlayerThree", 8200.0);
|
||||
|
||||
scores.keySet().forEach(player -> {
|
||||
jedis.zadd(key, scores.get(player), player);
|
||||
});
|
||||
|
||||
Set<String> players = jedis.zrevrange(key, 0, 1);
|
||||
Assert.assertEquals("PlayerThree", players.iterator().next());
|
||||
|
||||
long rank = jedis.zrevrank(key, "PlayerOne");
|
||||
Assert.assertEquals(1, rank);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleOperationsThatNeedToBeExecutedAtomically_thenWrapThemInATransaction() {
|
||||
String friendsPrefix = "friends#";
|
||||
|
||||
String userOneId = "4352523";
|
||||
String userTwoId = "5552321";
|
||||
|
||||
Transaction t = jedis.multi();
|
||||
t.sadd(friendsPrefix + userOneId, userTwoId);
|
||||
t.sadd(friendsPrefix + userTwoId, userOneId);
|
||||
t.exec();
|
||||
|
||||
boolean exists = jedis.sismember(friendsPrefix + userOneId, userTwoId);
|
||||
Assert.assertTrue(exists);
|
||||
|
||||
exists = jedis.sismember(friendsPrefix + userTwoId, userOneId);
|
||||
Assert.assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleIndependentOperations_whenNetworkOptimizationIsImportant_thenWrapThemInAPipeline() {
|
||||
String userOneId = "4352523";
|
||||
String userTwoId = "4849888";
|
||||
|
||||
Pipeline p = jedis.pipelined();
|
||||
p.sadd("searched#" + userOneId, "paris");
|
||||
p.zadd("ranking", 126, userOneId);
|
||||
p.zadd("ranking", 325, userTwoId);
|
||||
Response<Boolean> pipeExists = p.sismember("searched#" + userOneId, "paris");
|
||||
Response<Set<String>> pipeRanking = p.zrange("ranking", 0, -1);
|
||||
p.sync();
|
||||
|
||||
Assert.assertTrue(pipeExists.get());
|
||||
Assert.assertEquals(2, pipeRanking.get().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPoolConfiguration_thenCreateAJedisPool() {
|
||||
final JedisPoolConfig poolConfig = buildPoolConfig();
|
||||
|
||||
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) {
|
||||
|
||||
// do simple operation to verify that the Jedis resource is working
|
||||
// properly
|
||||
String key = "key";
|
||||
String value = "value";
|
||||
|
||||
jedis.set(key, value);
|
||||
String value2 = jedis.get(key);
|
||||
|
||||
Assert.assertEquals(value, value2);
|
||||
|
||||
// flush Redis
|
||||
jedis.flushAll();
|
||||
}
|
||||
}
|
||||
|
||||
private JedisPoolConfig buildPoolConfig() {
|
||||
final JedisPoolConfig poolConfig = new JedisPoolConfig();
|
||||
poolConfig.setMaxTotal(128);
|
||||
poolConfig.setMaxIdle(128);
|
||||
poolConfig.setMinIdle(16);
|
||||
poolConfig.setTestOnBorrow(true);
|
||||
poolConfig.setTestOnReturn(true);
|
||||
poolConfig.setTestWhileIdle(true);
|
||||
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
|
||||
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
|
||||
poolConfig.setNumTestsPerEvictionRun(3);
|
||||
poolConfig.setBlockWhenExhausted(true);
|
||||
return poolConfig;
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by johnson on 3/9/17.
|
||||
*/
|
||||
public class RedissonConfigurationIntegrationTest {
|
||||
private static RedisServer redisServer;
|
||||
private static RedissonClient client;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
redisServer = new RedisServer(6379);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
redisServer.stop();
|
||||
client.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaConfig_thenRedissonConnectToRedis() {
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("127.0.0.1:6379");
|
||||
|
||||
client = Redisson.create(config);
|
||||
|
||||
assert(client != null && client.getKeys().count() >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJSONFileConfig_thenRedissonConnectToRedis() throws IOException {
|
||||
Config config = Config.fromJSON(
|
||||
new File(getClass().getClassLoader().getResource(
|
||||
"singleNodeConfig.json").getFile()));
|
||||
|
||||
client = Redisson.create(config);
|
||||
|
||||
assert(client != null && client.getKeys().count() >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenYAMLFileConfig_thenRedissonConnectToRedis() throws IOException {
|
||||
Config config = Config.fromYAML(
|
||||
new File(getClass().getClassLoader().getResource(
|
||||
"singleNodeConfig.yaml").getFile()));
|
||||
|
||||
client = Redisson.create(config);
|
||||
|
||||
assert(client != null && client.getKeys().count() >= 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.RedissonMultiLock;
|
||||
import org.redisson.api.*;
|
||||
import org.redisson.client.RedisClient;
|
||||
import org.redisson.client.RedisConnection;
|
||||
import org.redisson.client.codec.StringCodec;
|
||||
import org.redisson.client.protocol.RedisCommands;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class RedissonIntegrationTest {
|
||||
|
||||
private static RedisServer redisServer;
|
||||
private static RedissonClient client;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
redisServer = new RedisServer(6379);
|
||||
redisServer.start();
|
||||
client = Redisson.create();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
redisServer.stop();
|
||||
client.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleKeysInRedis_thenGetAllKeys() {
|
||||
client.getBucket("key1").set("key1");
|
||||
client.getBucket("key2").set("key2");
|
||||
client.getBucket("key3").set("key3");
|
||||
|
||||
RKeys keys = client.getKeys();
|
||||
|
||||
assertTrue(keys.count() >= 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenKeysWithPatternInRedis_thenGetPatternKeys() {
|
||||
client.getBucket("key1").set("key1");
|
||||
client.getBucket("key2").set("key2");
|
||||
client.getBucket("key3").set("key3");
|
||||
client.getBucket("id4").set("id4");
|
||||
|
||||
RKeys keys = client.getKeys();
|
||||
|
||||
Iterable<String> keysWithPattern
|
||||
= keys.getKeysByPattern("key*");
|
||||
|
||||
List keyWithPatternList
|
||||
= StreamSupport.stream(
|
||||
keysWithPattern.spliterator(),
|
||||
false).collect(Collectors.toList());
|
||||
|
||||
assertTrue(keyWithPatternList.size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnObject_thenSaveToRedis() {
|
||||
RBucket<Ledger> bucket = client.getBucket("ledger");
|
||||
Ledger ledger = new Ledger();
|
||||
ledger.setName("ledger1");
|
||||
bucket.set(ledger);
|
||||
|
||||
Ledger returnedLedger = bucket.get();
|
||||
|
||||
assertTrue(
|
||||
returnedLedger != null
|
||||
&& returnedLedger.getName().equals("ledger1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenALong_thenSaveLongToRedisAndAtomicallyIncrement(){
|
||||
Long value = 5L;
|
||||
|
||||
RAtomicLong atomicLong
|
||||
= client.getAtomicLong("myAtomicLong");
|
||||
atomicLong.set(value);
|
||||
Long returnValue = atomicLong.incrementAndGet();
|
||||
|
||||
assertTrue(returnValue == 6L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTopicSubscribedToAChannel_thenReceiveMessageFromChannel() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
|
||||
RTopic<CustomMessage> subscribeTopic = client.getTopic("baeldung");
|
||||
subscribeTopic.addListener((channel, customMessage) -> future.complete(customMessage.getMessage()));
|
||||
|
||||
RTopic<CustomMessage> publishTopic = client.getTopic("baeldung");
|
||||
long clientsReceivedMessage
|
||||
= publishTopic.publish(new CustomMessage("This is a message"));
|
||||
|
||||
assertEquals("This is a message", future.get());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAMap_thenSaveMapToRedis(){
|
||||
RMap<String, Ledger> map = client.getMap("ledger");
|
||||
map.put("123", new Ledger("ledger"));
|
||||
|
||||
assertTrue(map.get("123").getName().equals("ledger"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASet_thenSaveSetToRedis(){
|
||||
RSet<Ledger> ledgerSet = client.getSet("ledgerSet");
|
||||
ledgerSet.add(new Ledger("ledger"));
|
||||
|
||||
assertTrue(ledgerSet.contains(new Ledger("ledger")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_thenSaveListToRedis(){
|
||||
RList<Ledger> ledgerList = client.getList("ledgerList");
|
||||
ledgerList.add(new Ledger("ledger"));
|
||||
|
||||
assertTrue(ledgerList.contains(new Ledger("ledger")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLockSet_thenEnsureCanUnlock(){
|
||||
RLock lock = client.getLock("lock");
|
||||
lock.lock();
|
||||
assertTrue(lock.isLocked());
|
||||
|
||||
lock.unlock();
|
||||
assertTrue(!lock.isLocked());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleLocksSet_thenEnsureAllCanUnlock(){
|
||||
RedissonClient clientInstance1 = Redisson.create();
|
||||
RedissonClient clientInstance2 = Redisson.create();
|
||||
RedissonClient clientInstance3 = Redisson.create();
|
||||
|
||||
RLock lock1 = clientInstance1.getLock("lock1");
|
||||
RLock lock2 = clientInstance2.getLock("lock2");
|
||||
RLock lock3 = clientInstance3.getLock("lock3");
|
||||
|
||||
RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
|
||||
lock.lock();
|
||||
assertTrue(lock1.isLocked() && lock2.isLocked() && lock3.isLocked());
|
||||
|
||||
lock.unlock();
|
||||
assertTrue(!(lock1.isLocked() || lock2.isLocked() || lock3.isLocked()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRemoteServiceMethodRegistered_thenInvokeMethod(){
|
||||
RRemoteService remoteService = client.getRemoteService();
|
||||
LedgerServiceImpl ledgerServiceImpl = new LedgerServiceImpl();
|
||||
|
||||
remoteService.register(LedgerServiceInterface.class, ledgerServiceImpl);
|
||||
|
||||
|
||||
LedgerServiceInterface ledgerService
|
||||
= remoteService.get(LedgerServiceInterface.class);
|
||||
|
||||
List<String> entries = ledgerService.getEntries(10);
|
||||
|
||||
assertTrue(entries.size() == 3 && entries.contains("entry1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLiveObjectPersisted_thenGetLiveObject(){
|
||||
RLiveObjectService service = client.getLiveObjectService();
|
||||
|
||||
LedgerLiveObject ledger = new LedgerLiveObject();
|
||||
ledger.setName("ledger1");
|
||||
|
||||
ledger = service.persist(ledger);
|
||||
|
||||
LedgerLiveObject returnLedger
|
||||
= service.get(LedgerLiveObject.class, "ledger1");
|
||||
|
||||
assertTrue(ledger.getName().equals(returnLedger.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleOperations_thenDoAllAtomically(){
|
||||
RBatch batch = client.createBatch();
|
||||
batch.getMap("ledgerMap").fastPutAsync("1", "2");
|
||||
batch.getMap("ledgerMap").putAsync("2", "5");
|
||||
|
||||
List<?> result = batch.execute();
|
||||
|
||||
RMap<String, String> map = client.getMap("ledgerMap");
|
||||
assertTrue(result.size() > 0 && map.get("1").equals("2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLUAScript_thenExecuteScriptOnRedis(){
|
||||
client.getBucket("foo").set("bar");
|
||||
String result = client.getScript().eval(RScript.Mode.READ_ONLY,
|
||||
"return redis.call('get', 'foo')", RScript.ReturnType.VALUE);
|
||||
|
||||
assertTrue(result.equals("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLowLevelRedisCommands_thenExecuteLowLevelCommandsOnRedis(){
|
||||
RedisClient client = new RedisClient("localhost", 6379);
|
||||
RedisConnection conn = client.connect();
|
||||
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
|
||||
|
||||
String testValue = conn.sync(StringCodec.INSTANCE, RedisCommands.GET, "test");
|
||||
|
||||
conn.closeAsync();
|
||||
client.shutdown();
|
||||
|
||||
assertTrue(testValue.equals("0"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user