RedisConfiguration.java

1
package org.cardanofoundation.explorer.api.config.redis.sentinel;
2
3
import java.util.Arrays;
4
import java.util.function.Function;
5
import java.util.stream.Collectors;
6
7
import lombok.RequiredArgsConstructor;
8
import lombok.extern.log4j.Log4j2;
9
import lombok.val;
10
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Qualifier;
13
import org.springframework.cache.annotation.CachingConfigurer;
14
import org.springframework.cache.annotation.EnableCaching;
15
import org.springframework.cache.interceptor.KeyGenerator;
16
import org.springframework.context.annotation.Bean;
17
import org.springframework.context.annotation.Configuration;
18
import org.springframework.context.annotation.Primary;
19
import org.springframework.context.annotation.Profile;
20
import org.springframework.data.redis.cache.RedisCacheManager;
21
import org.springframework.data.redis.connection.RedisConnectionFactory;
22
import org.springframework.data.redis.connection.RedisNode;
23
import org.springframework.data.redis.connection.RedisPassword;
24
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
25
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
26
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
27
import org.springframework.data.redis.core.HashOperations;
28
import org.springframework.data.redis.core.ListOperations;
29
import org.springframework.data.redis.core.RedisTemplate;
30
import org.springframework.data.redis.core.SetOperations;
31
import org.springframework.data.redis.core.ValueOperations;
32
import org.springframework.data.redis.core.ZSetOperations;
33
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
34
import org.springframework.data.redis.serializer.StringRedisSerializer;
35
36
import redis.clients.jedis.JedisPoolConfig;
37
38
/**
39
 * @author huynv
40
 * @since 04/08/2021
41
 */
