Skip to content

Commit

Permalink
test(apiv1): correct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Jul 20, 2021
1 parent 5523e54 commit c788d16
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class AbstractAuthenticatedRequestHandlerTest {
@BeforeEach
void setup() {
this.handler = new AuthenticatedHandler(auth);
Mockito.lenient().when(ctx.response()).thenReturn(resp);
Mockito.lenient()
.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.lenient()
.when(resp.putHeader(Mockito.anyString(), Mockito.anyString()))
.thenReturn(resp);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ void shouldThrow404IfNoMatchingRecordingFound() throws Exception {

Mockito.when(fs.listDirectoryChildren(Mockito.any())).thenReturn(List.of());

Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(404));
Expand Down Expand Up @@ -168,6 +174,12 @@ void shouldThrowExceptionIfDeletionFails() throws Exception {

Mockito.when(ctx.pathParam("recordingName")).thenReturn(recordingName);

Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(500));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ void shouldHandleCorrectPath() {
})
@NullAndEmptySource
void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(rawUrl);
Expand All @@ -126,6 +132,12 @@ void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {

@Test
void shouldThrowExceptionIfRecordingNotFound() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -173,6 +185,10 @@ public Void answer(InvocationOnMock args) throws Throwable {

HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

handler.handle(ctx);

Expand Down Expand Up @@ -221,6 +237,13 @@ public Void answer(InvocationOnMock args) throws Throwable {
.when(httpReq)
.sendMultipartForm(Mockito.any(), Mockito.any());

HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException e =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));

Expand All @@ -238,6 +261,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleNullStatusMessage() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -288,6 +317,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleNullResponseBody() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;

import io.cryostat.core.log.Logger;
import io.cryostat.net.AuthManager;
import io.cryostat.net.reports.ReportService;

import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -94,14 +98,44 @@ void shouldBeOrdered() {
Assertions.assertTrue(handler.isOrdered());
}

@Test
void shouldRespondBySendingFile() throws Exception {
when(authManager.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));

RoutingContext ctx = mock(RoutingContext.class);
HttpServerRequest req = mock(HttpServerRequest.class);
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.request()).thenReturn(req);
when(ctx.response()).thenReturn(resp);
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

Path fakePath = Paths.get("/some/fake/path.html");

when(ctx.pathParam("recordingName")).thenReturn("someRecording");
when(reportService.get(Mockito.anyString()))
.thenReturn(CompletableFuture.completedFuture(fakePath));

handler.handle(ctx);

Mockito.verify(reportService).get("someRecording");
Mockito.verify(resp).sendFile(fakePath.toString());
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_TYPE, "text/html");
}

@Test
void shouldRespond404IfRecordingNameNotFound() throws Exception {
when(authManager.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));

RoutingContext ctx = mock(RoutingContext.class);
HttpServerRequest req = mock(HttpServerRequest.class);
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.request()).thenReturn(req);
when(ctx.response()).thenReturn(resp);
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

