package es.mahulo.battleship;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import es.mahulo.battleship.model.ArrayShip;
import es.mahulo.battleship.model.Cell;
import es.mahulo.battleship.model.Ship;
import es.mahulo.battleship.model.ShipConfig;
import es.mahulo.battleship.model.Shot;
public class GameServerRESTTest {
public void getGameList() throws JSONException {
Client c = Client.create();
WebResource r = c.resource("http://192.168.1.33:8080/BattleShip/rest/gameserver/games/1");
String result = r.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
JSONObject gamesJSON = new JSONObject(result);
JSONArray games = gamesJSON.getJSONArray("game");
for (int i=0 ; i< games.length();i++) {
JSONObject game = (JSONObject)games.get(i);
System.out.println(game.get("id")+ " " +game.get("status"));
}
}
public void createGame() {
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/server/creategame/1");
String result = r.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
System.out.println(result);
}
public void joinGame() {
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/server/join/1903/2");
String result = r.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
System.out.println(result);
}
public void addShipTest() {
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/game/addship/1802");
Ship ship = new Ship();
ArrayList<Cell> cells = new ArrayList<Cell>();
ship.setCells(cells);
ArrayList<Ship> ships = new ArrayList<Ship>();
ships.add(ship);
ArrayShip arrayShip = new ArrayShip();
arrayShip.setShips(ships);
r.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, arrayShip);
}
public void shotTest() {
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/game/shot/1802");
Shot shot = new Shot();
shot.setX(1);
shot.setY(1);
r.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, shot);
}
public void configure1Test() {
ShipConfig shipConfig1 = new ShipConfig();
shipConfig1.setId(1L);
shipConfig1.setName("aircraft carrier");
shipConfig1.setNumber(1);
shipConfig1.setSize(5);
ShipConfig shipConfig2 = new ShipConfig();
shipConfig2.setId(2L);
shipConfig2.setName("battleship");
shipConfig2.setNumber(1);
shipConfig2.setSize(4);
ShipConfig shipConfig3 = new ShipConfig();
shipConfig3.setId(3L);
shipConfig3.setName("cruiser");
shipConfig3.setNumber(1);
shipConfig3.setSize(3);
ShipConfig shipConfig4 = new ShipConfig();
shipConfig4.setId(4L);
shipConfig4.setName("destroyer");
shipConfig4.setNumber(2);
shipConfig4.setSize(2);
ShipConfig shipConfig5 = new ShipConfig();
shipConfig5.setId(5L);
shipConfig5.setName("submarine");
shipConfig5.setNumber(2);
shipConfig5.setSize(1);
List<Ship> ships = new ArrayList<Ship>();
List<Cell> cells = new ArrayList<Cell>();
Cell cell1 = new Cell();
cell1.setX(9);
cell1.setY(1);
cells.add(cell1);
Cell cell2 = new Cell();
cell2.setX(9);
cell2.setY(2);
cells.add(cell2);
Cell cell3 = new Cell();
cell3.setX(9);
cell3.setY(3);
cells.add(cell3);
Cell cell4 = new Cell();
cell4.setX(9);
cell4.setY(4);
cells.add(cell4);
Cell cell5 = new Cell();
cell5.setX(9);
cell5.setY(5);
cells.add(cell5);
Ship ship1 = new Ship();
ship1.setShipConfig(shipConfig1);
ship1.setCells(cells);
ships.add(ship1);
cells = new ArrayList<Cell>();
Cell cell6 = new Cell();
cell6.setX(7);
cell6.setY(4);
cells.add(cell6);
Cell cell7 = new Cell();
cell7.setX(7);
cell7.setY(5);
cells.add(cell7);
Cell cell8 = new Cell();
cell8.setX(7);
cell8.setY(6);
cells.add(cell8);
Cell cell9 = new Cell();
cell9.setX(7);
cell9.setY(7);
cells.add(cell9);
Ship ship2 = new Ship();
ship2.setShipConfig(shipConfig2);
ship2.setCells(cells);
ships.add(ship2);
cells = new ArrayList<Cell>();
Cell cell11 = new Cell();
cell11.setX(2);
cell11.setY(6);
cells.add(cell11);
Cell cell12 = new Cell();
cell12.setX(3);
cell12.setY(6);
cells.add(cell12);
Cell cell13 = new Cell();
cell13.setX(4);
cell13.setY(6);
cells.add(cell13);
Ship ship3 = new Ship();
ship3.setShipConfig(shipConfig3);
ship3.setCells(cells);
ships.add(ship3);
cells = new ArrayList<Cell>();
Cell cell14 = new Cell();
cell14.setX(1);
cell14.setY(1);
cells.add(cell14);
Cell cell15 = new Cell();
cell15.setX(1);
cell15.setY(2);
cells.add(cell15);
Ship ship4 = new Ship();
ship4.setShipConfig(shipConfig4);
ship4.setCells(cells);
ships.add(ship4);
cells = new ArrayList<Cell>();
Cell cell16 = new Cell();
cell16.setX(3);
cell16.setY(4);
cells.add(cell16);
Cell cell17 = new Cell();
cell17.setX(4);
cell17.setY(4);
cells.add(cell17);
Ship ship5 = new Ship();
ship5.setShipConfig(shipConfig4);
ship5.setCells(cells);
ships.add(ship5);
cells = new ArrayList<Cell>();
Cell cell18 = new Cell();
cell18.setX(3);
cell18.setY(2);
cells.add(cell18);
Ship ship6 = new Ship();
ship6.setShipConfig(shipConfig5);
ship6.setCells(cells);
ships.add(ship6);
cells = new ArrayList<Cell>();
Cell cell19 = new Cell();
cell19.setX(1);
cell19.setY(8);
cells.add(cell19);
Ship ship7 = new Ship();
ship7.setShipConfig(shipConfig5);
ship7.setCells(cells);
ships.add(ship7);
ArrayShip arrayShip = new ArrayShip();
arrayShip.setShips(ships);
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/game/addship/2201");
r.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, arrayShip);
}
@Test public void configure2Test() {
ShipConfig shipConfig1 = new ShipConfig();
shipConfig1.setId(1L);
shipConfig1.setName("aircraft carrier");
shipConfig1.setNumber(1);
shipConfig1.setSize(5);
ShipConfig shipConfig2 = new ShipConfig();
shipConfig2.setId(2L);
shipConfig2.setName("battleship");
shipConfig2.setNumber(1);
shipConfig2.setSize(4);
ShipConfig shipConfig3 = new ShipConfig();
shipConfig3.setId(3L);
shipConfig3.setName("cruiser");
shipConfig3.setNumber(1);
shipConfig3.setSize(3);
ShipConfig shipConfig4 = new ShipConfig();
shipConfig4.setId(4L);
shipConfig4.setName("destroyer");
shipConfig4.setNumber(2);
shipConfig4.setSize(2);
ShipConfig shipConfig5 = new ShipConfig();
shipConfig5.setId(5L);
shipConfig5.setName("submarine");
shipConfig5.setNumber(2);
shipConfig5.setSize(1);
List<Ship> ships = new ArrayList<Ship>();
List<Cell> cells = new ArrayList<Cell>();
Cell cell1 = new Cell();
cell1.setX(9);
cell1.setY(1);
cells.add(cell1);
Cell cell2 = new Cell();
cell2.setX(9);
cell2.setY(2);
cells.add(cell2);
Cell cell3 = new Cell();
cell3.setX(9);
cell3.setY(3);
cells.add(cell3);
Cell cell4 = new Cell();
cell4.setX(9);
cell4.setY(4);
cells.add(cell4);
Cell cell5 = new Cell();
cell5.setX(9);
cell5.setY(5);
cells.add(cell5);
Ship ship1 = new Ship();
ship1.setShipConfig(shipConfig1);
ship1.setCells(cells);
ships.add(ship1);
cells = new ArrayList<Cell>();
Cell cell6 = new Cell();
cell6.setX(7);
cell6.setY(4);
cells.add(cell6);
Cell cell7 = new Cell();
cell7.setX(7);
cell7.setY(5);
cells.add(cell7);
Cell cell8 = new Cell();
cell8.setX(7);
cell8.setY(6);
cells.add(cell8);
Cell cell9 = new Cell();
cell9.setX(7);
cell9.setY(7);
cells.add(cell9);
Ship ship2 = new Ship();
ship2.setShipConfig(shipConfig2);
ship2.setCells(cells);
ships.add(ship2);
cells = new ArrayList<Cell>();
Cell cell11 = new Cell();
cell11.setX(2);
cell11.setY(6);
cells.add(cell11);
Cell cell12 = new Cell();
cell12.setX(3);
cell12.setY(6);
cells.add(cell12);
Cell cell13 = new Cell();
cell13.setX(4);
cell13.setY(6);
cells.add(cell13);
Ship ship3 = new Ship();
ship3.setShipConfig(shipConfig3);
ship3.setCells(cells);
ships.add(ship3);
cells = new ArrayList<Cell>();
Cell cell14 = new Cell();
cell14.setX(1);
cell14.setY(1);
cells.add(cell14);
Cell cell15 = new Cell();
cell15.setX(1);
cell15.setY(2);
cells.add(cell15);
Ship ship4 = new Ship();
ship4.setShipConfig(shipConfig4);
ship4.setCells(cells);
ships.add(ship4);
cells = new ArrayList<Cell>();
Cell cell16 = new Cell();
cell16.setX(3);
cell16.setY(4);
cells.add(cell16);
Cell cell17 = new Cell();
cell17.setX(4);
cell17.setY(4);
cells.add(cell17);
Ship ship5 = new Ship();
ship5.setShipConfig(shipConfig4);
ship5.setCells(cells);
ships.add(ship5);
cells = new ArrayList<Cell>();
Cell cell18 = new Cell();
cell18.setX(3);
cell18.setY(2);
cells.add(cell18);
Ship ship6 = new Ship();
ship6.setShipConfig(shipConfig5);
ship6.setCells(cells);
ships.add(ship6);
cells = new ArrayList<Cell>();
Cell cell19 = new Cell();
cell19.setX(1);
cell19.setY(8);
cells.add(cell19);
Ship ship7 = new Ship();
ship7.setShipConfig(shipConfig5);
ship7.setCells(cells);
ships.add(ship7);
ArrayShip arrayShip = new ArrayShip();
arrayShip.setShips(ships);
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/game/addship/2251");
r.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, arrayShip);
}
@Test public void shot1Test() {
Client c = Client.create();
WebResource r = c.resource("http://localhost:8080/BattleShip/rest/game/shot/2251");
Shot shot = new Shot();
shot.setX(1);
shot.setY(1);
r.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, shot);
}
}