Package net.cloudcodex.server.test

Source Code of net.cloudcodex.server.test.CharacterDescriptionTest

package net.cloudcodex.server.test;
import java.util.HashMap;
import java.util.Map;

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.CharacterNote;
import net.cloudcodex.server.data.campaign.character.CharacterDescriptionSDO;
import net.cloudcodex.server.data.campaign.character.CharacterSDO;
import net.cloudcodex.server.service.CampaignService;
import net.cloudcodex.server.service.HomeService;
import net.cloudcodex.shared.Errors;

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 Character Description read and write.
*/
public class CharacterDescriptionTest extends TestCase {

    private final LocalServiceTestHelper helper =
        new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

    private CampaignService campaignService;

    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;
   
    @Before
    public void setUp() {
        helper.setUp();
        DatastoreService store = DatastoreServiceFactory.getDatastoreService();
        campaignService = new CampaignService(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();
    }

    @After
    public void tearDown() {
        helper.tearDown();
        campaignService = null;
        homeService = null;
        dao = null;
    }

    /**
     * Test a GM can set the desc and sheet.
     * Test a player cannot do that.
     */
    @Test
    public void testSetDescAndSheet() {
     
      Context context = new Context(master);
     
      final String description = "This is a new description";
     
      final String sheet = "This is a new sheet";
     
      CharacterDescriptionSDO sdo =
        campaignService.updateCharacterDescription(context, campaignId, char1Id,
          null, description, sheet, null, null, null, null);

      assertEquals(sheet, sdo.getSheet());

      CharacterSDO char1 = campaignService.getCharacter(context, campaignId, char1Id);
      assertEquals(description, char1.getCharacter().getDescription());
      assertFalse(context.hasErrors());

      /*
       * not update them and test there still here
       */
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
              null, null, null, null, null, null, null);

      assertEquals(sheet, sdo.getSheet());

      char1 = campaignService.getCharacter(context, campaignId, char1Id);
      assertEquals(description, char1.getCharacter().getDescription());
      assertFalse(context.hasErrors());
     
      /*
       * Player can't do that.
       */
      context = new Context(player1);
     
      assertNull(campaignService.updateCharacterDescription(
          context, campaignId, char1Id,
          null, description, sheet, null, null, null, null));
     
      assertTrue(context.hasError(Errors.USER_USURPATION_GM));
    }
 
    @Test
    public void testSetAliases() {

      Context context = new Context(master);
     
      final Map<Long, String> aliases = new HashMap<Long, String>();
      aliases.put(char2Id, "Alias1ForChar2");
      aliases.put(null, "GlobalAlias");
     
      CharacterDescriptionSDO sdo =
        campaignService.updateCharacterDescription(context, campaignId, char1Id,
          null, null, null, null, null, null, aliases);

      sdo.getAliases().equals(aliases);
      assertFalse(context.hasErrors());

      /*
       * Don't update them and test they're still here
       */
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
              null, null, null, null, null, null, null);

      sdo.getAliases().equals(aliases);
      assertFalse(context.hasErrors());

      /*
       * Add one and modify one and test they're ALL still here, even the not modified
       */
      final Map<Long, String> aliases2 = new HashMap<Long, String>();
      aliases.put(char2Id, "Alias2ForChar2");
      aliases.put(char3Id, "Alias1ForChar3");
     
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
              null, null, null, null, null, null, aliases2);
      aliases.putAll(aliases2);
      sdo.getAliases().equals(aliases);
      assertFalse(context.hasErrors());

     
      /*
       * Player can't do that.
       */
      context = new Context(player1);
     
      assertNull(campaignService.updateCharacterDescription(
          context, campaignId, char1Id,
          null, null, null, null, null, null, aliases));
     
      assertTrue(context.hasError(Errors.USER_USURPATION_GM));
    }
   
    @Test
    public void testSetNotes() {

      Context context = new Context(master);
     
      final Map<Long, String> notes = new HashMap<Long, String>();
      notes.put(char2Id, "I don't like Char1");
      notes.put(null, "Remember his wife is searching him");
     
      CharacterDescriptionSDO sdo =
        campaignService.updateCharacterDescription(context, campaignId, char1Id,
          null, null, null, null, null, notes, null);

      assertFalse(context.hasErrors());
     
      assertEquals(notes.size(), sdo.getNotes().size());
      for(CharacterNote note : sdo.getNotes()) {
        assertEquals(note.getContent(),
            notes.get(note.getAuthor() == null ? null : note.getAuthor().getId()));
      }
     
      /*
       * Add one, update one, test ALL are here.
       */
        final Map<Long, String> notes2 = new HashMap<Long, String>();
      notes2.put(char2Id, "I love Char1 now");
      notes2.put(char3Id, "Strange guy ...");

      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
          null, null, null, null, null, notes2, null);

      assertFalse(context.hasErrors());
     
      notes.putAll(notes2);
      assertEquals(notes.size(), sdo.getNotes().size());
      for(CharacterNote note : sdo.getNotes()) {
        assertEquals(note.getContent(),
            notes.get(note.getAuthor() == null ? null : note.getAuthor().getId()));
      }

      /*
       * Check a user cannot update as another user.
       */
        final Map<Long, String> notes3 = new HashMap<Long, String>();
      notes3.put(char2Id, "I change again !!!");
      notes3.put(char3Id, "Hihi ! it's a hack !");

      context = new Context(player2);
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
          char3Id, null, null, null, null, notes2, null);
      assertTrue(context.hasError(Errors.USER_USURPATION));

      context = new Context(player2);
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
          npc1Id, null, null, null, null, notes2, null);
      assertTrue(context.hasError(Errors.USER_USURPATION));

      context = new Context(player2);
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
          null, null, null, null, null, notes2, null);
      assertTrue(context.hasError(Errors.USER_USURPATION_GM));
     
      /*
       * Check a user can only update its own note.
       */
      context = new Context(player2);
     
      sdo = campaignService.updateCharacterDescription(context, campaignId, char1Id,
          char2Id, null, null, null, null, notes3, null);

      assertFalse(context.hasErrors());
      assertEquals(1, sdo.getNotes().size());
      assertEquals(notes3.get(char2Id), sdo.getNotes().get(0).getContent());
     
      // read with GM rights, to see all notes
      context = new Context(master);
      sdo = campaignService.getCharacterDescription(context, campaignId, char1Id, null, null);

      notes.put(char2Id, notes3.get(char2Id));
      assertEquals(notes.size(), sdo.getNotes().size());
      for(CharacterNote note : sdo.getNotes()) {
        assertEquals(note.getContent(),
            notes.get(note.getAuthor() == null ? null : note.getAuthor().getId()));
      }
    }
}
TOP

Related Classes of net.cloudcodex.server.test.CharacterDescriptionTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.