Create JUnit tests for the following code: import com.google.common.cache.CacheBuilder; import com.google.common.cache.C

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Create JUnit tests for the following code: import com.google.common.cache.CacheBuilder; import com.google.common.cache.C

Post by answerhappygod »

Create JUnit tests for the following code:
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.visa.dps.model.VeesKey;
import com.visa.ip.settle.vees.impl.VeesEncryptionManager;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
@Slf4j
public class KeyCache {
private final VeesKeysService
veesKeysService;
private final VeesEncryptionManager
veesEncryptionManager;
@Autowired
public KeyCache(VeesKeysService vks,
VeesEncryptionManager vem) {
this.veesKeysService = vks;
this.veesEncryptionManager = vem;
}
@SneakyThrows
@PostConstruct
public void init() {
List<Long> keyids =
veesKeysService.getVeesKeys().stream().map(VeesKey::getKeyId).collect(Collectors.toList());

veesKeyCache.getAll(keyids);
log.info("Loaded Vees key
cache");
}
/**
* Setup Google {@link LoadingCache} to cache
{@link VeesKey}s for a certain amount of time. Once key(s)
* are evicted; subsequent requests for key will
be reloaded.
*/
private final LoadingCache<Long, VeesKey>
veesKeyCache = CacheBuilder.newBuilder()
.expireAfterWrite(15,
TimeUnit.MINUTES)

.removalListener(notification -> log.debug("Vees key {} was
evicted ({})", notification.getKey(),
notification.wasEvicted()))
.build(new
CacheLoader<Long, VeesKey>() {
/**
*
Computes or retrieves the value corresponding to {@code key}.

*
*
@param kid the non-null key whose value should be loaded
*
@return the value associated with {@code kid}; <b>must not be
null</b>

*/

@Override
public
VeesKey load(Long kid) {

return loadVeesKey(kid);
}
/**
*
Lookup the {@link VeesKey} by id

*
*
@param keyId the id to find with
*
@return a populated {@link VeesKey}

*/

@SneakyThrows
private
VeesKey loadVeesKey(Long keyId) {

return veesKeysService.getKeyById(keyId);
}
});
/**
* Every 10 minutes run encrypt and decrypts
against Vees to
* keep its internal caches warm.
*/
@SneakyThrows
@Scheduled(initialDelay = 1500L, fixedRate =
600_000L)
private void warmupVees() {
log.debug("Warming up Vees");
veesEncryptionManager.decryptPAN(
veesEncryptionManager.encryptPAN("1111222233334444"));
veesEncryptionManager.decryptPII(
veesEncryptionManager.encryptPII("warm-vees-pii-cache") );
}
/**
* Use the {@link LoadingCache} to find the
VeesKey by id
*
* @param keyId the desired vees key id
* @return a VeesKey pertaining to the
keyId
*/
public VeesKey getVeesKey(Long keyId) {
return
this.veesKeyCache.getUnchecked(keyId);
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply