Skip to content

Commit

Permalink
RESTEASY-1321: Add unit test for matrix parameters in LinkResource
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrashed committed Apr 2, 2016
1 parent 9c4998d commit bf9927a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
@@ -1,5 +1,6 @@
package org.jboss.resteasy.links.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand All @@ -9,6 +10,7 @@
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -99,16 +101,21 @@ public Collection<Comment> getComments(@PathParam("id") String bookId){
@LinkResource(value = ScrollableCollection.class, rel = "next", constraint = "${this.start + this.limit < this.totalRecords}", queryParameters = {
@ParamBinding(name = "start", value = "${this.start + this.limit}"),
@ParamBinding(name = "limit", value = "${this.limit}")
})
}, matrixParameters = {@ParamBinding(name = "query", value = "${this.query}")})
})
@GET
@Path("book/{id}/comment-collection")
public ScrollableCollection getScrollableComments(@PathParam("id") String id, @QueryParam("start") int start, @QueryParam("limit") @DefaultValue("1") int limit){
List<Comment> comments = books.get(id).getComments();
public ScrollableCollection getScrollableComments(@PathParam("id") String id, @QueryParam("start") int start, @QueryParam("limit") @DefaultValue("1") int limit, @MatrixParam("query") String query){
List<Comment> comments = new ArrayList<Comment>();
for (Comment comment : books.get(id).getComments()) {
if (comment.getText().contains(query)) {
comments.add(comment);
}
}
start = start < 0 ? 0 : start;
limit = limit < 1 ? 1 : limit;
limit = (start + limit) > comments.size() ? comments.size() - start : limit;
return new ScrollableCollection(id, start, limit, comments.size(), comments.subList(start, start + limit));
return new ScrollableCollection(id, start, limit, comments.size(), comments.subList(start, start + limit), query);
}

@Produces({"application/xml", "application/json"})
Expand Down
Expand Up @@ -9,13 +9,15 @@
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -99,16 +101,21 @@ public Collection<Comment> getComments(@PathParam("id") String bookId){
@LinkResource(value = ScrollableCollection.class, rel = "next", constraint = "${this.start + this.limit < this.totalRecords}", queryParameters = {
@ParamBinding(name = "start", value = "${this.start + this.limit}"),
@ParamBinding(name = "limit", value = "${this.limit}")
})
}, matrixParameters = {@ParamBinding(name = "query", value = "${this.query}")})
})
@GET
@Path("book/{id}/comment-collection")
public ScrollableCollection getScrollableComments(@PathParam("id") String id, @QueryParam("start") int start, @QueryParam("limit") @DefaultValue("1") int limit){
List<Comment> comments = books.get(id).getComments();
public ScrollableCollection getScrollableComments(@PathParam("id") String id, @QueryParam("start") int start, @QueryParam("limit") @DefaultValue("1") int limit, @MatrixParam("query") String query){
List<Comment> comments = new ArrayList<Comment>();
for (Comment comment : books.get(id).getComments()) {
if (comment.getText().contains(query)) {
comments.add(comment);
}
}
start = start < 0 ? 0 : start;
limit = limit < 1 ? 1 : limit;
limit = (start + limit) > comments.size() ? comments.size() - start : limit;
return new ScrollableCollection(id, start, limit, comments.size(), comments.subList(start, start + limit));
return new ScrollableCollection(id, start, limit, comments.size(), comments.subList(start, start + limit), query);
}

@Produces({"application/xml", "application/json"})
Expand Down
@@ -1,6 +1,7 @@
package org.jboss.resteasy.links.test;

import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
Expand Down Expand Up @@ -32,11 +33,11 @@ public interface BookStoreService {
@Produces({"application/xml"})
@GET
@Path("book/{id}/comment-collection")
public ScrollableCollection getScrollableCommentsXML(@PathParam("id") String id);
public ScrollableCollection getScrollableCommentsXML(@PathParam("id") String id, @MatrixParam("query") String query);

@Produces({"application/json"})
@GET
@Path("book/{id}/comment-collection")
public ScrollableCollection getScrollableCommentsJSON(@PathParam("id") String id);
public ScrollableCollection getScrollableCommentsJSON(@PathParam("id") String id, @MatrixParam("query") String query);

}
Expand Up @@ -9,6 +9,8 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -25,6 +27,8 @@ public class ScrollableCollection implements ResourceFacade<Comment> {
private int limit;
@XmlAttribute
private int totalRecords;
@XmlTransient
private String query;
@XmlElement
private List<Comment> comments = new ArrayList<Comment>();
@XmlElement
Expand All @@ -33,12 +37,13 @@ public class ScrollableCollection implements ResourceFacade<Comment> {
public ScrollableCollection() {}

public ScrollableCollection(String id, int start, int limit, int totalRecords,
List<Comment> comments) {
List<Comment> comments, String query) {
this.id = id;
this.start = start;
this.limit = limit;
this.totalRecords = totalRecords;
this.comments.addAll(comments);
this.setQuery(query);
}

public Class<Comment> facadeFor() {
Expand Down Expand Up @@ -91,4 +96,12 @@ public void setComments(List<Comment> comments) {
this.comments = comments;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

}
Expand Up @@ -79,9 +79,9 @@ public void after(){
@Test
public void testLinks() throws Exception
{
ScrollableCollection comments = client.getScrollableCommentsXML("foo");
ScrollableCollection comments = client.getScrollableCommentsXML("foo", "book");
checkCommentsLinks(url, comments);
comments = client.getScrollableCommentsJSON("foo");
comments = client.getScrollableCommentsJSON("foo", "book");
checkCommentsLinks(url, comments);
}

Expand All @@ -105,7 +105,7 @@ private void checkCommentsLinks(String url, ScrollableCollection comments) {
// next
atomLink = links.getLinkForRel("next");
Assert.assertNotNull(atomLink);
Assert.assertEquals(url+"/book/foo/comment-collection?start=1&limit=1", atomLink.getHref());
Assert.assertEquals(url+"/book/foo/comment-collection;query=book?start=1&limit=1", atomLink.getHref());
}

private void checkCommentLinks(String url, Comment comment) {
Expand Down

0 comments on commit bf9927a

Please sign in to comment.