Skip to content

Commit

Permalink
backport RESTEASY-2287 Add complete tests for different optional para…
Browse files Browse the repository at this point in the history
…mter types
  • Loading branch information
liweinan authored and asoldano committed Aug 9, 2019
1 parent 68a97d5 commit ac5e9b3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
Expand Up @@ -11,6 +11,8 @@
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.NotFoundException;

/**
* @tpSubChapter Resource tests
* @tpChapter Unit tests
Expand All @@ -33,6 +35,7 @@ public void testOptionalStringAbsent() throws Exception {

}


@Test
public void testOptionalStringPresent() throws Exception {
MockHttpRequest req = MockHttpRequest.get("/optional/string?value=88");
Expand Down Expand Up @@ -64,6 +67,81 @@ public void testOptionalLongAbsent() throws Exception {
.getEntity());
}

@Test
public void testMatrixParamAbsent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.post("/optional/matrix");
Assert.assertEquals("42", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}

@Test
public void testMatrixParamPresent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.post("/optional/matrix;value=24");
Assert.assertEquals("24", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}

@Test
public void testPathParamPresent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.get("/optional/path/24");
Assert.assertEquals("24", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}

@Test(expected = NotFoundException.class)
public void testPathParamNeverAbsentThrowsException() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.get("/optional/path/");
registry.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity();
}

@Test
public void testHeaderParamAbsent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.get("/optional/header");
Assert.assertEquals("42", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}


@Test
public void testHeaderParamPresent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.get("/optional/header");
httpRequest.header("value", "24");
Assert.assertEquals("24", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}

@Test
public void testCookieParamAbsent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.get("/optional/cookie");
Assert.assertEquals("42", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}

@Test
public void testCookieParamPresent() throws Exception {
MockHttpRequest httpRequest = MockHttpRequest.get("/optional/cookie");
httpRequest.cookie("value", "24");
Assert.assertEquals("24", registry
.getResourceInvoker(httpRequest)
.invoke(httpRequest, resp)
.getEntity());
}


@Test
public void testOptionalLongPresent() throws Exception {
MockHttpRequest req = MockHttpRequest.post("/optional/long");
Expand Down
@@ -1,15 +1,20 @@
package org.jboss.resteasy.test.resource.resource;

import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import org.jboss.resteasy.annotations.jaxrs.MatrixParam;
import org.jboss.resteasy.annotations.jaxrs.PathParam;

import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;


@Path("/optional")
public class OptionalResource {
Expand Down Expand Up @@ -57,4 +62,22 @@ public static Holder<String> valueOf(String value) {
return new Holder<>(value);
}
}

@Path("/matrix")
@POST
public String matrix(@MatrixParam("value") OptionalLong value) {
return Long.toString(value.orElse(42));
}

@Path("/path/{value}")
@GET
public String path(@PathParam("value") OptionalLong value) { return Long.toString(value.orElse(42)); }

@Path("/header")
@GET
public String header(@HeaderParam("value") OptionalLong value) { return Long.toString(value.orElse(42)); }

@Path("/cookie")
@GET
public String cookie(@CookieParam("value") OptionalLong value) { return Long.toString(value.orElse(42)); }
}

0 comments on commit ac5e9b3

Please sign in to comment.