2013-02-07 15:19:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-02-17 23:58:40 +01:00
|
|
|
package org.jivesoftware.smackx.bytestreams.ibb;
|
|
|
|
|
2014-08-15 23:16:18 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
|
import org.jxmpp.jid.JidTestUtil;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for InBandBytestreamRequest.
|
|
|
|
*
|
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
|
|
|
public class InBandBytestreamRequestTest {
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
static final Jid initiatorJID = JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE;
|
|
|
|
static final Jid targetJID = JidTestUtil.FULL_JID_1_RESOURCE_1;
|
2014-02-17 23:58:40 +01:00
|
|
|
String sessionID = "session_id";
|
|
|
|
|
2014-03-10 09:45:50 +01:00
|
|
|
XMPPConnection connection;
|
2014-02-17 23:58:40 +01:00
|
|
|
InBandBytestreamManager byteStreamManager;
|
|
|
|
Open initBytestream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize fields used in the tests.
|
|
|
|
*/
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
|
|
|
|
// mock connection
|
2014-03-10 09:45:50 +01:00
|
|
|
connection = mock(XMPPConnection.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// initialize InBandBytestreamManager to get the InitiationListener
|
|
|
|
byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
|
|
|
|
|
|
|
|
// create a In-Band Bytestream open packet
|
|
|
|
initBytestream = new Open(sessionID, 4096);
|
|
|
|
initBytestream.setFrom(initiatorJID);
|
|
|
|
initBytestream.setTo(targetJID);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test reject() method.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2015-02-14 09:43:44 +01:00
|
|
|
public void shouldReplyWithErrorIfRequestIsRejected() throws NotConnectedException, InterruptedException {
|
2014-02-17 23:58:40 +01:00
|
|
|
InBandBytestreamRequest ibbRequest = new InBandBytestreamRequest(
|
|
|
|
byteStreamManager, initBytestream);
|
|
|
|
|
|
|
|
// reject request
|
|
|
|
ibbRequest.reject();
|
|
|
|
|
|
|
|
// capture reply to the In-Band Bytestream open request
|
|
|
|
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
|
2015-03-04 21:44:43 +01:00
|
|
|
verify(connection).sendStanza(argument.capture());
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// assert that reply is the correct error packet
|
|
|
|
assertEquals(initiatorJID, argument.getValue().getTo());
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.error, argument.getValue().getType());
|
2014-11-25 13:11:24 +01:00
|
|
|
assertEquals(XMPPError.Condition.not_acceptable,
|
2014-02-17 23:58:40 +01:00
|
|
|
argument.getValue().getError().getCondition());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test accept() method.
|
|
|
|
*
|
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldReturnSessionIfRequestIsAccepted() throws Exception {
|
|
|
|
InBandBytestreamRequest ibbRequest = new InBandBytestreamRequest(
|
|
|
|
byteStreamManager, initBytestream);
|
|
|
|
|
|
|
|
// accept request
|
|
|
|
InBandBytestreamSession session = ibbRequest.accept();
|
|
|
|
|
|
|
|
// capture reply to the In-Band Bytestream open request
|
|
|
|
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
|
2015-03-04 21:44:43 +01:00
|
|
|
verify(connection).sendStanza(argument.capture());
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// assert that reply is the correct acknowledgment packet
|
|
|
|
assertEquals(initiatorJID, argument.getValue().getTo());
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.result, argument.getValue().getType());
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
assertNotNull(session);
|
|
|
|
assertNotNull(session.getInputStream());
|
|
|
|
assertNotNull(session.getOutputStream());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|