Search in sources :

Example 1 with ListOperations

use of org.springframework.data.redis.core.ListOperations in project rdbcache by rdbcache.

the class MockRedis method mockStringRedisTemplate.

public static StringRedisTemplate mockStringRedisTemplate() {
    StringRedisTemplate template = mock(StringRedisTemplate.class, Mockito.RETURNS_DEEP_STUBS);
    // mock StringRedisTemplate delete
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Set<String> keys = (Set<String>) args[0];
        LOGGER.trace("StringRedisTemplate delete " + keys);
        for (String key : keys) {
            data.remove(key);
        }
        return null;
    }).when(template).delete(anySet());
    HashOperations hashOps = mock(HashOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForHash()).thenReturn(hashOps);
    // mock HashOperations putAll
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        Map<String, Object> map = (Map<String, Object>) args[1];
        LOGGER.trace("StringRedisTemplate HashOperations putAll " + key + " " + map.keySet());
        Map<String, Object> wholeMap = (Map<String, Object>) data.get(key);
        if (wholeMap == null) {
            wholeMap = new LinkedHashMap<>();
            data.put(key, wholeMap);
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            wholeMap.put(entry.getKey(), entry.getValue());
        }
        return null;
    }).when(hashOps).putAll(anyString(), anyMap());
    // mock HashOperations entries
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        LOGGER.trace("StringRedisTemplate HashOperations entries " + key);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        Map<String, Object> mapClone = new LinkedHashMap<>(map);
        return mapClone;
    }).when(hashOps).entries(anyString());
    // opsForValue only use in ExpireOps for lua scripts, set it to null to bypass the real operations
    // 
    ValueOperations valueOps = mock(ValueOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForValue()).thenReturn(valueOps);
    // mock ValueOperations set
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String expire = (String) args[1];
        Integer expValue = (Integer) args[2];
        LOGGER.trace("StringRedisTemplate ValueOperations set " + key + " " + expire + " " + expValue);
        data.put(key, Arrays.asList(expire, expValue));
        return null;
    }).when(valueOps).set(anyString(), anyString(), anyInt());
    // mock ValueOperations get
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        if ("__is_mock_test__".equals(key)) {
            return "__TRUE__";
        }
        LOGGER.trace("StringRedisTemplate ValueOperations get " + key);
        return data.get(key);
    }).when(valueOps).get(anyString());
    // mock (ListOperations leftPop
    // 
    ListOperations listOps = mock(ListOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForList()).thenReturn(listOps);
    Mockito.when(listOps.leftPop(anyString(), anyLong(), anyObject())).thenAnswer(invocation -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        return null;
    });
    return template;
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) HashOperations(org.springframework.data.redis.core.HashOperations) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ValueOperations(org.springframework.data.redis.core.ValueOperations) ListOperations(org.springframework.data.redis.core.ListOperations)

Example 2 with ListOperations

use of org.springframework.data.redis.core.ListOperations in project rdbcache by rdbcache.

the class MockRedis method mockStringRedisTemplate.

public static StringRedisTemplate mockStringRedisTemplate() {
    StringRedisTemplate template = mock(StringRedisTemplate.class, Mockito.RETURNS_DEEP_STUBS);
    // mock StringRedisTemplate delete
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Set<String> keys = (Set<String>) args[0];
        LOGGER.trace("StringRedisTemplate delete " + keys);
        for (String key : keys) {
            data.remove(key);
        }
        return null;
    }).when(template).delete(anySet());
    HashOperations hashOps = mock(HashOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForHash()).thenReturn(hashOps);
    // mock HashOperations putAll
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        Map<String, Object> map = (Map<String, Object>) args[1];
        LOGGER.trace("StringRedisTemplate HashOperations putAll " + key + " " + map.keySet());
        Map<String, Object> wholeMap = (Map<String, Object>) data.get(key);
        if (wholeMap == null) {
            wholeMap = new LinkedHashMap<>();
            data.put(key, wholeMap);
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            wholeMap.put(entry.getKey(), entry.getValue());
        }
        return null;
    }).when(hashOps).putAll(anyString(), anyMap());
    // mock HashOperations entries
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        LOGGER.trace("StringRedisTemplate HashOperations entries " + key);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        Map<String, Object> mapClone = new LinkedHashMap<>(map);
        return mapClone;
    }).when(hashOps).entries(anyString());
    // opsForValue only use in ExpireOps for lua scripts, set it to null to bypass the real operations
    // 
    ValueOperations valueOps = mock(ValueOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForValue()).thenReturn(valueOps);
    // mock ValueOperations set
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String expire = (String) args[1];
        Integer expValue = (Integer) args[2];
        LOGGER.trace("StringRedisTemplate ValueOperations set " + key + " " + expire + " " + expValue);
        data.put(key, Arrays.asList(expire, expValue));
        return null;
    }).when(valueOps).set(anyString(), anyString(), anyInt());
    // mock ValueOperations get
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        if ("__is_mock_test__".equals(key)) {
            return "__TRUE__";
        }
        LOGGER.trace("StringRedisTemplate ValueOperations get " + key);
        return data.get(key);
    }).when(valueOps).get(anyString());
    // mock (ListOperations leftPop
    // 
    ListOperations listOps = mock(ListOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForList()).thenReturn(listOps);
    Mockito.when(listOps.leftPop(anyString(), anyLong(), anyObject())).thenAnswer(invocation -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        return null;
    });
    return template;
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) HashOperations(org.springframework.data.redis.core.HashOperations) ValueOperations(org.springframework.data.redis.core.ValueOperations) ListOperations(org.springframework.data.redis.core.ListOperations)

Aggregations

HashOperations (org.springframework.data.redis.core.HashOperations)2 ListOperations (org.springframework.data.redis.core.ListOperations)2 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)2 ValueOperations (org.springframework.data.redis.core.ValueOperations)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1