Skip to content

Commit

Permalink
[RESTEASY-2411] test-case for WhiteListPolymorphicTypeValidatorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
istudens committed Nov 20, 2019
1 parent d046c8d commit 7f1e0fa
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 0 deletions.
@@ -0,0 +1,11 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* @author bmaxwell
*/
@ApplicationPath("/")
public class JaxRsActivator extends Application {
}
@@ -0,0 +1,22 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.spi.HttpResponseCodes;
import org.jboss.resteasy.test.providers.jackson2.whitelist.model.TestPolymorphicType;

/**
* @author bmaxwell
*/
@Path("/test")
public class TestRESTService {
@POST
@Path("/post")
@Consumes("application/json")
public Response postTest(TestPolymorphicType test) {
return Response.status(HttpResponseCodes.SC_CREATED).entity("Test success: " + test).build();
}
}
@@ -0,0 +1,150 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.logging.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.plugins.providers.jackson.WhiteListPolymorphicTypeValidatorBuilder;
import org.jboss.resteasy.spi.HttpResponseCodes;
import org.jboss.resteasy.test.providers.jackson2.whitelist.model.AbstractVehicle;
import org.jboss.resteasy.test.providers.jackson2.whitelist.model.TestPolymorphicType;
import org.jboss.resteasy.test.providers.jackson2.whitelist.model.air.Aircraft;
import org.jboss.resteasy.test.providers.jackson2.whitelist.model.land.Automobile;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.ws.rs.client.ClientBuilder;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


/**
* @tpSubChapter Jackson2 provider
* @tpChapter Integration tests
* @tpSince RESTEasy 4.5.0
*/
@RunWith(Arquillian.class)
@RunAsClient
public class WhiteListPolymorphicTypeValidatorTest {

protected static final Logger logger = Logger.getLogger(WhiteListPolymorphicTypeValidatorTest.class.getName());

static ResteasyClient client;

@Deployment(name = "default")
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(WhiteListPolymorphicTypeValidatorTest.class.getSimpleName());
war.addClass(WhiteListPolymorphicTypeValidatorTest.class);
Map<String, String> contextParam = new HashMap<>();
contextParam.put("jackson.deserialization.whitelist.allowIfBaseType.prefix", Automobile.class.getPackage().getName());
contextParam.put("jackson.deserialization.whitelist.allowIfSubType.prefix", Automobile.class.getPackage().getName());
return TestUtil.finishContainerPrepare(war, contextParam, JaxRsActivator.class, TestRESTService.class,
TestPolymorphicType.class, AbstractVehicle.class, Automobile.class, Aircraft.class);
}

@Before
public void init() {
client = (ResteasyClient) ClientBuilder.newClient();
}

@After
public void after() throws Exception {
client.close();
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, WhiteListPolymorphicTypeValidatorTest.class.getSimpleName());
}

/**
* @tpTestDetails Client sends POST request with polymorphic type enabled by configuration of {@link WhiteListPolymorphicTypeValidatorBuilder}.
* @tpPassCrit The resource returns successfully, deserialization passed.
* @tpSince RESTEasy 4.5.0
*/
@Test
public void testGood() throws Exception {
String response = sendPost(new TestPolymorphicType(new Automobile()));
logger.info("response: " + response);
Assert.assertNotNull(response);
Assert.assertTrue(response.contains("Response code: " + HttpResponseCodes.SC_CREATED));
Assert.assertTrue(response.contains("Created"));
}

/**
* @tpTestDetails Client sends POST request with polymorphic type not enabled by configuration of {@link WhiteListPolymorphicTypeValidatorBuilder}.
* @tpPassCrit The resource returns HttpResponseCodes.SC_BAD_REQUEST, deserialization failed with 'PolymorphicTypeValidator denied resolution'.
* @tpSince RESTEasy 4.5.0
*/
@Test
public void testBad() throws Exception {
String response = sendPost(new TestPolymorphicType(new Aircraft()));
logger.info("response: " + response);
Assert.assertNotNull(response);
Assert.assertTrue(response.contains("Response code: " + HttpResponseCodes.SC_BAD_REQUEST));
Assert.assertTrue(response.contains("Configured `PolymorphicTypeValidator`") && response.contains("denied resolution"));
}

private String createJSONString(TestPolymorphicType t) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(t);
}

private String sendPost(TestPolymorphicType t) throws Exception {

logger.info("Creating JSON test data");
String jsonData = createJSONString(t);

logger.info("jsonData: " + jsonData);

String urlString = generateURL("/test/post");
logger.info("POST data to : " + urlString);
URL url = new URL(urlString);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST"); // PUT is another valid option
http.setDoOutput(true);

byte[] out = jsonData.getBytes(StandardCharsets.UTF_8);

http.setFixedLengthStreamingMode(out.length);
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}

InputStream is = null;
if (http.getResponseCode() != 200) {
is = http.getErrorStream();
} else {
/* error from server */
is = http.getInputStream();
}

String result = is == null ? "" : new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
String response = String.format("Response code: %s response message: %s %s", http.getResponseCode(), http.getResponseMessage(), result);

logger.info("Response: " + response);

return response;
}

}
@@ -0,0 +1,27 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist.model;

import java.io.Serializable;

/**
* @author bmaxwell
*/
public abstract class AbstractVehicle implements Serializable {

private String type;

protected AbstractVehicle(final String type) {
this.type = type;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String toString() {
return String.format("type; %s", this.getType());
}
}
@@ -0,0 +1,36 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist.model;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

/**
* @author bmaxwell
*/
public class TestPolymorphicType implements Serializable {

private String name;

// Using JsonTypeInfo.Id.CLASS enables polymorphic type handling.
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
public Serializable vehicle;

public TestPolymorphicType() {
}

public TestPolymorphicType(final Serializable vehicle) {
this.vehicle = vehicle;
}

public Serializable getVehicle() {
return vehicle;
}

public void setVehicle(Serializable vehicle) {
this.vehicle = vehicle;
}

public String toString() {
return String.format("name: %s vehicle: %s", this.name, this.vehicle);
}
}
@@ -0,0 +1,36 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist.model.air;

import org.jboss.resteasy.test.providers.jackson2.whitelist.model.AbstractVehicle;

/**
* @author bmaxwell
*/
public class Aircraft extends AbstractVehicle {

private int landSpeed;
private int airSpeed;

public Aircraft() {
super("Aircraft");
}

public int getLandSpeed() {
return landSpeed;
}

public void setLandSpeed(int landSpeed) {
this.landSpeed = landSpeed;
}

public int getAirSpeed() {
return airSpeed;
}

public void setAirSpeed(int airSpeed) {
this.airSpeed = airSpeed;
}

public String toString() {
return String.format("%s landSpeed: %d airSpeed: %d", super.toString(), landSpeed, airSpeed);
}
}
@@ -0,0 +1,27 @@
package org.jboss.resteasy.test.providers.jackson2.whitelist.model.land;

import org.jboss.resteasy.test.providers.jackson2.whitelist.model.AbstractVehicle;

/**
* @author bmaxwell
*/
public class Automobile extends AbstractVehicle {

private int speed;

public Automobile() {
super("Automobile");
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public String toString() {
return String.format("%s speed: %d", super.toString(), speed);
}
}

0 comments on commit 7f1e0fa

Please sign in to comment.