webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 25 Apr 2010 21:20:09 +0200
changeset 238 a4f6aca595e8
parent 178 4b78d4f028b3
child 258 935118a5831a
permissions -rw-r--r--
Support for permission.resign to allow the emailer cancel long running games
     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 java.util.Properties;
    30 import com.sun.jersey.api.client.GenericType;
    31 import com.sun.jersey.api.client.UniformInterfaceException;
    32 import com.sun.jersey.test.framework.JerseyTest;
    33 import java.io.File;
    34 import java.io.FileOutputStream;
    35 import java.io.IOException;
    36 import java.util.List;
    37 import javax.ws.rs.core.MediaType;
    38 import org.junit.Test;
    39 import static org.junit.Assert.*;
    40 
    41 /**
    42  *
    43  * @author Jaroslav Tulach <jtulach@netbeans.org>
    44  */
    45 public class FinishedGameTest extends JerseyTest {
    46     static {
    47         System.setProperty("JERSEY_HTTP_PORT", "33435");
    48     }
    49     private File dir;
    50 
    51     public FinishedGameTest() throws Exception {
    52         super("cz.xelfi.quoridor.webidor.resources");
    53     }
    54 
    55     @Override
    56     public void setUp() throws Exception {
    57         dir = File.createTempFile("quoridor", ".dir");
    58         dir.delete();
    59         System.setProperty("quoridor.dir", dir.getPath());
    60         dir.mkdirs();
    61         File passwd = new File(dir, "passwd");
    62         FileOutputStream os = new FileOutputStream(passwd);
    63         os.write("Jarda=heslo\nJirka=pesko\nMaster=mr\n".getBytes("UTF-8"));
    64         os.close();
    65         super.setUp();
    66     }
    67 
    68     @Override
    69     public void tearDown() throws Exception {
    70         super.tearDown();
    71         deleteRec(dir);
    72     }
    73 
    74     static void deleteRec(File dir) throws IOException {
    75         if (dir == null) {
    76             return;
    77         }
    78         File[] arr = dir.listFiles();
    79         if (arr != null) {
    80             for (File f : arr) {
    81                 deleteRec(f);
    82             }
    83         }
    84         dir.delete();
    85     }
    86     @Test public void testNotLoggedIn() {
    87         String status  = webResource.path("login").queryParam("id", "not-logged-in").
    88                 accept(MediaType.TEXT_PLAIN).get(String.class);
    89         assertEquals("Nobody is logged in", "", status);
    90     }
    91 
    92 
    93     @Test public void testCreateAGame() throws Exception {
    94         String logJarda = webResource.path("login").
    95             queryParam("name", "Jarda").
    96             queryParam("password", "heslo").
    97             accept(MediaType.TEXT_PLAIN).
    98             put(String.class);
    99         String logJirka = webResource.path("login").
   100             queryParam("name", "Jirka").
   101             queryParam("password", "pesko").
   102             accept(MediaType.TEXT_PLAIN).
   103             put(String.class);
   104         assertNotNull("Logged in ok", logJarda);
   105         GameId s = webResource.path("games").queryParam("white", "Jarda")
   106                 .queryParam("loginID", logJarda)
   107                 .queryParam("black", "Jirka").post(GameId.class);
   108 
   109         for (int i = 0; i < 3; i++) {
   110             webResource.path("games/" + s.getId())
   111                 .queryParam("loginID", logJarda)
   112                 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   113             webResource.path("games/" + s.getId())
   114                 .queryParam("loginID", logJirka)
   115                 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   116         }
   117 
   118         webResource.path("games/" + s.getId())
   119             .queryParam("loginID", logJarda)
   120             .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   121         webResource.path("games/" + s.getId())
   122             .queryParam("loginID", logJirka)
   123             .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
   124 
   125         
   126         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   127         List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   128         assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
   129 
   130         for (int i = 0; i < 3; i++) {
   131             webResource.path("games/" + s.getId())
   132                 .queryParam("loginID", logJarda)
   133                 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   134             webResource.path("games/" + s.getId())
   135                 .queryParam("loginID", logJirka)
   136                 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   137         }
   138 
   139 
   140         try {
   141             Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   142             fail("If the game is finished, one cannot get its status without login");
   143         } catch (UniformInterfaceException ex) {
   144             // OK
   145         }
   146         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
   147         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   148 
   149         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   150 
   151         List<GameId> none = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   152         assertEquals("No games, for not logged in users: " + none, 0, none.size());
   153 
   154         List<GameId> something = webResource.path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
   155         assertEquals("One game finished: " + something, 1, something.size());
   156         assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
   157     }
   158 
   159     @Test public void testResignAGame() throws Exception {
   160         String logJarda = webResource.path("login").
   161             queryParam("name", "Jarda").
   162             queryParam("password", "heslo").
   163             accept(MediaType.TEXT_PLAIN).
   164             put(String.class);
   165         GameId s = webResource.path("games").queryParam("white", "Jarda")
   166                 .queryParam("loginID", logJarda)
   167                 .queryParam("black", "Jirka").post(GameId.class);
   168 
   169         assertTrue("In progress", s.getStatus().isInProgress());
   170 
   171         webResource.path("games/" + s.getId()).
   172             queryParam("loginID", logJarda).
   173             queryParam("player", "Jarda").
   174             queryParam("move", "RESIGN").put(GameId.class);
   175         try {
   176             Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   177             fail("Should not be able to get game when finished");
   178         } catch (UniformInterfaceException ex) {
   179             // OK
   180         }
   181         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   182         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   183         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   184 
   185         assertFalse("is finished", end.getId().getStatus().isInProgress());
   186     }
   187 
   188     @Test public void testResignBGame() throws Exception {
   189         String logJarda = webResource.path("login").
   190             queryParam("name", "Jarda").
   191             queryParam("password", "heslo").
   192             accept(MediaType.TEXT_PLAIN).
   193             put(String.class);
   194         String logJirka = webResource.path("login").
   195             queryParam("name", "Jirka").
   196             queryParam("password", "pesko").
   197             accept(MediaType.TEXT_PLAIN).
   198             put(String.class);
   199 
   200         GameId s = webResource.path("games").queryParam("white", "Jarda")
   201                 .queryParam("loginID", logJarda)
   202                 .queryParam("black", "Jirka").post(GameId.class);
   203 
   204         assertTrue("In progress", s.getStatus().isInProgress());
   205 
   206         webResource.path("games/" + s.getId()).
   207             queryParam("loginID", logJarda).
   208             queryParam("player", "Jarda").
   209             queryParam("move", "N").put(GameId.class);
   210         webResource.path("games/" + s.getId()).
   211             queryParam("loginID", logJirka).
   212             queryParam("player", "Jirka").
   213             queryParam("move", "RESIGN").put(GameId.class);
   214 
   215 
   216         try {
   217             Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   218             fail("Should not be able to get game when finished");
   219         } catch (UniformInterfaceException ex) {
   220             // OK
   221         }
   222         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   223         assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
   224         assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
   225 
   226         assertFalse("is finished", end.getId().getStatus().isInProgress());
   227     }
   228 
   229     @Test public void testResignForeignGame() throws Exception {
   230         String logJarda = webResource.path("login").
   231             queryParam("name", "Jarda").
   232             queryParam("password", "heslo").
   233             accept(MediaType.TEXT_PLAIN).
   234             put(String.class);
   235         GameId s = webResource.path("games").queryParam("white", "Jarda")
   236                 .queryParam("loginID", logJarda)
   237                 .queryParam("black", "Jirka").post(GameId.class);
   238         File usersDir = new File(dir, "users");
   239         usersDir.mkdirs();
   240         File Master = new File(usersDir, "Master");
   241         {
   242             Properties p = new Properties();
   243             p.setProperty("email", "mas@ter.cz");
   244             p.setProperty("permission.resign", "true");
   245             p.store(new FileOutputStream(Master), "");
   246         }
   247 
   248         assertTrue("In progress", s.getStatus().isInProgress());
   249         String logMaster = webResource.path("login").
   250                 queryParam("name", "Master").
   251                 queryParam("password", "mr").
   252                 accept(MediaType.TEXT_PLAIN).
   253                 put(String.class);
   254 
   255         webResource.path("games/" + s.getId()).
   256             queryParam("loginID", logMaster).
   257             queryParam("player", "Jarda").
   258             queryParam("move", "RESIGN").put(GameId.class);
   259         try {
   260             Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   261             fail("Should not be able to get game when finished");
   262         } catch (UniformInterfaceException ex) {
   263             // OK
   264         }
   265         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
   266         assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
   267         assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
   268 
   269         assertFalse("is finished", end.getId().getStatus().isInProgress());
   270     }
   271 
   272 }