webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java
changeset 171 524c7f359c4e
child 258 935118a5831a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/AllGamesTest.java	Wed Dec 23 07:59:16 2009 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.webidor;
    1.31 +
    1.32 +import com.sun.jersey.api.client.GenericType;
    1.33 +import java.util.List;
    1.34 +import com.sun.jersey.api.client.UniformInterfaceException;
    1.35 +import com.sun.jersey.test.framework.JerseyTest;
    1.36 +import java.io.File;
    1.37 +import java.io.FileOutputStream;
    1.38 +import java.io.IOException;
    1.39 +import java.util.Properties;
    1.40 +import javax.ws.rs.core.MediaType;
    1.41 +import org.junit.Test;
    1.42 +import static org.junit.Assert.*;
    1.43 +
    1.44 +/**
    1.45 + *
    1.46 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.47 + */
    1.48 +public class AllGamesTest extends JerseyTest {
    1.49 +    static {
    1.50 +        System.setProperty("JERSEY_HTTP_PORT", "45420");
    1.51 +    }
    1.52 +
    1.53 +    private File dir;
    1.54 +
    1.55 +    public AllGamesTest() throws Exception {
    1.56 +        super("cz.xelfi.quoridor.webidor.resources");
    1.57 +    }
    1.58 +
    1.59 +    @Override
    1.60 +    public void setUp() throws Exception {
    1.61 +        dir = File.createTempFile("quoridor", ".dir");
    1.62 +        dir.delete();
    1.63 +        System.setProperty("quoridor.dir", dir.getPath());
    1.64 +        dir.mkdirs();
    1.65 +        File passwd = new File(dir, "passwd");
    1.66 +        FileOutputStream os = new FileOutputStream(passwd);
    1.67 +        os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
    1.68 +        os.close();
    1.69 +        super.setUp();
    1.70 +    }
    1.71 +
    1.72 +    @Override
    1.73 +    public void tearDown() throws Exception {
    1.74 +        deleteRec(dir);
    1.75 +    }
    1.76 +
    1.77 +    static void deleteRec(File dir) throws IOException {
    1.78 +        if (dir == null) {
    1.79 +            return;
    1.80 +        }
    1.81 +        File[] arr = dir.listFiles();
    1.82 +        if (arr != null) {
    1.83 +            for (File f : arr) {
    1.84 +                deleteRec(f);
    1.85 +            }
    1.86 +        }
    1.87 +        dir.delete();
    1.88 +    }
    1.89 +    @Test public void testListGames() throws Exception {
    1.90 +        File usersDir = new File(dir, "users");
    1.91 +        usersDir.mkdirs();
    1.92 +        File fJarda = new File(usersDir, "Jarda");
    1.93 +        {
    1.94 +            Properties p = new Properties();
    1.95 +            p.setProperty("email", "jar@da.cz");
    1.96 +            p.setProperty("permission.games", "true");
    1.97 +            p.store(new FileOutputStream(fJarda), "");
    1.98 +        }
    1.99 +        File fJirka = new File(usersDir, "Jirka");
   1.100 +        {
   1.101 +            Properties p = new Properties();
   1.102 +            p.setProperty("email", "jir@ka.cz");
   1.103 +            p.store(new FileOutputStream(fJirka), "");
   1.104 +        }
   1.105 +        File fJan = new File(usersDir, "Jan");
   1.106 +        {
   1.107 +            Properties p = new Properties();
   1.108 +            p.setProperty("email", "j@an.cz");
   1.109 +            p.store(new FileOutputStream(fJan), "");
   1.110 +        }
   1.111 +
   1.112 +        String logRoot = webResource.path("login").
   1.113 +            queryParam("name", "Jarda").
   1.114 +            queryParam("password", "heslo").
   1.115 +            accept(MediaType.TEXT_PLAIN).
   1.116 +            put(String.class);
   1.117 +        String logJirka = webResource.path("login").
   1.118 +            queryParam("name", "Jirka").
   1.119 +            queryParam("password", "pesko").
   1.120 +            accept(MediaType.TEXT_PLAIN).
   1.121 +            put(String.class);
   1.122 +        String logJan = webResource.path("login").
   1.123 +            queryParam("name", "Jan").
   1.124 +            queryParam("password", "pan").
   1.125 +            accept(MediaType.TEXT_PLAIN).
   1.126 +            put(String.class);
   1.127 +
   1.128 +        GameId s = webResource.path("games").queryParam("white", "Jan")
   1.129 +                .queryParam("loginID", logJan)
   1.130 +                .queryParam("black", "Jirka").post(GameId.class);
   1.131 +            webResource.path("games/" + s.getId())
   1.132 +                .queryParam("loginID", logJan)
   1.133 +                .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
   1.134 +
   1.135 +        GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   1.136 +        List<GameId> all = webResource.path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
   1.137 +        boolean found = false;
   1.138 +        for (GameId id : all) {
   1.139 +            if (id.getId().equals(s.getId())) {
   1.140 +                found = true;
   1.141 +                break;
   1.142 +            }
   1.143 +        }
   1.144 +        assertTrue("List of games shall contai all games: " + all, found);
   1.145 +
   1.146 +        Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
   1.147 +        assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());
   1.148 +    }
   1.149 +}