webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 18 Oct 2009 21:33:27 +0200
changeset 128 eba04a2569d0
parent 124 90371f3eb106
child 140 04f9d559b8a1
permissions -rw-r--r--
Can list just games with certain status
jaroslav@57
     1
/*
jaroslav@57
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@57
     3
 *
jaroslav@57
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@57
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@57
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@57
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@57
     8
 * License. You can obtain a copy of the License at
jaroslav@57
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@57
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@57
    11
 * specific language governing permissions and limitations under the
jaroslav@57
    12
 * License.  When distributing the software, include this License Header
jaroslav@57
    13
 * Notice in each file and include the License file at
jaroslav@57
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@57
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@57
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@57
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@57
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@57
    19
 * your own identifying information:
jaroslav@57
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@57
    21
 *
jaroslav@57
    22
 * Contributor(s):
jaroslav@57
    23
 *
jaroslav@57
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@57
    25
 */
jaroslav@57
    26
jaroslav@57
    27
package cz.xelfi.quoridor.webidor;
jaroslav@57
    28
jaroslav@128
    29
import com.sun.jersey.api.client.GenericType;
jaroslav@57
    30
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@57
    31
import java.io.File;
jtulach@82
    32
import java.io.FileOutputStream;
jaroslav@57
    33
import java.io.IOException;
jaroslav@128
    34
import java.util.List;
jaroslav@57
    35
import javax.ws.rs.core.MediaType;
jaroslav@57
    36
import org.junit.Test;
jaroslav@57
    37
import static org.junit.Assert.*;
jaroslav@57
    38
jaroslav@57
    39
/**
jaroslav@57
    40
 *
jaroslav@57
    41
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@57
    42
 */
jaroslav@57
    43
public class FinishedGameTest extends JerseyTest {
jaroslav@57
    44
    private File dir;
jaroslav@57
    45
jaroslav@57
    46
    public FinishedGameTest() throws Exception {
jaroslav@57
    47
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@57
    48
    }
jaroslav@57
    49
jaroslav@57
    50
    @Override
jaroslav@57
    51
    public void setUp() throws Exception {
jaroslav@57
    52
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@57
    53
        dir.delete();
jaroslav@57
    54
        System.setProperty("quoridor.dir", dir.getPath());
jtulach@82
    55
        dir.mkdirs();
jtulach@82
    56
        File passwd = new File(dir, "passwd");
jtulach@82
    57
        FileOutputStream os = new FileOutputStream(passwd);
jtulach@82
    58
        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
jtulach@82
    59
        os.close();
jaroslav@57
    60
        super.setUp();
jaroslav@57
    61
    }
jaroslav@57
    62
jaroslav@57
    63
    @Override
jaroslav@57
    64
    public void tearDown() throws Exception {
jaroslav@57
    65
        super.tearDown();
jaroslav@57
    66
        deleteRec(dir);
jaroslav@57
    67
    }
jaroslav@57
    68
jaroslav@57
    69
    static void deleteRec(File dir) throws IOException {
jaroslav@57
    70
        if (dir == null) {
jaroslav@57
    71
            return;
jaroslav@57
    72
        }
jaroslav@57
    73
        File[] arr = dir.listFiles();
jaroslav@57
    74
        if (arr != null) {
jaroslav@57
    75
            for (File f : arr) {
jaroslav@57
    76
                deleteRec(f);
jaroslav@57
    77
            }
jaroslav@57
    78
        }
jaroslav@57
    79
        dir.delete();
jaroslav@57
    80
    }
jtulach@83
    81
    @Test public void testNotLoggedIn() {
jtulach@83
    82
        String status  = webResource.path("login").queryParam("id", "not-logged-in").
jtulach@83
    83
                accept(MediaType.TEXT_PLAIN).get(String.class);
jtulach@83
    84
        assertEquals("Nobody is logged in", "", status);
jtulach@83
    85
    }
jtulach@83
    86
jaroslav@57
    87
jaroslav@57
    88
    @Test public void testCreateAGame() throws Exception {
jtulach@82
    89
        String logJarda = webResource.path("login").
jtulach@82
    90
            queryParam("name", "Jarda").
jtulach@82
    91
            queryParam("password", "heslo").
jtulach@82
    92
            accept(MediaType.TEXT_PLAIN).
jtulach@82
    93
            put(String.class);
jtulach@82
    94
        String logJirka = webResource.path("login").
jtulach@82
    95
            queryParam("name", "Jirka").
jtulach@82
    96
            queryParam("password", "pesko").
jtulach@82
    97
            accept(MediaType.TEXT_PLAIN).
jtulach@82
    98
            put(String.class);
jtulach@82
    99
        assertNotNull("Logged in ok", logJarda);
jaroslav@57
   100
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jtulach@82
   101
                .queryParam("loginID", logJarda)
jaroslav@57
   102
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@57
   103
jaroslav@57
   104
        for (int i = 0; i < 3; i++) {
jtulach@82
   105
            webResource.path("games/" + s.getId())
jtulach@82
   106
                .queryParam("loginID", logJarda)
jtulach@82
   107
                .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jtulach@82
   108
            webResource.path("games/" + s.getId())
jtulach@82
   109
                .queryParam("loginID", logJirka)
jtulach@82
   110
                .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
   111
        }
jaroslav@57
   112
jtulach@82
   113
        webResource.path("games/" + s.getId())
jtulach@82
   114
            .queryParam("loginID", logJarda)
jtulach@82
   115
            .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jtulach@82
   116
        webResource.path("games/" + s.getId())
jtulach@82
   117
            .queryParam("loginID", logJirka)
jtulach@82
   118
            .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
jaroslav@57
   119
jaroslav@128
   120
        
jaroslav@128
   121
        GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
jaroslav@128
   122
        List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
jaroslav@128
   123
        assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
jaroslav@128
   124
jaroslav@57
   125
        for (int i = 0; i < 3; i++) {
jtulach@82
   126
            webResource.path("games/" + s.getId())
jtulach@82
   127
                .queryParam("loginID", logJarda)
jtulach@82
   128
                .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jtulach@82
   129
            webResource.path("games/" + s.getId())
jtulach@82
   130
                .queryParam("loginID", logJirka)
jtulach@82
   131
                .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
   132
        }
jaroslav@57
   133
jaroslav@128
   134
jaroslav@57
   135
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jtulach@77
   136
        assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
jaroslav@128
   137
jaroslav@128
   138
        List<GameId> something = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
jaroslav@128
   139
jaroslav@128
   140
        assertEquals("One game finished: " + something, 1, something.size());
jaroslav@128
   141
        assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
jaroslav@57
   142
    }
jaroslav@57
   143
jtulach@75
   144
    @Test public void testResignAGame() throws Exception {
jtulach@82
   145
        String logJarda = webResource.path("login").
jtulach@82
   146
            queryParam("name", "Jarda").
jtulach@82
   147
            queryParam("password", "heslo").
jtulach@82
   148
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   149
            put(String.class);
jtulach@75
   150
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jtulach@82
   151
                .queryParam("loginID", logJarda)
jtulach@75
   152
                .queryParam("black", "Jirka").post(GameId.class);
jtulach@75
   153
jtulach@82
   154
        webResource.path("games/" + s.getId()).
jtulach@82
   155
            queryParam("loginID", logJarda).
jtulach@82
   156
            queryParam("player", "Jarda").
jtulach@82
   157
            queryParam("move", "RESIGN").put(GameId.class);
jtulach@75
   158
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jtulach@77
   159
        assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
jtulach@75
   160
    }
jtulach@75
   161
jaroslav@57
   162
}