jaroslav@171: /* jaroslav@264: * Quoridor server and related libraries jaroslav@264: * Copyright (C) 2009-2010 Jaroslav Tulach jaroslav@171: * jaroslav@264: * This program is free software: you can redistribute it and/or modify jaroslav@264: * it under the terms of the GNU General Public License as published by jaroslav@264: * the Free Software Foundation, either version 3 of the License. jaroslav@171: * jaroslav@264: * This program is distributed in the hope that it will be useful, jaroslav@264: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@264: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@264: * GNU General Public License for more details. jaroslav@171: * jaroslav@264: * You should have received a copy of the GNU General Public License jaroslav@264: * along with this program. Look for COPYING file in the top folder. jaroslav@264: * If not, see http://www.gnu.org/licenses/. jaroslav@171: */ jaroslav@171: jaroslav@171: package cz.xelfi.quoridor.webidor; jaroslav@171: jaroslav@258: import com.sun.jersey.test.framework.WebAppDescriptor; jaroslav@258: import com.sun.jersey.test.framework.AppDescriptor; jaroslav@171: import com.sun.jersey.api.client.GenericType; jaroslav@171: import java.util.List; jaroslav@171: import com.sun.jersey.test.framework.JerseyTest; jaroslav@171: import java.io.File; jaroslav@171: import java.io.FileOutputStream; jaroslav@171: import java.io.IOException; jaroslav@171: import java.util.Properties; jaroslav@171: import javax.ws.rs.core.MediaType; jaroslav@171: import org.junit.Test; jaroslav@171: import static org.junit.Assert.*; jaroslav@171: jaroslav@171: /** jaroslav@171: * jaroslav@171: * @author Jaroslav Tulach jaroslav@171: */ jaroslav@171: public class AllGamesTest extends JerseyTest { jaroslav@171: static { jaroslav@171: System.setProperty("JERSEY_HTTP_PORT", "45420"); jaroslav@171: } jaroslav@171: jaroslav@171: private File dir; jaroslav@171: jaroslav@171: @Override jaroslav@258: protected AppDescriptor configure() { jaroslav@258: try { jaroslav@258: dir = File.createTempFile("quoridor", ".dir"); jaroslav@258: dir.delete(); jaroslav@258: System.setProperty("quoridor.dir", dir.getPath()); jaroslav@258: dir.mkdirs(); jaroslav@258: File passwd = new File(dir, "passwd"); jaroslav@258: FileOutputStream os = new FileOutputStream(passwd); jaroslav@258: os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8")); jaroslav@258: os.close(); jaroslav@258: } catch (Exception ex) { jaroslav@258: throw new IllegalStateException(ex); jaroslav@258: } jaroslav@285: return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("allgamess").build(); jaroslav@171: } jaroslav@171: jaroslav@171: @Override jaroslav@171: public void tearDown() throws Exception { jaroslav@171: deleteRec(dir); jaroslav@171: } jaroslav@171: jaroslav@171: static void deleteRec(File dir) throws IOException { jaroslav@171: if (dir == null) { jaroslav@171: return; jaroslav@171: } jaroslav@171: File[] arr = dir.listFiles(); jaroslav@171: if (arr != null) { jaroslav@171: for (File f : arr) { jaroslav@171: deleteRec(f); jaroslav@171: } jaroslav@171: } jaroslav@171: dir.delete(); jaroslav@171: } jaroslav@171: @Test public void testListGames() throws Exception { jaroslav@171: File usersDir = new File(dir, "users"); jaroslav@171: usersDir.mkdirs(); jaroslav@171: File fJarda = new File(usersDir, "Jarda"); jaroslav@171: { jaroslav@171: Properties p = new Properties(); jaroslav@171: p.setProperty("email", "jar@da.cz"); jaroslav@171: p.setProperty("permission.games", "true"); jaroslav@171: p.store(new FileOutputStream(fJarda), ""); jaroslav@171: } jaroslav@171: File fJirka = new File(usersDir, "Jirka"); jaroslav@171: { jaroslav@171: Properties p = new Properties(); jaroslav@171: p.setProperty("email", "jir@ka.cz"); jaroslav@171: p.store(new FileOutputStream(fJirka), ""); jaroslav@171: } jaroslav@171: File fJan = new File(usersDir, "Jan"); jaroslav@171: { jaroslav@171: Properties p = new Properties(); jaroslav@171: p.setProperty("email", "j@an.cz"); jaroslav@171: p.store(new FileOutputStream(fJan), ""); jaroslav@171: } jaroslav@171: jaroslav@258: String logRoot = resource().path("login"). jaroslav@171: queryParam("name", "Jarda"). jaroslav@171: queryParam("password", "heslo"). jaroslav@171: accept(MediaType.TEXT_PLAIN). jaroslav@171: put(String.class); jaroslav@258: String logJirka = resource().path("login"). jaroslav@171: queryParam("name", "Jirka"). jaroslav@171: queryParam("password", "pesko"). jaroslav@171: accept(MediaType.TEXT_PLAIN). jaroslav@171: put(String.class); jaroslav@258: String logJan = resource().path("login"). jaroslav@171: queryParam("name", "Jan"). jaroslav@171: queryParam("password", "pan"). jaroslav@171: accept(MediaType.TEXT_PLAIN). jaroslav@171: put(String.class); jaroslav@171: jaroslav@258: GameId s = resource().path("games").queryParam("white", "Jan") jaroslav@171: .queryParam("loginID", logJan) jaroslav@171: .queryParam("black", "Jirka").post(GameId.class); jaroslav@258: resource().path("games/" + s.getId()) jaroslav@171: .queryParam("loginID", logJan) jaroslav@171: .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class); jaroslav@171: jaroslav@171: GenericType> gType = new GenericType>() {}; jaroslav@258: List all = resource().path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType); jaroslav@171: boolean found = false; jaroslav@171: for (GameId id : all) { jaroslav@171: if (id.getId().equals(s.getId())) { jaroslav@171: found = true; jaroslav@171: break; jaroslav@171: } jaroslav@171: } jaroslav@171: assertTrue("List of games shall contai all games: " + all, found); jaroslav@171: jaroslav@258: Game end = resource().path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class); jaroslav@171: assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus()); jaroslav@171: } jaroslav@171: }