}
@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()) {