webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:51:17 +0100
branchstatistics-and-elo
changeset 179 c5fbddc4c590
parent 178 4b78d4f028b3
child 258 935118a5831a
permissions -rw-r--r--
Renaming Board.board2HashCode to getCode()
     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 cz.xelfi.quoridor.IllegalPositionException;
    30 import java.io.StringWriter;
    31 import com.sun.jersey.api.client.GenericType;
    32 import com.sun.jersey.api.client.UniformInterfaceException;
    33 import com.sun.jersey.core.header.MediaTypes;
    34 import com.sun.jersey.test.framework.JerseyTest;
    35 import cz.xelfi.quoridor.Board;
    36 import cz.xelfi.quoridor.Move;
    37 import cz.xelfi.quoridor.webidor.resources.Games;
    38 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    39 import java.io.File;
    40 import java.io.FileOutputStream;
    41 import java.io.FileReader;
    42 import java.io.IOException;
    43 import java.io.StringReader;
    44 import java.util.List;
    45 import java.util.Map;
    46 import javax.ws.rs.core.MediaType;
    47 import org.junit.Test;
    48 import static org.junit.Assert.*;
    49 
    50 /**
    51  *
    52  * @author Jaroslav Tulach <jtulach@netbeans.org>
    53  */
    54 public class QuoridorTest extends JerseyTest {
    55     static {
    56         System.setProperty("JERSEY_HTTP_PORT", "33434");
    57     }
    58     private File dir;
    59 
    60     public QuoridorTest() throws Exception {
    61         super("cz.xelfi.quoridor.webidor.resources");
    62     }
    63 
    64     @Override
    65     public void setUp() throws Exception {
    66         dir = File.createTempFile("quoridor", ".dir");
    67         dir.delete();
    68         System.setProperty("quoridor.dir", dir.getPath());
    69         dir.mkdirs();
    70         File passwd = new File(dir, "passwd");
    71         FileOutputStream os = new FileOutputStream(passwd);
    72         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    73         os.close();
    74         super.setUp();
    75     }
    76 
    77     @Override
    78     public void tearDown() throws Exception {
    79         super.tearDown();
    80         deleteRec(dir);
    81     }
    82 
    83     static void deleteRec(File dir) throws IOException {
    84         if (dir == null) {
    85             return;
    86         }
    87         File[] arr = dir.listFiles();
    88         if (arr != null) {
    89             for (File f : arr) {
    90                 deleteRec(f);
    91             }
    92         }
    93         dir.delete();
    94     }
    95 
    96     @Test public void testApplicationWadl() {
    97         String serviceWadl = webResource.path("application.wadl").
    98                 accept(MediaTypes.WADL).get(String.class);
    99         assertTrue(serviceWadl.length() > 0);
   100     }
   101 
   102     @Test public void testCreateAGame() throws Exception {
   103         String logJarda = webResource.path("login").
   104             queryParam("name", "Jarda").
   105             queryParam("password", "heslo").
   106             accept(MediaType.TEXT_PLAIN).
   107             put(String.class);
   108         String logJirka = webResource.path("login").
   109             queryParam("name", "Jirka").
   110             queryParam("password", "pesko").
   111             accept(MediaType.TEXT_PLAIN).
   112             put(String.class);
   113 
   114         User uJirka = webResource.path("users").
   115             queryParam("loginID", logJirka).
   116             accept(MediaType.TEXT_XML).
   117             get(User.class);
   118 
   119         assertEquals("Jirka", uJirka.getId());
   120 
   121 
   122     GameId s = webResource.path("games").queryParam("loginID", logJarda).
   123                 queryParam("white", "Jarda")
   124                 .queryParam("black", "Jirka").post(GameId.class);
   125 
   126         Thread.sleep(100);
   127         final long now = System.currentTimeMillis();
   128         if (s.getModified() >= now) {
   129             fail("The game is supposed to be modified in past");
   130         }
   131         Thread.sleep(100);
   132 
   133         String msg = webResource.path("games").get(String.class);
   134         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   135 
   136         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   137 
   138         List<GameId> games = webResource.path("games").accept("application/json").get(gType);
   139         assertEquals("One game", 1, games.size());
   140         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   141         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   142 
   143         GameId s1 = webResource.path("games/" + s.getId()).
   144             queryParam("loginID", logJarda).
   145             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   146         try {
   147             GameId s2 = webResource.path("games/" + s.getId()).
   148                 queryParam("loginID", logJarda).
   149                 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   150             fail("Not Jarda's turn, previous call shall fail");
   151         } catch (UniformInterfaceException ex) {
   152             // OK
   153         }
   154         try {
   155             GameId s2 = webResource.path("games/" + s.getId()).
   156                 queryParam("loginID", logJirka).
   157                 queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
   158             fail("Invalid move");
   159         } catch (UniformInterfaceException ex) {
   160             // OK
   161         }
   162         GameId s2 = webResource.path("games/" + s.getId()).
   163             queryParam("loginID", logJirka).
   164             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   165         assertNotNull("Successful move", s2);
   166         if (s2.getModified() <= now) {
   167             fail("The game is newly modified");
   168         }
   169         Game snapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
   170         String ssnapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
   171         assertEquals("All moves listed:\n" + ssnapshot, 2, snapshot.getMoves().size());
   172         assertEquals("Current move", 0, snapshot.getCurrentMove());
   173         assertEquals("Position 0", 0, snapshot.getBoard().getPlayers().get(0).getRow());
   174         assertEquals("Position 8", 8, snapshot.getBoard().getPlayers().get(1).getRow());
   175         assertEquals("Moves numbered from 1", 1, snapshot.getMoves().get(0).getIndex());
   176         assertEquals("Moves numbered from 1, 2", 2, snapshot.getMoves().get(1).getIndex());
   177 
   178         File game = new File(new File(dir, "games"), s2.getId());
   179         assertTrue("File for game exists", game.exists());
   180 
   181         char[] arr = new char[4096];
   182         FileReader gameContent = new FileReader(game);
   183         int len = gameContent.read(arr);
   184         String content = new String(arr, 0, len);
   185 
   186         if (!content.contains("# white: Jarda")) {
   187             fail(content);
   188         }
   189         if (!content.contains("# black: Jirka")) {
   190             fail(content);
   191         }
   192         if (!content.contains("N S")) {
   193             fail(content);
   194         }
   195 
   196         Games read = new Games(new File(dir, "games"), new Quoridor());
   197         List<Game> readGames = read.getGames();
   198         assertEquals("One game read", 1, readGames.size());
   199         Board board = readGames.get(0).getBoard();
   200         assertEquals(1, board.getPlayers().get(0).getRow());
   201         assertEquals(7, board.getPlayers().get(1).getRow());
   202         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
   203         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   204 
   205         class GMap extends GenericType<Map<String,Object>>{}
   206         String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   207         text = (boardToPicture(Board.valueOf(text)));
   208         if (text.indexOf("-----") == -1) {
   209             fail("Expecting board:\n" + text);
   210         }
   211         Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   212         String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   213         assertNotNull("Game really returned", readGame);
   214 //        assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
   215         assertEquals("Same game as in text representation", readGame.getBoard(), picture2board(text));
   216 //        assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
   217         assertEquals("It is same as text of our game", boardToPicture(readGame.getBoard()), text);
   218 
   219         assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
   220         assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
   221     }
   222     private static String boardToPicture(Board b) {
   223         StringWriter w = new StringWriter();
   224         try {
   225             b.write(w);
   226         } catch (IOException ex) {
   227             return ex.toString();
   228         }
   229         return w.toString();
   230     }
   231 
   232     private static Board picture2board(String text) throws IOException, IllegalPositionException {
   233         StringReader sr = new StringReader(text);
   234         return Board.read(sr);
   235     }
   236 }