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
jaroslav@143
     1
/*
jaroslav@143
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@143
     3
 *
jaroslav@143
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@143
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@143
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@143
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@143
     8
 * License. You can obtain a copy of the License at
jaroslav@143
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@143
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@143
    11
 * specific language governing permissions and limitations under the
jaroslav@143
    12
 * License.  When distributing the software, include this License Header
jaroslav@143
    13
 * Notice in each file and include the License file at
jaroslav@143
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@143
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@143
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@143
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@143
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@143
    19
 * your own identifying information:
jaroslav@143
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@143
    21
 *
jaroslav@143
    22
 * Contributor(s):
jaroslav@143
    23
 *
jaroslav@143
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@143
    25
 */
jaroslav@143
    26
jaroslav@143
    27
package cz.xelfi.quoridor.webidor;
jaroslav@143
    28
jaroslav@143
    29
import com.sun.jersey.api.client.GenericType;
jaroslav@143
    30
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@143
    31
import java.io.File;
jaroslav@143
    32
import java.io.FileOutputStream;
jaroslav@143
    33
import java.io.IOException;
jaroslav@143
    34
import java.util.List;
jaroslav@143
    35
import java.util.Properties;
jaroslav@143
    36
import javax.ws.rs.core.MediaType;
jaroslav@143
    37
import org.junit.Test;
jaroslav@143
    38
import static org.junit.Assert.*;
jaroslav@143
    39
jaroslav@143
    40
/**
jaroslav@143
    41
 *
jaroslav@143
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@143
    43
 */
jaroslav@143
    44
public class UsersTest extends JerseyTest {
jaroslav@143
    45
    private File dir;
jaroslav@143
    46
jaroslav@143
    47
    public UsersTest() throws Exception {
jaroslav@143
    48
        super("cz.xelfi.quoridor.webidor.resources");
jaroslav@143
    49
    }
jaroslav@143
    50
jaroslav@143
    51
    @Override
jaroslav@143
    52
    public void setUp() throws Exception {
jaroslav@143
    53
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@143
    54
        dir.delete();
jaroslav@143
    55
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@143
    56
        dir.mkdirs();
jaroslav@143
    57
        File passwd = new File(dir, "passwd");
jaroslav@143
    58
        FileOutputStream os = new FileOutputStream(passwd);
jaroslav@143
    59
        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
jaroslav@143
    60
        os.close();
jaroslav@143
    61
        super.setUp();
jaroslav@143
    62
    }
jaroslav@143
    63
jaroslav@143
    64
    @Override
jaroslav@143
    65
    public void tearDown() throws Exception {
jaroslav@143
    66
        deleteRec(dir);
jaroslav@143
    67
    }
jaroslav@143
    68
jaroslav@143
    69
    static void deleteRec(File dir) throws IOException {
jaroslav@143
    70
        if (dir == null) {
jaroslav@143
    71
            return;
jaroslav@143
    72
        }
jaroslav@143
    73
        File[] arr = dir.listFiles();
jaroslav@143
    74
        if (arr != null) {
jaroslav@143
    75
            for (File f : arr) {
jaroslav@143
    76
                deleteRec(f);
jaroslav@143
    77
            }
jaroslav@143
    78
        }
jaroslav@143
    79
        dir.delete();
jaroslav@143
    80
    }
jaroslav@143
    81
jaroslav@143
    82
    @Test public void testListUsers() throws Exception {
jaroslav@143
    83
        File usersDir = new File(dir, "users");
jaroslav@143
    84
        usersDir.mkdirs();
jaroslav@143
    85
        File fJarda = new File(usersDir, "Jarda");
jaroslav@143
    86
        {
jaroslav@143
    87
            Properties p = new Properties();
jaroslav@143
    88
            p.setProperty("email", "jar@da.cz");
jaroslav@143
    89
            p.setProperty("permission.email", "true");
jaroslav@143
    90
            p.store(new FileOutputStream(fJarda), "");
jaroslav@143
    91
        }
jaroslav@143
    92
        File fJirka = new File(usersDir, "Jirka");
jaroslav@143
    93
        {
jaroslav@143
    94
            Properties p = new Properties();
jaroslav@143
    95
            p.setProperty("email", "jir@ka.cz");
jaroslav@143
    96
            p.store(new FileOutputStream(fJirka), "");
jaroslav@143
    97
        }
jaroslav@143
    98
jaroslav@143
    99
        String logJarda = webResource.path("login").
jaroslav@143
   100
            queryParam("name", "Jarda").
jaroslav@143
   101
            queryParam("password", "heslo").
jaroslav@143
   102
            accept(MediaType.TEXT_PLAIN).
jaroslav@143
   103
            put(String.class);
jaroslav@143
   104
        String logJirka = webResource.path("login").
jaroslav@143
   105
            queryParam("name", "Jirka").
jaroslav@143
   106
            queryParam("password", "pesko").
jaroslav@143
   107
            accept(MediaType.TEXT_PLAIN).
jaroslav@143
   108
            put(String.class);
jaroslav@143
   109
jaroslav@143
   110
        User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
jaroslav@143
   111
        assertEquals("Jirka", uJirka.getId());
jaroslav@143
   112
        assertNull("Cannot get email without login", uJirka.getProperty("email"));
jaroslav@143
   113
jaroslav@143
   114
        uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
jaroslav@143
   115
        assertEquals("Jirka", uJirka.getId());
jaroslav@143
   116
        assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
jaroslav@143
   117
jaroslav@143
   118
        uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
jaroslav@143
   119
        assertEquals("Jirka", uJirka.getId());
jaroslav@143
   120
        assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
jaroslav@143
   121
jaroslav@143
   122
        String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
jaroslav@143
   123
        if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
jaroslav@143
   124
            fail(txt);
jaroslav@143
   125
        }
jaroslav@143
   126
    }
jaroslav@143
   127
}