Skip to content

Commit

Permalink
Test cleanup
Browse files Browse the repository at this point in the history
* Make use of Assertions.assertAll to group related assertions
* Split InstrumentedInvokerFactoryTest#exceptionMeteredAnnotation test
  into separate tests: one where exception is thrown, one where
  exception is not thrown.
* Split JakartaXmlWsEnvironmentTest#getClient test into separate
  tests: one for the "simple" client and one for the "complex" client
  with handler, interceptors, MTOM, etc.
* Add MTOM references in Javadoc on AbstractBuilder#enableMtom
  • Loading branch information
sleberknight committed Jul 6, 2024
1 parent 4145522 commit d89cd5d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public AbstractBuilder cxfOutFaultInterceptors(Interceptor<? extends Message>...

/**
* Enable MTOM for binary attachments.
*
* @see <a href="https://www.w3.org/TR/soap12-mtom/">SOAP Message Transmission Optimization Mechanism (W3C)</a>
* @see <a href="https://en.wikipedia.org/wiki/Message_Transmission_Optimization_Mechanism">MTOM (Wikipedia)</a>
*/
public AbstractBuilder enableMtom() {
this.mtomEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kiwiproject.dropwizard.jakarta.xml.ws;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.mock;

import jakarta.xml.ws.handler.Handler;
Expand Down Expand Up @@ -29,14 +30,16 @@ void buildClient() {
.cxfOutInterceptors(outInterceptor, outInterceptor)
.cxfOutFaultInterceptors(outFaultInterceptor, outFaultInterceptor);

assertThat(builder.getAddress()).isEqualTo("address");
assertThat(builder.getServiceClass()).isEqualTo(Object.class);
assertThat(builder.getConnectTimeout()).isEqualTo(1234);
assertThat(builder.getReceiveTimeout()).isEqualTo(5678);
assertThat(builder.getBindingId()).isEqualTo("binding id");
assertThat(builder.getCxfInInterceptors()).contains(inInterceptor, inInterceptor);
assertThat(builder.getCxfInFaultInterceptors()).contains(inFaultInterceptor, inFaultInterceptor);
assertThat(builder.getCxfOutInterceptors()).contains(outInterceptor, outInterceptor);
assertThat(builder.getCxfOutFaultInterceptors()).contains(outFaultInterceptor, outFaultInterceptor);
assertAll(
() -> assertThat(builder.getAddress()).isEqualTo("address"),
() -> assertThat(builder.getServiceClass()).isEqualTo(Object.class),
() -> assertThat(builder.getConnectTimeout()).isEqualTo(1234),
() -> assertThat(builder.getReceiveTimeout()).isEqualTo(5678),
() -> assertThat(builder.getBindingId()).isEqualTo("binding id"),
() -> assertThat(builder.getCxfInInterceptors()).contains(inInterceptor, inInterceptor),
() -> assertThat(builder.getCxfInFaultInterceptors()).contains(inFaultInterceptor, inFaultInterceptor),
() -> assertThat(builder.getCxfOutInterceptors()).contains(outInterceptor, outInterceptor),
() -> assertThat(builder.getCxfOutFaultInterceptors()).contains(outFaultInterceptor, outFaultInterceptor)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kiwiproject.dropwizard.jakarta.xml.ws;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.mock;

import org.apache.cxf.interceptor.Interceptor;
Expand Down Expand Up @@ -36,15 +37,17 @@ void buildEndpoint() {
.cxfOutFaultInterceptors(outFaultInterceptor, outFaultInterceptor)
.properties(props);

assertThat(builder.getPath()).isEqualTo(path);
assertThat(builder.getService()).isEqualTo(service);
assertThat(builder.publishedEndpointUrl()).isEqualTo(publishedUrl);
assertThat(builder.getAuthentication()).isEqualTo(basicAuth);
assertThat(builder.getSessionFactory()).isEqualTo(sessionFactory);
assertThat(builder.getCxfInInterceptors()).contains(inInterceptor, inInterceptor);
assertThat(builder.getCxfInFaultInterceptors()).contains(inFaultInterceptor, inFaultInterceptor);
assertThat(builder.getCxfOutInterceptors()).contains(outInterceptor, outInterceptor);
assertThat(builder.getCxfOutFaultInterceptors()).contains(outFaultInterceptor, outFaultInterceptor);
assertThat(builder.getProperties()).containsEntry("key", "value");
assertAll(
() -> assertThat(builder.getPath()).isEqualTo(path),
() -> assertThat(builder.getService()).isEqualTo(service),
() -> assertThat(builder.publishedEndpointUrl()).isEqualTo(publishedUrl),
() -> assertThat(builder.getAuthentication()).isEqualTo(basicAuth),
() -> assertThat(builder.getSessionFactory()).isEqualTo(sessionFactory),
() -> assertThat(builder.getCxfInInterceptors()).contains(inInterceptor, inInterceptor),
() -> assertThat(builder.getCxfInFaultInterceptors()).contains(inFaultInterceptor, inFaultInterceptor),
() -> assertThat(builder.getCxfOutInterceptors()).contains(outInterceptor, outInterceptor),
() -> assertThat(builder.getCxfOutFaultInterceptors()).contains(outFaultInterceptor, outFaultInterceptor),
() -> assertThat(builder.getProperties()).containsEntry("key", "value")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -139,10 +140,12 @@ void noAnnotation() {
this.setTargetMethod(exchange, "foo"); // simulate CXF behavior

var result = invoker.invoke(exchange, null);
assertThat(result).isEqualTo("fooReturn");

assertThat(timer.getCount()).isEqualTo(oldTimerValue);
assertThat(meter.getCount()).isEqualTo(oldMeterValue);
assertAll(
() -> assertThat(result).isEqualTo("fooReturn"),
() -> assertThat(timer.getCount()).isEqualTo(oldTimerValue),
() -> assertThat(meter.getCount()).isEqualTo(oldMeterValue)
);
}

@Test
Expand All @@ -159,10 +162,11 @@ void meteredAnnotation() {
this.setTargetMethod(exchange, "metered"); // simulate CXF behavior

var result = invoker.invoke(exchange, null);
assertThat(result).isEqualTo("meteredReturn");

assertThat(timer.getCount()).isEqualTo(oldTimerValue);
assertThat(meter.getCount()).isEqualTo(1 + oldMeterValue);
assertAll(
() -> assertThat(result).isEqualTo("meteredReturn"),
() -> assertThat(timer.getCount()).isEqualTo(oldTimerValue),
() -> assertThat(meter.getCount()).isEqualTo(1 + oldMeterValue)
);
}

@Test
Expand All @@ -186,39 +190,54 @@ void timedAnnotation() {
}

@Test
void exceptionMeteredAnnotation() {
void exceptionMeteredAnnotation_WhenExceptionIsNotThrown() {
var timer = testMetricRegistry.timer("timed");
var meter = testMetricRegistry.meter("metered");
var exceptionmeter = testMetricRegistry.meter("exceptionMeteredExceptions");
var exceptionMeter = testMetricRegistry.meter("exceptionMeteredExceptions");
when(mockMetricRegistry.timer(anyString())).thenReturn(timer);
when(mockMetricRegistry.meter(contains("metered"))).thenReturn(meter);
when(mockMetricRegistry.meter(contains("exceptionMetered"))).thenReturn(exceptionmeter);
when(mockMetricRegistry.meter(contains("exceptionMetered"))).thenReturn(exceptionMeter);

var oldTimerValue = timer.getCount();
var oldMeterValue = meter.getCount();
var oldExceptionMeterValue = exceptionmeter.getCount();

// Invoke InstrumentedResource.exceptionMetered without exception being thrown
var oldExceptionMeterValue = exceptionMeter.getCount();

var invoker = invokerBuilder.create(instrumentedService, new ExceptionMeteredInvoker(false));
this.setTargetMethod(exchange, "exceptionMetered", boolean.class); // simulate CXF behavior

var result = invoker.invoke(exchange, null);
assertThat(result).isEqualTo("exceptionMeteredReturn");

assertThat(timer.getCount()).isEqualTo(oldTimerValue);
assertThat(meter.getCount()).isEqualTo(oldMeterValue);
assertThat(exceptionmeter.getCount()).isEqualTo(oldExceptionMeterValue);
assertAll(
() -> assertThat(result).isEqualTo("exceptionMeteredReturn"),
() -> assertThat(timer.getCount()).isEqualTo(oldTimerValue),
() -> assertThat(meter.getCount()).isEqualTo(oldMeterValue),
() -> assertThat(exceptionMeter.getCount()).isEqualTo(oldExceptionMeterValue)
);
}

// Invoke InstrumentedResource.exceptionMetered with exception being thrown
@Test
void exceptionMeteredAnnotation_WhenExceptionIsThrown() {
var timer = testMetricRegistry.timer("timed");
var meter = testMetricRegistry.meter("metered");
var exceptionMeter = testMetricRegistry.meter("exceptionMeteredExceptions");
when(mockMetricRegistry.timer(anyString())).thenReturn(timer);
when(mockMetricRegistry.meter(contains("metered"))).thenReturn(meter);
when(mockMetricRegistry.meter(contains("exceptionMetered"))).thenReturn(exceptionMeter);

var throwingInvoker = invokerBuilder.create(instrumentedService, new ExceptionMeteredInvoker(true));
var oldTimerValue = timer.getCount();
var oldMeterValue = meter.getCount();
var oldExceptionMeterValue = exceptionMeter.getCount();

assertThatRuntimeException().isThrownBy(() -> throwingInvoker.invoke(exchange, null));
this.setTargetMethod(exchange, "exceptionMetered", boolean.class); // simulate CXF behavior

assertThat(timer.getCount()).isEqualTo(oldTimerValue);
assertThat(meter.getCount()).isEqualTo(oldMeterValue);
assertThat(exceptionmeter.getCount()).isEqualTo(1 + oldExceptionMeterValue);
var throwingInvoker = invokerBuilder.create(instrumentedService, new ExceptionMeteredInvoker(true));

assertAll(
() -> assertThatRuntimeException().isThrownBy(() -> throwingInvoker.invoke(exchange, null)),
() -> assertThat(timer.getCount()).isEqualTo(oldTimerValue),
() -> assertThat(meter.getCount()).isEqualTo(oldMeterValue),
() -> assertThat(exceptionMeter.getCount()).isEqualTo(1 + oldExceptionMeterValue)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.startsWith;
Expand Down Expand Up @@ -51,20 +52,22 @@ void setUp() {

@Test
void constructorArgumentChecks() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new JakartaXmlWsBundle<>(null, jwsEnvironment))
.withMessage("Servlet path is null");

assertThatIllegalArgumentException()
.isThrownBy(() -> new JakartaXmlWsBundle<>("soap", jwsEnvironment))
.withMessage("soap is not an absolute path");

assertThatIllegalArgumentException()
.isThrownBy(() -> new JakartaXmlWsBundle<>("/soap", null))
.withMessage("jwsEnvironment is null");

assertThatCode(() -> new JakartaXmlWsBundle<>("/soap", jwsEnvironment))
.doesNotThrowAnyException();
assertAll(
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> new JakartaXmlWsBundle<>(null, jwsEnvironment))
.withMessage("Servlet path is null"),

() -> assertThatIllegalArgumentException()
.isThrownBy(() -> new JakartaXmlWsBundle<>("soap", jwsEnvironment))
.withMessage("soap is not an absolute path"),

() -> assertThatIllegalArgumentException()
.isThrownBy(() -> new JakartaXmlWsBundle<>("/soap", null))
.withMessage("jwsEnvironment is null"),

() -> assertThatCode(() -> new JakartaXmlWsBundle<>("/soap", jwsEnvironment))
.doesNotThrowAnyException()
);
}

@Test
Expand Down Expand Up @@ -112,17 +115,20 @@ protected String getPublishedEndpointUrlPrefix(Configuration configuration) {
void publishEndpoint() {
var jwsBundle = new JakartaXmlWsBundle<>("/soap", jwsEnvironment);
var service = new Object();
assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.publishEndpoint(new EndpointBuilder("foo", null)))
.withMessage("Service is null");

assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.publishEndpoint(new EndpointBuilder(null, service)))
.withMessage("Path is null");
assertAll(
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.publishEndpoint(new EndpointBuilder("foo", null)))
.withMessage("Service is null"),

assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.publishEndpoint(new EndpointBuilder(" ", service)))
.withMessage("Path is empty");
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.publishEndpoint(new EndpointBuilder(null, service)))
.withMessage("Path is null"),

() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.publishEndpoint(new EndpointBuilder(" ", service)))
.withMessage("Path is empty")
);

var builder = mock(EndpointBuilder.class);
jwsBundle.publishEndpoint(builder);
Expand All @@ -136,21 +142,23 @@ void getClient() {
Class<?> cls = Object.class;
var url = "http://foo";

assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(null, null)))
.withMessage("ServiceClass is null");
assertAll(
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(null, null)))
.withMessage("ServiceClass is null"),

assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(null, url)))
.withMessage("ServiceClass is null");
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(null, url)))
.withMessage("ServiceClass is null"),

assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(cls, null)))
.withMessage("Address is null");
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(cls, null)))
.withMessage("Address is null"),

assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(cls, " ")))
.withMessage("Address is empty");
() -> assertThatIllegalArgumentException()
.isThrownBy(() -> jwsBundle.getClient(new ClientBuilder<>(cls, " ")))
.withMessage("Address is empty")
);

var builder = new ClientBuilder<>(cls, url);
jwsBundle.getClient(builder);
Expand Down
Loading

0 comments on commit d89cd5d

Please sign in to comment.