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
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import com.sun.jersey.api.client.GenericType;
    30 import com.sun.jersey.api.client.UniformInterfaceException;
    31 import com.sun.jersey.core.header.MediaTypes;
    32 import com.sun.jersey.test.framework.JerseyTest;
    33 import cz.xelfi.quoridor.Board;
    34 import cz.xelfi.quoridor.Move;
    35 import cz.xelfi.quoridor.webidor.resources.Games;
    36 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    37 import java.io.File;
    38 import java.io.FileReader;
    39 import java.io.IOException;
    40 import java.util.List;
    41 import java.util.Map;
    42 import javax.ws.rs.core.MediaType;
    43 import org.codehaus.jettison.json.JSONObject;
    44 import org.junit.Test;
    45 import static org.junit.Assert.*;
    46 
    47 /**
    48  *
    49  * @author Jaroslav Tulach <jtulach@netbeans.org>
    50  */
    51 public class QuoridorTest extends JerseyTest {
    52     private File dir;
    53 
    54     public QuoridorTest() throws Exception {
    55         super("cz.xelfi.quoridor.webidor.resources");
    56     }
    57 
    58     @Override
    59     public void setUp() throws Exception {
    60         dir = File.createTempFile("quoridor", ".dir");
    61         dir.delete();
    62         System.setProperty("quoridor.dir", dir.getPath());
    63         super.setUp();
    64     }
    65 
    66     @Override
    67     public void tearDown() throws Exception {
    68         super.tearDown();
    69         deleteRec(dir);
    70     }
    71 
    72     static void deleteRec(File dir) throws IOException {
    73         if (dir == null) {
    74             return;
    75         }
    76         File[] arr = dir.listFiles();
    77         if (arr != null) {
    78             for (File f : arr) {
    79                 deleteRec(f);
    80             }
    81         }
    82         dir.delete();
    83     }
    84 
    85     /**
    86      * Test if a WADL document is available at the relative path
    87      * "application.wadl".
    88      */
    89     @Test public void testApplicationWadl() {
    90         String serviceWadl = webResource.path("application.wadl").
    91                 accept(MediaTypes.WADL).get(String.class);
    92         assertTrue(serviceWadl.length() > 0);
    93     }
    94 
    95     @Test public void testCreateAGame() throws Exception {
    96         webResource = webResource.path("api");
    97         Game s = webResource.path("games").queryParam("white", "Jarda")
    98                 .queryParam("black", "Jirka").post(Game.class);
    99 
   100         String msg = webResource.path("games").get(String.class);
   101         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   102 
   103         GenericType<List<Game>> gType = new GenericType<List<Game>>() {};
   104 
   105         List<Game> games = webResource.path("games").accept("application/json").get(gType);
   106         assertEquals("One game", 1, games.size());
   107         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   108         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   109 
   110         Game s1 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   111         try {
   112             Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(Game.class);
   113             fail("Not Jarda's turn, previous call shall fail");
   114         } catch (UniformInterfaceException ex) {
   115             // OK
   116         }
   117         try {
   118             Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(Game.class);
   119             fail("Invalid move");
   120         } catch (UniformInterfaceException ex) {
   121             // OK
   122         }
   123         Game s2 = webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(Game.class);
   124         assertNotNull("Successful move", s2);
   125 
   126         File game = new File(new File(dir, "games"), s2.getId());
   127         assertTrue("File for game exists", game.exists());
   128 
   129         char[] arr = new char[4096];
   130         FileReader gameContent = new FileReader(game);
   131         int len = gameContent.read(arr);
   132         String content = new String(arr, 0, len);
   133 
   134         if (!content.contains("# white: Jarda")) {
   135             fail(content);
   136         }
   137         if (!content.contains("# black: Jirka")) {
   138             fail(content);
   139         }
   140         if (!content.contains("N S")) {
   141             fail(content);
   142         }
   143 
   144         Games read = new Games(new File(dir, "games"));
   145         List<Game> readGames = read.getGames();
   146         assertEquals("One game read", 1, readGames.size());
   147         Board board = read.getGames().get(0).getBoard();
   148         assertEquals(1, board.getPlayers().get(0).getRow());
   149         assertEquals(7, board.getPlayers().get(1).getRow());
   150         assertEquals(Move.NORTH, read.getGames().get(0).getMoves().get(0));
   151         assertEquals(Move.SOUTH, read.getGames().get(0).getMoves().get(1));
   152 
   153         class GMap extends GenericType<Map<String,Object>>{}
   154         JSONObject map = webResource.path("games").path(s.getId()).accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
   155         assertNotNull("Map really returned", map);
   156         String txtBoard = (String) map.get("board");
   157         assertNotNull("Contains its textual form", txtBoard);
   158         assertEquals("It is same as text of our game", board.toString(), txtBoard);
   159 
   160         Object og = map.getJSONObject("game");
   161         assertTrue("Instance of JSON: " + og, og instanceof JSONObject);
   162         JSONObject jg = (JSONObject)og;
   163     }
   164 
   165 }