All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.arquillian.ape.nosql.redis.RedisPopulatorService Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha.7
Show newest version
package org.arquillian.ape.nosql.redis;

import com.lordofthejars.nosqlunit.redis.DefaultRedisInsertionStrategy;
import com.lordofthejars.nosqlunit.redis.RedisConnectionCallback;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.arquillian.ape.core.DataSetLoader;
import org.arquillian.ape.nosql.NoSqlPopulatorService;
import redis.clients.jedis.BinaryJedisCommands;
import redis.clients.jedis.Jedis;

/**
 * Integration to NoSqlUnit Redis module.
 */
class RedisPopulatorService implements NoSqlPopulatorService {

    private Jedis jedis;

    @Override
    public void connect(String host, int port, String database, Map customOptions) {
        jedis = new Jedis(host, port);
        jedis.connect();
    }

    @Override
    public void connect(URI uri, String database, Map customOptions) {
        jedis = new Jedis(uri);
        jedis.connect();
    }

    @Override
    public void disconnect() {
        if (jedis != null) {
            jedis.disconnect();
        }
    }

    @Override
    public void execute(List resources) {
        final DefaultRedisInsertionStrategy redisInsertionStrategy = new DefaultRedisInsertionStrategy();
        final RedisConnectionCallback connection = new RedisConnectionCallback() {
            @Override
            public BinaryJedisCommands insertionJedis() {
                return jedis;
            }

            @Override
            public Jedis getActiveJedis(byte[] bytes) {
                return jedis;
            }

            @Override
            public Collection getAllJedis() {
                return Collections.singletonList(jedis);
            }
        };

        resources.stream()
            .map(DataSetLoader::resolve)
            .forEach(dataset -> {
                try {
                    redisInsertionStrategy.insert(connection, dataset);
                } catch (Throwable throwable) {
                    throw new IllegalStateException(throwable);
                }
            });
    }

    @Override
    public void clean() {
        this.jedis.flushDB();
    }

    @Override
    public Class getPopulatorAnnotation() {
        return Redis.class;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy