diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-07-09 21:49:15 (GMT) |
---|---|---|
committer | Nick Hudson <nick.hudson@isode.com> | 2015-07-22 16:23:29 (GMT) |
commit | 2341df7c72714df988b39cb2019562987ea28cbb (patch) | |
tree | 266eceb8732a75ff5fc78ac20b0ba7b5fa191267 | |
parent | 7f96c8b224099f1c0e5c6996984fe79558e0d550 (diff) | |
download | stroke-2341df7c72714df988b39cb2019562987ea28cbb.zip stroke-2341df7c72714df988b39cb2019562987ea28cbb.tar.bz2 |
Complete VCard functionalities.
Updates VCardManager, VCardMemoryStorage and VCardStorage.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Test added for VCardManager, which passes.
Change-Id: I0d4f1dbb647f262ff2a8967807fb798a8181b0b5
-rw-r--r-- | src/com/isode/stroke/vcards/VCardManager.java | 19 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardMemoryStorage.java | 14 | ||||
-rw-r--r-- | src/com/isode/stroke/vcards/VCardStorage.java | 2 | ||||
-rw-r--r-- | test/com/isode/stroke/vcards/VCardManagerTest.java | 233 |
4 files changed, 263 insertions, 5 deletions
diff --git a/src/com/isode/stroke/vcards/VCardManager.java b/src/com/isode/stroke/vcards/VCardManager.java index 2a7837c..a5b62e8 100644 --- a/src/com/isode/stroke/vcards/VCardManager.java +++ b/src/com/isode/stroke/vcards/VCardManager.java @@ -14,12 +14,14 @@ import com.isode.stroke.queries.IQRouter; import com.isode.stroke.signals.Signal1; import com.isode.stroke.signals.Signal2; import com.isode.stroke.signals.Slot2; +import java.util.Date; +import java.util.TimeZone; public class VCardManager { - private final JID ownJID; - private final IQRouter iqRouter; - private final VCardStorage storage; - private final Set<JID> requestedVCards = new HashSet<JID>(); + private JID ownJID = new JID(); + private IQRouter iqRouter; + private VCardStorage storage; + private Set<JID> requestedVCards = new HashSet<JID>(); /** * The JID will always be bare. @@ -37,6 +39,7 @@ public class VCardManager { this.ownJID = ownJID; this.iqRouter = iqRouter; this.storage = vcardStorage; + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } public void delete() { @@ -47,8 +50,14 @@ public class VCardManager { } public VCard getVCardAndRequestWhenNeeded(final JID jid) { + return getVCardAndRequestWhenNeeded(jid, null); + } + + public VCard getVCardAndRequestWhenNeeded(final JID jid, final Date allowedAge) { VCard vcard = storage.getVCard(jid); - if (vcard == null) { + Date vcardFetchedTime = storage.getVCardWriteTime(jid); + boolean vcardTooOld = (vcard != null) && (vcardFetchedTime == null || (allowedAge != null && ((new Date().getTime() - vcardFetchedTime.getTime()) > allowedAge.getTime()))); + if (vcard == null || vcardTooOld) { requestVCard(jid); } return vcard; diff --git a/src/com/isode/stroke/vcards/VCardMemoryStorage.java b/src/com/isode/stroke/vcards/VCardMemoryStorage.java index d666d98..24ca6f4 100644 --- a/src/com/isode/stroke/vcards/VCardMemoryStorage.java +++ b/src/com/isode/stroke/vcards/VCardMemoryStorage.java @@ -10,22 +10,36 @@ import java.util.Map; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; +import java.util.Date; +import java.util.TimeZone; public class VCardMemoryStorage extends VCardStorage { public VCardMemoryStorage(CryptoProvider crypto) { super(crypto); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); } private Map<JID, VCard> vcards = new HashMap<JID, VCard>(); + private Map<JID, Date> vcardWriteTimes = new HashMap<JID, Date>(); @Override public VCard getVCard(JID jid) { return vcards.get(jid); } + /** + * @param JID jid. + * @return Date, May be Null. + */ + @Override + public Date getVCardWriteTime(final JID jid) { + return vcardWriteTimes.get(jid); + } + @Override public void setVCard(JID jid, VCard vcard) { vcards.put(jid, vcard); + vcardWriteTimes.put(jid, new Date()); } } diff --git a/src/com/isode/stroke/vcards/VCardStorage.java b/src/com/isode/stroke/vcards/VCardStorage.java index 9021599..cb732ea 100644 --- a/src/com/isode/stroke/vcards/VCardStorage.java +++ b/src/com/isode/stroke/vcards/VCardStorage.java @@ -8,11 +8,13 @@ import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; import com.isode.stroke.stringcodecs.Hexify; +import java.util.Date; public abstract class VCardStorage { private CryptoProvider crypto; public abstract VCard getVCard(JID jid); + public abstract Date getVCardWriteTime(JID jid); public abstract void setVCard(JID jid, VCard vcard); public VCardStorage(CryptoProvider crypto) { diff --git a/test/com/isode/stroke/vcards/VCardManagerTest.java b/test/com/isode/stroke/vcards/VCardManagerTest.java new file mode 100644 index 0000000..4c51081 --- /dev/null +++ b/test/com/isode/stroke/vcards/VCardManagerTest.java @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2010-2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.vcards; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.vcards.VCardManager; +import com.isode.stroke.vcards.VCardMemoryStorage; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.client.DummyStanzaChannel; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.jid.JID; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.Slot2; +import java.util.Vector; + +public class VCardManagerTest { + + private JID ownJID; + private DummyStanzaChannel stanzaChannel; + private IQRouter iqRouter; + private VCardMemoryStorage vcardStorage; + private Vector<Pair> changes = new Vector<Pair>(); + private Vector<VCard> ownChanges = new Vector<VCard>(); + private CryptoProvider crypto; + + private class Pair { + public JID jid; + public VCard vcard; + + Pair(JID j, VCard v) {jid = j; vcard = v;} + + @Override + public boolean equals(Object o) { + if (!(o instanceof Pair)) return false; + Pair o1 = (Pair) o; + return jid.equals(o1.jid) && vcard.equals(o1.vcard); + } + + @Override public int hashCode() {return jid.hashCode() * 5 + vcard.hashCode();} + } + + @Before + public void setUp() { + ownJID = new JID("baz@fum.com/dum"); + crypto = new JavaCryptoProvider(); + stanzaChannel = new DummyStanzaChannel(); + iqRouter = new IQRouter(stanzaChannel); + vcardStorage = new VCardMemoryStorage(crypto); + } + + private VCardManager createManager() { + VCardManager manager = new VCardManager(ownJID, iqRouter, vcardStorage); + manager.onVCardChanged.connect(new Slot2<JID, VCard>() { + @Override + public void call(JID j1, VCard v1) { + handleVCardChanged(j1, v1); + } + }); + manager.onOwnVCardChanged.connect(new Slot1<VCard>() { + @Override + public void call(VCard v1) { + handleOwnVCardChanged(v1); + } + }); + return manager; + } + + private void handleVCardChanged(final JID jid, VCard vcard) { + changes.add(new Pair(jid, vcard)); + } + + private void handleOwnVCardChanged(VCard vcard) { + ownChanges.add(vcard); + } + + private IQ createVCardResult() { + VCard vcard = new VCard(); + vcard.setFullName("Foo Bar"); + return IQ.createResult(new JID("baz@fum.com/dum"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), vcard); + } + + private IQ createOwnVCardResult() { + VCard vcard = new VCard(); + vcard.setFullName("Myself"); + return IQ.createResult(new JID(), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), vcard); + } + + private IQ createSetVCardResult() { + return IQ.createResult(new JID("baz@fum.com/dum"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), null); + } + + @Test + public void testGet_NewVCardRequestsVCard() { + VCardManager testling = createManager(); + VCard result = testling.getVCardAndRequestWhenNeeded(new JID("foo@bar.com/baz")); + + assertNull(result); + assertEquals(1, (stanzaChannel.sentStanzas.size())); + assertTrue(stanzaChannel.isRequestAtIndex(0, new JID("foo@bar.com/baz"), IQ.Type.Get, new VCard())); + } + + @Test + public void testGet_ExistingVCard() { + VCardManager testling = createManager(); + VCard vcard = new VCard(); + vcard.setFullName("Foo Bar"); + vcardStorage.setVCard(new JID("foo@bar.com/baz"), vcard); + + VCard result = testling.getVCardAndRequestWhenNeeded(new JID("foo@bar.com/baz")); + + assertEquals("Foo Bar", result.getFullName()); + assertEquals(0, (stanzaChannel.sentStanzas.size())); + } + + @Test + public void testRequest_RequestsVCard() { + VCardManager testling = createManager(); + testling.requestVCard(new JID("foo@bar.com/baz")); + + assertEquals(1, (stanzaChannel.sentStanzas.size())); + assertTrue(stanzaChannel.isRequestAtIndex(0, new JID("foo@bar.com/baz"), IQ.Type.Get, new VCard())); + } + + @Test + public void testRequest_ReceiveEmitsNotification() { + VCardManager testling = createManager(); + testling.requestVCard(new JID("foo@bar.com/baz")); + stanzaChannel.onIQReceived.emit(createVCardResult()); + + assertEquals(1, (changes.size())); + assertEquals(new JID("foo@bar.com/baz"), changes.get(0).jid); + assertEquals(("Foo Bar"), changes.get(0).vcard.getFullName()); + assertEquals(("Foo Bar"), vcardStorage.getVCard(new JID("foo@bar.com/baz")).getFullName()); + + assertEquals(0, (ownChanges.size())); + } + + @Test + public void testRequest_Error() { + VCardManager testling = createManager(); + testling.requestVCard(new JID("foo@bar.com/baz")); + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID())); + + assertEquals(1, (changes.size())); + assertEquals(new JID("foo@bar.com/baz"), changes.get(0).jid); + assertEquals((""), changes.get(0).vcard.getFullName()); + assertEquals((""), vcardStorage.getVCard(new JID("foo@bar.com/baz")).getFullName()); + } + + @Test + public void testRequest_VCardAlreadyRequested() { + VCardManager testling = createManager(); + testling.requestVCard(new JID("foo@bar.com/baz")); + VCard result = testling.getVCardAndRequestWhenNeeded(new JID("foo@bar.com/baz")); + + assertNull(result); + assertEquals(1, (stanzaChannel.sentStanzas.size())); + } + + @Test + public void testRequest_AfterPreviousRequest() { + VCardManager testling = createManager(); + testling.requestVCard(new JID("foo@bar.com/baz")); + stanzaChannel.onIQReceived.emit(createVCardResult()); + testling.requestVCard(new JID("foo@bar.com/baz")); + + assertEquals(2, (stanzaChannel.sentStanzas.size())); + assertTrue(stanzaChannel.isRequestAtIndex(1, new JID("foo@bar.com/baz"), IQ.Type.Get, new VCard())); + } + + @Test + public void testRequestOwnVCard() { + VCardManager testling = createManager(); + testling.requestVCard(ownJID); + stanzaChannel.onIQReceived.emit(createOwnVCardResult()); + + assertEquals(1, (stanzaChannel.sentStanzas.size())); + assertTrue(stanzaChannel.isRequestAtIndex(0, new JID(), IQ.Type.Get, new VCard())); + assertEquals(1, (changes.size())); + assertEquals(ownJID.toBare(), changes.get(0).jid); + assertEquals(("Myself"), changes.get(0).vcard.getFullName()); + assertEquals(("Myself"), vcardStorage.getVCard(ownJID.toBare()).getFullName()); + + assertEquals(1, (ownChanges.size())); + assertEquals(("Myself"), ownChanges.get(0).getFullName()); + } + + @Test + public void testCreateSetVCardRequest() { + VCardManager testling = createManager(); + VCard vcard = new VCard(); + vcard.setFullName("New Name"); + SetVCardRequest request = testling.createSetVCardRequest(vcard); + request.send(); + + stanzaChannel.onIQReceived.emit(createSetVCardResult()); + + assertEquals(1, (changes.size())); + assertEquals(ownJID.toBare(), changes.get(0).jid); + assertEquals(("New Name"), changes.get(0).vcard.getFullName()); + } + + @Test + public void testCreateSetVCardRequest_Error() { + VCardManager testling = createManager(); + VCard vcard = new VCard(); + vcard.setFullName("New Name"); + SetVCardRequest request = testling.createSetVCardRequest(vcard); + request.send(); + + stanzaChannel.onIQReceived.emit(IQ.createError(new JID("baz@fum.com/foo"), stanzaChannel.sentStanzas.get(0).getID())); + + assertEquals(0, (changes.size())); + } +}
\ No newline at end of file |