Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions simple-java-dedup/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
**
!target/simple-java-dedup.jar
!target/lib/**
!target/classes/**
!target/keploy-sdk.jar
!target/jacocoagent.jar
1 change: 1 addition & 0 deletions simple-java-dedup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN groupadd --gid 10001 appuser \
&& useradd --uid 10001 --gid 10001 --home-dir /home/appuser --create-home --shell /usr/sbin/nologin appuser

COPY --chown=10001:10001 target/simple-java-dedup.jar /app/app.jar
COPY --chown=10001:10001 target/lib /app/lib
COPY --chown=10001:10001 target/classes /app/classes
COPY --chown=10001:10001 target/keploy-sdk.jar /app/keploy-sdk.jar
COPY --chown=10001:10001 target/jacocoagent.jar /app/jacocoagent.jar
Expand Down
12 changes: 11 additions & 1 deletion simple-java-dedup/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Simple Java Dynamic Deduplication Sample

This is a small plain-Java HTTP service used to smoke-test Keploy Enterprise Java dynamic deduplication on Java 8 and Java 17. It uses only JDK APIs (`com.sun.net.httpserver.HttpServer`) and does not compile against the Keploy SDK.
This is a small plain-Java HTTP service used to smoke-test Keploy Enterprise Java dynamic deduplication on Java 8 and Java 17. It runs on embedded Tomcat (`tomcat-embed-core`) and does not compile against the Keploy SDK.

## Why Embedded Tomcat (Supported Java HTTP Servers)

Keploy's Java dynamic deduplication agent (`keploy-sdk` >= 2.3.x) counts HTTP request windows at the server's request entry point so background threads or post-response work cannot leak coverage between test cases. Supported Java HTTP servers are:

- **Apache Tomcat** (8.5–11, including Spring Boot's default embedded Tomcat)
- **Eclipse Jetty** (9.4–12)
- **Undertow**

Uncounted servers (such as `com.sun.net.httpserver.HttpServer`, Netty, Reactor Netty, gRPC, Vert.x, and Micronaut) fail closed with `unverified:no-server-entry` in Kubernetes dynamic dedup (`k8s-proxy`), which keeps all test cases and reports 0 duplicates rather than risking unsafe drops. This sample uses embedded Tomcat so it works out of the box with both Kubernetes (`k8s-proxy`) and CLI dynamic deduplication.

The checked-in fixtures exercise `/healthz`, `/grade`, `/shipping`, `/inventory`, and `/invoice`. They intentionally contain duplicate coverage paths:

Expand Down
27 changes: 26 additions & 1 deletion simple-java-dedup/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@
<packaging>jar</packaging>

<name>simple-java-dedup</name>
<description>Small plain-Java sample for Keploy dynamic deduplication smoke tests.</description>
<description>Small plain-Java sample on embedded Tomcat for Keploy dynamic deduplication smoke tests.</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.8.12</jacoco.version>
<tomcat.embed.version>9.0.121</tomcat.embed.version>
</properties>

<dependencies>
<!-- Embedded Tomcat 9 (Java 8+ compatible) is counted by keploy-sdk's
request-entry tracker (org.apache.catalina.connector.CoyoteAdapter),
so k8s-proxy dynamic dedup measures per-request coverage windows. -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.embed.version}</version>
</dependency>
</dependencies>

<build>
<finalName>simple-java-dedup</finalName>
<plugins>
Expand All @@ -39,6 +51,8 @@
<archive>
<manifest>
<mainClass>io.keploy.samples.simplededup.SimpleJavaDedupApplication</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
Expand All @@ -48,6 +62,17 @@
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-runtime-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
<execution>
<id>copy-jacoco-agent</id>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,69 @@
package io.keploy.samples.simplededup;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;

public final class SimpleJavaDedupApplication {
private SimpleJavaDedupApplication() {
}

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
server.createContext("/healthz", new HealthHandler());
server.createContext("/grade", new GradeHandler());
server.createContext("/shipping", new ShippingHandler());
server.createContext("/inventory", new InventoryHandler());
server.createContext("/invoice", new InvoiceHandler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
Path baseDir = Files.createTempDirectory("simple-java-dedup-tomcat-");
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir(baseDir.toAbsolutePath().toString());
tomcat.setHostname("0.0.0.0");

Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(port);
connector.setProperty("address", "0.0.0.0");
tomcat.setConnector(connector);

Context ctx = tomcat.addContext("", baseDir.toAbsolutePath().toString());
register(ctx, "healthz", "/healthz", new HealthHandler());
register(ctx, "grade", "/grade", new GradeHandler());
register(ctx, "shipping", "/shipping", new ShippingHandler());
register(ctx, "inventory", "/inventory", new InventoryHandler());
register(ctx, "invoice", "/invoice", new InvoiceHandler());

tomcat.start();
System.out.println("simple-java-dedup listening on " + port);
tomcat.getServer().await();
}

private static final class HealthHandler implements HttpHandler {
private static void register(Context ctx, String name, String path, HttpServlet servlet) {
Tomcat.addServlet(ctx, name, servlet);
ctx.addServletMappingDecoded(path, name);
}

private static final class HealthHandler extends HttpServlet {
@Override
public void handle(HttpExchange exchange) throws IOException {
if (!"GET".equals(exchange.getRequestMethod())) {
respond(exchange, 405, "{\"error\":\"method_not_allowed\"}");
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
if (!"GET".equals(req.getMethod())) {
respond(resp, 405, "{\"error\":\"method_not_allowed\"}");
return;
}
respond(exchange, 200, "{\"status\":\"ok\"}");
respond(resp, 200, "{\"status\":\"ok\"}");
}
}

private static final class GradeHandler implements HttpHandler {
private static final class GradeHandler extends HttpServlet {
@Override
public void handle(HttpExchange exchange) throws IOException {
Map<String, String> query = parseQuery(exchange.getRequestURI().getRawQuery());
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Map<String, String> query = parseQuery(req.getQueryString());
int score = parseInt(query.get("score"), 0);
String grade;
String message;
Expand All @@ -61,14 +80,14 @@ public void handle(HttpExchange exchange) throws IOException {
grade = "F";
message = "retry";
}
respond(exchange, 200, "{\"score\":" + score + ",\"grade\":\"" + grade + "\",\"message\":\"" + message + "\"}");
respond(resp, 200, "{\"score\":" + score + ",\"grade\":\"" + grade + "\",\"message\":\"" + message + "\"}");
}
}

private static final class ShippingHandler implements HttpHandler {
private static final class ShippingHandler extends HttpServlet {
@Override
public void handle(HttpExchange exchange) throws IOException {
Map<String, String> query = parseQuery(exchange.getRequestURI().getRawQuery());
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Map<String, String> query = parseQuery(req.getQueryString());
String country = query.getOrDefault("country", "US");
int total = parseInt(query.get("total"), 0);
String tier;
Expand All @@ -83,14 +102,14 @@ public void handle(HttpExchange exchange) throws IOException {
tier = "standard";
etaDays = 5;
}
respond(exchange, 200, "{\"country\":\"" + country.toUpperCase() + "\",\"total\":" + total + ",\"tier\":\"" + tier + "\",\"etaDays\":" + etaDays + "}");
respond(resp, 200, "{\"country\":\"" + country.toUpperCase() + "\",\"total\":" + total + ",\"tier\":\"" + tier + "\",\"etaDays\":" + etaDays + "}");
}
}

private static final class InventoryHandler implements HttpHandler {
private static final class InventoryHandler extends HttpServlet {
@Override
public void handle(HttpExchange exchange) throws IOException {
Map<String, String> query = parseQuery(exchange.getRequestURI().getRawQuery());
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Map<String, String> query = parseQuery(req.getQueryString());
String sku = query.getOrDefault("sku", "BOOK-1").toUpperCase();
int quantity = parseInt(query.get("quantity"), 1);
String lane;
Expand All @@ -105,14 +124,14 @@ public void handle(HttpExchange exchange) throws IOException {
lane = "manual-review";
reserved = false;
}
respond(exchange, 200, "{\"sku\":\"" + sku + "\",\"quantity\":" + quantity + ",\"lane\":\"" + lane + "\",\"reserved\":" + reserved + "}");
respond(resp, 200, "{\"sku\":\"" + sku + "\",\"quantity\":" + quantity + ",\"lane\":\"" + lane + "\",\"reserved\":" + reserved + "}");
}
}

private static final class InvoiceHandler implements HttpHandler {
private static final class InvoiceHandler extends HttpServlet {
@Override
public void handle(HttpExchange exchange) throws IOException {
Map<String, String> query = parseQuery(exchange.getRequestURI().getRawQuery());
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Map<String, String> query = parseQuery(req.getQueryString());
String customer = query.getOrDefault("customer", "guest").toLowerCase();
int subtotal = parseInt(query.get("subtotal"), 0);
int discount;
Expand All @@ -131,7 +150,7 @@ public void handle(HttpExchange exchange) throws IOException {
segment = "standard";
}
int total = subtotal - discount;
respond(exchange, 200, "{\"customer\":\"" + customer + "\",\"subtotal\":" + subtotal + ",\"discount\":" + discount + ",\"total\":" + total + ",\"segment\":\"" + segment + "\"}");
respond(resp, 200, "{\"customer\":\"" + customer + "\",\"subtotal\":" + subtotal + ",\"discount\":" + discount + ",\"total\":" + total + ",\"segment\":\"" + segment + "\"}");
}
}

Expand Down Expand Up @@ -171,15 +190,13 @@ private static String decode(String value) {
}
}

private static void respond(HttpExchange exchange, int status, String body) throws IOException {
private static void respond(HttpServletResponse resp, int status, String body) throws IOException {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(status, bytes.length);
OutputStream responseBody = exchange.getResponseBody();
try {
responseBody.write(bytes);
} finally {
responseBody.close();
}
resp.setStatus(status);
resp.setContentType("application/json");
resp.setContentLength(bytes.length);
OutputStream responseBody = resp.getOutputStream();
responseBody.write(bytes);
responseBody.flush();
}
}
Loading