Skip to content

Commit

Permalink
quarkus-redis-service-map test demo
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Feb 1, 2024
1 parent 237b68f commit 445cad5
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 2 deletions.
7 changes: 6 additions & 1 deletion fj-service-helper-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-config-yaml</artifactId>
<artifactId>quarkus-redis-client</artifactId>
</dependency>

<dependency>
Expand All @@ -82,6 +82,11 @@
<groupId>org.fugerit.java</groupId>
<artifactId>emp-service-model</artifactId>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>data-service-base</artifactId>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.fugerit.java.sh.demo.redis;

import java.util.Arrays;

import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.fugerit.java.dsb.attributes.SimpleServiceMap;
import org.fugerit.java.emp.sm.service.ServiceMessage;
import org.fugerit.java.emp.sm.service.ServiceResponse;

import io.vertx.core.cli.annotations.Description;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@ApplicationScoped
@Path("/service/redis")
@Description("Demo service for Service Map Quarkus Redis")
public class ServiceMapRedisRest {

private SimpleServiceMap serviceMap;

public ServiceMapRedisRest( SimpleServiceMap serviceMap ) {
this.serviceMap = serviceMap;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@APIResponse(responseCode = "200", description = "Operation OK!",
content = @Content(mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = ServiceResponse.class)))
@Path("/set/{key}/{value}")
@Operation(operationId = "redis set ok", summary = "set the key/value pair from path pararmeters.")
public Response redisSet( @PathParam("key") String key, @PathParam("value") String value ) {
ServiceResponse sr = new ServiceResponse();
serviceMap.set(key, value);
sr.setSuccess(Arrays.asList(new ServiceMessage("200002", ServiceMessage.SEVERITY_SUCCESS, String.format( "Set OK key : %s, value : %s" , key, value ) )));
return Response.ok().entity(sr).build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@APIResponse(responseCode = "200", description = "Operation OK!",
content = @Content(mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = ServiceResponse.class)))
@Path("/get/{key}")
@Operation(operationId = "redis get ok", summary = "get the value from key path pararmeter.")
public Response redisGet( @PathParam("key") String key ) {
ServiceResponse sr = new ServiceResponse();
String value = serviceMap.get(key);
sr.setSuccess(Arrays.asList(new ServiceMessage("200003", ServiceMessage.SEVERITY_SUCCESS, String.format( "Get OK key : %s, value : %s" , key, value ) )));
return Response.ok().entity(sr).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fugerit.java.sh.demo.redis;

import org.fugerit.java.dsb.attributes.SimpleServiceMap;
import org.fugerit.java.dsb.attributes.redis.SimpleServiceMapRedisDefault;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Default;

@ApplicationScoped
@Default
public class SessionRedis extends SimpleServiceMapRedisDefault implements SimpleServiceMap {

}
4 changes: 4 additions & 0 deletions fj-service-helper-demo/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
enabled: false
swagger-ui:
always-include: true
redis:
hosts: redis://localhost:6379
devservices:
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test.org.fugerit.java.sh.demo.redis;

import static io.restassured.RestAssured.given;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.core.Response;

@QuarkusTest
class TestServiceMapRedisRest {

@Test
void testRedisSetOK() {
given()
.when()
.get( "/service/redis/set/myKey/myValue" )
.then()
.statusCode( Response.Status.OK.getStatusCode() );
}

@Test
void testRedisGetOK() {
given()
.when()
.get( "/service/redis/get/myKey" )
.then()
.statusCode( Response.Status.OK.getStatusCode() );
}

}
7 changes: 6 additions & 1 deletion quarkus-redis-service-map/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
<artifactId>fj-core</artifactId>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>data-service-base</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-config-yaml</artifactId>
<artifactId>quarkus-redis-client</artifactId>
<version>${quarkus.platform.version}</version>
<scope>provided</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.fugerit.java.dsb.attributes.redis;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.fugerit.java.dsb.attributes.SimpleServiceMap;

import io.quarkus.redis.datasource.RedisDataSource;
import io.quarkus.redis.datasource.value.ValueCommands;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SimpleServiceMapRedisDefault implements SimpleServiceMap {

@ConfigProperty( name = "cache.ttl-ms", defaultValue = "600000" ) // 10 minutes
private long ttl;

@Inject
private RedisDataSource ds;

private ValueCommands<String, String> com() {
return this.ds.value( String.class );
}

@Override
public String get(String key) {
log.debug( "get key:{}", key );
return this.com().get(key);
}

@Override
public void set(String key, String value) {
log.debug( "set key:{}, ttl:{}", key, this.ttl );
this.com().psetex(key, this.ttl, value);
}

@Override
public void remove(String key) {
log.warn( "remove method not implemented, key:{}", key );
}

}

0 comments on commit 445cad5

Please sign in to comment.