Skip to content

Commit

Permalink
RESTEASY-2099 (#1850)
Browse files Browse the repository at this point in the history
* RESTEASY-2099
Fix the 3 failed resteasy-link tests after replacing jettison with jackson

* fix done

* - split XML and JSON tests into two tests because now they are using separate JAXB and Jackson providers.
- Remove not-used code

* remove useless commented code

* clear spaces to pass checkstyle tests
  • Loading branch information
liweinan authored and asoldano committed Feb 1, 2019
1 parent 20b23eb commit 13d9648
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 77 deletions.
Expand Up @@ -10,6 +10,7 @@
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Collection;


public class JsonLinkDecorator implements DecoratorProcessor<DecoratedEntityContainer, AddLinks> { public class JsonLinkDecorator implements DecoratorProcessor<DecoratedEntityContainer, AddLinks> {


Expand All @@ -19,7 +20,15 @@ public DecoratedEntityContainer decorate(DecoratedEntityContainer target, AddLin
ResourceMethodRegistry registry = (ResourceMethodRegistry) ResteasyContext.getContextData(Registry.class); ResourceMethodRegistry registry = (ResourceMethodRegistry) ResteasyContext.getContextData(Registry.class);


// find all rest service classes and scan them // find all rest service classes and scan them
RESTUtils.addDiscovery(target.getEntity(), uriInfo, registry); if (Collection.class.isAssignableFrom(target.getEntity().getClass())) {
Collection coll = (Collection) target.getEntity();
for (Object entity : coll) {
RESTUtils.addDiscovery(entity, uriInfo, registry);
}
} else {
RESTUtils.addDiscovery(target.getEntity(), uriInfo, registry);
}

return target; return target;
} }
} }
Expand Up @@ -72,13 +72,13 @@ public void setComments(List<Comment> comments) {
this.comments = comments; this.comments = comments;
} }


public void addComment(int id, String text){ public void addComment(String id, String text){
comments.add(new Comment(id, text, this)); comments.add(new Comment(id, text, this));
} }


