package com.vtence.molecule.middlewares;
import com.vtence.molecule.Application;
import com.vtence.molecule.Request;
import com.vtence.molecule.Response;
import com.vtence.molecule.support.MockRequest;
import com.vtence.molecule.support.MockResponse;
import com.vtence.molecule.FailureReporter;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class FailureMonitorTest {
@Rule public JUnitRuleMockery context = new JUnitRuleMockery();
FailureReporter failureReporter = context.mock(FailureReporter.class);
FailureMonitor monitor = new FailureMonitor(failureReporter);
Exception error = new Exception("An internal error occurred!");
MockRequest request = new MockRequest();
MockResponse response = new MockResponse();
@Test public void
notifiesFailureReporterAndRethrowsExceptionInCaseOfError() throws Exception {
monitor.connectTo(crashWith(error));
context.checking(new Expectations() {{
oneOf(failureReporter).errorOccurred(with(same(error)));
}});
try {
monitor.handle(request, response);
fail("Exception did not bubble up");
} catch (Exception e) {
assertThat("error", e, sameInstance(error));
}
}
private Application crashWith(final Exception error) {
return new Application() {
public void handle(Request request, Response response) throws Exception {
throw error;
}
};
}
}