webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 13 Sep 2009 16:48:54 +0200
changeset 82 9ac7acee7d9f
parent 78 5ea4172dcf8b
child 100 8b899ed24f9f
permissions -rw-r--r--
Providing REST like authentication
     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.FileOutputStream;
    39 import java.io.FileReader;
    40 import java.io.IOException;
    41 import java.util.List;
    42 import java.util.Map;
    43 import javax.ws.rs.core.MediaType;
    44 import org.junit.Test;
    45 import org.w3c.dom.Document;
    46 import static org.junit.Assert.*;
    47 
    48 /**
    49  *
    50  * @author Jaroslav Tulach <jtulach@netbeans.org>
    51  */
    52 public class QuoridorTest extends JerseyTest {
    53     private File dir;
    54 
    55     public QuoridorTest() throws Exception {
    56         super("cz.xelfi.quoridor.webidor.resources");
    57     }
    58 
    59     @Override
    60     public void setUp() throws Exception {
    61         dir = File.createTempFile("quoridor", ".dir");
    62         dir.delete();
    63         System.setProperty("quoridor.dir", dir.getPath());
    64         dir.mkdirs();
    65         File passwd = new File(dir, "passwd");
    66         FileOutputStream os = new FileOutputStream(passwd);
    67         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    68         os.close();
    69         super.setUp();
    70     }
    71 
    72     @Override
    73     public void tearDown() throws Exception {
    74         super.tearDown();
    75         deleteRec(dir);
    76     }
    77 
    78     static void deleteRec(File dir) throws IOException {
    79         if (dir == null) {
    80             return;
    81         }
    82         File[] arr = dir.listFiles();
    83         if (arr != null) {
    84             for (File f : arr) {
    85                 deleteRec(f);
    86             }
    87         }
    88         dir.delete();
    89     }
    90 
    91     /**
    92      * Test if a WADL document is available at the relative path
    93      * "application.wadl".
    94      */
    95     @Test public void testApplicationWadl() {
    96         String serviceWadl = webResource.path("application.wadl").
    97                 accept(MediaTypes.WADL).get(String.class);
    98         assertTrue(serviceWadl.length() > 0);
    99     }
   100 
   101     @Test public void testCreateAGame() throws Exception {
   102         webResource = webResource.path("api");
   103         String logJarda = webResource.path("login").
   104             queryParam("name", "Jarda").
   105             queryParam("password", "heslo").
   106             accept(MediaType.TEXT_PLAIN).
   107             put(String.class);
   108         String logJirka = webResource.path("login").
   109             queryParam("name", "Jirka").
   110             queryParam("password", "pesko").
   111             accept(MediaType.TEXT_PLAIN).
   112             put(String.class);
   113     GameId s = webResource.path("games").queryParam("loginID", logJarda).
   114                 queryParam("white", "Jarda")
   115                 .queryParam("black", "Jirka").post(GameId.class);
   116 
   117         Thread.sleep(100);
   118         final long now = System.currentTimeMillis();
   119         if (s.getModified() >= now) {
   120             fail("The game is supposed to be modified in past");
   121         }
   122         Thread.sleep(100);
   123 
   124         String msg = webResource.path("games").get(String.class);
   125         //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   126 
   127         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   128 
   129         Document doc = webResource.path("games").accept("text/xml").get(Document.class);
   130         assertNotNull("Can read games in form of XML", doc);
   131         assertEquals("Name is correct", "gameIds", doc.getDocumentElement().getNodeName());
   132         assertEquals("One child", 1, doc.getDocumentElement().getChildNodes().getLength());
   133         List<GameId> games = webResource.path("games").accept("application/json").get(gType);
   134         assertEquals("One game", 1, games.size());
   135         assertEquals("Same white", "Jarda", games.get(0).getWhite());
   136         assertEquals("Same black", "Jirka", games.get(0).getBlack());
   137 
   138         GameId s1 = webResource.path("games/" + s.getId()).
   139             queryParam("loginID", logJarda).
   140             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   141         try {
   142             GameId s2 = webResource.path("games/" + s.getId()).
   143                 queryParam("loginID", logJarda).
   144                 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   145             fail("Not Jarda's turn, previous call shall fail");
   146         } catch (UniformInterfaceException ex) {
   147             // OK
   148         }
   149         try {
   150             GameId s2 = webResource.path("games/" + s.getId()).
   151                 queryParam("loginID", logJirka).
   152                 queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
   153             fail("Invalid move");
   154         } catch (UniformInterfaceException ex) {
   155             // OK
   156         }
   157         GameId s2 = webResource.path("games/" + s.getId()).
   158             queryParam("loginID", logJirka).
   159             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   160         assertNotNull("Successful move", s2);
   161         if (s2.getModified() <= now) {
   162             fail("The game is newly modified");
   163         }
   164 
   165         File game = new File(new File(dir, "games"), s2.getId());
   166         assertTrue("File for game exists", game.exists());
   167 
   168         char[] arr = new char[4096];
   169         FileReader gameContent = new FileReader(game);
   170         int len = gameContent.read(arr);
   171         String content = new String(arr, 0, len);
   172 
   173         if (!content.contains("# white: Jarda")) {
   174             fail(content);
   175         }
   176         if (!content.contains("# black: Jirka")) {
   177             fail(content);
   178         }
   179         if (!content.contains("N S")) {
   180             fail(content);
   181         }
   182 
   183         Games read = new Games(new File(dir, "games"), new Quoridor());
   184         List<Game> readGames = read.getGames();
   185         assertEquals("One game read", 1, readGames.size());
   186         Board board = readGames.get(0).getBoard();
   187         assertEquals(1, board.getPlayers().get(0).getRow());
   188         assertEquals(7, board.getPlayers().get(1).getRow());
   189         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0));
   190         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1));
   191 
   192         class GMap extends GenericType<Map<String,Object>>{}
   193         String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   194         if (text.indexOf("-----") == -1) {
   195             fail("Expecting board:\n" + text);
   196         }
   197         Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   198         assertNotNull("Game really returned", readGame);
   199         assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
   200         assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
   201 
   202         assertEquals(Move.NORTH, readGame.getMoves().get(0));
   203         assertEquals(Move.SOUTH, readGame.getMoves().get(1));
   204     }
   205 
   206 }