webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 15:23:14 +0100
changeset 143 4eb88f05c207
child 144 cc04ede4cb5e
permissions -rw-r--r--
Support for properties associated with every user
     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.webidor;
    28 
    29 import com.sun.jersey.api.client.GenericType;
    30 import com.sun.jersey.test.framework.JerseyTest;
    31 import java.io.File;
    32 import java.io.FileOutputStream;
    33 import java.io.IOException;
    34 import java.util.List;
    35 import java.util.Properties;
    36 import javax.ws.rs.core.MediaType;
    37 import org.junit.Test;
    38 import static org.junit.Assert.*;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 public class UsersTest extends JerseyTest {
    45     private File dir;
    46 
    47     public UsersTest() throws Exception {
    48         super("cz.xelfi.quoridor.webidor.resources");
    49     }
    50 
    51     @Override
    52     public void setUp() throws Exception {
    53         dir = File.createTempFile("quoridor", ".dir");
    54         dir.delete();
    55         System.setProperty("quoridor.dir", dir.getPath());
    56         dir.mkdirs();
    57         File passwd = new File(dir, "passwd");
    58         FileOutputStream os = new FileOutputStream(passwd);
    59         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    60         os.close();
    61         super.setUp();
    62     }
    63 
    64     @Override
    65     public void tearDown() throws Exception {
    66         deleteRec(dir);
    67     }
    68 
    69     static void deleteRec(File dir) throws IOException {
    70         if (dir == null) {
    71             return;
    72         }
    73         File[] arr = dir.listFiles();
    74         if (arr != null) {
    75             for (File f : arr) {
    76                 deleteRec(f);
    77             }
    78         }
    79         dir.delete();
    80     }
    81 
    82     @Test public void testListUsers() throws Exception {
    83         File usersDir = new File(dir, "users");
    84         usersDir.mkdirs();
    85         File fJarda = new File(usersDir, "Jarda");
    86         {
    87             Properties p = new Properties();
    88             p.setProperty("email", "jar@da.cz");
    89             p.setProperty("permission.email", "true");
    90             p.store(new FileOutputStream(fJarda), "");
    91         }
    92         File fJirka = new File(usersDir, "Jirka");
    93         {
    94             Properties p = new Properties();
    95             p.setProperty("email", "jir@ka.cz");
    96             p.store(new FileOutputStream(fJirka), "");
    97         }
    98 
    99         String logJarda = webResource.path("login").
   100             queryParam("name", "Jarda").
   101             queryParam("password", "heslo").
   102             accept(MediaType.TEXT_PLAIN).
   103             put(String.class);
   104         String logJirka = webResource.path("login").
   105             queryParam("name", "Jirka").
   106             queryParam("password", "pesko").
   107             accept(MediaType.TEXT_PLAIN).
   108             put(String.class);
   109 
   110         User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
   111         assertEquals("Jirka", uJirka.getId());
   112         assertNull("Cannot get email without login", uJirka.getProperty("email"));
   113 
   114         uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   115         assertEquals("Jirka", uJirka.getId());
   116         assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
   117 
   118         uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
   119         assertEquals("Jirka", uJirka.getId());
   120         assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
   121 
   122         String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
   123         if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
   124             fail(txt);
   125         }
   126     }
   127 }