webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Dec 2009 07:59:16 +0100
changeset 171 524c7f359c4e
child 258 935118a5831a
permissions -rw-r--r--
Adding support for 'permission.games' to allow special roles to enlist all the available 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 com.sun.jersey.api.client.GenericType;
    30 import java.util.List;
    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.Properties;
    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 AllGamesTest extends JerseyTest {
    46     static {
    47         System.setProperty("JERSEY_HTTP_PORT", "45420");
    48     }
    49 
    50     private File dir;
    51 
    52     public AllGamesTest() throws Exception {
    53         super("cz.xelfi.quoridor.webidor.resources");
    54     }
    55 
    56     @Override
    57     public void setUp() throws Exception {
    58         dir = File.createTempFile("quoridor", ".dir");
    59         dir.delete();
    60         System.setProperty("quoridor.dir", dir.getPath());
    61         dir.mkdirs();
    62         File passwd = new File(dir, "passwd");
    63         FileOutputStream os = new FileOutputStream(passwd);
    64         os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
    65         os.close();
    66         super.setUp();
    67     }
    68 
    69     @Override
    70     public void tearDown() throws Exception {
    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 testListGames() throws Exception {
    87         File usersDir = new File(dir, "users");
    88         usersDir.mkdirs();
    89         File fJarda = new File(usersDir, "Jarda");
    90         {
    91             Properties p = new Properties();
    92             p.setProperty("email", "jar@da.cz");
    93             p.setProperty("permission.games", "true");
    94             p.store(new FileOutputStream(fJarda), "");
    95         }
    96         File fJirka = new File(usersDir, "Jirka");
    97         {
    98             Properties p = new Properties();
    99             p.setProperty("email", "jir@ka.cz");
   100             p.store(new FileOutputStream(fJirka), "");
   101         }
   102         File fJan = new File(usersDir, "Jan");
   103         {
   104             Properties p = new Properties();
   105             p.setProperty("email", "j@an.cz");
   106             p.store(new FileOutputStream(fJan), "");
   107         }
   108 
   109         String logRoot = webResource.path("login").
   110             queryParam("name", "Jarda").
   111             queryParam("password", "heslo").
   112             accept(MediaType.TEXT_PLAIN).
   113             put(String.class);
   114         String logJirka = webResource.path("login").
   115             queryParam("name", "Jirka").
   116             queryParam("password", "pesko").
   117             accept(MediaType.TEXT_PLAIN).
   118             put(String.class);
   119         String logJan = webResource.path("login").
   120             queryParam("name", "Jan").
   121             queryParam("password", "pan").
   122             accept(MediaType.TEXT_PLAIN).
   123             put(String.class);
   124 
   125         GameId s = webResource.path("games").queryParam("white", "Jan")
   126                 .queryParam("loginID", logJan)
   127                 .queryParam("black", "Jirka").post(GameId.class);
   128             webResource.path("games/" + s.getId())
   129                 .queryParam("loginID", logJan)
   130                 .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
   131 
   132         GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   133         List<GameId> all = webResource.path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
   134         boolean found = false;
   135         for (GameId id : all) {
   136             if (id.getId().equals(s.getId())) {
   137                 found = true;
   138                 break;
   139             }
   140         }
   141         assertTrue("List of games shall contai all games: " + all, found);
   142 
   143         Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
   144         assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
   145     }
   146 }