Skip to content

Commit

Permalink
Use FauxPas instead of custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasniemeier-zalando committed Nov 16, 2019
1 parent b449df8 commit af70c79
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apiguardian.api.API;
import org.zalando.fauxpas.ThrowingUnaryOperator;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -14,10 +15,5 @@
@Getter
public final class Compression {
private final String contentEncoding;
private final OutputStreamDecorator outputStreamDecorator;

@FunctionalInterface
public interface OutputStreamDecorator {
OutputStream wrap(final OutputStream stream) throws IOException;
}
private final ThrowingUnaryOperator<OutputStream, IOException> outputStreamDecorator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.zalando.fauxpas.ThrowingUnaryOperator;

import javax.annotation.Nonnull;
import java.io.IOException;
Expand All @@ -10,10 +11,10 @@
final class WrappingHttpOutputMessage implements HttpOutputMessage, AutoCloseable {

private final HttpOutputMessage message;
private final Compression.OutputStreamDecorator wrapper;
private final ThrowingUnaryOperator<OutputStream, IOException> wrapper;
private OutputStream stream;

WrappingHttpOutputMessage(HttpOutputMessage message, Compression.OutputStreamDecorator wrapper) {
WrappingHttpOutputMessage(HttpOutputMessage message, ThrowingUnaryOperator<OutputStream, IOException> wrapper) {
this.message = message;
this.wrapper = wrapper;
}
Expand All @@ -22,7 +23,7 @@ final class WrappingHttpOutputMessage implements HttpOutputMessage, AutoCloseabl
@Override
public OutputStream getBody() throws IOException {
if (stream == null) {
stream = wrapper.wrap(message.getBody());
stream = wrapper.apply(message.getBody());
}
return stream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.zalando.riptide.compression.Compression.OutputStreamDecorator;
import org.zalando.fauxpas.ThrowingUnaryOperator;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -21,7 +21,10 @@
class WrappingHttpOutputMessageTest {

private final HttpOutputMessage message = mock(HttpOutputMessage.class);
private final OutputStreamDecorator wrapper = mock(OutputStreamDecorator.class);

@SuppressWarnings("unchecked")
private final ThrowingUnaryOperator<OutputStream, IOException> wrapper = mock(ThrowingUnaryOperator.class);

private final WrappingHttpOutputMessage unit = new WrappingHttpOutputMessage(message, wrapper);

@BeforeEach
Expand All @@ -39,7 +42,7 @@ void shouldDelegateHeaders() {
@Test
void shouldReturnWrapped() throws IOException {
final OutputStream wrappedStream = mock(OutputStream.class);
when(wrapper.wrap(any())).thenReturn(wrappedStream);
when(wrapper.apply(any())).thenReturn(wrappedStream);

assertThat(unit.getBody(), equalTo(wrappedStream));
assertThat(unit.getBody(), equalTo(wrappedStream));
Expand All @@ -48,19 +51,19 @@ void shouldReturnWrapped() throws IOException {
@Test
void shouldWrapOnFirstAccessOnly() throws IOException {
final OutputStream wrappedStream = mock(OutputStream.class);
when(wrapper.wrap(any())).thenReturn(wrappedStream);
when(wrapper.apply(any())).thenReturn(wrappedStream);

unit.getBody();
unit.getBody();

verify(wrapper).wrap(message.getBody());
verify(wrapper).apply(message.getBody());
verifyNoMoreInteractions(wrapper);
}

@Test
void shouldNotCloseStreamIfNeverAccessed() throws IOException {
final OutputStream wrappedStream = mock(OutputStream.class);
when(wrapper.wrap(any())).thenReturn(wrappedStream);
when(wrapper.apply(any())).thenReturn(wrappedStream);

unit.close();

Expand Down

0 comments on commit af70c79

Please sign in to comment.