42
@Log4j2
43
@Configuration
44
@EnableCaching
45
@Profile("sentinel")
46
@RequiredArgsConstructor
47
public class RedisConfiguration implements CachingConfigurer {
48
49
  /** Redis properties config */
50
  private final RedisProperties redisProperties;
51
52
  @Bean
53
  @Primary
54
  JedisPoolConfig poolConfig() {
55
    var jedisPoolConfig = new JedisPoolConfig();
56 1 1. poolConfig : removed call to redis/clients/jedis/JedisPoolConfig::setTestOnBorrow → NO_COVERAGE
    jedisPoolConfig.setTestOnBorrow(redisProperties.getTestOnBorrow());
57 1 1. poolConfig : removed call to redis/clients/jedis/JedisPoolConfig::setMaxTotal → NO_COVERAGE
    jedisPoolConfig.setMaxTotal(redisProperties.getMaxTotal());
58 1 1. poolConfig : removed call to redis/clients/jedis/JedisPoolConfig::setMaxIdle → NO_COVERAGE
    jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
59 1 1. poolConfig : removed call to redis/clients/jedis/JedisPoolConfig::setMinIdle → NO_COVERAGE
    jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
60 1 1. poolConfig : removed call to redis/clients/jedis/JedisPoolConfig::setTestOnReturn → NO_COVERAGE
    jedisPoolConfig.setTestOnReturn(redisProperties.getTestOnReturn());
61 1 1. poolConfig : removed call to redis/clients/jedis/JedisPoolConfig::setTestWhileIdle → NO_COVERAGE
    jedisPoolConfig.setTestWhileIdle(redisProperties.getTestWhileIdle());
62 1 1. poolConfig : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::poolConfig → NO_COVERAGE
    return jedisPoolConfig;
63
  }
64
65
  @Bean
66
  @Primary
67
  RedisSentinelConfiguration sentinelConfig() {
68
    var sentinelConfig = new RedisSentinelConfiguration();
69
70
    sentinelConfig.master(redisProperties.getMaster());
71 1 1. sentinelConfig : removed call to org/springframework/data/redis/connection/RedisSentinelConfiguration::setSentinelPassword → NO_COVERAGE
    sentinelConfig.setSentinelPassword(RedisPassword.of(redisProperties.getPassword()));
72 1 1. sentinelConfig : removed call to org/springframework/data/redis/connection/RedisSentinelConfiguration::setDatabase → NO_COVERAGE
    sentinelConfig.setDatabase(redisProperties.getDatabaseIndex());
73
    var sentinels =
74
        redisProperties.getSentinels().stream()
75
            .map(getSentinelNodeRedisNodeFunction())
76
            .collect(Collectors.toSet());
77
78 1 1. sentinelConfig : removed call to org/springframework/data/redis/connection/RedisSentinelConfiguration::setSentinels → NO_COVERAGE
    sentinelConfig.setSentinels(sentinels);
79 1 1. sentinelConfig : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::sentinelConfig → NO_COVERAGE
    return sentinelConfig;
80
  }
81
82
  private static Function<RedisProperties.SentinelNode, RedisNode>
83
      getSentinelNodeRedisNodeFunction() {
84 2 1. lambda$getSentinelNodeRedisNodeFunction$0 : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::lambda$getSentinelNodeRedisNodeFunction$0 → NO_COVERAGE
2. getSentinelNodeRedisNodeFunction : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::getSentinelNodeRedisNodeFunction → NO_COVERAGE
    return sentinel -> new RedisNode(sentinel.getHost(), sentinel.getPort());
85
  }
86
87
  /**
88
   * jedis connection factory configuration
89
   *
90
   * @return JedisConnectionFactory
91
   */
92
  @Bean(name = "jedisConnectionFactory")
93
  @Autowired
94
  JedisConnectionFactory jedisConnectionFactory(RedisSentinelConfiguration sentinelConfig) {
95 1 1. jedisConnectionFactory : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::jedisConnectionFactory → NO_COVERAGE
    return new JedisConnectionFactory(sentinelConfig, poolConfig());
96
  }
97
98
  /**
99
   * Lettuce connection factory configuration
100
   *
101
   * @return LettuceConnectionFactory
102
   */
103
  @Bean(name = "lettuceConnectionFactory")
104
  @Autowired
105
  LettuceConnectionFactory lettuceConnectionFactory(RedisSentinelConfiguration sentinelConfig) {
106
107 1 1. lettuceConnectionFactory : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::lettuceConnectionFactory → NO_COVERAGE
    return new LettuceConnectionFactory(sentinelConfig);
108
  }
109
110
  /**
111
   * RedisTemplate configuration
112
   *
113
   * @return redisTemplate
114
   */
115
  @Bean
116
  @Autowired
117
  RedisTemplate<String, ?> redisTemplate( // NOSONAR
118
      final LettuceConnectionFactory lettuceConnectionFactory) {
119
    var redisTemplate = new RedisTemplate<String, Object>();
120 1 1. redisTemplate : removed call to org/springframework/data/redis/core/RedisTemplate::setConnectionFactory → NO_COVERAGE
    redisTemplate.setConnectionFactory(lettuceConnectionFactory);
121 1 1. redisTemplate : removed call to org/springframework/data/redis/core/RedisTemplate::setKeySerializer → NO_COVERAGE
    redisTemplate.setKeySerializer(new StringRedisSerializer());
122 1 1. redisTemplate : removed call to org/springframework/data/redis/core/RedisTemplate::setValueSerializer → NO_COVERAGE
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
123 1 1. redisTemplate : removed call to org/springframework/data/redis/core/RedisTemplate::setDefaultSerializer → NO_COVERAGE
    redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
124 1 1. redisTemplate : removed call to org/springframework/data/redis/core/RedisTemplate::setHashValueSerializer → NO_COVERAGE
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
125 1 1. redisTemplate : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::redisTemplate → NO_COVERAGE
    return redisTemplate;
126
  }
127
128
  /**
129
   * Config bean hashOperations
130
   *
131
   * @param redisTemplate bean
132
   * @param <HK> hash key type
133
   * @param <V> value type
134
   * @return bean hashOperations
135
   */
136
  @Bean
137
  <HK, V> HashOperations<String, HK, V> hashOperations(
138
      final RedisTemplate<String, V> redisTemplate) { // NOSONAR
139 1 1. hashOperations : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::hashOperations → NO_COVERAGE
    return redisTemplate.opsForHash();
140
  }
141
142
  /**
143
   * ListOperations bean configuration
144
   *
145
   * @param redisTemplate inject bean
146
   * @param <V> value type
147
   * @return listOperations
148
   */
149
  @Bean
150
  <V> ListOperations<String, V> listOperations(final RedisTemplate<String, V> redisTemplate) {
151 1 1. listOperations : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::listOperations → NO_COVERAGE
    return redisTemplate.opsForList();
152
  }
153
154
  /**
155
   * ZSetOperations configuration
156
   *
157
   * @param redisTemplate inject bean
158
   * @param <V> value type
159
   * @return ZSetOperations<String, V>
160
   */
161
  @Bean
162
  <V> ZSetOperations<String, V> zSetOperations(final RedisTemplate<String, V> redisTemplate) {
163 1 1. zSetOperations : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::zSetOperations → NO_COVERAGE
    return redisTemplate.opsForZSet();
164
  }
165
166
  /**
167
   * SetOperations configuration
168
   *
169
   * @param redisTemplate inject bean
170
   * @param <V> value type
171
   * @return SetOperations<String, V>
172
   */
173
  @Bean
174
  <V> SetOperations<String, V> setOperations(final RedisTemplate<String, V> redisTemplate) {
175 1 1. setOperations : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::setOperations → NO_COVERAGE
    return redisTemplate.opsForSet();
176
  }
177
178
  /**
179
   * ValueOperations configuration
180
   *
181
   * @param redisTemplate inject bean
182
   * @param <V> value type
183
   * @return ValueOperations<String, V>
184
   */
185
  @Bean
186
  <V> ValueOperations<String, V> valueOperations(final RedisTemplate<String, V> redisTemplate) {
187 1 1. valueOperations : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::valueOperations → NO_COVERAGE
    return redisTemplate.opsForValue();
188
  }
189
190
  /**
191
   * Customize rules for generating keys
192
   *
193
   * @return KeyGenerator
194
   */
195
  @Override
196
  public KeyGenerator keyGenerator() {
197 1 1. keyGenerator : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::keyGenerator → NO_COVERAGE
    return (target, method, params) -> {
198
      val sb = new StringBuilder();
199
      sb.append(target.getClass().getName());
200
      sb.append(method.getName());
201 1 1. lambda$keyGenerator$1 : removed call to java/util/stream/Stream::forEach → NO_COVERAGE
      Arrays.stream(params).sequential().forEach(sb::append);
202
      log.info("call Redis cache Key : " + sb);
203 1 1. lambda$keyGenerator$1 : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::lambda$keyGenerator$1 → NO_COVERAGE
      return sb.toString();
204
    };
205
  }
206
207
  @Bean(name = "cacheManager")
208
  public RedisCacheManager cacheManager(
209
      @Qualifier("jedisConnectionFactory") RedisConnectionFactory connectionFactory) {
210 1 1. cacheManager : replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::cacheManager → NO_COVERAGE
    return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory)
211
        .build();
212
  }
