Package com.calclab.emite.xep.vcard

Source Code of com.calclab.emite.xep.vcard.VCardManagerTests

/*
* ((e)) emite: A pure Google Web Toolkit XMPP library
* Copyright (c) 2008-2011 The Emite development team
*
* This file is part of Emite.
*
* Emite is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Emite is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Emite.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.calclab.emite.xep.vcard;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import com.calclab.emite.base.xml.XMLBuilder;
import com.calclab.emite.core.XmppURI;
import com.calclab.emite.core.stanzas.IQ;
import com.calclab.emite.core.stanzas.IQ.Type;
import com.calclab.emite.xep.vcard.VCard;
import com.calclab.emite.xep.vcard.VCardManager;
import com.calclab.emite.xep.vcard.VCardManagerImpl;
import com.calclab.emite.xtesting.XmppSessionTester;
import com.calclab.emite.xtesting.handlers.VCardResponseTestHandler;
import com.google.web.bindery.event.shared.SimpleEventBus;

public class VCardManagerTests {
  String VALID_VCARD = "<iq id='v1'\n" + "    to='stpeter@jabber.org/roundabout'\n" + "    type='result'>\n" + "  <vCard xmlns='vcard-temp'>\n"
      + "    <FN>Peter Saint-Andre</FN>\n" + "    <N>\n" + "      <FAMILY>Saint-Andre</FAMILY>\n" + "      <GIVEN>Peter</GIVEN>\n" + "      <MIDDLE/>\n"
      + "    </N>\n" + "    <NICKNAME>stpeter</NICKNAME>\n" + "    <URL>http://www.xmpp.org/xsf/people/stpeter.shtml</URL>\n"
      + "    <BDAY>1966-08-06</BDAY>\n" + "    <ORG>\n" + "      <ORGNAME>XMPP Standards Foundation</ORGNAME>\n" + "      <ORGUNIT/>\n" + "    </ORG>\n"
      + "    <TITLE>Executive Director</TITLE>\n" + "    <ROLE>Patron Saint</ROLE>\n" + "    <TEL><WORK/><VOICE/><NUMBER>303-308-3282</NUMBER></TEL>\n"
      + "    <TEL><WORK/><FAX/><NUMBER/></TEL>\n" + "    <TEL><WORK/><MSG/><NUMBER/></TEL>\n" + "    <ADR>\n" + "      <WORK/>\n"
      + "      <EXTADD>Suite 600</EXTADD>\n" + "      <STREET>1899 Wynkoop Street</STREET>\n" + "      <LOCALITY>Denver</LOCALITY>\n"
      + "      <REGION>CO</REGION>\n" + "      <PCODE>80202</PCODE>\n" + "      <CTRY>USA</CTRY>\n" + "    </ADR>\n"
      + "    <TEL><HOME/><VOICE/><NUMBER>303-555-1212</NUMBER></TEL>\n" + "    <TEL><HOME/><FAX/><NUMBER/></TEL>\n"
      + "    <TEL><HOME/><MSG/><NUMBER/></TEL>\n" + "    <ADR>\n" + "      <HOME/>\n" + "      <EXTADD/>\n" + "      <STREET/>\n"
      + "      <LOCALITY>Denver</LOCALITY>\n" + "      <REGION>CO</REGION>\n" + "      <PCODE>80209</PCODE>\n" + "      <CTRY>USA</CTRY>\n"
      + "    </ADR>\n" + "    <EMAIL><INTERNET/><PREF/><USERID>stpeter@jabber.org</USERID></EMAIL>\n" + "    <JABBERID>stpeter@jabber.org</JABBERID>\n"
      + "    <DESC>\n" + "      More information about me is located on my \n" + "      personal website: http://www.saint-andre.com/\n"
      + "    </DESC>\n" + "  </vCard>\n" + "</iq>";

  String OTHER_VALID_VCARD = "<iq type=\"result\" id=\"vcard_2\" to=\"admin@localhost/Gajim\">\n" + "<vCard xmlns=\"vcard-temp\">\n" + "<PHOTO>\n"
      + "<TYPE>jpeg</TYPE>\n" + "<BINVAL>/9j/4AAQSkZJRgABAQEASABIAAD/4QAWRXhpZgAATU0AKgAAAAgAAAAAAAD/2wBDAAUDBAQEAwUE\n"
      + "BAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/\n"
      + "wAALCAA8ACkBAREA/8QAHAAAAgMBAAMAAAAAAAAAAAAAAAQDBQYCAQcI/8QAMBAAAgEDAwQBAgIL\n"
      + "AAAAAAAAAQIDAAQRBRIhBhMxQVEikRVhBxQjMlNxcoGToeH/2gAIAQEAAD8A+y6KS1iW/S0ddNg7\n"
      + "lyUJQsQFBAJ9+84A/nnwDWXvour1BtY5Z3h3cSAosjKsn1sWAOMoBtUYOXwRtU0l0f11Lf63JpN5\n"
      + "2Eitn7TXDErvBQdtsknJYo5PwHjHk17CopFtRPcYQ2F3PGrMplTYFyuQQAWDHBBHApSLqbSpzClq\n"
      + "9zcSzgmGNLZwZAPOCwA49kkY909Bfo92trLBNbzOhdFk2/WoIBIKkjjI4PPNY270mxTrUP2zKiXA\n"
      + "nuSpOQ0hVUB4xjL/ALvBxk/1b2uZZEijaWV1SNBuZmOAAPZNZyTTb7U5pjHdXdlZkmS3kfY00bsC\n"
      + "GZAyFkBVmwS24biMAcVPp3TdlpNwJtNt4wTBHExkc727ZbaS/JbhyMH4GPGKetLCX8ROo3kyyTiM\n"
      + "xQogwkKEgsPliSoyTjhRgDnNDLc291cPLaRZbU7u3aCVBkTRwyqHz8YUOeccHjkVqNlz/Hj/AMf/\n"
      + "AGq2e6sn1KUaheQQpbSKIoZJAoZsAhznzycD0CM+cYL/AF0W3dZLR3ihALySOEBBJAKjliODzgDg\n"
      + "nOAatYJY54+5E4ZdxXI+QSCP7EEVHeQpd2s1o5PblRo3wcHBGOPvXdtDFbW8dvAgSKNQqKPAA8Cp\n"
      + "KKorgLNrslvLd3ETu6osaxxkSRiMtzuUnbneMjHLEVLo04Md1OFeO2lkEyK3O0MAWz6AOQ35bj+d\n"
      + "PafewXhmEAb9lIyPkYwwZgR9x/sU1RRQQD5ANYX9MCatcaC+l2Wp/h8GqodPEqRZZZZcqvPoEkLn\n"
      + "jGT5zxJ010kmj9U2t09/eqyWsqxW360ZY2GUDu5KrljlfIOcA5yK21FFFIa3YwalBDZ3VmLmAzxy\n"
      + "uGI2qY2DqSD5+pRxzUlsryyQ3d1AIZwjRhA27bk5POPB2qabooope+tRddnM88PalEgMT7dxAPB+\n" + "Rz4915hhVQiOzS9nGxn5bO3Gc/OCfvU9Ff/Z</BINVAL>\n"
      + "</PHOTO>\n" + "<NICKNAME>ad</NICKNAME>\n" + "<FN>adminnnn</FN>\n" + "</vCard>\n" + "</iq>";

  String OTHER_VCARD = "<iq from='test@domain' to='test2@domain'" + "type='get'><vCard xmlns='vcard-temp'/></iq>";
  String VCARD_SETTED = "<iq type='set'><vCard xmlns='vcard-temp'><FN>Peter Saint-Andre</FN><N><FAMILY>Saint-Andre</FAMILY><GIVEN>Peter</GIVEN><MIDDLE/></N><NICKNAME>stpeter</NICKNAME></vCard></iq>";

  private XmppSessionTester session;
  private VCardManager manager;

  @Before
  public void setup() {
    session = new XmppSessionTester("test@domain");
    manager = new VCardManagerImpl(new SimpleEventBus(), session);
  }

  @Test
  public void shouldParseOtherVCard() {
    shouldParseVCardImpl(OTHER_VALID_VCARD);
  }

  @Test
  public void shouldParseVCard() {
    shouldParseVCardImpl(VALID_VCARD);
  }

  @Test
  public void shouldRequestVCard() {
    final VCardResponseTestHandler handler = new VCardResponseTestHandler();
    manager.getUserVCard(XmppURI.uri("test2@domain"), handler);
    session.verifyIQSent(OTHER_VCARD);
  }

  @Test
  public void shouldSendRetrievalRequest() {
    manager.requestOwnVCard(null);
    final IQ iq = new IQ(IQ.Type.get);
    iq.setFrom(XmppURI.uri("test@domain"));
    iq.addChild("vCard", "vcard-temp");
    session.verifyIQSent(iq);
  }

  @Test
  public void shouldSetOwnVCard() {
    final VCardResponseTestHandler handler = new VCardResponseTestHandler();
    final VCard vCard = new VCard();
    vCard.setDisplayName("Peter Saint-Andre");
    vCard.setFamilyName("Saint-Andre");
    vCard.setGivenName("Peter");
    vCard.setMiddleName("");
    vCard.setNickName("stpeter");
    manager.updateOwnVCard(vCard, handler);
    session.verifyIQSent(VCARD_SETTED);
  }

  private void shouldParseVCardImpl(final String vcard) {
    final VCardResponseTestHandler handler = new VCardResponseTestHandler();
    manager.requestOwnVCard(handler);
    session.verifyIQSent(new IQ(Type.get));
    session.answerSuccess(new IQ(XMLBuilder.fromXML(vcard)));
    assertTrue(handler.isCalledOnce());
    assertTrue(handler.getLastVCardResponse().hasVCard());
    assertNotNull(handler.getLastVCardResponse().getVCard().getNickName());
  }
}
TOP

Related Classes of com.calclab.emite.xep.vcard.VCardManagerTests

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.