webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Sep 2009 14:42:19 +0200
changeset 115 6a80463a74c0
child 117 c1057591a344
permissions -rw-r--r--
Support for comments among the moves
jaroslav@115
     1
/*
jaroslav@115
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@115
     3
 *
jaroslav@115
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@115
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@115
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@115
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@115
     8
 * License. You can obtain a copy of the License at
jaroslav@115
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@115
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@115
    11
 * specific language governing permissions and limitations under the
jaroslav@115
    12
 * License.  When distributing the software, include this License Header
jaroslav@115
    13
 * Notice in each file and include the License file at
jaroslav@115
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@115
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@115
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@115
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@115
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@115
    19
 * your own identifying information:
jaroslav@115
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@115
    21
 *
jaroslav@115
    22
 * Contributor(s):
jaroslav@115
    23
 *
jaroslav@115
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@115
    25
 */
jaroslav@115
    26
jaroslav@115
    27
package cz.xelfi.quoridor.webidor.resources;
jaroslav@115
    28
jaroslav@115
    29
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@115
    30
import cz.xelfi.quoridor.Board;
jaroslav@115
    31
import cz.xelfi.quoridor.Move;
jaroslav@115
    32
import cz.xelfi.quoridor.webidor.Game;
jaroslav@115
    33
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@115
    34
import cz.xelfi.quoridor.webidor.Note;
jaroslav@115
    35
import cz.xelfi.quoridor.webidor.QuoridorTest;
jaroslav@115
    36
import java.io.File;
jaroslav@115
    37
import java.io.FileNotFoundException;
jaroslav@115
    38
import java.io.FileOutputStream;
jaroslav@115
    39
import java.io.FileReader;
jaroslav@115
    40
import java.io.IOException;
jaroslav@115
    41
import java.util.List;
jaroslav@115
    42
import javax.ws.rs.core.MediaType;
jaroslav@115
    43
import org.junit.Test;
jaroslav@115
    44
import static org.junit.Assert.*;
jaroslav@115
    45
jaroslav@115
    46
/**
jaroslav@115
    47
 *
jaroslav@115
    48
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@115
    49
 */
jaroslav@115
    50