public Comment getComment(int commentId) { public Comment getComment(int commentId) {
for(Comment c : comments) for(Comment c : comments)
if(c.getId() == commentId) if(c.getId().equals(commentId))
return c; return c;
return null; return null;
} }
Expand Down
Expand Up @@ -33,8 +33,8 @@ public class BookStore {


{ {
Book book = new Book("foo", "bar"); Book book = new Book("foo", "bar");
book.addComment(0, "great book"); book.addComment(Integer.toString(0), "great book");
book.addComment(1, "terrible book"); book.addComment(Integer.toString(1), "terrible book");
books.put(book.getTitle(), book); books.put(book.getTitle(), book);
} }


Expand Down Expand Up @@ -160,5 +160,4 @@ public void deleteComment(@PathParam("id") String bookId, @PathParam("cid") int
Comment c = book.getComment(commentId); Comment c = book.getComment(commentId);
book.getComments().remove(c); book.getComments().remove(c);
} }

} }
Expand Up @@ -33,8 +33,9 @@ public class BookStoreMinimal {


{ {
Book book = new Book("foo", "bar"); Book book = new Book("foo", "bar");
book.addComment(0, "great book"); book.addComment(Integer.toString(0), "great book");
book.addComment(1, "terrible book"); book.addComment(Integer.toString(1), "terrible book");

books.put(book.getTitle(), book); books.put(book.getTitle(), book);
} }


Expand Down
Expand Up @@ -13,42 +13,48 @@
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.NONE) @XmlAccessorType(XmlAccessType.NONE)
public class Comment { public class Comment {
public int id;
@XmlAttribute
@XmlID
public String id;

@XmlElement @XmlElement
public String text; public String text;
@ParentResource @ParentResource
public Book book; public Book book;


@XmlElement @XmlElement
// These both fail deserialisation for some reason
// @XmlElement(name = "link", namespace = "http://www.w3.org/2005/Atom")
// @XmlElementRef
private RESTServiceDiscovery rest; private RESTServiceDiscovery rest;


public Comment() { public Comment() {
} }


public Comment(final int id, final String text, final Book book) { public Comment(final String id, final String text, final Book book) {
this.id = id; this.id = id;
this.text = text; this.text = text;
this.book = book; this.book = book;
} }


public int getId() { public String getId() {
return id; return id;
} }
public void setId(int id) {
public void setId(String id) {
this.id = id; this.id = id;
} }

public String getText() { public String getText() {
return text; return text;
} }

public void setText(String text) { public void setText(String text) {
this.text = text; this.text = text;
} }

public Book getBook() { public Book getBook() {
return book; return book;
} }

public void setBook(Book book) { public void setBook(Book book) {
this.book = book; this.book = book;
} }
Expand All @@ -60,11 +66,4 @@ public RESTServiceDiscovery getRest() {
public void setRest(RESTServiceDiscovery rest) { public void setRest(RESTServiceDiscovery rest) {
this.rest = rest; this.rest = rest;
} }

// JAXB wants an ID to be a String...
@XmlAttribute
@XmlID
public String getXMLID(){
return Integer.toString(id);
}
} }
Expand Up @@ -23,8 +23,8 @@ public class SecureBookStore {


{ {
Book book = new Book("foo", "bar"); Book book = new Book("foo", "bar");
book.addComment(0, "great book"); book.addComment(Integer.toString(0), "great book");
book.addComment(1, "terrible book"); book.addComment(Integer.toString(1), "terrible book");
books.put(book.getTitle(), book); books.put(book.getTitle(), book);
} }


Expand Down
Expand Up @@ -23,8 +23,8 @@ public class SecureBookStoreMinimal {


{ {
Book book = new Book("foo", "bar"); Book book = new Book("foo", "bar");
book.addComment(0, "great book"); book.addComment(Integer.toString(0), "great book");
book.addComment(1, "terrible book"); book.addComment(Integer.toString(1), "terrible book");
books.put(book.getTitle(), book); books.put(book.getTitle(), book);
} }


Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -89,12 +88,14 @@ public void after(){
} }


@Test @Test
@Ignore public void testLinksXML() throws Exception {
public void testLinks() throws Exception
{
ScrollableCollection comments = client.getScrollableCommentsXML("foo", "book"); ScrollableCollection comments = client.getScrollableCommentsXML("foo", "book");
checkCommentsLinks(url, comments); checkCommentsLinks(url, comments);
comments = client.getScrollableCommentsJSON("foo", "book"); }

@Test
public void testLinksJSON() throws Exception {
ScrollableCollection comments = client.getScrollableCommentsJSON("foo", "book");
checkCommentsLinks(url, comments); checkCommentsLinks(url, comments);
} }


Expand Down Expand Up @@ -124,36 +125,4 @@ private void checkCommentsLinks(String url, ScrollableCollection comments) {
Assert.assertNotNull(atomLink); Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/", atomLink.getHref()); Assert.assertEquals(url+"/", atomLink.getHref());
} }

private void checkCommentLinks(String url, Comment comment) {
Assert.assertNotNull(comment);
Assert.assertEquals(0, comment.getId());
RESTServiceDiscovery links = comment.getRest();
Assert.assertNotNull(links);
Assert.assertEquals(6, links.size());
// self
AtomLink atomLink = links.getLinkForRel("self");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comment/0", atomLink.getHref());
// update
atomLink = links.getLinkForRel("update");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comment/0", atomLink.getHref());
// remove
atomLink = links.getLinkForRel("remove");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comment/0", atomLink.getHref());
// list
atomLink = links.getLinkForRel("list");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comments", atomLink.getHref());
// add
atomLink = links.getLinkForRel("add");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comments", atomLink.getHref());
// collection
atomLink = links.getLinkForRel("collection");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comment-collection", atomLink.getHref());
}
} }
Expand Up @@ -24,11 +24,9 @@
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;


