-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ExpediaDotCom/update-version-readme
add unit tests and upgrade opentracing jaxrs
- Loading branch information
Showing
2 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/java/com/expedia/haystack/dropwizard/decorators/BlobServerSpanDecoratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.expedia.haystack.dropwizard.decorators; | ||
|
||
|
||
import com.expedia.blobs.core.BlobType; | ||
import com.expedia.blobs.core.BlobWriter; | ||
import com.expedia.blobs.core.ContentType; | ||
import com.expedia.haystack.dropwizard.configuration.BlobContent; | ||
import com.expedia.haystack.dropwizard.configuration.BlobFactory; | ||
import com.expedia.www.haystack.client.Span; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Answers; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import java.io.ByteArrayOutputStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
public class BlobServerSpanDecoratorTest { | ||
|
||
private final static String request_data = "{\"name\": \"Alice\"}"; | ||
private final static String response_data = "{\"name\": \"Blob\"}"; | ||
|
||
@Mock | ||
Span span; | ||
|
||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
BlobFactory blobs; | ||
|
||
@Mock | ||
ContainerRequestContext requestCtx; | ||
|
||
@Mock | ||
ContainerResponseContext responseCtx; | ||
|
||
@Before | ||
public void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
public void decorateServerRequest() throws Exception { | ||
final int[] writerInvoked = new int[] { 0 }; | ||
final BlobWriter blobWriter = newBlobWriter(writerInvoked); | ||
|
||
when(blobs.isEnabled()).thenReturn(true); | ||
when(blobs.factory().create(Matchers.any())).thenReturn(blobWriter); | ||
|
||
when(blobs.getBlobable().isServerRequestValidForBlob(requestCtx)).thenReturn(true); | ||
when(blobs.getBlobable().extractBlobFromServerRequest(requestCtx)).thenReturn( | ||
new BlobContent(request_data.getBytes("utf-8"), ContentType.JSON)); | ||
|
||
when(blobs.getBlobable().isServerResponseValidForBlob(responseCtx)).thenReturn(true); | ||
when(blobs.getBlobable().extractBlobFromServerResponse(responseCtx)).thenReturn( | ||
new BlobContent(response_data.getBytes("utf-8"), ContentType.JSON)); | ||
|
||
final BlobServerSpanDecorator decorator = new BlobServerSpanDecorator(blobs); | ||
decorator.decorateRequest(requestCtx, span); | ||
decorator.decorateResponse(responseCtx, span); | ||
assertThat(writerInvoked[0]).isEqualTo(2); | ||
} | ||
|
||
|
||
@Test | ||
public void skipDecorateServerRequest() throws Exception { | ||
final int[] writerInvoked = new int[] { 0 }; | ||
final BlobWriter blobWriter = newBlobWriter(writerInvoked); | ||
|
||
when(blobs.isEnabled()).thenReturn(true); | ||
when(blobs.factory().create(Matchers.any())).thenReturn(blobWriter); | ||
when(blobs.getBlobable().isServerRequestValidForBlob(requestCtx)).thenReturn(false); | ||
when(blobs.getBlobable().isServerResponseValidForBlob(responseCtx)).thenReturn(false); | ||
|
||
final BlobServerSpanDecorator decorator = new BlobServerSpanDecorator(blobs); | ||
decorator.decorateRequest(requestCtx, span); | ||
decorator.decorateResponse(responseCtx, span); | ||
assertThat(writerInvoked[0]).isEqualTo(0); | ||
} | ||
|
||
private static BlobWriter newBlobWriter(final int[] writerInvoked) { | ||
return (blobType, contentType, dataStream, metadata) -> { | ||
assertThat(contentType).isEqualTo(ContentType.JSON); | ||
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | ||
dataStream.accept(outStream); | ||
|
||
if(blobType.equals(BlobType.REQUEST)) { | ||
writerInvoked[0] = writerInvoked[0] + 1; | ||
assertThat(new String(outStream.toByteArray())).isEqualTo(request_data); | ||
} else if (blobType.equals(BlobType.RESPONSE)) { | ||
writerInvoked[0] = writerInvoked[0] + 1; | ||
assertThat(new String(outStream.toByteArray())).isEqualTo(response_data); | ||
} | ||
}; | ||
} | ||
} |