freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 13 Sep 2009 16:48:54 +0200
changeset 82 9ac7acee7d9f
parent 81 1174c9dcab41
child 106 7d090f2c5b91
permissions -rw-r--r--
Providing REST like authentication
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.freemarkerdor;
    28 
    29 import com.sun.jersey.api.client.Client;
    30 import com.sun.jersey.api.client.ClientResponse;
    31 import com.sun.jersey.api.client.WebResource;
    32 import com.sun.jersey.core.header.MediaTypes;
    33 import com.sun.jersey.core.util.MultivaluedMapImpl;
    34 import java.io.File;
    35 import java.io.FileOutputStream;
    36 import java.io.IOException;
    37 import java.net.URI;
    38 import java.util.Locale;
    39 import java.util.concurrent.Callable;
    40 import javax.ws.rs.core.Cookie;
    41 import javax.ws.rs.core.MediaType;
    42 import javax.ws.rs.core.MultivaluedMap;
    43 import org.junit.AfterClass;
    44 import org.junit.Before;
    45 import org.junit.BeforeClass;
    46 import org.junit.Test;
    47 import static org.junit.Assert.*;
    48 
    49 /**
    50  *
    51  * @author Jaroslav Tulach <jtulach@netbeans.org>
    52  */
    53 public class UITest extends Object {
    54     private static File dir;
    55     private static Callable<Void> stop;
    56     private WebResource webResource;
    57 
    58     public UITest() throws Exception {
    59     }
    60 
    61     @BeforeClass
    62     public static void localeEnglish() throws Exception {
    63         Locale.setDefault(Locale.ENGLISH);
    64         dir = File.createTempFile("quoridor", ".dir");
    65         dir.delete();
    66         dir.mkdirs();
    67         System.setProperty("quoridor.dir", dir.getPath());
    68         stop = UI.startServers(9991);
    69 
    70         File passwd = new File(dir, "passwd");
    71         FileOutputStream os = new FileOutputStream(passwd);
    72         os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
    73         os.close();
    74     }
    75 
    76     @Before
    77     public void setUp() throws Exception {
    78 
    79         Client client = new Client();
    80         webResource = client.resource(new URI("http://localhost:9991/"));
    81     }
    82 
    83     @AfterClass
    84     public static void cleanUpAll() throws Exception {
    85         deleteRec(dir);
    86         if (stop != null) {
    87             stop.call();
    88         }
    89     }
    90 
    91     static void deleteRec(File dir) throws IOException {
    92         if (dir == null) {
    93             return;
    94         }
    95         File[] arr = dir.listFiles();
    96         if (arr != null) {
    97             for (File f : arr) {
    98                 deleteRec(f);
    99             }
   100         }
   101         dir.delete();
   102     }
   103 
   104     /**
   105      * Test if a WADL document is available at the relative path
   106      * "application.wadl".
   107      */
   108     @Test public void testApplicationWadl() {
   109         String serviceWadl = webResource.path("application.wadl").
   110                 accept(MediaTypes.WADL).get(String.class);
   111         assertTrue(serviceWadl.length() > 0);
   112     }
   113 
   114     @Test public void testGetIndexPage() throws Exception {
   115         String logJarda = webResource.path("api/login").
   116             queryParam("name", "Jarda").
   117             queryParam("password", "darda").
   118             accept(MediaType.TEXT_PLAIN).
   119             put(String.class);
   120         assertNotNull("Logged in OK", logJarda);
   121 
   122         String res = webResource.accept("text/html").get(String.class);
   123         if (res.indexOf("Quoridor") == -1) {
   124             fail("Wrong index.html:\n" + res);
   125         }
   126         if (res.indexOf("Login") == -1) {
   127             fail("Wrong index.html:\n" + res);
   128         }
   129         if (res.indexOf("action=\"/login\"") == -1) {
   130             fail("Wrong index.html:\n" + res);
   131         }
   132         if (res.toLowerCase().indexOf("error") != -1) {
   133             fail("There was an error:\n" + res);
   134         }
   135         res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
   136         if (res.indexOf("action=\"/games/create\"") == -1) {
   137             fail(res);
   138         }
   139     }
   140 
   141     @Test public void testLogin() throws Exception {
   142         MultivaluedMap formData = new MultivaluedMapImpl();
   143         formData.add("name", "test");
   144         formData.add("password", "pes");
   145         ClientResponse response = webResource.path("login").
   146             accept("text/html").type("application/x-www-form-urlencoded").
   147             post(ClientResponse.class, formData);
   148         final String res = response.getEntity(String.class);
   149         assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
   150         if (res.indexOf("You are logged in as test") == -1) {
   151             fail("res: " + res);
   152         }
   153     }
   154 
   155 
   156     @Test public void testCreateGameWrongUsers() throws Exception {
   157         String logTest = webResource.path("api/login").
   158             queryParam("name", "test").
   159             queryParam("password", "pes").
   160             accept(MediaType.TEXT_PLAIN).
   161             put(String.class);
   162         ClientResponse res = webResource.path("games/create").
   163             queryParam("white", "unknown1").
   164             queryParam("black", "unknown2").
   165             cookie(Cookie.valueOf("login=" + logTest)).
   166             accept("text/html").
   167             get(ClientResponse.class);
   168         final String txt = res.getEntity(String.class);
   169         assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
   170         if (txt.indexOf("You (test) must be") == -1) {
   171             fail(txt);
   172         }
   173     }
   174 
   175     @Test public void testCreateGameOK() throws Exception {
   176         String logTest = webResource.path("api/login").
   177             queryParam("name", "test").
   178             queryParam("password", "pes").
   179             accept(MediaType.TEXT_PLAIN).
   180             put(String.class);
   181         ClientResponse res = webResource.path("games/create").
   182             queryParam("white", "unknown1").
   183             queryParam("black", "test").
   184             cookie(Cookie.valueOf("login=" + logTest)).
   185             accept("text/html").
   186             get(ClientResponse.class);
   187 
   188         final String txt = res.getEntity(String.class);
   189         assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   190 
   191         String[] games = new File(dir, "games").list();
   192         assertEquals("One game exists", 1, games.length);
   193 
   194         if (txt.indexOf(games[0]) == -1) {
   195             fail(games[0] + " expected inside of:\n" + txt);
   196         }
   197     }
   198 
   199 }