Last active
March 25, 2018 14:31
-
-
Save mintster/8bede353f7243251eb5cee3015c5037b to your computer and use it in GitHub Desktop.
REST Test Examples
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
package com.nixmash.web.rest; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.google.inject.Inject; | |
import com.nixmash.jangles.dto.ClientInfo; | |
import com.nixmash.jangles.dto.Post; | |
import com.nixmash.web.core.TestUI; | |
import com.nixmash.web.utils.PostTestUtils; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.JUnit4; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.client.WebTarget; | |
import javax.ws.rs.core.*; | |
import static com.nixmash.web.core.TestUI.*; | |
import static com.nixmash.web.rest.RestTestResource.INCLIENT; | |
import static junit.framework.TestCase.assertEquals; | |
import static junit.framework.TestCase.assertTrue; | |
@RunWith(JUnit4.class) | |
public class RestClientTest extends RestTestBase { | |
@Inject | |
private TestUI testUI; | |
private Response response; | |
private String result; | |
@Test | |
public void sendStringAsPathParamTest() { | |
WebTarget base = ClientBuilder.newClient().target(TEST_RESOURCE_URL); | |
response = base.path("/apikey/" + GOOD_TEST_APIKEY).request().get(); | |
assertTrue(isSuccess(response)); | |
result = base.path("/apikey/" + BAD_TEST_APIKEY).request().get(String.class); | |
assertTrue(result.equals(FAILED_RESULT)); | |
} | |
@Test | |
public void sendStringAsHeaderTest() { | |
WebTarget base = ClientBuilder.newClient().target(TEST_RESOURCE_URL); | |
result = base.path("/apikeyheader").request().header("apiKey", GOOD_TEST_APIKEY).get(String.class); | |
assertTrue(result.equals(SUCCESS_RESULT)); | |
} | |
@Test | |
public void sendObjectInHeaderTest() throws JsonProcessingException { | |
WebTarget base = ClientBuilder.newClient().target(TEST_RESOURCE_URL); | |
Post post = PostTestUtils.createPostWithTitleAndName(POST_TITLE, POST_NAME); | |
ObjectMapper mapper = new ObjectMapper(); | |
String json = mapper.writeValueAsString(post); | |
result = base.path("/post_in_header").request().header("post", json).get(String.class); | |
assertTrue(result.equals(SUCCESS_RESULT)); | |
} | |
@Test | |
public void sendContainerPropertiesTest() { | |
WebTarget base = client | |
.target(TEST_RESOURCE_URL); | |
result = base.path("/send_properties_container").request() | |
.property(INCLIENT, "will be null in ContainerRequestFilter") | |
.get(String.class); | |
assertTrue(result.equals(SUCCESS_RESULT)); | |
} | |
@Test | |
public void sendRequestPropertiesTest() { | |
WebTarget base = client | |
.register(TestClientFilter.class) | |
.target(TEST_RESOURCE_URL); | |
result = base.path("/send_properties_request").request() | |
.property(INCLIENT, "will be available in Client RequestFilter, but not in Request") | |
.get(String.class); | |
assertTrue(result.equals(SUCCESS_RESULT)); | |
} | |
@Test | |
public void sendMultiObjectsInHeaderTest() throws JsonProcessingException { | |
WebTarget base = client | |
.target(TEST_RESOURCE_URL); | |
Post postOne = PostTestUtils.createPost(1); | |
Post postTwo = PostTestUtils.createPost(2); | |
ObjectMapper mapper = new ObjectMapper(); | |
String jsonOne = mapper.writeValueAsString(postOne); | |
String jsonTwo = mapper.writeValueAsString(postTwo); | |
result = base.path("/multiple_posts_in_header").request() | |
.header("postone", jsonOne) | |
.header("posttwo", jsonTwo) | |
.get(String.class); | |
assertTrue(result.equals(SUCCESS_RESULT)); | |
} | |
@Test | |
public void sendHeaderMapTest() throws JsonProcessingException { | |
WebTarget base = ClientBuilder.newClient() | |
.target(TEST_RESOURCE_URL); | |
Post postOne = PostTestUtils.createPost(1); | |
Post postTwo = PostTestUtils.createPost(2); | |
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>(); | |
ObjectMapper mapper = new ObjectMapper(); | |
String jsonOne = mapper.writeValueAsString(postOne); | |
String jsonTwo = mapper.writeValueAsString(postTwo); | |
map.add("jsonOne", jsonOne); | |
map.add("jsonTwo", jsonTwo); | |
result = base.path("/header_map").request() | |
.headers(map) | |
.get(String.class); | |
assertTrue(result.equals(SUCCESS_RESULT)); | |
} | |
@Test | |
public void sendObjectAsPostParameterTest() { | |
WebTarget base = ClientBuilder.newClient().target(TEST_RESOURCE_URL); | |
Post post = PostTestUtils.createPostWithTitleAndName(POST_TITLE, POST_NAME); | |
response = base.path("/post_as_param").request().post(Entity.json(post)); | |
assertTrue(isSuccess(response)); | |
response.close(); | |
} | |
@Test | |
public void sendClientInfoAsParameterTest() { | |
WebTarget base = ClientBuilder.newClient().target(TEST_RESOURCE_URL); | |
ClientInfo clientInfo = ClientInfo.getBuilder("1.2.3.4", true, 1L) | |
.countryCode("US") | |
.pageKey("post") | |
.regionCode("VT") | |
.zipCode("05454") | |
.build(); | |
response = base.path("/clientinfo_as_param").request().post(Entity.json(clientInfo)); | |
assertTrue(isSuccess(response)); | |
response.close(); | |
} | |
@Test | |
public void sendForm() { | |
WebTarget base = ClientBuilder.newClient().target(TEST_RESOURCE_URL); | |
Form form = getSignupForm(); | |
response = base.path("/send_form") | |
.request() | |
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED), Response.class); | |
assertTrue(isSuccess(response)); | |
response.close(); | |
} | |
private Form getSignupForm() { | |
Form form = new Form(); | |
form.param("username", "testguy"); | |
form.param("firstName", "Testguy"); | |
form.param("lastName", "Jones"); | |
form.param("email", "testguy@nixmash.com"); | |
form.param("providerUserid", "twitter"); | |
return form; | |
} | |
private Boolean isSuccess(Response response) { | |
result = response.readEntity(String.class); | |
return result.equals(SUCCESS_RESULT); | |
} | |
@Test | |
public void testLongVsStringJsonValue() { | |
Long postId = 6974311181367341056L; | |
Entity stringEntity = Entity.json(String.valueOf(postId)); | |
Entity longEntity = Entity.json(postId); | |
assertEquals(stringEntity.toString(), longEntity.toString()); | |
} | |
} |
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
package com.nixmash.web.rest; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import com.nixmash.web.core.TestModule; | |
import io.bootique.jersey.JerseyModule; | |
import io.bootique.jetty.test.junit.JettyTestFactory; | |
import io.bootique.shiro.ShiroModule; | |
import org.apache.shiro.realm.Realm; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.ClassRule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.JUnit4; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.WebTarget; | |
import javax.ws.rs.core.Response; | |
import java.io.IOException; | |
import static junit.framework.TestCase.assertTrue; | |
import static org.mockito.Mockito.mock; | |
@RunWith(JUnit4.class) | |
public class RestTestBase { | |
public static final String TEST_URL = "http://127.0.1.1:9001"; | |
private static final String YAML_CONFIG = "--config=classpath:test.yml"; | |
@ClassRule | |
public static JettyTestFactory JETTY_FACTORY = new JettyTestFactory(); | |
protected Client client; | |
// region @BeforeClass and @AfterClass | |
@BeforeClass | |
public static void setupClass() throws IOException { | |
/* BQRuntime runtime = JETTY_FACTORY.app() | |
.autoLoadModules() | |
.args(YAML_CONFIG) | |
.module(b -> b.bind(IConnection.class).to(TestConnection.class)) | |
.module(b -> b.bind(WebUI.class)) | |
.module(b -> b.bind(UserService.class).to(UserServiceImpl.class)) | |
.module(b -> b.bind(UsersDb.class).to(UsersDbImpl.class)) | |
.module(b -> b.bind(AdminService.class).to(AdminServiceImpl.class)) | |
.module(b -> b.bind(AdminDb.class).to(AdminDbImpl.class)) | |
.module(b -> JerseyModule.extend(b).addResource(TestResource.class)) | |
.module(b -> JerseyModule.extend(b).addResource(TestController.class)) | |
.module(b -> ShiroModule.extend(b).addRealm(mock(Realm.class))) | |
.start();*/ | |
JETTY_FACTORY.app() | |
.autoLoadModules() | |
.args(YAML_CONFIG) | |
.module(b -> JerseyModule.extend(b).addResource(RestTestResource.class)) | |
.module(b -> JerseyModule.extend(b).addResource(TestContainerFilter.class)) | |
.module(b -> ShiroModule.extend(b).addRealm(mock(Realm.class))) | |
.start(); | |
} | |
@Before | |
public void startJetty() { | |
Injector injector = Guice.createInjector(new TestModule()); | |
injector.injectMembers(this); | |
ClientConfig config = new ClientConfig(); | |
this.client = ClientBuilder.newClient(config); | |
} | |
// endregion | |
@Test | |
public void msgGoAway() { | |
assertTrue(true); | |
} | |
protected WebTarget getTargetUrl(String path) { | |
return client.target("http://localhost:9001" + path); | |
} | |
protected Response pathResponse(String path) { | |
WebTarget base = client.target("http://localhost:9001"); | |
return base.path(path).request().get(); | |
} | |
protected Boolean responseOK(Response response) { | |
return Response.Status.OK.getStatusCode() == response.getStatus(); | |
} | |
} |
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
package com.nixmash.web.rest; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.google.inject.Inject; | |
import com.nixmash.jangles.dto.ClientInfo; | |
import com.nixmash.jangles.dto.Post; | |
import com.nixmash.web.core.TestUI; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.ws.rs.*; | |
import javax.ws.rs.container.ContainerRequestContext; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.core.Response; | |
import java.io.IOException; | |
import static com.nixmash.web.core.TestUI.GOOD_TEST_APIKEY; | |
import static com.nixmash.web.core.TestUI.POST_TITLE; | |
import static junit.framework.TestCase.assertNull; | |
import static junit.framework.TestCase.assertTrue; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
@Path("/test") | |
@Produces(MediaType.APPLICATION_JSON) | |
public class RestTestResource { | |
public static final String INFILTER = "infilter"; | |
public static final String INCLIENT = "inclient"; | |
@Inject | |
private TestUI testUI; | |
@Context | |
private HttpServletRequest httpRequest; | |
// region Parameter and Header Test Endpoints | |
@GET | |
@Path("/apikey/{apiKey}") | |
public Response sendStringAsPathParamTest(@PathParam("apiKey") String apiKey) { | |
if (apiKey.equals(GOOD_TEST_APIKEY)) { | |
return Response.ok().entity("SUCCESS").build(); | |
} else | |
return Response.ok().entity("FAILED").build(); | |
} | |
@GET | |
@Path("/apikeyheader") | |
public Response sendStringAsHeaderTest(@HeaderParam("apiKey") String apiKey) { | |
if (apiKey.equals(GOOD_TEST_APIKEY)) { | |
return Response.ok().entity("SUCCESS").build(); | |
} else | |
return Response.ok().entity("FAILED").build(); | |
} | |
@GET | |
@Path("/post_in_header") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response sendPostAsHeaderTest(@HeaderParam("post") String post) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
Post received = mapper.readValue(post, Post.class); | |
assertEquals(received.getPostTitle(), POST_TITLE); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@GET | |
@Path("/send_properties_container") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response handleContainerProperties(@Context ContainerRequestContext context) { | |
assertNotNull(context.getProperty(INFILTER)); | |
assertNull(context.getProperty(INCLIENT)); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@GET | |
@Path("/send_properties_request") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response handleRequestProperties(@Context ContainerRequestContext context) { | |
assertNotNull(context.getProperty(INFILTER)); | |
assertNull(context.getProperty(INCLIENT)); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@GET | |
@Path("/multiple_posts_in_header") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response handleMultiPostsInHeaderTest(@HeaderParam("postone") String postOne, | |
@HeaderParam("posttwo") String postTwo) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
Post receivedOne = mapper.readValue(postOne, Post.class); | |
Post receivedTwo = mapper.readValue(postTwo, Post.class); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@GET | |
@Path("/header_map") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response handleHeaderMapTest() throws IOException { | |
String postOne = httpRequest.getHeader("jsonOne"); | |
String postTwo = httpRequest.getHeader("jsonTwo"); | |
ObjectMapper mapper = new ObjectMapper(); | |
Post receivedOne = mapper.readValue(postOne, Post.class); | |
Post receivedTwo = mapper.readValue(postTwo, Post.class); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@POST | |
@Path("/post_as_param") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response sendPostAsParamTest(Post post) throws IOException { | |
assertEquals(post.getPostTitle(), POST_TITLE); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@POST | |
@Path("/clientinfo_as_param") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public Response handleMultiProperties(ClientInfo clientInfo) throws IOException { | |
assertTrue(clientInfo.getAdblocker()); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
@POST | |
@Path("/send_form") | |
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | |
public Response handleMultiProperties(MultivaluedMap<String, String> formParams) throws IOException { | |
assertTrue(formParams.getFirst("username").equals("testguy")); | |
return Response.ok().entity("SUCCESS").build(); | |
} | |
// endregion | |
} |
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
package com.nixmash.web.rest; | |
import javax.ws.rs.client.ClientRequestContext; | |
import javax.ws.rs.client.ClientRequestFilter; | |
import javax.ws.rs.client.ClientResponseContext; | |
import javax.ws.rs.client.ClientResponseFilter; | |
import java.io.IOException; | |
import static com.nixmash.web.rest.RestTestResource.INCLIENT; | |
import static org.junit.Assert.assertNotNull; | |
public class TestClientFilter implements ClientRequestFilter, ClientResponseFilter { | |
@Override | |
public void filter(ClientRequestContext reqContext) throws IOException { | |
assertNotNull(reqContext.getProperty(INCLIENT)); | |
} | |
@Override | |
public void filter(ClientRequestContext reqContext, | |
ClientResponseContext resContext) throws IOException { | |
assertNotNull(reqContext.getProperty(INCLIENT)); | |
} | |
} |
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
package com.nixmash.web.rest; | |
import javax.ws.rs.container.ContainerRequestContext; | |
import javax.ws.rs.container.ContainerRequestFilter; | |
import java.io.IOException; | |
import static com.nixmash.web.rest.RestTestResource.INCLIENT; | |
import static com.nixmash.web.rest.RestTestResource.INFILTER; | |
import static junit.framework.TestCase.assertNull; | |
public class TestContainerFilter implements ContainerRequestFilter { | |
@Override | |
public void filter(ContainerRequestContext containerRequestContext) throws IOException { | |
assertNull(containerRequestContext.getProperty(INCLIENT)); | |
containerRequestContext.setProperty(INFILTER, | |
"properties set in filter available to Request Endpoint"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment