Ability to get User by loginID and making sure tests pass in the same VM
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 23:26:03 +0100
changeset 145ac9bd9be5263
parent 144 cc04ede4cb5e
child 146 0b889d9e4ee1
Ability to get User by loginID and making sure tests pass in the same VM
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sat Nov 07 18:31:47 2009 +0100
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sat Nov 07 23:26:03 2009 +0100
     1.3 @@ -31,6 +31,7 @@
     1.4  import com.sun.jersey.api.core.ResourceConfig;
     1.5  import com.sun.jersey.spi.resource.Singleton;
     1.6  import com.sun.net.httpserver.HttpServer;
     1.7 +import cz.xelfi.quoridor.webidor.User;
     1.8  import java.io.File;
     1.9  import java.io.FileInputStream;
    1.10  import java.io.IOException;
    1.11 @@ -86,7 +87,7 @@
    1.12  
    1.13      @Path("login")
    1.14      @PUT
    1.15 -    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.16 +    @Produces({ MediaType.TEXT_PLAIN })
    1.17      public String login(
    1.18          @QueryParam("name") String name,
    1.19          @QueryParam("password") String password
    1.20 @@ -110,7 +111,7 @@
    1.21  
    1.22      @Path("login")
    1.23      @GET
    1.24 -    @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.25 +    @Produces({ MediaType.TEXT_PLAIN })
    1.26      public String isLoggedIn(
    1.27          @QueryParam("id") String id
    1.28      ) {
    1.29 @@ -125,6 +126,23 @@
    1.30          return ret == null ? "" : ret;
    1.31      }
    1.32  
    1.33 +    @Path("login")
    1.34 +    @GET
    1.35 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.36 +    public User loggedInInfo(
    1.37 +        @QueryParam("id") String id
    1.38 +    ) throws IOException {
    1.39 +        String ret = null;
    1.40 +        try {
    1.41 +            if (id != null) {
    1.42 +                ret = loggedIn.get(UUID.fromString(id));
    1.43 +            }
    1.44 +        } catch (IllegalArgumentException ex) {
    1.45 +            // OK, happens for invalid ids
    1.46 +        }
    1.47 +        return ret == null ? null : getUsers().getUserInfo(id, ret);
    1.48 +    }
    1.49 +
    1.50      //
    1.51      // start the server
    1.52      //
     2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java	Sat Nov 07 18:31:47 2009 +0100
     2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java	Sat Nov 07 23:26:03 2009 +0100
     2.3 @@ -63,6 +63,13 @@
     2.4      }
     2.5  
     2.6      @GET
     2.7 +    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
     2.8 +    public User getOwnInfo(
     2.9 +            @QueryParam("loginID") String loginId
    2.10 +    ) throws IOException {
    2.11 +        return getUserInfo(loginId, null);
    2.12 +    }
    2.13 +    @GET
    2.14      @Path("{id}")
    2.15      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    2.16      public User getUserInfo(
    2.17 @@ -70,6 +77,9 @@
    2.18          @PathParam("id") String id
    2.19      ) throws IOException {
    2.20          String  myid = quoridor.isLoggedIn(loginId);
    2.21 +        if (id == null) {
    2.22 +            id = myid;
    2.23 +        }
    2.24          Properties myp = getProp(myid);
    2.25          Properties p = getProp(id);
    2.26          User user = new User(id);
    2.27 @@ -114,9 +124,11 @@
    2.28          Properties p = new Properties();
    2.29          if (id != null && id.length() > 0) {
    2.30              File f = new File(dir, id);
    2.31 -            FileInputStream is = new FileInputStream(f);
    2.32 -            p.load(is);
    2.33 -            is.close();
    2.34 +            if (f.exists()) {
    2.35 +                FileInputStream is = new FileInputStream(f);
    2.36 +                p.load(is);
    2.37 +                is.close();
    2.38 +            }
    2.39          }
    2.40          return p;
    2.41      }
     3.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sat Nov 07 18:31:47 2009 +0100
     3.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sat Nov 07 23:26:03 2009 +0100
     3.3 @@ -49,6 +49,9 @@
     3.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     3.5   */
     3.6  public class QuoridorTest extends JerseyTest {
     3.7 +    static {
     3.8 +        System.setProperty("JERSEY_HTTP_PORT", "33434");
     3.9 +    }
    3.10      private File dir;
    3.11  
    3.12      public QuoridorTest() throws Exception {
    3.13 @@ -104,6 +107,15 @@
    3.14              queryParam("password", "pesko").
    3.15              accept(MediaType.TEXT_PLAIN).
    3.16              put(String.class);
    3.17 +
    3.18 +        User uJirka = webResource.path("users").
    3.19 +            queryParam("loginID", logJirka).
    3.20 +            accept(MediaType.TEXT_XML).
    3.21 +            get(User.class);
    3.22 +
    3.23 +        assertEquals("Jirka", uJirka.getId());
    3.24 +
    3.25 +
    3.26      GameId s = webResource.path("games").queryParam("loginID", logJarda).
    3.27                  queryParam("white", "Jarda")
    3.28                  .queryParam("black", "Jirka").post(GameId.class);
     4.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java	Sat Nov 07 18:31:47 2009 +0100
     4.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java	Sat Nov 07 23:26:03 2009 +0100
     4.3 @@ -26,13 +26,11 @@
     4.4  
     4.5  package cz.xelfi.quoridor.webidor;
     4.6  
     4.7 -import com.sun.jersey.api.client.GenericType;
     4.8  import com.sun.jersey.api.client.UniformInterfaceException;
     4.9  import com.sun.jersey.test.framework.JerseyTest;
    4.10  import java.io.File;
    4.11  import java.io.FileOutputStream;
    4.12  import java.io.IOException;
    4.13 -import java.util.List;
    4.14  import java.util.Properties;
    4.15  import javax.ws.rs.core.MediaType;
    4.16  import org.junit.Test;
    4.17 @@ -43,6 +41,10 @@
    4.18   * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.19   */
    4.20  public class UsersTest extends JerseyTest {
    4.21 +    static {
    4.22 +        System.setProperty("JERSEY_HTTP_PORT", "39434");
    4.23 +    }
    4.24 +
    4.25      private File dir;
    4.26  
    4.27      public UsersTest() throws Exception {