public class ChatTest extends JerseyTest {
jaroslav@115
    51
    private File dir;
jaroslav@115
    52
jaroslav@115
    53
    public ChatTest() throws Exception {
jaroslav@115
    54
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@115
    55
    }
jaroslav@115
    56
jaroslav@115
    57
    @Override
jaroslav@115
    58
    public void setUp() throws Exception {
jaroslav@115
    59
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@115
    60
        dir.delete();
jaroslav@115
    61
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@115
    62
        dir.mkdirs();
jaroslav@115
    63
        File passwd = new File(dir, "passwd");
jaroslav@115
    64
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@115
    65
        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
jaroslav@115
    66
        os.close();
jaroslav@115
    67
        super.setUp();
jaroslav@115
    68
    }
jaroslav@115
    69
jaroslav@115
    70
    @Override
jaroslav@115
    71
    public void tearDown() throws Exception {
jaroslav@115
    72
        super.tearDown();
jaroslav@115
    73
        deleteRec(dir);
jaroslav@115
    74
    }
jaroslav@115
    75
jaroslav@115
    76
    static void deleteRec(File dir) throws IOException {
jaroslav@115
    77
        if (dir == null) {
jaroslav@115
    78
            return;
jaroslav@115
    79
        }
jaroslav@115
    80
        File[] arr = dir.listFiles();
jaroslav@115
    81
        if (arr != null) {
jaroslav@115
    82
            for (File f : arr) {
jaroslav@115
    83
                deleteRec(f);
jaroslav@115
    84
            }
jaroslav@115
    85
        }
jaroslav@115
    86
        dir.delete();
jaroslav@115
    87
    }
jaroslav@115
    88
jaroslav@115
    89
    @Test public void testCreateAGame() throws Exception {
jaroslav@115
    90
        webResource = webResource.path("api");
jaroslav@115
    91
        String logJarda = webResource.path("login").
jaroslav@115
    92
            queryParam("name", "Jarda").
jaroslav@115
    93
            queryParam("password", "heslo").
jaroslav@115
    94
            accept(MediaType.TEXT_PLAIN).
jaroslav@115
    95
            put(String.class);
jaroslav@115
    96
        String logJirka = webResource.path("login").
jaroslav@115
    97
            queryParam("name", "Jirka").
jaroslav@115
    98
            queryParam("password", "pesko").
jaroslav@115
    99
            accept(MediaType.TEXT_PLAIN).
jaroslav@115
   100
            put(String.class);
jaroslav@115
   101
    GameId s = webResource.path("games").queryParam("loginID", logJarda).
jaroslav@115
   102
                queryParam("white", "Jarda")
jaroslav@115
   103
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@115
   104
jaroslav@115
   105
        Thread.sleep(100);
jaroslav@115
   106
        final long now = System.currentTimeMillis();
jaroslav@115
   107
        if (s.getModified() >= now) {
jaroslav@115
   108
            fail("The game is supposed to be modified in past");
jaroslav@115
   109
        }
jaroslav@115
   110
        Thread.sleep(100);
jaroslav@115
   111
jaroslav@115
   112
        GameId s1 = webResource.path("games/" + s.getId()).
jaroslav@115
   113
            queryParam("loginID", logJarda).
jaroslav@115
   114
            queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@115
   115
jaroslav@115
   116
        GameId comment1 = webResource.path("games/" + s.getId()).
jaroslav@115
   117
            queryParam("loginID", logJarda).
jaroslav@115
   118
            queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
jaroslav@115
   119
jaroslav@115
   120
        GameId comment2 = webResource.path("games/" + s.getId()).
jaroslav@115
   121
            queryParam("loginID", logJirka).
jaroslav@115
   122
            queryParam("player", "Jirka").queryParam("comment", "I love it too!").put(GameId.class);
jaroslav@115
   123
        GameId s2 = webResource.path("games/" + s.getId()).
jaroslav@115
   124
            queryParam("loginID", logJirka).
jaroslav@115
   125
            queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@115
   126
        assertNotNull("Successful move", s2);
jaroslav@115
   127
jaroslav@115
   128
        File game = new File(new File(dir, "games"), s1.getId());
jaroslav@115
   129
        assertTrue("File for game exists", game.exists());
jaroslav@115
   130
        String content = readFile(game);
jaroslav@115
   131
jaroslav@115
   132
        if (!content.contains("# white: Jarda")) {
jaroslav@115
   133
            fail(content);
jaroslav@115
   134
        }
jaroslav@115
   135
        if (!content.contains("# black: Jirka")) {
jaroslav@115
   136
            fail(content);
jaroslav@115
   137
        }
jaroslav@115
   138
        if (!content.contains("N")) {
jaroslav@115
   139
            fail(content);
jaroslav@115
   140
        }
jaroslav@115
   141
        if (!content.contains("... S")) {
jaroslav@115
   142
            fail(content);
jaroslav@115
   143
        }
jaroslav@115
   144
        if (!content.contains("I like")) {
jaroslav@115
   145
            fail(content);
jaroslav@115
   146
        }
jaroslav@115
   147
        if (!content.contains("I love")) {
jaroslav@115
   148
            fail(content);
jaroslav@115
   149
        }
jaroslav@115
   150
jaroslav@115
   151
        Games read = new Games(new File(dir, "games"), new Quoridor());
jaroslav@115
   152
        List<Game> readGames = read.getGames();
jaroslav@115
   153
        assertEquals("One game read", 1, readGames.size());
jaroslav@115
   154
        Board board = readGames.get(0).getBoard();
jaroslav@115
   155
        assertEquals(1, board.getPlayers().get(0).getRow());
jaroslav@115
   156
        assertEquals(7, board.getPlayers().get(1).getRow());
jaroslav@115
   157
        assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
jaroslav@115
   158
        assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
jaroslav@115
   159
jaroslav@115
   160
        Game rg = readGames.get(0);
jaroslav@115
   161
        assertNull("No comments on second move", rg.getMoves().get(1).getComments());
jaroslav@115
   162
        List<Note> cmnts = rg.getMoves().get(0).getComments();
jaroslav@115
   163
        assertNotNull("Some comments on first move", cmnts);
jaroslav@115
   164
        assertEquals("Two comments: " + cmnts, 2, cmnts.size());
jaroslav@115
   165
jaroslav@115
   166
        if (!cmnts.get(0).getComment().contains("I like")) {
jaroslav@115
   167
            fail();
jaroslav@115
   168
        }
jaroslav@115
   169
        if (!cmnts.get(1).getComment().contains("I love")) {
jaroslav@115
   170
            fail();
jaroslav@115
   171
        }
jaroslav@115
   172
jaroslav@115
   173
        File tmp = File.createTempFile("test-board", "game");
jaroslav@115
   174
        read.storeGame(rg, tmp);
jaroslav@115
   175
jaroslav@115
   176
        assertEquals("Newly written file remains the same", readFile(game), readFile(tmp));
jaroslav@115
   177
jaroslav@115
   178
        String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
jaroslav@115
   179
        Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@115
   180
        assertNotNull("Game really returned", readGame);
jaroslav@115
   181
jaroslav@115
   182
        assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
jaroslav@115
   183
        assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
jaroslav@115
   184
jaroslav@115
   185
        assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
jaroslav@115
   186
        cmnts = readGame.getMoves().get(0).getComments();
jaroslav@115
   187
        assertNotNull("Some comments on first move", cmnts);
jaroslav@115
   188
        assertEquals("Two comments: " + cmnts, 2, cmnts.size());
jaroslav@115
   189
        if (!cmnts.get(0).getComment().contains("I like")) {
jaroslav@115
   190
            fail();
jaroslav@115
   191
        }
jaroslav@115
   192
        if (!cmnts.get(1).getComment().contains("I love")) {
jaroslav@115
   193
            fail();
jaroslav@115
   194
        }
jaroslav@115
   195
    }
jaroslav@115
   196
jaroslav@115
   197
    private String readFile(File game) throws IOException, FileNotFoundException {
jaroslav@115
   198
        char[] arr = new char[4096];
jaroslav@115
   199
        FileReader gameContent = new FileReader(game);
jaroslav@115
   200
        int len = gameContent.read(arr);
jaroslav@115
   201
        String content = new String(arr, 0, len);
jaroslav@115
   202
        return content;
jaroslav@115
   203
    }
jaroslav@115
   204
jaroslav@115
   205
}