webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 08 Dec 2009 19:57:02 +0100
branchstrict-games-access
changeset 166 8c9131715765
parent 162 c1bfbe98152b
child 178 4b78d4f028b3
permissions -rw-r--r--
Also the list of games needs to be restricted for not-logged in users
     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.test.framework.JerseyTest;
    32 import java.io.File;
    33 import java.io.FileOutputStream;
    34 import java.io.IOException;
    35 import java.util.List;
    36 import javax.ws.rs.core.MediaType;
    37 import org.junit.Test;
    38 import static org.junit.Assert.*;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 public class FinishedGameTest extends JerseyTest {
    45     static {
    46         System.setProperty("JERSEY_HTTP_PORT", "33435");
    47     }
    48     private File dir;
    49 
    50     public FinishedGameTest() throws Exception {
    51         super("cz.xelfi.quoridor.webidor.resources");
    52     }
    53 
    54     @Override
    55     public void setUp() throws Exception {
    56         dir = File.createTempFile("quoridor", ".dir");
    57         dir.delete();
    58         System.setProperty("quoridor.dir", dir.getPath());
    59         dir.mkdirs();
    60         File passwd = new File(dir, "passwd");
    61         FileOutputStream os = new FileOutputStream(passwd);
    62         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    63         os.close();
    64         super.setUp();
    65     }
    66 
    67     @Override
    68     public void tearDown() throws Exception {
    69         super.tearDown();
    70         deleteRec(dir);
    71     }
    72 
    73     static void deleteRec(File dir) throws IOException {
    74         if (dir == null) {
    75             return;
    76         }
    77         File[] arr = dir.listFiles();
    78         if (arr != null) {
    79             for (File f : arr) {
    80                 deleteRec(f);
    81             }
    82         }
    83         dir.delete();
    84     }
    85     @Test public void testNotLoggedIn() {
    86         String status  = webResource.path("login").queryParam("id", "not-logged-in").
    87                 accept(MediaType.TEXT_PLAIN).get(String.class);
    88         assertEquals("Nobody is logged in", "", status);
    89     }
    90 
    91 
    92     @Test public void testCreateAGame() throws Exception {
    93         String logJarda = webResource.path("login").
    94             queryParam("name", "Jarda").
    95             queryParam("password", "heslo").
    96             accept(MediaType.TEXT_PLAIN).
    97             put(String.class);
    98         String logJirka = webResource.path("login").
    99             queryParam("name", "Jirka").
   100             queryParam("password", "pesko").
   101             accept(MediaType.TEXT_PLAIN).
   102             put(String.class);
   103         assertNotNull("Logged in ok", logJarda);
   104         GameId s = webResource.path("games").queryParam("white", "Jarda")
   105                 .queryParam("loginID", logJarda)
   106                 .queryParam("black", "Jirka").post(GameId.class);
   107 
   108         for (int i = 0; i < 3; i++) {
   109             webResource.path("games/" + s.getId())
   110                 .queryParam("loginID", logJarda)
   111                 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   112             webResource.path("games/" + s.getId())
   113                 .queryParam("loginID", logJirka)
   114                 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   115         }
   116 
   117         webResource.path("games/" + s.getId())
   118             .queryParam("loginID", logJarda)
   119             .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   120         webResource.path("games/" + s.getId())
   121             .queryParam("loginID", logJirka)
   122             .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
   123 
   124         
   125         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   126         List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   127         assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
   128 
   129         for (int i = 0; i < 3; i++) {
   130             webResource.path("games/" + s.getId())
   131                 .queryParam("loginID", logJarda)
   132                 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   133             webResource.path("games/" + s.getId())
   134                 .queryParam("loginID", logJirka)
   135                 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   136         }
   137 
   138 
   139         try {
   140             Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   141             fail("If the game is finished, one cannot get its status without login");
   142         } catch (UniformInterfaceException ex) {
   143             // OK
   144         }
   145         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
   146         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   147 
   148         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   149 
   150         List<GameId> none = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   151         assertEquals("No games, for not logged in users: " + none, 0, none.size());
   152 
   153         List<GameId> something = webResource.path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   154         assertEquals("One game finished: " + something, 1, something.size());
   155         assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
   156     }
   157 
   158     @Test public void testResignAGame() throws Exception {
   159         String logJarda = webResource.path("login").
   160             queryParam("name", "Jarda").
   161             queryParam("password", "heslo").
   162             accept(MediaType.TEXT_PLAIN).
   163             put(String.class);
   164         GameId s = webResource.path("games").queryParam("white", "Jarda")
   165                 .queryParam("loginID", logJarda)
   166                 .queryParam("black", "Jirka").post(GameId.class);
   167 
   168         assertTrue("In progress", s.getStatus().isInProgress());
   169 
   170         webResource.path("games/" + s.getId()).
   171             queryParam("loginID", logJarda).
   172             queryParam("player", "Jarda").
   173             queryParam("move", "RESIGN").put(GameId.class);
   174         try {
   175             Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   176             fail("Should not be able to get game when finished");
   177         } catch (UniformInterfaceException ex) {
   178             // OK
   179         }
   180         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   181         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   182         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   183 
   184         assertFalse("is finished", end.getId().getStatus().isInProgress());
   185     }
   186 
   187 }