@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class TestLinks public class TestLinks
Expand All @@ -54,7 +52,7 @@ public static void afterClass() throws Exception
dispatcher = null; dispatcher = null;
} }


@Parameters @Parameterized.Parameters
public static List<Class<?>[]> getParameters(){ public static List<Class<?>[]> getParameters(){
return Arrays.asList(new Class<?>[]{BookStore.class}, new Class<?>[]{BookStoreMinimal.class}); return Arrays.asList(new Class<?>[]{BookStore.class}, new Class<?>[]{BookStoreMinimal.class});
} }
Expand Down Expand Up @@ -90,29 +88,29 @@ public void after(){


@Test @Test
public void testLinksXML() throws Exception { public void testLinksXML() throws Exception {

Book book = client.getBookXML("foo"); Book book = client.getBookXML("foo");
checkBookLinks1(url, book); checkBookLinks1(url, book);


} }


@Test @Test
public void testLinksJson() throws Exception { public void testLinksJson() throws Exception {

Book book = client.getBookJSON("foo"); Book book = client.getBookJSON("foo");
checkBookLinks1(url, book); checkBookLinks1(url, book);

} }


@Test @Test
@Ignore public void testCommentsXML() throws Exception
public void testComments() throws Exception
{ {
List<Comment> comments = client.getBookCommentsXML("foo"); List<Comment> comments = client.getBookCommentsXML("foo");
Assert.assertNotNull(comments); Assert.assertNotNull(comments);
Assert.assertFalse(comments.isEmpty()); Assert.assertFalse(comments.isEmpty());
checkCommentLinks(url, comments.get(0)); checkCommentLinks(url, comments.get(0));
comments = client.getBookCommentsJSON("foo"); }

@Test
public void testCommentsJson() throws Exception {
List<Comment> comments = client.getBookCommentsJSON("foo");
Assert.assertNotNull(comments); Assert.assertNotNull(comments);
Assert.assertFalse(comments.isEmpty()); Assert.assertFalse(comments.isEmpty());
checkCommentLinks(url, comments.get(0)); checkCommentLinks(url, comments.get(0));
Expand Down Expand Up @@ -157,7 +155,7 @@ private void checkBookLinks1(String url, Book book) {


private void checkCommentLinks(String url, Comment comment) { private void checkCommentLinks(String url, Comment comment) {
Assert.assertNotNull(comment); Assert.assertNotNull(comment);
Assert.assertEquals(0, comment.getId()); Assert.assertEquals(Integer.toString(0), comment.getId());
RESTServiceDiscovery links = comment.getRest(); RESTServiceDiscovery links = comment.getRest();
Assert.assertNotNull(links); Assert.assertNotNull(links);
Assert.assertEquals(6, links.size()); Assert.assertEquals(6, links.size());
Expand Down
Expand Up @@ -18,8 +18,8 @@ public class BookStoreInvalidEL {


{ {
Book book = new Book("foo", "bar"); Book book = new Book("foo", "bar");
book.addComment(0, "great book"); book.addComment(Integer.toString(0), "great book");
book.addComment(1, "terrible book"); book.addComment(Integer.toString(1), "terrible book");
books.put(book.getTitle(), book); books.put(book.getTitle(), book);
} }


Expand Down
Expand Up @@ -18,8 +18,8 @@ public class BookStoreNoPackage {


{ {
Book book = new Book("foo", "bar"); Book book = new Book("foo", "bar");
book.addComment(0, "great book"); book.addComment(Integer.toString(0), "great book");
book.addComment(1, "terrible book"); book.addComment(Integer.toString(1), "terrible book");
books.put(book.getTitle(), book); books.put(book.getTitle(), book);
} }


Expand Down

0 comments on commit 13d9648

Please sign in to comment.