webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java
changeset 143 4eb88f05c207
child 144 cc04ede4cb5e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java	Sat Nov 07 15:23:14 2009 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.webidor;
    1.31 +
    1.32 +import com.sun.jersey.api.client.GenericType;
    1.33 +import com.sun.jersey.test.framework.JerseyTest;
    1.34 +import java.io.File;
    1.35 +import java.io.FileOutputStream;
    1.36 +import java.io.IOException;
    1.37 +import java.util.List;
    1.38 +import java.util.Properties;
    1.39 +import javax.ws.rs.core.MediaType;
    1.40 +import org.junit.Test;
    1.41 +import static org.junit.Assert.*;
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.46 + */
    1.47 +public class UsersTest extends JerseyTest {
    1.48 +    private File dir;
    1.49 +
    1.50 +    public UsersTest() throws Exception {
    1.51 +        super("cz.xelfi.quoridor.webidor.resources");
    1.52 +    }
    1.53 +
    1.54 +    @Override
    1.55 +    public void setUp() throws Exception {
    1.56 +        dir = File.createTempFile("quoridor", ".dir");
    1.57 +        dir.delete();
    1.58 +        System.setProperty("quoridor.dir", dir.getPath());
    1.59 +        dir.mkdirs();
    1.60 +        File passwd = new File(dir, "passwd");
    1.61 +        FileOutputStream os = new FileOutputStream(passwd);
    1.62 +        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    1.63 +        os.close();
    1.64 +        super.setUp();
    1.65 +    }
    1.66 +
    1.67 +    @Override
    1.68 +    public void tearDown() throws Exception {
    1.69 +        deleteRec(dir);
    1.70 +    }
    1.71 +
    1.72 +    static void deleteRec(File dir) throws IOException {
    1.73 +        if (dir == null) {
    1.74 +            return;
    1.75 +        }
    1.76 +        File[] arr = dir.listFiles();
    1.77 +        if (arr != null) {
    1.78 +            for (File f : arr) {
    1.79 +                deleteRec(f);
    1.80 +            }
    1.81 +        }
    1.82 +        dir.delete();
    1.83 +    }
    1.84 +
    1.85 +    @Test public void testListUsers() throws Exception {
    1.86 +        File usersDir = new File(dir, "users");
    1.87 +        usersDir.mkdirs();
    1.88 +        File fJarda = new File(usersDir, "Jarda");
    1.89 +        {
    1.90 +            Properties p = new Properties();
    1.91 +            p.setProperty("email", "jar@da.cz");
    1.92 +            p.setProperty("permission.email", "true");
    1.93 +            p.store(new FileOutputStream(fJarda), "");
    1.94 +        }
    1.95 +        File fJirka = new File(usersDir, "Jirka");
    1.96 +        {
    1.97 +            Properties p = new Properties();
    1.98 +            p.setProperty("email", "jir@ka.cz");
    1.99 +            p.store(new FileOutputStream(fJirka), "");
   1.100 +        }
   1.101 +
   1.102 +        String logJarda = webResource.path("login").
   1.103 +            queryParam("name", "Jarda").
   1.104 +            queryParam("password", "heslo").
   1.105 +            accept(MediaType.TEXT_PLAIN).
   1.106 +            put(String.class);
   1.107 +        String logJirka = webResource.path("login").
   1.108 +            queryParam("name", "Jirka").
   1.109 +            queryParam("password", "pesko").
   1.110 +            accept(MediaType.TEXT_PLAIN).
   1.111 +            put(String.class);
   1.112 +
   1.113 +        User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
   1.114 +        assertEquals("Jirka", uJirka.getId());
   1.115 +        assertNull("Cannot get email without login", uJirka.getProperty("email"));
   1.116 +
   1.117 +        uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   1.118 +        assertEquals("Jirka", uJirka.getId());
   1.119 +        assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
   1.120 +
   1.121 +        uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
   1.122 +        assertEquals("Jirka", uJirka.getId());
   1.123 +        assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
   1.124 +
   1.125 +        String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
   1.126 +        if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
   1.127 +            fail(txt);
   1.128 +        }
   1.129 +    }
   1.130 +}