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