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
jaroslav@171
     1
/*
jaroslav@171
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@171
     3
 *
jaroslav@171
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@171
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@171
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@171
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@171
     8
 * License. You can obtain a copy of the License at
jaroslav@171
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@171
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@171
    11
 * specific language governing permissions and limitations under the
jaroslav@171
    12
 * License.  When distributing the software, include this License Header
jaroslav@171
    13
 * Notice in each file and include the License file at
jaroslav@171
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@171
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@171
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@171
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@171
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@171
    19
 * your own identifying information:
jaroslav@171
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@171
    21
 *
jaroslav@171
    22
 * Contributor(s):
jaroslav@171
    23
 *
jaroslav@171
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@171
    25
 */
jaroslav@171
    26
jaroslav@171
    27
package cz.xelfi.quoridor.webidor;
jaroslav@171
    28
jaroslav@171
    29
import com.sun.jersey.api.client.GenericType;
jaroslav@171
    30
import java.util.List;
jaroslav@171
    31
import com.sun.jersey.api.client.UniformInterfaceException;
jaroslav@171
    32
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@171
    33
import java.io.File;
jaroslav@171
    34
import java.io.FileOutputStream;
jaroslav@171
    35
import java.io.IOException;
jaroslav@171
    36
import java.util.Properties;
jaroslav@171
    37
import javax.ws.rs.core.MediaType;
jaroslav@171
    38
import org.junit.Test;
jaroslav@171
    39
import static org.junit.Assert.*;
jaroslav@171
    40
jaroslav@171
    41
/**
jaroslav@171
    42
 *
jaroslav@171
    43
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@171
    44
 */
jaroslav@171
    45
public class AllGamesTest extends JerseyTest {
jaroslav@171
    46
    static {
jaroslav@171
    47
        System.setProperty("JERSEY_HTTP_PORT", "45420");
jaroslav@171
    48
    }
jaroslav@171
    49
jaroslav@171
    50
    private File dir;
jaroslav@171
    51
jaroslav@171
    52
    public AllGamesTest() throws Exception {
jaroslav@171
    53
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@171
    54
    }
jaroslav@171
    55
jaroslav@171
    56
    @Override
jaroslav@171
    57
    public void setUp() throws Exception {
jaroslav@171
    58
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@171
    59
        dir.delete();
jaroslav@171
    60
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@171
    61
        dir.mkdirs();
jaroslav@171
    62
        File passwd = new File(dir, "passwd");
jaroslav@171
    63
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@171
    64
        os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
jaroslav@171
    65
        os.close();
jaroslav@171
    66
        super.setUp();
jaroslav@171
    67
    }
jaroslav@171
    68
jaroslav@171
    69
    @Override
jaroslav@171
    70
    public void tearDown() throws Exception {
jaroslav@171
    71
        deleteRec(dir);
jaroslav@171
    72
    }
jaroslav@171
    73
jaroslav@171
    74
    static void deleteRec(File dir) throws IOException {
jaroslav@171
    75
        if (dir == null) {
jaroslav@171
    76
            return;
jaroslav@171
    77
        }
jaroslav@171
    78
        File[] arr = dir.listFiles();
jaroslav@171
    79
        if (arr != null) {
jaroslav@171
    80
            for (File f : arr) {
jaroslav@171
    81
                deleteRec(f);
jaroslav@171
    82
            }
jaroslav@171
    83
        }
jaroslav@171
    84
        dir.delete();
jaroslav@171
    85
    }
jaroslav@171
    86
    @Test public void testListGames() throws Exception {
jaroslav@171
    87
        File usersDir = new File(dir, "users");
jaroslav@171
    88
        usersDir.mkdirs();
jaroslav@171
    89
        File fJarda = new File(usersDir, "Jarda");
jaroslav@171
    90
        {
jaroslav@171
    91
            Properties p = new Properties();
jaroslav@171
    92
            p.setProperty("email", "jar@da.cz");
jaroslav@171
    93
            p.setProperty("permission.games", "true");
jaroslav@171
    94
            p.store(new FileOutputStream(fJarda), "");
jaroslav@171
    95
        }
jaroslav@171
    96
        File fJirka = new File(usersDir, "Jirka");
jaroslav@171
    97
        {
jaroslav@171
    98
            Properties p = new Properties();
jaroslav@171
    99
            p.setProperty("email", "jir@ka.cz");
jaroslav@171
   100
            p.store(new FileOutputStream(fJirka), "");
jaroslav@171
   101
        }
jaroslav@171
   102
        File fJan = new File(usersDir, "Jan");
jaroslav@171
   103
        {
jaroslav@171
   104
            Properties p = new Properties();
jaroslav@171
   105
            p.setProperty("email", "j@an.cz");
jaroslav@171
   106
            p.store(new FileOutputStream(fJan), "");
jaroslav@171
   107
        }
jaroslav@171
   108
jaroslav@171
   109
        String logRoot = webResource.path("login").
jaroslav@171
   110
            queryParam("name", "Jarda").
jaroslav@171
   111
            queryParam("password", "heslo").
jaroslav@171
   112
            accept(MediaType.TEXT_PLAIN).
jaroslav@171
   113
            put(String.class);
jaroslav@171
   114
        String logJirka = webResource.path("login").
jaroslav@171
   115
            queryParam("name", "Jirka").
jaroslav@171
   116
            queryParam("password", "pesko").
jaroslav@171
   117
            accept(MediaType.TEXT_PLAIN).
jaroslav@171
   118
            put(String.class);
jaroslav@171
   119
        String logJan = webResource.path("login").
jaroslav@171
   120
            queryParam("name", "Jan").
jaroslav@171
   121
            queryParam("password", "pan").
jaroslav@171
   122
            accept(MediaType.TEXT_PLAIN).
jaroslav@171
   123
            put(String.class);
jaroslav@171
   124
jaroslav@171
   125
        GameId s = webResource.path("games").queryParam("white", "Jan")
jaroslav@171
   126
                .queryParam("loginID", logJan)
jaroslav@171
   127
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@171
   128
            webResource.path("games/" + s.getId())
jaroslav@171
   129
                .queryParam("loginID", logJan)
jaroslav@171
   130
                .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
jaroslav@171
   131
jaroslav@171
   132
        GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
jaroslav@171
   133
        List<GameId> all = webResource.path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
jaroslav@171
   134
        boolean found = false;
jaroslav@171
   135
        for (GameId id : all) {
jaroslav@171
   136
            if (id.getId().equals(s.getId())) {
jaroslav@171
   137
                found = true;
jaroslav@171
   138
                break;
jaroslav@171
   139
            }
jaroslav@171
   140
        }
jaroslav@171
   141
        assertTrue("List of games shall contai all games: " + all, found);
jaroslav@171
   142
jaroslav@171
   143
        Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@171
   144
        assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
jaroslav@171
   145
    }
jaroslav@171
   146
}