Can list just games with certain status
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 18 Oct 2009 21:33:27 +0200
changeset 128eba04a2569d0
parent 127 ac8fff40babc
child 129 fd95f7873b96
Can list just games with certain status
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Oct 18 20:55:21 2009 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Oct 18 21:33:27 2009 +0200
     1.3 @@ -34,8 +34,6 @@
     1.4  import java.io.File;
     1.5  import java.io.FileInputStream;
     1.6  import java.io.FileOutputStream;
     1.7 -import java.io.FileReader;
     1.8 -import java.io.FileWriter;
     1.9  import java.io.IOException;
    1.10  import java.io.InputStream;
    1.11  import java.io.InputStreamReader;
    1.12 @@ -181,10 +179,14 @@
    1.13  
    1.14      @GET
    1.15      @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.16 -    public List<GameId> listGames() {
    1.17 +    public List<GameId> listGames(
    1.18 +        @DefaultValue("") @QueryParam("status") String status
    1.19 +    ) {
    1.20          List<GameId> arr = new ArrayList<GameId>(games.size());
    1.21          for (Game g : games) {
    1.22 -            arr.add(g.getId());
    1.23 +            if (status.length() == 0 || g.getId().getStatus().toString().equals(status)) {
    1.24 +                arr.add(g.getId());
    1.25 +            }
    1.26          }
    1.27          Collections.sort(arr, GameId.NEWEST_FIRST);
    1.28          return arr;
     2.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Sun Oct 18 20:55:21 2009 +0200
     2.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Sun Oct 18 21:33:27 2009 +0200
     2.3 @@ -26,10 +26,12 @@
     2.4  
     2.5  package cz.xelfi.quoridor.webidor;
     2.6  
     2.7 +import com.sun.jersey.api.client.GenericType;
     2.8  import com.sun.jersey.test.framework.JerseyTest;
     2.9  import java.io.File;
    2.10  import java.io.FileOutputStream;
    2.11  import java.io.IOException;
    2.12 +import java.util.List;
    2.13  import javax.ws.rs.core.MediaType;
    2.14  import org.junit.Test;
    2.15  import static org.junit.Assert.*;
    2.16 @@ -115,6 +117,11 @@
    2.17              .queryParam("loginID", logJirka)
    2.18              .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
    2.19  
    2.20 +        
    2.21 +        GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
    2.22 +        List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
    2.23 +        assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
    2.24 +
    2.25          for (int i = 0; i < 3; i++) {
    2.26              webResource.path("games/" + s.getId())
    2.27                  .queryParam("loginID", logJarda)
    2.28 @@ -124,8 +131,14 @@
    2.29                  .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    2.30          }
    2.31  
    2.32 +
    2.33          Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    2.34          assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
    2.35 +
    2.36 +        List<GameId> something = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
    2.37 +
    2.38 +        assertEquals("One game finished: " + something, 1, something.size());
    2.39 +        assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
    2.40      }
    2.41  
    2.42      @Test public void testResignAGame() throws Exception {