-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
per
committed
Feb 11, 2023
1 parent
60b4bd0
commit b0a8b03
Showing
7 changed files
with
199 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package test.alipsa.simplerest; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import se.alipsa.simplerest.RestClient; | ||
import se.alipsa.simplerest.RestException; | ||
import test.alipsa.simplerest.servlets.ComplexServlet; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class ComplexRestTest { | ||
|
||
private static Server server; | ||
private static String serverUrl; | ||
private static RestClient restClient; | ||
|
||
|
||
|
||
@BeforeAll | ||
public static void startJetty() throws Exception { | ||
//System.out.println("Starting jetty server"); | ||
server = new Server(); | ||
ServerConnector connector = new ServerConnector(server); | ||
connector.setPort(0); // auto-bind to available port | ||
server.addConnector(connector); | ||
|
||
ServletContextHandler context = new ServletContextHandler(); | ||
context.addServlet(ComplexServlet.class,"/complex/*"); | ||
server.setHandler(context); | ||
|
||
server.start(); | ||
|
||
String host = connector.getHost(); | ||
if (host == null) { | ||
host = "localhost"; | ||
} | ||
int port = connector.getLocalPort(); | ||
serverUrl = String.format("http://%s:%d/",host,port); | ||
restClient = new RestClient(); | ||
} | ||
|
||
@AfterAll | ||
public static void stopJetty() { | ||
try { | ||
server.stop(); | ||
} | ||
catch (Exception e) { | ||
fail(e); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void getComplexReturn() throws RestException, JsonProcessingException { | ||
var response = restClient.get(serverUrl + "complex"); | ||
assertEquals(200, response.getResponseCode(), "get /complex, response Code"); | ||
Map<String, List<String>> map = response.getForType(new TypeReference<>() {}); | ||
assertEquals(2, map.size()); | ||
assertEquals("bar", map.get("two").get(1)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/test/alipsa/simplerest/servlets/ComplexServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package test.alipsa.simplerest.servlets; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import se.alipsa.simplerest.MediaType; | ||
import test.alipsa.simplerest.model.Company; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ComplexServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
//System.out.println("In SimpleServlet.doGet for " + req.getRequestURI()); | ||
resp.setStatus(200); | ||
resp.setContentType(MediaType.APPLICATION_JSON.getValue()); | ||
var writer = resp.getWriter(); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
Map<String, List<String>> map = new HashMap<>(); | ||
map.put("one", List.of("a", "b", "c")); | ||
map.put("two", List.of("foo", "bar", "baz")); | ||
String value = mapper.writeValueAsString(map); | ||
resp.setContentLength(value.getBytes(StandardCharsets.UTF_8).length); | ||
writer.print(value); | ||
writer.close(); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
//System.out.println("In SimpleServlet.doPost for " + req.getRequestURI()); | ||
resp.setStatus(200); | ||
resp.setContentType(MediaType.APPLICATION_JSON.getValue()); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
var company = mapper.readValue(req.getInputStream(), Company.class); | ||
company.setNumber(191919); | ||
var writer = resp.getWriter(); | ||
writer.print(mapper.writeValueAsString(company)); | ||
writer.close(); | ||
} | ||
|
||
@Override | ||
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
//System.out.println("In SimpleServlet.doPut for " + req.getRequestURI()); | ||
resp.setStatus(200); | ||
resp.setContentType(MediaType.APPLICATION_JSON.getValue()); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
var company = mapper.readValue(req.getInputStream(), Company.class); | ||
var writer = resp.getWriter(); | ||
writer.print(mapper.writeValueAsString(company)); | ||
writer.close(); | ||
} | ||
|
||
@Override | ||
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
//System.out.println("In SimpleServlet.doDelete for " + req.getRequestURI()); | ||
if (req.getRequestURI().endsWith("/company/123")) { | ||
resp.setStatus(204); | ||
} else { | ||
resp.setStatus(404); | ||
} | ||
resp.setContentLength(0); | ||
} | ||
|
||
@Override | ||
protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
//System.out.println("In SimpleServlet.doHead for " + req.getRequestURI()); | ||
resp.setStatus(200); | ||
resp.setContentType(MediaType.APPLICATION_JSON.getValue()); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
Company company = new Company("ABC", 123); | ||
String value = mapper.writeValueAsString(company); | ||
resp.setContentLength(value.getBytes(StandardCharsets.UTF_8).length); | ||
} | ||
|
||
@Override | ||
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
//System.out.println("In SimpleServlet.doOptions for " + req.getRequestURI()); | ||
resp.setContentType(MediaType.APPLICATION_JSON.getValue()); | ||
super.doOptions(req, resp); | ||
} | ||
} |