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