webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Aug 2009 15:55:53 +0200
changeset 46 71e4cf307c93
parent 41 c94f68ddef59
child 48 69e897fe8140
permissions -rw-r--r--
Support for logging in
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
jtulach@35
    29
import com.sun.jersey.api.client.GenericType;
jtulach@35
    30
import com.sun.jersey.api.client.UniformInterfaceException;
jtulach@35
    31
import com.sun.jersey.core.header.MediaTypes;
jtulach@35
    32
import com.sun.jersey.test.framework.JerseyTest;
jtulach@39
    33
import cz.xelfi.quoridor.Board;
jtulach@39
    34
import cz.xelfi.quoridor.Move;
jtulach@39
    35
import cz.xelfi.quoridor.webidor.resources.Games;
jtulach@39
    36
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jtulach@37
    37
import java.io.File;
jtulach@37
    38
import java.io.FileReader;
jtulach@37
    39
import java.io.IOException;
jtulach@35
    40
import java.util.List;
jaroslav@46
    41
import java.util.Map;
jaroslav@46
    42
import javax.ws.rs.core.MediaType;
jaroslav@46
    43
import org.codehaus.jettison.json.JSONObject;
jtulach@35
    44
import org.junit.Test;
jtulach@35
    45
import static org.junit.Assert.*;
jtulach@35
    46
jtulach@35
    47
/**
jtulach@35
    48
 *
jtulach@35
    49
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    50
 */
jtulach@35
    51
