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