webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Oct 2009 14:55:51 +0100
changeset 135 232e42153945
parent 124 90371f3eb106
child 145 ac9bd9be5263
permissions -rw-r--r--
Showing the number of comments associated with each game
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import com.sun.jersey.api.client.GenericType;
    30 import com.sun.jersey.api.client.UniformInterfaceException;
    31 import com.sun.jersey.core.header.MediaTypes;
    32 import com.sun.jersey.test.framework.JerseyTest;
    33 import cz.xelfi.quoridor.Board;
    34 import cz.xelfi.quoridor.Move;
    35 import cz.xelfi.quoridor.webidor.resources.Games;
    36 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    37 import java.io.File;
    38 import java.io.FileOutputStream;
    39 import java.io.FileReader;
    40 import java.io.IOException;
    41 import java.util.List;
    42 import java.util.Map;
    43 import javax.ws.rs.core.MediaType;
    44 import org.junit.Test;
    45 import static org.junit.Assert.*;
    46 
    47 /**
    48  *
    49  * @author Jaroslav Tulach <jtulach@netbeans.org>
    50  */
    51 public class QuoridorTest extends JerseyTest {
    52     private File dir;
    53 
    54     public QuoridorTest() throws Exception {
    55         super("cz.xelfi.quoridor.webidor.resources");
    56     }
    57 
    58     @Override
    59     public void setUp() throws Exception {
    60         dir = File.createTempFile("quoridor", ".dir");
    61         dir.delete();
    62         System.setProperty("quoridor.dir", dir.getPath());
    63         dir.mkdirs();
    64         File passwd = new File(dir, "passwd");
    65         FileOutputStream os = new FileOutputStream(passwd);
    66         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    67         os.close();
    68         super.setUp();
    69     }
    70 
    71     @Override
    72     public void tearDown() throws Exception {
    73         super.tearDown();
    74         deleteRec(dir);
    75     }
    76 
    77     static void deleteRec(File dir) throws IOException {
    78         if (dir == null) {
    79             return;
    80         }
    81         File[] arr = dir.listFiles();
    82         if (arr != null) {
    83             for (File f : arr) {
    84                 deleteRec(f);
    85             }
    86         }
    87         dir.delete();
    88     }
    89 
    90     @Test public void testApplicationWadl() {
    91         String serviceWadl = webResource.path("application.wadl").
    92                 accept(MediaTypes.WADL).get(String.class);
    93         assertTrue(serviceWadl.length() > 0);
    94     }
    95 
    96     @Test public void testCreateAGame() throws Exception {
    97         String logJarda = webResource.path("login").
    98             queryParam("name", "Jarda").
    99             queryParam("password", "heslo").
   100             accept(MediaType.TEXT_PLAIN).
   101             put(String.class);
   102         String logJirka = webResource.path("login").
   103             queryParam("name", "Jirka").
   104             queryParam("password", "pesko").
   105             accept(MediaType.TEXT_PLAIN).
   106             put(String.class);
   107     GameId s = webResource.path("games").queryParam("loginID", logJarda).
   108                 queryParam("white", "Jarda")
   109                 .queryParam("black", "Jirka").post(GameId.class);
   110 
   111         Thread.sleep(100);
   112         final long now = System.currentTimeMillis();
   113         if (s.getModified() >= now) {
   114             fail("The game is supposed to be modified in past");
   115         }
   116         Thread.sleep(100);
   117 
   118         String msg = webResource.path("games").get(String.class);
   119         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   120 
   121         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   122 
   123         List<GameId> games = webResource.path("games").accept("application/json").get(gType);
   124         assertEquals("One game", 1, games.size());
   125         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   126         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   127 
   128         GameId s1 = webResource.path("games/" + s.getId()).
   129             queryParam("loginID", logJarda).
   130             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   131         try {
   132             GameId s2 = webResource.path("games/" + s.getId()).
   133                 queryParam("loginID", logJarda).
   134                 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   135             fail("Not Jarda's turn, previous call shall fail");
   136         } catch (UniformInterfaceException ex) {
   137             // OK
   138         }
   139         try {
   140             GameId s2 = webResource.path("games/" + s.getId()).
   141                 queryParam("loginID", logJirka).
   142                 queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
   143             fail("Invalid move");
   144         } catch (UniformInterfaceException ex) {
   145             // OK
   146         }
   147         GameId s2 = webResource.path("games/" + s.getId()).
   148             queryParam("loginID", logJirka).
   149             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   150         assertNotNull("Successful move", s2);
   151         if (s2.getModified() <= now) {
   152             fail("The game is newly modified");
   153         }
   154         Game snapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
   155         String ssnapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
   156         assertEquals("All moves listed:\n" + ssnapshot, 2, snapshot.getMoves().size());
   157         assertEquals("Current move", 0, snapshot.getCurrentMove());
   158         assertEquals("Position 0", 0, snapshot.getBoard().getPlayers().get(0).getRow());
   159         assertEquals("Position 8", 8, snapshot.getBoard().getPlayers().get(1).getRow());
   160         assertEquals("Moves numbered from 1", 1, snapshot.getMoves().get(0).getIndex());
   161         assertEquals("Moves numbered from 1, 2", 2, snapshot.getMoves().get(1).getIndex());
   162 
   163         File game = new File(new File(dir, "games"), s2.getId());
   164         assertTrue("File for game exists", game.exists());
   165 
   166         char[] arr = new char[4096];
   167         FileReader gameContent = new FileReader(game);
   168         int len = gameContent.read(arr);
   169         String content = new String(arr, 0, len);
   170 
   171         if (!content.contains("# white: Jarda")) {
   172             fail(content);
   173         }
   174         if (!content.contains("# black: Jirka")) {
   175             fail(content);
   176         }
   177         if (!content.contains("N S")) {
   178             fail(content);
   179         }
   180 
   181         Games read = new Games(new File(dir, "games"), new Quoridor());
   182         List<Game> readGames = read.getGames();
   183         assertEquals("One game read", 1, readGames.size());
   184         Board board = readGames.get(0).getBoard();
   185         assertEquals(1, board.getPlayers().get(0).getRow());
   186         assertEquals(7, board.getPlayers().get(1).getRow());
   187         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
   188         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   189 
   190         class GMap extends GenericType<Map<String,Object>>{}
   191         String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   192         if (text.indexOf("-----") == -1) {
   193             fail("Expecting board:\n" + text);
   194         }
   195         Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   196         String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   197         assertNotNull("Game really returned", readGame);
   198         assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
   199         assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
   200 
   201         assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
   202         assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
   203     }
   204 
   205 }