package nl.enovation.commons.monitor;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import javax.xml.bind.JAXBElement;
import nl.enovation.ems.archivewebservice.schema.Message;
import nl.enovation.ems.archivewebservice.schema.MessageInternalIdRquest;
import nl.enovation.ems.archivewebservice.schema.ObjectFactory;
import org.apache.commons.lang.math.RandomUtils;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ws.client.WebServiceFaultException;
import org.springframework.ws.client.core.WebServiceTemplate;
public class MailArchiveWebserviceCheckerTest {
private MailArchiveWebserviceChecker mailArchiveWebserviceChecker = new MailArchiveWebserviceChecker();
private WebServiceTemplate webServiceTemplate;
private ObjectFactory objectFactory;
private Mockery context = new JUnit4Mockery() {
{
setImposteriser(ClassImposteriser.INSTANCE);
}
};
private MessageInternalIdRquest messageInternalIdRquest;
private Message message;
@Before
public void setUp() {
webServiceTemplate = context.mock(WebServiceTemplate.class);
objectFactory = new ObjectFactory();
messageInternalIdRquest = objectFactory.createMessageInternalIdRquest();
messageInternalIdRquest.setMessageInternalId(createRandomMessageId());
message = objectFactory.createMessage();
mailArchiveWebserviceChecker.setWebServiceTemplate(webServiceTemplate);
}
@Test
public void shouldReturnTrue() {
String messageId = createRandomMessageId();
message.setMessageId(messageId);
createMockedResponse();
assertTrue(mailArchiveWebserviceChecker.check(messageId));
context.assertIsSatisfied();
}
@Test
public void shouldReturnFalse() {
String messageId = createRandomMessageId();
String anotherMessageId = createRandomMessageId();
message.setMessageId(messageId);
createMockedResponse();
assertFalse(mailArchiveWebserviceChecker.check(anotherMessageId));
context.assertIsSatisfied();
}
@Test
public void shouldReturnFalseCausedByException() {
String messageId = createRandomMessageId();
String anotherMessageId = createRandomMessageId();
message.setMessageId(messageId);
createMockedExceptionResponse();
assertFalse(mailArchiveWebserviceChecker.check(anotherMessageId));
}
private String createRandomMessageId() {
return String.valueOf(RandomUtils.nextInt());
}
private void createMockedResponse() {
final JAXBElement<Message> retrieveResponseObj = objectFactory
.createRetrieveMessageThroughMessageIdResponse(message);
// expectations
context.checking(new Expectations() {
{
one(webServiceTemplate).marshalSendAndReceive(
with(any(objectFactory.createMessageInternalIdRquest().getClass())));
will(returnValue(retrieveResponseObj));
}
});
}
private void createMockedExceptionResponse() {
// expectations
context.checking(new Expectations() {
{
one(webServiceTemplate).marshalSendAndReceive(
with(any(objectFactory.createMessageInternalIdRquest().getClass())));
will(throwException(new WebServiceFaultException("")));
}
});
}
}