public class QuoridorTest extends JerseyTest {
jtulach@37
    52
    private File dir;
jtulach@35
    53
jtulach@35
    54
    public QuoridorTest() throws Exception {
jtulach@35
    55
        super("cz.xelfi.quoridor.webidor.resources");
jtulach@35
    56
    }
jtulach@35
    57
jtulach@37
    58
    @Override
jtulach@37
    59
    public void setUp() throws Exception {
jtulach@37
    60
        dir = File.createTempFile("quoridor", ".dir");
jtulach@37
    61
        dir.delete();
jtulach@37
    62
        System.setProperty("quoridor.dir", dir.getPath());
jtulach@37
    63
        super.setUp();
jtulach@37
    64
    }
jtulach@37
    65
jtulach@37
    66
    @Override
jtulach@37
    67
    public void tearDown() throws Exception {
jtulach@37
    68
        super.tearDown();
jtulach@37
    69
        deleteRec(dir);
jtulach@37
    70
    }
jtulach@37
    71
jtulach@37
    72
    static void deleteRec(File dir) throws IOException {
jtulach@37
    73
        if (dir == null) {
jtulach@37
    74
            return;
jtulach@37
    75
        }
jtulach@37
    76
        File[] arr = dir.listFiles();
jtulach@37
    77
        if (arr != null) {
jtulach@37
    78
            for (File f : arr) {
jtulach@37
    79
                deleteRec(f);
jtulach@37
    80
            }
jtulach@37
    81
        }
jtulach@37
    82
        dir.delete();
jtulach@37
    83
    }
jtulach@37
    84
jtulach@35
    85
    /**
jtulach@35
    86
     * Test if a WADL document is available at the relative path
jtulach@35
    87
     * "application.wadl".
jtulach@35
    88
     */
jtulach@35
    89
    @Test public void testApplicationWadl() {
jtulach@35
    90
        String serviceWadl = webResource.path("application.wadl").
jtulach@35
    91
                accept(MediaTypes.WADL).get(String.class);
jtulach@35
    92
        assertTrue(serviceWadl.length() > 0);
jtulach@35
    93
    }
jtulach@35
    94
jtulach@37
    95
    @Test public void testCreateAGame() throws Exception {
jtulach@41
    96
        webResource = webResource.path("api");
jtulach@35
    97
        Game s = webResource.path("games").queryParam("white", "Jarda")
jtulach@35
    98
                .queryParam("black", "Jirka").post(Game.class);
jtulach@35
    99
jtulach@35
   100
        String msg = webResource.path("games").get(String.class);
jtulach@35
   101
        //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
jtulach@35
   102
jtulach@35
   103
        GenericType<List<Game>> gType = new GenericType<List<Game>>() {};
jtulach@35
   104
jtulach@35
   105
        List<Game> games = webResource.path("games").accept("application/json").get(gType);
jtulach@35
   106
        assertEquals("One game", 1, games.size());
jtulach@35
   107
        assertEquals("Same white", "Jarda", games.get(0).getWhite());
jtulach@35
   108
        assertEquals("Same black", "Jirka", games.get(0).getBlack());
jtulach@35
   109
jtulach@35
   110
        Game s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
jtulach@35
   111
        try {
jtulach@35
   112
            Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
jtulach@35
   113
            fail("Not Jarda's turn, previous call shall fail");
jtulach@35
   114
        } catch (UniformInterfaceException ex) {
jtulach@35
   115
            // OK
jtulach@35
   116
        }
jtulach@35
   117
        try {
jtulach@35
   118
            Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(Game.class);
jtulach@35
   119
            fail("Invalid move");
jtulach@35
   120
        } catch (UniformInterfaceException ex) {
jtulach@35
   121
            // OK
jtulach@35
   122
        }
jtulach@35
   123
        Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(Game.class);
jtulach@35
   124
        assertNotNull("Successful move", s2);
jtulach@37
   125
jtulach@37
   126
        File game = new File(new File(dir, "games"), s2.getId());
jtulach@37
   127
        assertTrue("File for game exists", game.exists());
jtulach@37
   128
jtulach@37
   129
        char[] arr = new char[4096];
jtulach@37
   130
        FileReader gameContent = new FileReader(game);
jtulach@37
   131
        int len = gameContent.read(arr);
jtulach@37
   132
        String content = new String(arr, 0, len);
jtulach@37
   133
jtulach@37
   134
        if (!content.contains("# white: Jarda")) {
jtulach@37
   135
            fail(content);
jtulach@37
   136
        }
jtulach@37
   137
        if (!content.contains("# black: Jirka")) {
jtulach@37
   138
            fail(content);
jtulach@37
   139
        }
jtulach@37
   140
        if (!content.contains("N S")) {
jtulach@37
   141
            fail(content);
jtulach@37
   142
        }
jtulach@39
   143
jtulach@39
   144
        Games read = new Games(new File(dir, "games"));
jtulach@39
   145
        List<Game> readGames = read.getGames();
jtulach@39
   146
        assertEquals("One game read", 1, readGames.size());
jtulach@39
   147
        Board board = read.getGames().get(0).getBoard();
jtulach@39
   148
        assertEquals(1, board.getPlayers().get(0).getRow());
jtulach@39
   149
        assertEquals(7, board.getPlayers().get(1).getRow());
jtulach@39
   150
        assertEquals(Move.NORTH, read.getGames().get(0).getMoves().get(0));
jtulach@39
   151
        assertEquals(Move.SOUTH, read.getGames().get(0).getMoves().get(1));
jaroslav@46
   152
jaroslav@46
   153
        class GMap extends GenericType<Map<String,Object>>{}
jaroslav@46
   154
        JSONObject map = webResource.path("games").path(s.getId()).accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
jaroslav@46
   155
        assertNotNull("Map really returned", map);
jaroslav@46
   156
        String txtBoard = (String) map.get("board");
jaroslav@46
   157
        assertNotNull("Contains its textual form", txtBoard);
jaroslav@46
   158
        assertEquals("It is same as text of our game", board.toString(), txtBoard);
jaroslav@46
   159
jaroslav@46
   160
        Object og = map.getJSONObject("game");
jaroslav@46
   161
        assertTrue("Instance of JSON: " + og, og instanceof JSONObject);
jaroslav@46
   162
        JSONObject jg = (JSONObject)og;
jtulach@35
   163
    }
jtulach@35
   164
jtulach@35
   165
}