webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 20 Aug 2011 17:14:23 +0200
branchglassfish
changeset 285 bc4ddef89763
parent 264 d60370059c3c
permissions -rw-r--r--
Adjusting tests to use different path
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.webidor;
    19 
    20 import com.sun.jersey.test.framework.AppDescriptor;
    21 import cz.xelfi.quoridor.IllegalPositionException;
    22 import java.io.StringWriter;
    23 import com.sun.jersey.api.client.GenericType;
    24 import com.sun.jersey.api.client.UniformInterfaceException;
    25 import com.sun.jersey.core.header.MediaTypes;
    26 import com.sun.jersey.test.framework.JerseyTest;
    27 import com.sun.jersey.test.framework.WebAppDescriptor;
    28 import cz.xelfi.quoridor.Board;
    29 import cz.xelfi.quoridor.Move;
    30 import cz.xelfi.quoridor.webidor.resources.Games;
    31 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    32 import java.io.File;
    33 import java.io.FileOutputStream;
    34 import java.io.FileReader;
    35 import java.io.IOException;
    36 import java.io.StringReader;
    37 import java.util.List;
    38 import java.util.Map;
    39 import javax.ws.rs.core.MediaType;
    40 import org.junit.Test;
    41 import static org.junit.Assert.*;
    42 
    43 /**
    44  *
    45  * @author Jaroslav Tulach <jtulach@netbeans.org>
    46  */
    47 public class QuoridorTest extends JerseyTest {
    48     static {
    49         System.setProperty("JERSEY_HTTP_PORT", "33434");
    50     }
    51     private File dir;
    52 
    53     @Override
    54     protected AppDescriptor configure() {
    55         try {
    56             dir = File.createTempFile("quoridor", ".dir");
    57             dir.delete();
    58             System.setProperty("quoridor.dir", dir.getPath());
    59             dir.mkdirs();
    60             File passwd = new File(dir, "passwd");
    61             FileOutputStream os = new FileOutputStream(passwd);
    62             os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    63             os.close();
    64         } catch (Exception ex) {
    65             throw new IllegalStateException(ex);
    66         }
    67         return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("quoTest").build();
    68     }
    69 
    70 
    71 
    72     @Override
    73     public void tearDown() throws Exception {
    74         super.tearDown();
    75         deleteRec(dir);
    76     }
    77 
    78     static void deleteRec(File dir) throws IOException {
    79         if (dir == null) {
    80             return;
    81         }
    82         File[] arr = dir.listFiles();
    83         if (arr != null) {
    84             for (File f : arr) {
    85                 deleteRec(f);
    86             }
    87         }
    88         dir.delete();
    89     }
    90 
    91     @Test public void testApplicationWadl() {
    92         String serviceWadl = resource().path("application.wadl").
    93                 accept(MediaTypes.WADL).get(String.class);
    94         assertTrue(serviceWadl.length() > 0);
    95     }
    96 
    97     @Test public void testCreateAGame() throws Exception {
    98         String logJarda = resource().path("login").
    99             queryParam("name", "Jarda").
   100             queryParam("password", "heslo").
   101             accept(MediaType.TEXT_PLAIN).
   102             put(String.class);
   103         String logJirka = resource().path("login").
   104             queryParam("name", "Jirka").
   105             queryParam("password", "pesko").
   106             accept(MediaType.TEXT_PLAIN).
   107             put(String.class);
   108 
   109         User uJirka = resource().path("users").
   110             queryParam("loginID", logJirka).
   111             accept(MediaType.TEXT_XML).
   112             get(User.class);
   113 
   114         assertEquals("Jirka", uJirka.getId());
   115 
   116 
   117     GameId s = resource().path("games").queryParam("loginID", logJarda).
   118                 queryParam("white", "Jarda")
   119                 .queryParam("black", "Jirka").post(GameId.class);
   120 
   121         Thread.sleep(100);
   122         final long now = System.currentTimeMillis();
   123         if (s.getModified() >= now) {
   124             fail("The game is supposed to be modified in past");
   125         }
   126         Thread.sleep(100);
   127 
   128         String msg = resource().path("games").get(String.class);
   129         //List<Game> games =  resource().path("games").get(new GenericType<List<Game>>() {});
   130 
   131         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   132 
   133         List<GameId> games = resource().path("games").accept("application/json").get(gType);
   134         assertEquals("One game", 1, games.size());
   135         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   136         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   137 
   138         GameId s1 = resource().path("games/" + s.getId()).
   139             queryParam("loginID", logJarda).
   140             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   141         try {
   142             GameId s2 = resource().path("games/" + s.getId()).
   143                 queryParam("loginID", logJarda).
   144                 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   145             fail("Not Jarda's turn, previous call shall fail");
   146         } catch (UniformInterfaceException ex) {
   147             // OK
   148         }
   149         try {
   150             GameId s2 = resource().path("games/" + s.getId()).
   151                 queryParam("loginID", logJirka).
   152                 queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
   153             fail("Invalid move");
   154         } catch (UniformInterfaceException ex) {
   155             // OK
   156         }
   157         GameId s2 = resource().path("games/" + s.getId()).
   158             queryParam("loginID", logJirka).
   159             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   160         assertNotNull("Successful move", s2);
   161         if (s2.getModified() <= now) {
   162             fail("The game is newly modified");
   163         }
   164         Game snapshot = resource().path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
   165         String ssnapshot = resource().path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
   166         assertEquals("All moves listed:\n" + ssnapshot, 2, snapshot.getMoves().size());
   167         assertEquals("Current move", 0, snapshot.getCurrentMove());
   168         assertEquals("Position 0", 0, snapshot.getBoard().getPlayers().get(0).getRow());
   169         assertEquals("Position 8", 8, snapshot.getBoard().getPlayers().get(1).getRow());
   170         assertEquals("Moves numbered from 1", 1, snapshot.getMoves().get(0).getIndex());
   171         assertEquals("Moves numbered from 1, 2", 2, snapshot.getMoves().get(1).getIndex());
   172 
   173         File game = new File(new File(dir, "games"), s2.getId());
   174         assertTrue("File for game exists", game.exists());
   175 
   176         char[] arr = new char[4096];
   177         FileReader gameContent = new FileReader(game);
   178         int len = gameContent.read(arr);
   179         String content = new String(arr, 0, len);
   180 
   181         if (!content.contains("# white: Jarda")) {
   182             fail(content);
   183         }
   184         if (!content.contains("# black: Jirka")) {
   185             fail(content);
   186         }
   187         if (!content.contains("N S")) {
   188             fail(content);
   189         }
   190 
   191         Games read = new Games(new File(dir, "games"), new Quoridor());
   192         List<Game> readGames = read.getGames();
   193         assertEquals("One game read", 1, readGames.size());
   194         Board board = readGames.get(0).getBoard();
   195         assertEquals(1, board.getPlayers().get(0).getRow());
   196         assertEquals(7, board.getPlayers().get(1).getRow());
   197         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
   198         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   199 
   200         class GMap extends GenericType<Map<String,Object>>{}
   201         String text = resource().path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   202         text = (boardToPicture(Board.valueOf(text)));
   203         if (text.indexOf("-----") == -1) {
   204             fail("Expecting board:\n" + text);
   205         }
   206         Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   207         String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   208         assertNotNull("Game really returned", readGame);
   209 //        assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
   210         assertEquals("Same game as in text representation", readGame.getBoard(), picture2board(text));
   211 //        assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
   212         assertEquals("It is same as text of our game", boardToPicture(readGame.getBoard()), text);
   213 
   214         assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
   215         assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
   216     }
   217     private static String boardToPicture(Board b) {
   218         StringWriter w = new StringWriter();
   219         try {
   220             b.write(w);
   221         } catch (IOException ex) {
   222             return ex.toString();
   223         }
   224         return w.toString();
   225     }
   226 
   227     private static Board picture2board(String text) throws IOException, IllegalPositionException {
   228         StringReader sr = new StringReader(text);
   229         return Board.read(sr);
   230     }
   231 }