webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 08 Nov 2009 10:21:46 +0100
changeset 149 a441b02a638a
parent 140 04f9d559b8a1
child 162 c1bfbe98152b
permissions -rw-r--r--
Changing the port value for each test
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@149
    44
    static {
jaroslav@149
    45
        System.setProperty("JERSEY_HTTP_PORT", "33435");
jaroslav@149
    46
    }
jaroslav@57
    47
    private File dir;
jaroslav@57
    48
jaroslav@57
    49
    public FinishedGameTest() throws Exception {
jaroslav@57
    50
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@57
    51
    }
jaroslav@57
    52
jaroslav@57
    53
    @Override
jaroslav@57
    54
    public void setUp() throws Exception {
jaroslav@57
    55
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@57
    56
        dir.delete();
jaroslav@57
    57
        System.setProperty("quoridor.dir", dir.getPath());
jtulach@82
    58
        dir.mkdirs();
jtulach@82
    59
        File passwd = new File(dir, "passwd");
jtulach@82
    60
        FileOutputStream os = new FileOutputStream(passwd);
jtulach@82
    61
        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
jtulach@82
    62
        os.close();
jaroslav@57
    63
        super.setUp();
jaroslav@57
    64
    }
jaroslav@57
    65
jaroslav@57
    66
    @Override
jaroslav@57
    67
    public void tearDown() throws Exception {
jaroslav@57
    68
        super.tearDown();
jaroslav@57
    69
        deleteRec(dir);
jaroslav@57
    70
    }
jaroslav@57
    71
jaroslav@57
    72
    static void deleteRec(File dir) throws IOException {
jaroslav@57
    73
        if (dir == null) {
jaroslav@57
    74
            return;
jaroslav@57
    75
        }
jaroslav@57
    76
        File[] arr = dir.listFiles();
jaroslav@57
    77
        if (arr != null) {
jaroslav@57
    78
            for (File f : arr) {
jaroslav@57
    79
                deleteRec(f);
jaroslav@57
    80
            }
jaroslav@57
    81
        }
jaroslav@57
    82
        dir.delete();
jaroslav@57
    83
    }
jtulach@83
    84
    @Test public void testNotLoggedIn() {
jtulach@83
    85
        String status  = webResource.path("login").queryParam("id", "not-logged-in").
jtulach@83
    86
                accept(MediaType.TEXT_PLAIN).get(String.class);
jtulach@83
    87
        assertEquals("Nobody is logged in", "", status);
jtulach@83
    88
    }
jtulach@83
    89
jaroslav@57
    90
jaroslav@57
    91
    @Test public void testCreateAGame() throws Exception {
jtulach@82
    92
        String logJarda = webResource.path("login").
jtulach@82
    93
            queryParam("name", "Jarda").
jtulach@82
    94
            queryParam("password", "heslo").
jtulach@82
    95
            accept(MediaType.TEXT_PLAIN).
jtulach@82
    96
            put(String.class);
jtulach@82
    97
        String logJirka = webResource.path("login").
jtulach@82
    98
            queryParam("name", "Jirka").
jtulach@82
    99
            queryParam("password", "pesko").
jtulach@82
   100
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   101
            put(String.class);
jtulach@82
   102
        assertNotNull("Logged in ok", logJarda);
jaroslav@57
   103
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jtulach@82
   104
                .queryParam("loginID", logJarda)
jaroslav@57
   105
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@57
   106
jaroslav@57
   107
        for (int i = 0; i < 3; i++) {
jtulach@82
   108
            webResource.path("games/" + s.getId())
jtulach@82
   109
                .queryParam("loginID", logJarda)
jtulach@82
   110
                .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jtulach@82
   111
            webResource.path("games/" + s.getId())
jtulach@82
   112
                .queryParam("loginID", logJirka)
jtulach@82
   113
                .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
   114
        }
jaroslav@57
   115
jtulach@82
   116
        webResource.path("games/" + s.getId())
jtulach@82
   117
            .queryParam("loginID", logJarda)
jtulach@82
   118
            .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jtulach@82
   119
        webResource.path("games/" + s.getId())
jtulach@82
   120
            .queryParam("loginID", logJirka)
jtulach@82
   121
            .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
jaroslav@57
   122
jaroslav@128
   123
        
jaroslav@128
   124
        GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
jaroslav@128
   125
        List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
jaroslav@128
   126
        assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
jaroslav@128
   127
jaroslav@57
   128
        for (int i = 0; i < 3; i++) {
jtulach@82
   129
            webResource.path("games/" + s.getId())
jtulach@82
   130
                .queryParam("loginID", logJarda)
jtulach@82
   131
                .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jtulach@82
   132
            webResource.path("games/" + s.getId())
jtulach@82
   133
                .queryParam("loginID", logJirka)
jtulach@82
   134
                .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@57
   135
        }
jaroslav@57
   136
jaroslav@128
   137
jaroslav@57
   138
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jtulach@77
   139
        assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
jaroslav@128
   140
jaroslav@140
   141
        assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
jaroslav@140
   142
jaroslav@128
   143
        List<GameId> something = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
jaroslav@128
   144
jaroslav@128
   145
        assertEquals("One game finished: " + something, 1, something.size());
jaroslav@128
   146
        assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
jaroslav@57
   147
    }
jaroslav@57
   148
jtulach@75
   149
    @Test public void testResignAGame() throws Exception {
jtulach@82
   150
        String logJarda = webResource.path("login").
jtulach@82
   151
            queryParam("name", "Jarda").
jtulach@82
   152
            queryParam("password", "heslo").
jtulach@82
   153
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   154
            put(String.class);
jtulach@75
   155
        GameId s = webResource.path("games").queryParam("white", "Jarda")
jtulach@82
   156
                .queryParam("loginID", logJarda)
jtulach@75
   157
                .queryParam("black", "Jirka").post(GameId.class);
jtulach@75
   158
jtulach@82
   159
        webResource.path("games/" + s.getId()).
jtulach@82
   160
            queryParam("loginID", logJarda).
jtulach@82
   161
            queryParam("player", "Jarda").
jtulach@82
   162
            queryParam("move", "RESIGN").put(GameId.class);
jtulach@75
   163
        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jtulach@77
   164
        assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
jaroslav@140
   165
        assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
jtulach@75
   166
    }
jtulach@75
   167
jaroslav@57
   168
}