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 org.jivesoftware.smack.packet.IQ;
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement this interface to verify a request/response pair.
|
|
|
|
* <p>
|
|
|
|
* For convenience there are some useful predefined implementations.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param <T> class of the request
|
|
|
|
* @param <S> class of the response
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public interface Verification<T extends Stanza, S extends Stanza> {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that the "To" field of the request corresponds with the "From" field of
|
|
|
|
* the response.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
Verification<Stanza, Stanza> correspondingSenderReceiver = new Verification<Stanza, Stanza>() {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
public void verify(Stanza request, Stanza response) {
|
2014-02-17 23:58:40 +01:00
|
|
|
assertEquals(response.getFrom(), request.getTo());
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that the type of the request is a GET.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
Verification<IQ, Stanza> requestTypeGET = new Verification<IQ, Stanza>() {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
public void verify(IQ request, Stanza response) {
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.get, request.getType());
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that the type of the request is a SET.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
Verification<IQ, Stanza> requestTypeSET = new Verification<IQ, Stanza>() {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
public void verify(IQ request, Stanza response) {
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.set, request.getType());
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that the type of the request is a RESULT.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
Verification<IQ, Stanza> requestTypeRESULT = new Verification<IQ, Stanza>() {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
public void verify(IQ request, Stanza response) {
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.result, request.getType());
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that the type of the request is an ERROR.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
Verification<IQ, Stanza> requestTypeERROR = new Verification<IQ, Stanza>() {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
public void verify(IQ request, Stanza response) {
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.error, request.getType());
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement this method to make assertions of the request/response pairs.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param request the request collected by the mocked XMPP connection
|
|
|
|
* @param response the response added to the protocol instance
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
void verify(T request, S response);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
}
|