webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 11 Sep 2009 22:25:21 +0200
changeset 78 5ea4172dcf8b
parent 54 f041b6570ff9
child 82 9ac7acee7d9f
permissions -rw-r--r--
Showing the time that has passed since the last move on the board
     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 java.io.File;
    37 import java.io.FileReader;
    38 import java.io.IOException;
    39 import java.util.List;
    40 import java.util.Map;
    41 import javax.ws.rs.core.MediaType;
    42 import org.junit.Test;
    43 import org.w3c.dom.Document;
    44 import static org.junit.Assert.*;
    45 
    46 /**
    47  *
    48  * @author Jaroslav Tulach <jtulach@netbeans.org>
    49  */
    50 public class QuoridorTest extends JerseyTest {
    51     private File dir;
    52 
    53     public QuoridorTest() throws Exception {
    54         super("cz.xelfi.quoridor.webidor.resources");
    55     }
    56 
    57     @Override
    58     public void setUp() throws Exception {
    59         dir = File.createTempFile("quoridor", ".dir");
    60         dir.delete();
    61         System.setProperty("quoridor.dir", dir.getPath());
    62         super.setUp();
    63     }
    64 
    65     @Override
    66     public void tearDown() throws Exception {
    67         super.tearDown();
    68         deleteRec(dir);
    69     }
    70 
    71     static void deleteRec(File dir) throws IOException {
    72         if (dir == null) {
    73             return;
    74         }
    75         File[] arr = dir.listFiles();
    76         if (arr != null) {
    77             for (File f : arr) {
    78                 deleteRec(f);
    79             }
    80         }
    81         dir.delete();
    82     }
    83 
    84     /**
    85      * Test if a WADL document is available at the relative path
    86      * "application.wadl".
    87      */
    88     @Test public void testApplicationWadl() {
    89         String serviceWadl = webResource.path("application.wadl").
    90                 accept(MediaTypes.WADL).get(String.class);
    91         assertTrue(serviceWadl.length() > 0);
    92     }
    93 
    94     @Test public void testCreateAGame() throws Exception {
    95         webResource = webResource.path("api");
    96         GameId s = webResource.path("games").queryParam("white", "Jarda")
    97                 .queryParam("black", "Jirka").post(GameId.class);
    98 
    99         Thread.sleep(100);
   100         final long now = System.currentTimeMillis();
   101         if (s.getModified() >= now) {
   102             fail("The game is supposed to be modified in past");
   103         }
   104         Thread.sleep(100);
   105 
   106         String msg = webResource.path("games").get(String.class);
   107         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   108 
   109         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   110 
   111         Document doc = webResource.path("games").accept("text/xml").get(Document.class);
   112         assertNotNull("Can read games in form of XML", doc);
   113         assertEquals("Name is correct", "gameIds", doc.getDocumentElement().getNodeName());
   114         assertEquals("One child", 1, doc.getDocumentElement().getChildNodes().getLength());
   115         List<GameId> games = webResource.path("games").accept("application/json").get(gType);
   116         assertEquals("One game", 1, games.size());
   117         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   118         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   119 
   120         GameId s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   121         try {
   122             GameId s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   123             fail("Not Jarda's turn, previous call shall fail");
   124         } catch (UniformInterfaceException ex) {
   125             // OK
   126         }
   127         try {
   128             GameId s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
   129             fail("Invalid move");
   130         } catch (UniformInterfaceException ex) {
   131             // OK
   132         }
   133         GameId s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   134         assertNotNull("Successful move", s2);
   135         if (s2.getModified() <= now) {
   136             fail("The game is newly modified");
   137         }
   138 
   139         File game = new File(new File(dir, "games"), s2.getId());
   140         assertTrue("File for game exists", game.exists());
   141 
   142         char[] arr = new char[4096];
   143         FileReader gameContent = new FileReader(game);
   144         int len = gameContent.read(arr);
   145         String content = new String(arr, 0, len);
   146 
   147         if (!content.contains("# white: Jarda")) {
   148             fail(content);
   149         }
   150         if (!content.contains("# black: Jirka")) {
   151             fail(content);
   152         }
   153         if (!content.contains("N S")) {
   154             fail(content);
   155         }
   156 
   157         Games read = new Games(new File(dir, "games"));
   158         List<Game> readGames = read.getGames();
   159         assertEquals("One game read", 1, readGames.size());
   160         Board board = readGames.get(0).getBoard();
   161         assertEquals(1, board.getPlayers().get(0).getRow());
   162         assertEquals(7, board.getPlayers().get(1).getRow());
   163         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0));
   164         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1));
   165 
   166         class GMap extends GenericType<Map<String,Object>>{}
   167         String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   168         if (text.indexOf("-----") == -1) {
   169             fail("Expecting board:\n" + text);
   170         }
   171         Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   172         assertNotNull("Game really returned", readGame);
   173         assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
   174         assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
   175 
   176         assertEquals(Move.NORTH, readGame.getMoves().get(0));
   177         assertEquals(Move.SOUTH, readGame.getMoves().get(1));
   178     }
   179 
   180 }