JAVA-21440: Changes made for upgrade jooby module to 2.16.1 to make j… (#14064)

* JAVA-21440: Chnages made for upgrade jooby module to 2.16.1 to make java-17 compatable

* JAVA-21440: commenting redis code as this will only work in the local or infra should have a redis setup
This commit is contained in:
Bipin kumar
2023-05-21 15:15:40 +05:30
committed by GitHub
parent 280e309354
commit de133c6dc0
9 changed files with 218 additions and 144 deletions

View File

@@ -1,95 +1,135 @@
package com.baeldung.jooby;
import org.jooby.Jooby;
import org.jooby.Mutant;
import org.jooby.Session;
import org.jooby.jedis.Redis;
import org.jooby.jedis.RedisSessionStore;
import com.baeldung.jooby.bean.Employee;
import io.jooby.Jooby;
import io.jooby.ServerOptions;
import io.jooby.Session;
import io.jooby.SessionStore;
import io.jooby.redis.RedisModule;
import io.jooby.redis.RedisSessionStore;
import io.lettuce.core.RedisClient;
public class App extends Jooby {
{
setServerOptions(new ServerOptions().setPort(8080)
.setSecurePort(8433));
}
{
port(8080);
securePort(8443);
}
{
get("/", () -> "Hello World!");
}
{
get("/", ctx -> "Hello World!");
}
{
get("/user/{id}", req -> "Hello user : " + req.param("id").value());
get("/user/:id", req -> "Hello user: " + req.param("id").value());
get("/uid:{id}", req -> "Hello User with id : uid" + req.param("id").value());
}
{
get("/user/{id}", ctx -> "Hello user : " + ctx.path("id")
.value());
get("/user/:id", ctx -> "Hello user: " + ctx.path("id")
.value());
get("/uid:{id}", ctx -> "Hello User with id : uid = " + ctx.path("id")
.value());
}
{
onStart(() -> {
System.out.println("starting app");
});
{
onStarting(() -> {
System.out.println("starting app");
});
onStop(() -> {
System.out.println("stopping app");
});
onStop(() -> {
System.out.println("stopping app");
});
onStarted(() -> {
System.out.println("app started");
});
}
onStarted(() -> {
System.out.println("app started");
});
}
{
get("/login", () -> "Hello from Baeldung");
}
{
get("/login", ctx -> "Hello from Baeldung");
}
{
post("/save", req -> {
Mutant token = req.param("token");
return token.intValue();
});
}
{
post("/save", ctx -> {
String userId = ctx.query("id")
.value();
return userId;
});
}
{
{
assets("/employee", "form.html");
}
{
{
assets("/employee", "public/form.html");
}
post("/submitForm", req -> {
Employee employee = req.params(Employee.class);
// TODO
return "empoyee data saved successfullly";
});
}
post("/submitForm", ctx -> {
Employee employee = ctx.path(Employee.class);
// TODO
return "employee data saved successfully";
});
{
get("/filter", (req, resp, chain) -> {
// TODO
// resp.send(...);
chain.next(req, resp);
});
get("/filter", (req, resp) -> {
resp.send("filter response");
});
}
}
{
// cookieSession();
// use(new Redis());
//
// session(RedisSessionStore.class);
get("/session", req -> {
Session session = req.session();
session.set("token", "value");
return session.get("token").value();
});
}
{
decorator(next -> ctx -> {
System.out.println("first");
// Moves execution to next handler: second
return next.apply(ctx);
});
decorator(next -> ctx -> {
System.out.println("second");
// Moves execution to next handler: third
return next.apply(ctx);
});
public static void main(final String[] args) {
get("/handler", ctx -> {
return "third";
});
}
run(App::new, args);
}
{
get("/sessionInMemory", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token")
.value();
});
}
{
String secret = "super secret token";
setSessionStore(SessionStore.signed(secret));
get("/signedSession", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token").value();
});
}
{
get("/sessionInMemoryRedis", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token")
.value();
});
}
/* This will work once redis is installed locally
{
install(new RedisModule("redis"));
setSessionStore(new RedisSessionStore(require(RedisClient.class)));
get("/redisSession", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token");
});
}*/
public static void main(final String[] args) {
runApp(args, App::new);
}
}

View File

@@ -1,22 +1,23 @@
package com.baeldung.jooby.mvc;
import org.jooby.Result;
import org.jooby.Results;
import org.jooby.mvc.GET;
import org.jooby.mvc.Path;
import java.util.HashMap;
import io.jooby.ModelAndView;
import io.jooby.annotations.GET;
import io.jooby.annotations.Path;
@Path("/hello")
public class GetController {
@GET
public String hello() {
return "Hello Baeldung";
}
@GET
public String hello() {
return "Hello Baeldung";
}
@GET
@Path("/home")
public Result home() {
return Results.html("welcome").put("model", new Object());
}
@GET
@Path("/home")
public ModelAndView home() {
return new ModelAndView("welcome.html", new HashMap<>());
}
}

View File

@@ -1,14 +1,13 @@
package com.baeldung.jooby.mvc;
import org.jooby.mvc.POST;
import org.jooby.mvc.Path;
import io.jooby.annotations.POST;
import io.jooby.annotations.Path;
@Path("/submit")
public class PostController {
@POST
public String hello() {
return "Submit Baeldung";
}
@POST
public String hello() {
return "Submit Baeldung";
}
}

View File

@@ -1,21 +1,32 @@
package com.baeldung.jooby;
import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.jooby.test.JoobyRule;
import org.junit.ClassRule;
import org.junit.Test;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import io.jooby.JoobyTest;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@JoobyTest(value = App.class, port = 8080)
public class AppLiveTest {
@ClassRule
public static JoobyRule app = new JoobyRule(new App());
static OkHttpClient client = new OkHttpClient();
@Test
public void given_defaultUrl_expect_fixedString() {
get("/").then().assertThat().body(equalTo("Hello World!")).statusCode(200)
.contentType("text/html;charset=UTF-8");
}
@Test
public void given_defaultUrl_expect_fixedString() {
Request request = new Request.Builder().url("http://localhost:8080")
.build();
try (Response response = client.newCall(request)
.execute()) {
assertEquals("Hello World!", response.body()
.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -2,15 +2,16 @@ package com.baeldung.jooby;
import static org.junit.Assert.assertEquals;
import org.jooby.test.MockRouter;
import org.junit.Test;
import io.jooby.MockRouter;
public class AppUnitTest {
@Test
public void given_defaultUrl_with_mockrouter_expect_fixedString() throws Throwable {
String result = new MockRouter(new App()).get("/");
assertEquals("Hello World!", result);
}
@Test
public void given_defaultUrl_with_mockrouter_expect_fixedString() {
MockRouter router = new MockRouter(new App());
assertEquals("Hello World!", router.get("/")
.value());
}
}