package net.cloudcodex.server.test;
import java.util.Date;
import junit.framework.TestCase;
import net.cloudcodex.server.Context;
import net.cloudcodex.server.data.AdvancedDAO;
import net.cloudcodex.server.data.Data;
import net.cloudcodex.server.data.Data.Campaign;
import net.cloudcodex.server.data.Data.Scene;
import net.cloudcodex.server.service.CampaignService;
import net.cloudcodex.server.service.HomeService;
import net.cloudcodex.server.service.MessageService;
import net.cloudcodex.shared.Errors;
import net.cloudcodex.shared.MessageAction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
/**
* jUnit Test for posting methods for players.
*/
public class PlayerPostActionMessageTest extends TestCase {
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
private CampaignService campaignService;
private MessageService messageService;
private HomeService homeService;
private AdvancedDAO dao;
private Data.User master;
private Data.User player1;
private Data.User player2;
private Data.User player3;
private Campaign campaign;
private long campaignId;
private Data.Character char1;
private long char1Id;
private Data.Character char2;
private long char2Id;
private Data.Character char3;
private long char3Id;
private Data.Character npc1;
private long npc1Id;
private Scene scene;
private Scene scene2;
@Before
public void setUp() {
helper.setUp();
DatastoreService store = DatastoreServiceFactory.getDatastoreService();
campaignService = new CampaignService(store);
messageService = new MessageService(store);
homeService = new HomeService(store);
dao = new AdvancedDAO(store);
master = homeService.createUser(null, "master@test.com", "Master");
player1 = homeService.createUser(null, "player1@test.com", "Player1");
player2 = homeService.createUser(null, "player2@test.com", "Player2");
player3 = homeService.createUser(null, "player3@test.com", "Player3");
campaign = campaignService.createCampaign(null, "Test Campaign", "Some game", "Some desc", null, master);
campaignId = campaign.getKey().getId();
char1 = campaignService.createCharacter(null, campaign, "Char1", player1, null, null);
char1Id = char1.getKey().getId();
char2 = campaignService.createCharacter(null, campaign, "Char2", player2, null, null);
char2Id = char2.getKey().getId();
char3 = campaignService.createCharacter(null, campaign, "Char3", player3, null, null);
char3Id = char3.getKey().getId();
npc1 = campaignService.createCharacter(null, campaign, "NPC1", null, null, null);
npc1Id = npc1.getKey().getId();
Context context = new Context(master);
scene = messageService.startScene(context, "introduction",
char1.getKey(), char2.getKey());
assertNotNull(scene);
scene2 = messageService.startScene(context, "introduction", char3.getKey());
assertNotNull(scene2);
}
@After
public void tearDown() {
helper.tearDown();
campaignService = null;
homeService = null;
dao = null;
}
@Test
public void testPostingOnInvalidCampaign() {
// try to post in an invalid campaign.
Context context = new Context(player1);
boolean result = messageService.playerPostAction(
context, -campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, "a speech");
assertFalse(result);
assertTrue(context.hasError(Errors.NOT_FOUND));
}
@Test
public void testPostingOnInvalidScene() {
// try to post in an invalid scene
Context context = new Context(player1);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId() + 1000, scene.getTimestamp(),
MessageAction.SPEECH, "a speech");
assertFalse(result);
assertTrue(context.hasError(Errors.OUTOFDATE)); // scene is checked after
}
@Test
public void testOnAnotherScene() {
// try to post in a scene of others players (note that it throws OUTOFDATE error)
Context context = new Context(player1);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene2.getKey().getId(), scene2.getTimestamp(),
MessageAction.SPEECH, "a speech");
assertFalse(result);
assertTrue(context.hasError(Errors.OUTOFDATE));
}
@Test
public void testPostingWithObsoleteTimestamp() throws Exception {
// try to post after a scene update.
Context context = new Context(player1);
final Date oldTimestamp = scene.getTimestamp();
Thread.currentThread().sleep(1000);
scene.setIntroduction("Introduction updated");
dao.save(context, scene);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), oldTimestamp,
MessageAction.SPEECH, "a speech");
assertFalse(result);
assertTrue(context.hasError(Errors.OUTOFDATE));
}
@Test
public void testPostingWithoutRequiredParams() {
// Check required params
Context context = new Context(player1);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), null,
MessageAction.SPEECH, "a speech");
assertFalse(result);
assertTrue(context.hasError(Errors.REQUIRED));
context = new Context(player1);
result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
null, "a speech");
assertFalse(result);
assertTrue(context.hasError(Errors.REQUIRED));
context = new Context(player1);
result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, null);
assertFalse(result);
assertTrue(context.hasError(Errors.REQUIRED));
}
@Test
public void testPostingOnPausedScene() {
Context context = new Context(player1);
// Test with a paused scene.
scene.setPaused(Boolean.TRUE);
dao.save(context, scene);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, "action");
assertFalse(result);
assertTrue(context.hasError(Errors.IMPOSSIBLE_PAUSED));
}
@Test
public void testPostingOnClosedScene() {
// Test with a closed scene.
Context context = new Context(player1);
scene.setClosed(Boolean.TRUE);
dao.save(context, scene);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, "action");
assertFalse(result);
assertTrue(context.hasError(Errors.IMPOSSIBLE_CLOSED));
}
@Test
public void testPostingWithLockedCharacter() {
// Test with locked character.
Context context = new Context(player1);
char1.setLocked(true);
dao.save(context, char1);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, "action");
assertFalse(result);
assertTrue(context.hasError(Errors.IMPOSSIBLE_LOCKED));
}
@Test
public void testPostingWithDeadCharacter() {
// Test with dead character.
Context context = new Context(player1);
char1.setLocked(false);
char1.setDead(true);
dao.save(context, char1);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, "action");
assertFalse(result);
assertTrue(context.hasError(Errors.IMPOSSIBLE_DEAD));
}
@Test
public void testPostingAsAnotherPlayer() {
// Test with dead character.
Context context = new Context(player3);
boolean result = messageService.playerPostAction(
context, campaignId, char1Id,
scene.getKey().getId(), scene.getTimestamp(),
MessageAction.SPEECH, "action");
assertFalse(result);
assertTrue(context.hasError(Errors.USER_USURPATION_PC));
}
}