when(ctx.pathParam("recordingName")).thenReturn("someRecording");
when(reportService.get(Mockito.anyString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ void shouldHandleRecordingDownloadRequest() throws Exception {
RoutingContext ctx = mock(RoutingContext.class);
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.response()).thenReturn(resp);
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
HttpServerRequest req = mock(HttpServerRequest.class);
when(ctx.request()).thenReturn(req);
when(ctx.request().headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Expand Down Expand Up @@ -166,6 +168,8 @@ void shouldHandleRecordingDownloadRequestWithJfrSuffix() throws Exception {
RoutingContext ctx = mock(RoutingContext.class);
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.response()).thenReturn(resp);
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
HttpServerRequest req = mock(HttpServerRequest.class);
when(ctx.request()).thenReturn(req);
when(ctx.request().headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Expand Down Expand Up @@ -216,6 +220,10 @@ void shouldRespond404IfRecordingNameNotFound() throws Exception {
HttpServerRequest req = mock(HttpServerRequest.class);
when(ctx.request()).thenReturn(req);
when(ctx.request().headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.response()).thenReturn(resp);
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

when(targetConnectionManager.executeConnectedTask(Mockito.any(), Mockito.any()))
.thenAnswer(
Expand Down Expand Up @@ -247,6 +255,10 @@ void shouldRespond500IfUnexpectedExceptionThrown() throws Exception {
HttpServerRequest req = mock(HttpServerRequest.class);
when(ctx.request()).thenReturn(req);
when(ctx.request().headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.response()).thenReturn(resp);
when(resp.putHeader(Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

when(targetConnectionManager.executeConnectedTask(Mockito.any(), Mockito.any()))
.thenAnswer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

import com.google.gson.Gson;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand Down Expand Up @@ -153,6 +154,10 @@ public Map answer(InvocationOnMock args) throws Throwable {
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
IFlightRecorderService service = Mockito.mock(IFlightRecorderService.class);
Mockito.when(jfrConnection.getService()).thenReturn(service);

Expand All @@ -164,5 +169,6 @@ public Map answer(InvocationOnMock args) throws Throwable {
MatcherAssert.assertThat(
responseCaptor.getValue(),
Matchers.equalTo("{\"maxAge\":50,\"toDisk\":true,\"maxSize\":32}"));
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_TYPE, "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
import org.hamcrest.MatcherAssert;
Expand All @@ -69,6 +70,7 @@ class TargetRecordingPatchHandlerTest {
@Mock TargetRecordingPatchStop patchStop;
@Mock RoutingContext ctx;
@Mock HttpServerRequest req;
@Mock HttpServerResponse resp;
@Mock ConnectionDescriptor connectionDescriptor;

@BeforeEach
Expand Down Expand Up @@ -111,6 +113,11 @@ void shouldThrow400InvalidOperations(String mtd) {
Mockito.when(authManager.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(ctx.getBodyAsString()).thenReturn(mtd);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
Expand All @@ -126,6 +133,11 @@ void shouldDelegateSupportedOperations(String mtd) throws Exception {
Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(ctx.getBodyAsString()).thenReturn(mtd);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

handler.handle(ctx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(rawUrl);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
Expand All @@ -143,6 +148,11 @@ void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {
@Test
void shouldThrowExceptionIfRecordingNotFound() throws Exception {
Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(ctx.pathParam("targetId")).thenReturn("fooHost:1234");
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(auth.validateHttpHeader(Mockito.any()))
Expand Down Expand Up @@ -220,6 +230,10 @@ public Void answer(InvocationOnMock args) throws Throwable {
Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

handler.handle(ctx);

Expand Down Expand Up @@ -281,6 +295,11 @@ public Void answer(InvocationOnMock args) throws Throwable {

Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException e =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
Expand Down Expand Up @@ -345,6 +364,11 @@ public Void answer(InvocationOnMock args) throws Throwable {

Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException e =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
Expand Down Expand Up @@ -409,6 +433,11 @@ public Void answer(InvocationOnMock args) throws Throwable {

Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException e =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ void shouldStartRecording() throws Exception {
attrs.add("maxAge", "50");
attrs.add("maxSize", "64");
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

IRecordingDescriptor descriptor = createDescriptor("someRecording");
Mockito.when(
Expand Down Expand Up @@ -256,6 +260,11 @@ void shouldHandleNameCollision() throws Exception {
Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
Mockito.when(req.formAttributes()).thenReturn(attrs);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
attrs.add("recordingName", "someRecording");
attrs.add("events", "template=Foo");

Expand Down Expand Up @@ -313,6 +322,11 @@ void shouldThrowInvalidOptionException(Map<String, String> requestValues) throws
Mockito.when(ctx.pathParam("targetId")).thenReturn("fooHost:9091");
MultiMap attrs = MultiMap.caseInsensitiveMultiMap();
Mockito.when(ctx.request()).thenReturn(req);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
attrs.addAll(requestValues);
attrs.add("recordingName", "someRecording");
Expand Down
Loading

0 comments on commit c788d16

Please sign in to comment.