Skip to content

Commit

Permalink
[PPANTT-82] feat: Updated docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini committed Sep 11, 2024
1 parent 93be9ca commit 6d2113c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import java.util.List;

/**
* Client for the api-config-cache services
*/
@Path("/cache")
@RegisterRestClient
public interface ApiConfigCacheClient {

/**
* Retrieve cache from the provided service
* @param keys list of strings to be used as filter for provided data
* @return required cache data
*/
@GET
ConfigDataV1 getCache(@QueryParam("keys") List<String> keys);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Consumer for events coming from the nodo-dei-pagamenti-cache topic, triggering
* the process for local cache-config update
*/
@ApplicationScoped
public class ConfigCacheUpdatesConsumer {

Expand All @@ -17,6 +21,10 @@ public class ConfigCacheUpdatesConsumer {
@Inject
public ConfigCacheService configCacheService;

/**
* Consume method, containing the trigger of the local cache config update
* @param event cache update event
*/
@Incoming("nodo-dei-pagamenti-cache")
@Transactional
public void consume(CacheUpdateEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;
import java.util.List;

/**
* Service to manage local instance of the config cache, to contain and update
* based on retrieved data
*/
@ApplicationScoped
public class ConfigCacheService {

Expand All @@ -18,11 +22,27 @@ public class ConfigCacheService {

private ConfigCacheData configCacheData;

public ConfigCacheData getConfigCacheData() {
return this.configCacheData != null ?
this.configCacheData : checkAndUpdateCache(null);
/**
* Provides instance of the local cache data, if not yet provided,
* it will call the checkAndUpdate method
* @return local instance of the configCacheData
*/
public ConfigDataV1 getConfigCacheData() {
return this.configCacheData != null || this.configCacheData.getConfigDataV1() == null ?
this.configCacheData.getConfigDataV1() :
checkAndUpdateCache(null).getConfigDataV1();
}

/**
* Executes a check and update process based on the update event (if provided). If the
* input event is null it will always execute a call using the client instance
*
* If the event is provided, it will check if the version is obsolete, and will execute
* the update only when a new cache version is passed through the update event
*
* @param cacheUpdateEvent contains version of the update event
* @return instance of the configCacheData
*/
public ConfigCacheData checkAndUpdateCache(CacheUpdateEvent cacheUpdateEvent) {

if (configCacheData == null || cacheUpdateEvent == null ||
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ quarkus.log.console.json.additional-field."app_environment".value=${quarkus.appl
## KAFKA
###################

%test.kafka.bootstrap.servers=${KAFKA_HOST:localhost:9092}
mp.messaging.incoming.nodo-dei-pagamenti-cache.bootstrap.servers=${CACHE_EVT_HOST:localhost:9092}
mp.messaging.incoming.nodo-dei-pagamenti-cache.topic=${CACHE_EVT_TOPIC:nodo-dei-pagamenti-cache}
mp.messaging.incoming.nodo-dei-pagamenti-cache.offset.reset=latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void checkAndUpdateCacheOk() {
ConfigDataV1.builder().version("2").build()
);
configCacheService.checkAndUpdateCache(null);
ConfigCacheData configCacheData = configCacheService.getConfigCacheData();
ConfigDataV1 configCacheData = configCacheService.getConfigCacheData();
assertNotNull(configCacheData);
assertEquals(configCacheData.getVersion(), "2");
configCacheService.checkAndUpdateCache(CacheUpdateEvent.builder()
Expand Down

0 comments on commit 6d2113c

Please sign in to comment.