View Javadoc

1   package it.com.atlassian.plugins.rest.sample.expansion;
2   
3   import com.atlassian.plugins.rest.sample.expansion.UriBuilder;
4   import com.atlassian.plugins.rest.sample.expansion.entity.Game;
5   import com.atlassian.plugins.rest.sample.expansion.entity.Player;
6   import com.sun.jersey.api.client.Client;
7   import junit.framework.Assert;
8   import static org.junit.Assert.*;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  /**
13   */
14  public class ExpansionTest
15  {
16      private UriBuilder builder;
17  
18      @Before
19      public void setUp()
20      {
21          builder = UriBuilder.create().path("rest").path("ex").path("1").param("os_username", "admin").param("os_password", "admin");
22      }
23  
24      @Test
25      public void testGetGameWithNonExpandedPlayers()
26      {
27          final Game game = Client.create().resource(builder.build()).path("game").get(Game.class);
28          assertEquals("Rugby", game.getName());
29          assertNull(game.getPlayers().getPlayers());
30          assertEquals(5, game.getPlayers().getMaxResults());
31          assertEquals(10, game.getPlayers().getSize());
32          assertNull(game.getPlayers().getStartIndex());
33      }
34  
35      @Test
36      public void testGetGameWithExpandedPlayers()
37      {
38          final Game game = Client.create().resource(builder.build()).path("game").queryParam("expand", "players").get(Game.class);
39          assertEquals("Rugby", game.getName());
40          assertEquals(5, game.getPlayers().getPlayers().size());
41          assertEquals(5, game.getPlayers().getMaxResults());
42          assertEquals(10, game.getPlayers().getSize());
43          assertEquals(0, game.getPlayers().getStartIndex().intValue());
44  
45          assertEquals(0, game.getPlayers().getPlayers().get(0).getId());
46          assertEquals(4, game.getPlayers().getPlayers().get(4).getId());
47      }
48  
49      @Test
50      public void testGetGameWithExpandedPlayersAndOffset()
51      {
52          final Game game = Client.create().resource(builder.build()).path("game").queryParam("expand", "players[3:]").get(Game.class);
53          assertEquals("Rugby", game.getName());
54          assertEquals(5, game.getPlayers().getPlayers().size());
55          assertEquals(5, game.getPlayers().getMaxResults());
56          assertEquals(10, game.getPlayers().getSize());
57          assertEquals(3, game.getPlayers().getStartIndex().intValue());
58  
59          assertEquals(3, game.getPlayers().getPlayers().get(0).getId());
60          assertEquals(7, game.getPlayers().getPlayers().get(4).getId());
61      }
62  
63      @Test
64      public void testGetGameWithExpandedPlayersAndOffsetOutOfBounds()
65      {
66          final Game game = Client.create().resource(builder.build()).path("game").queryParam("expand", "players[12:]").get(Game.class);
67          assertEquals("Rugby", game.getName());
68          assertNull(game.getPlayers().getPlayers());
69          assertEquals(5, game.getPlayers().getMaxResults());
70          assertEquals(10, game.getPlayers().getSize());
71          assertNull(game.getPlayers().getStartIndex());
72      }
73  
74      @Test
75      public void testGetPlayerNames()
76      {
77          final Player player1 = Client.create().resource(builder.build()).path("player").path("0").get(Player.class);
78          final Player player2 = Client.create().resource(builder.build()).path("player").path("1").get(Player.class);
79          assertEquals("Adam Ashley-Cooper", player1.getFullName());
80          assertEquals("Matt Giteau", player2.getFullName());
81      }
82  
83      @Test
84      public void testPlayerWithRecordCanBeExpanded()
85      {
86          // get without expansion
87          Player player = Client.create().resource(builder.build()).path("player").path("2").get(Player.class);
88          Assert.assertNotNull(player.getRecord());
89          // now expand it
90          player = Client.create().resource(builder.param("expand", "record").build()).path("player").path("2").get(Player.class);
91          Assert.assertNotNull(player.getRecord());
92          Assert.assertEquals((Integer) 513, player.getRecord().getPointsScored());
93      }
94  
95      @Test
96      public void testPlayerWithNoRecordCanNotBeExpanded()
97      {
98          // get without expansion
99          Player player = Client.create().resource(builder.build()).path("player").path("1").get(Player.class);
100         Assert.assertNull(player.getRecord());
101         // now expand it
102         player = Client.create().resource(builder.param("expand", "record").build()).path("player").path("1").get(Player.class);
103         Assert.assertNull(player.getRecord());
104     }
105 
106 }