2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.util;
|
|
|
|
|
2020-04-06 13:25:07 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Queue;
|
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2018-04-18 13:16:57 +02:00
|
|
|
import org.jivesoftware.smack.util.XmlUtil;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class can be used in conjunction with a mocked XMPP connection (
|
2015-05-27 19:29:51 +02:00
|
|
|
* {@link ConnectionUtils#createMockedConnection(Protocol, org.jxmpp.jid.EntityFullJid, org.jxmpp.jid.DomainBareJid)}) to
|
2015-02-12 12:13:19 +01:00
|
|
|
* verify an XMPP protocol. This can be accomplished in the following was:
|
2014-02-17 23:58:40 +01:00
|
|
|
* <ul>
|
|
|
|
* <li>add responses to packets sent over the mocked XMPP connection by the
|
|
|
|
* method to test in the order the tested method awaits them</li>
|
|
|
|
* <li>call the method to test</li>
|
|
|
|
* <li>call {@link #verifyAll()} to run assertions on the request/response pairs
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* Example:
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* <pre>
|
|
|
|
* <code>
|
|
|
|
* public void methodToTest() {
|
2018-04-18 13:20:23 +02:00
|
|
|
* Stanza stanza = new Packet(); // create an XMPP packet
|
2017-01-03 11:12:34 +01:00
|
|
|
* StanzaCollector collector = connection.createStanzaCollector(new StanzaIdFilter());
|
2015-03-04 21:44:43 +01:00
|
|
|
* connection.sendStanza(packet);
|
2018-04-18 13:20:23 +02:00
|
|
|
* Stanza reply = collector.nextResult();
|
2014-02-17 23:58:40 +01:00
|
|
|
* }
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* public void testMethod() {
|
2018-04-18 13:20:23 +02:00
|
|
|
* EntityFullJid userJid = JidCreate.entityFullFrom("user@xmpp-server.org");
|
|
|
|
* DomainBareJid serverJid = JidCreate.domainBareFrom("user-server.org");
|
2014-02-17 23:58:40 +01:00
|
|
|
* // create protocol
|
|
|
|
* Protocol protocol = new Protocol();
|
|
|
|
* // create mocked connection
|
2018-04-18 13:20:23 +02:00
|
|
|
* XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, userJid, serverJid);
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2018-03-31 14:17:30 +02:00
|
|
|
* // add reply stanza to protocol
|
2018-04-18 13:20:23 +02:00
|
|
|
* Stanza reply = new Packet();
|
2014-02-17 23:58:40 +01:00
|
|
|
* protocol.add(reply);
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* // call method to test
|
|
|
|
* methodToTest();
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* // verify protocol
|
|
|
|
* protocol.verifyAll();
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
* </pre>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* Additionally to adding the response to the protocol instance you can pass
|
|
|
|
* verifications that will be executed when {@link #verifyAll()} is invoked.
|
|
|
|
* (See {@link Verification} for more details.)
|
|
|
|
* <p>
|
|
|
|
* If the {@link #printProtocol} flag is set to true {@link #verifyAll()} will
|
|
|
|
* also print out the XML messages in the order they are sent to the console.
|
|
|
|
* This may be useful to inspect the whole protocol "by hand".
|
2018-04-18 13:20:23 +02:00
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
|
|
|
public class Protocol {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to <code>true</code> to print XML messages to the console while
|
|
|
|
* verifying the protocol.
|
|
|
|
*/
|
|
|
|
public boolean printProtocol = false;
|
|
|
|
|
|
|
|
// responses to requests are taken form this queue
|
2017-12-13 23:10:11 +01:00
|
|
|
private final Queue<Stanza> responses = new LinkedList<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// list of verifications
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<Verification<?, ?>[]> verificationList = new ArrayList<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// list of requests
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<Stanza> requests = new ArrayList<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// list of all responses
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<Stanza> responsesList = new ArrayList<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a responses and all verifications for the request/response pair to
|
|
|
|
* the protocol.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param response the response for a request
|
|
|
|
* @param verifications verifications for request/response pair
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public void addResponse(Stanza response, Verification<?, ?>... verifications) {
|
2014-02-17 23:58:40 +01:00
|
|
|
responses.offer(response);
|
|
|
|
verificationList.add(verifications);
|
|
|
|
responsesList.add(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies the request/response pairs by checking if their numbers match
|
|
|
|
* and executes the verification for each pair.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void verifyAll() {
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:OFF
|
2014-02-17 23:58:40 +01:00
|
|
|
assertEquals(requests.size(), responsesList.size());
|
|
|
|
|
|
|
|
if (printProtocol)
|
|
|
|
System.out.println("=================== Start ===============\n");
|
|
|
|
|
|
|
|
for (int i = 0; i < requests.size(); i++) {
|
2015-02-05 11:17:27 +01:00
|
|
|
Stanza request = requests.get(i);
|
|
|
|
Stanza response = responsesList.get(i);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
if (printProtocol) {
|
|
|
|
System.out.println("------------------- Request -------------\n");
|
2019-02-04 13:27:41 +01:00
|
|
|
System.out.println(XmlUtil.prettyFormatXml(request.toXML()));
|
2014-02-17 23:58:40 +01:00
|
|
|
System.out.println("------------------- Response ------------\n");
|
|
|
|
if (response != null) {
|
2019-02-04 13:27:41 +01:00
|
|
|
System.out.println(XmlUtil.prettyFormatXml(response.toXML()));
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.println("No response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
Verification<Stanza, Stanza>[] verifications = (Verification<Stanza, Stanza>[]) verificationList.get(i);
|
2014-02-17 23:58:40 +01:00
|
|
|
if (verifications != null) {
|
2015-02-05 11:17:27 +01:00
|
|
|
for (Verification<Stanza, Stanza> verification : verifications) {
|
2014-02-17 23:58:40 +01:00
|
|
|
verification.verify(request, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (printProtocol)
|
|
|
|
System.out.println("=================== End =================\n");
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:ON
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the responses queue.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @return the responses queue
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
protected Queue<Stanza> getResponses() {
|
2014-02-17 23:58:40 +01:00
|
|
|
return responses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of all collected requests.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @return list of requests
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public List<Stanza> getRequests() {
|
2014-02-17 23:58:40 +01:00
|
|
|
return requests;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|