Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(report): Throw 404 when recording not found #614

Merged
merged 4 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,20 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {

Exception rootCause = (Exception) ExceptionUtils.getRootCause(ee);

if (rootCause instanceof ReportService.RecordingNotFoundException) {
throw new HttpStatusException(404, ee);
} else if (rootCause instanceof SubprocessReportGenerator.ReportGenerationException
&& ((SubprocessReportGenerator.ReportGenerationException) rootCause).getStatus()
== SubprocessReportGenerator.ExitStatus.TARGET_CONNECTION_FAILURE) {
if (rootCause instanceof ReportService.RecordingNotFoundException
|| targetRecordingNotFound(rootCause)) {
throw new HttpStatusException(404, ee);
}
throw ee;
}
}

private boolean targetRecordingNotFound(Exception rootCause) {
return rootCause instanceof SubprocessReportGenerator.ReportGenerationException
&& (((SubprocessReportGenerator.ReportGenerationException) rootCause)
.getStatus()
== SubprocessReportGenerator.ExitStatus.TARGET_CONNECTION_FAILURE)
|| (((SubprocessReportGenerator.ReportGenerationException) rootCause).getStatus()
== SubprocessReportGenerator.ExitStatus.NO_SUCH_RECORDING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,33 @@ void shouldRespond404IfTargetNotFound() throws Exception {
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(404));
}

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

RoutingContext ctx = mock(RoutingContext.class);
HttpServerRequest req = mock(HttpServerRequest.class);
when(ctx.request()).thenReturn(req);
when(req.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
HttpServerResponse resp = mock(HttpServerResponse.class);
when(ctx.response()).thenReturn(resp);

String targetId = "fooHost:0";
String recordingName = "foo";
Future<String> content =
CompletableFuture.failedFuture(
new ExecutionException(
new SubprocessReportGenerator.ReportGenerationException(
SubprocessReportGenerator.ExitStatus.NO_SUCH_RECORDING)));
when(reportService.get(Mockito.any(), Mockito.anyString())).thenReturn(content);

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

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(404));
}
}
74 changes: 74 additions & 0 deletions src/test/java/itest/TargetReportIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package itest;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.impl.HttpStatusException;
import itest.bases.StandardSelfTest;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TargetReportIT extends StandardSelfTest {

static final String TEST_RECORDING_NAME = "someRecording";
static final String REPORT_REQ_URL =
String.format(
"/api/v1/targets/%s/reports/%s", SELF_REFERENCE_TARGET_ID, TEST_RECORDING_NAME);

@Test
void testGetReportThrowsWithNonExistentRecordingName() throws Exception {

CompletableFuture<JsonObject> response = new CompletableFuture<>();
webClient
.get(REPORT_REQ_URL)
.send(
ar -> {
assertRequestStatus(ar, response);
});
ExecutionException ex =
Assertions.assertThrows(ExecutionException.class, () -> response.get());
MatcherAssert.assertThat(
((HttpStatusException) ex.getCause()).getStatusCode(), Matchers.equalTo(404));
MatcherAssert.assertThat(ex.getCause().getMessage(), Matchers.equalTo("Not Found"));
}
}