Skip to content

Commit

Permalink
[RESTEASY-1826] added plugins to generated i18n files
Browse files Browse the repository at this point in the history
  • Loading branch information
rsearls authored and asoldano committed Mar 21, 2018
1 parent 2b60817 commit 1003c54
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 12 deletions.
63 changes: 63 additions & 0 deletions providers/json-binding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,71 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
</dependency>
</dependencies>

<profiles>
<profile>
<id>i18n</id>
<activation>
<property>
<name>i18n</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>initialize</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/resources/org/jboss/resteasy/plugins/providers/jsonb/i18n</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/test/resources/i18n</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>i18</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<reuseForks>false</reuseForks>
<includes>
<include>**/I18nTestMessages_*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.xml.bind.annotation.XmlType;

import org.jboss.resteasy.util.FindAnnotation;
import org.jboss.resteasy.plugins.providers.jsonb.i18n.LogMessages;

/**
* Created by rsearls on 6/26/17.
Expand Down Expand Up @@ -53,7 +54,12 @@ public Object readFrom(Class<Object> type, Type genericType,
return jsonb.fromJson(entityStream, genericType);
} catch (Throwable e)
{
throw new ProcessingException(Messages.MESSAGES.jsonBDeserializationError(e.toString()));
if (LogMessages.LOGGER.isDebugEnabled()) {
LogMessages.LOGGER.debugf(Messages.MESSAGES.jsonBDeserializationError(e.toString()));
}

// detail text provided in logger message
throw new ProcessingException(Messages.MESSAGES.jsonBDeserializationError(""));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
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.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.test.providers.jsonb.basic.resource.Cat;
import org.jboss.resteasy.test.providers.jsonb.basic.resource.JsonBindingCustomRepeaterProvider;
import org.jboss.resteasy.test.providers.jsonb.basic.resource.JsonBindingResource;
import org.jboss.resteasy.utils.LogCounter;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
Expand All @@ -21,6 +25,7 @@
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static org.hamcrest.CoreMatchers.is;

Expand Down Expand Up @@ -100,7 +105,6 @@ public void jsonbOnServerNotOnClientTest() throws Exception {
Assert.assertThat("Variable with JsonbTransient annotation should be transient, if JSON-B is used",
json.getTransientVar(), is(Cat.DEFAULT_TRANSIENT_VAR_VALUE));
}

/**
* @tpTestDetails JSON-B is not used on both server and client
* check that @JsonbTransient annotation is ignored
Expand All @@ -120,4 +124,35 @@ public void jsonbNotOnServerNotOnClientTest() throws Exception {
Assert.assertThat("Variable with JsonbTransient annotation should not be transient, if JSON-B is not used",
json.getTransientVar(), is(JsonBindingResource.RETURNED_TRANSIENT_VALUE));
}
}

/**
* @tpTestDetails JSON-B is not used on client, JSON-B is used on server
* client uses custom json provider that returns corrupted json data
* client sends corrupted json data to server
* JSON-B provider on server should throw relevant exception
* Server should returns relevant error message in response
* @tpSince RESTEasy 3.5
*/
@Test
public void negativeScenarioOnServer() throws Exception {

try {
ResteasyClient client = new ResteasyClientBuilder().register(JsonBindingCustomRepeaterProvider.class).build();
String charset = "UTF-8";
WebTarget target = client.target(PortProviderUtil.generateURL("/test/jsonBinding/repeater", WAR_WITH_JSONB));
MediaType mediaType = MediaType.APPLICATION_JSON_TYPE.withCharset(charset);
Entity<Cat> entity = Entity.entity(
new Cat("Rosa", "semi-british", "tabby", true, JsonBindingResource.CLIENT_TRANSIENT_VALUE), mediaType);
logger.info("Request entity: " + entity);
Response response = target.request().post(entity);
// check response
int responseCode = response.getStatus();
Assert.assertThat("Wrong response code", responseCode, is(400));
String responseBody = response.readEntity(String.class);
Assert.assertTrue("Wrong response error message: " + responseBody,
responseBody.startsWith("javax.ws.rs.ProcessingException: RESTEASY008200"));
} finally {
client.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
import org.junit.Test;
import org.junit.runner.RunWith;


import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.io.PrintWriter;
import java.io.StringWriter;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;

/**
* @tpSubChapter Json-binding provider.JAX-RS 2.1 spec (JSR-370), section 11.2.7 states,
Expand Down Expand Up @@ -131,4 +138,42 @@ public void jsonbOnClientTest() throws Exception {
}


}
/**
* @tpTestDetails JSON-B is used on client, JSON-B is not used on server, server uses test's custom json provider
* Client send GET request to server
* Server returns Cat object, custom provider uses toString method, that doesn't doesn't create correct JSON data
* Client receive data with "json" media type, but data was created by toString method
* JSON-B on client should throw user-friendly exception, because toString method doesn't create correct JSON data
* @tpSince RESTEasy 3.5
*/
@Test
public void negativeScenarioOnClient() throws Exception {
// call and log get request
WebTarget target = client.target(PortProviderUtil.generateURL("/test/jsonBinding/get/cat", CUSTOM_JSON_PROVIDER));
Response response = target.request().get();
String responseAsString = response.readEntity(String.class);
Assert.assertThat("Server should use custom JSON provider", responseAsString, containsString(Cat.CUSTOM_TO_STRING_FORMAT));
logger.info("Response as a String: " + responseAsString);
response.close();

// call get request, try to get Cat data
response = target.request().get();
try {
Cat wrongObject = response.readEntity(Cat.class);
logger.info("JSON-B parse server toString method, although JSON-B should not do that. Received object:");
logger.info(wrongObject.toString());;
Assert.fail("Client should throw exception because JSON-B should not be able to parse wrong data");
}
catch (Throwable e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
String stackTraceString = errors.toString();
logger.info("StackTrace of exception:");
logger.info(stackTraceString);
for (String stackTraceLine : stackTraceString.split(System.lineSeparator())) {
Assert.assertThat("User-unfriendly error message in JSON-B", stackTraceLine,
not(containsString("Messages (implementation not found)")));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@JsonbPropertyOrder({"color", "sort", "name", "domesticated"})
public class Cat {

public static final String CUSTOM_TO_STRING_FORMAT = "custom toString format";

public static final Integer DEFAULT_TRANSIENT_VAR_VALUE = -1;

private String name;
Expand Down Expand Up @@ -82,13 +84,8 @@ public void setTransientVar(int transientVar) {

@Override
public String toString() {
return "Cat - custom toString format {" +
"name='" + name + '\'' +
", sort='" + sort + '\'' +
", color='" + color + '\'' +
", domesticated=" + domesticated +
", transientVar=" + transientVar +
'}';
return String.format("Cat - %s {name='%s', sort='%s', color='%s', domesticated=%s, transientVar=%s}",
CUSTOM_TO_STRING_FORMAT, name, sort, color, domesticated, transientVar);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jboss.resteasy.test.providers.jsonb.basic.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand Down Expand Up @@ -56,4 +57,20 @@ public String repeaterTransient(String data) throws Exception {
return "{\"color\":\"tabby\",\"sort\":\"semi-british\",\"name\":\"Rosa\",\"domesticated\":true,\"transientVar\":\""
+ RETURNED_TRANSIENT_VALUE + "\"}";
}
}

@Path("get/cat")
@GET
@Produces("application/json")
public Cat getCat() {
return new Cat("a", "b", "c", true, 0);
}


@Path("repeater")
@POST
@Produces("application/json")
@Consumes("application/json")
public Cat repeater(Cat cat) {
return cat;
}
}

0 comments on commit 1003c54

Please sign in to comment.