213
}

Mutations

56

1.1
Location : poolConfig
Killed by : none
removed call to redis/clients/jedis/JedisPoolConfig::setTestOnBorrow → NO_COVERAGE

57

1.1
Location : poolConfig
Killed by : none
removed call to redis/clients/jedis/JedisPoolConfig::setMaxTotal → NO_COVERAGE

58

1.1
Location : poolConfig
Killed by : none
removed call to redis/clients/jedis/JedisPoolConfig::setMaxIdle → NO_COVERAGE

59

1.1
Location : poolConfig
Killed by : none
removed call to redis/clients/jedis/JedisPoolConfig::setMinIdle → NO_COVERAGE

60

1.1
Location : poolConfig
Killed by : none
removed call to redis/clients/jedis/JedisPoolConfig::setTestOnReturn → NO_COVERAGE

61

1.1
Location : poolConfig
Killed by : none
removed call to redis/clients/jedis/JedisPoolConfig::setTestWhileIdle → NO_COVERAGE

62

1.1
Location : poolConfig
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::poolConfig → NO_COVERAGE

71

1.1
Location : sentinelConfig
Killed by : none
removed call to org/springframework/data/redis/connection/RedisSentinelConfiguration::setSentinelPassword → NO_COVERAGE

72

1.1
Location : sentinelConfig
Killed by : none
removed call to org/springframework/data/redis/connection/RedisSentinelConfiguration::setDatabase → NO_COVERAGE

78

1.1
Location : sentinelConfig
Killed by : none
removed call to org/springframework/data/redis/connection/RedisSentinelConfiguration::setSentinels → NO_COVERAGE

79

1.1
Location : sentinelConfig
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::sentinelConfig → NO_COVERAGE

84

1.1
Location : lambda$getSentinelNodeRedisNodeFunction$0
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::lambda$getSentinelNodeRedisNodeFunction$0 → NO_COVERAGE

2.2
Location : getSentinelNodeRedisNodeFunction
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::getSentinelNodeRedisNodeFunction → NO_COVERAGE

95

1.1
Location : jedisConnectionFactory
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::jedisConnectionFactory → NO_COVERAGE

107

1.1
Location : lettuceConnectionFactory
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::lettuceConnectionFactory → NO_COVERAGE

120

1.1
Location : redisTemplate
Killed by : none
removed call to org/springframework/data/redis/core/RedisTemplate::setConnectionFactory → NO_COVERAGE

121

1.1
Location : redisTemplate
Killed by : none
removed call to org/springframework/data/redis/core/RedisTemplate::setKeySerializer → NO_COVERAGE

122

1.1
Location : redisTemplate
Killed by : none
removed call to org/springframework/data/redis/core/RedisTemplate::setValueSerializer → NO_COVERAGE

123

1.1
Location : redisTemplate
Killed by : none
removed call to org/springframework/data/redis/core/RedisTemplate::setDefaultSerializer → NO_COVERAGE

124

1.1
Location : redisTemplate
Killed by : none
removed call to org/springframework/data/redis/core/RedisTemplate::setHashValueSerializer → NO_COVERAGE

125

1.1
Location : redisTemplate
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::redisTemplate → NO_COVERAGE

139

1.1
Location : hashOperations
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::hashOperations → NO_COVERAGE

151

1.1
Location : listOperations
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::listOperations → NO_COVERAGE

163

1.1
Location : zSetOperations
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::zSetOperations → NO_COVERAGE

175

1.1
Location : setOperations
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::setOperations → NO_COVERAGE

187

1.1
Location : valueOperations
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::valueOperations → NO_COVERAGE

197

1.1
Location : keyGenerator
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::keyGenerator → NO_COVERAGE

201

1.1
Location : lambda$keyGenerator$1
Killed by : none
removed call to java/util/stream/Stream::forEach → NO_COVERAGE

203

1.1
Location : lambda$keyGenerator$1
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::lambda$keyGenerator$1 → NO_COVERAGE

210

1.1
Location : cacheManager
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/config/redis/sentinel/RedisConfiguration::cacheManager → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.14.2