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