Skip to content

Commit

Permalink
fix up recording tests with jvmId in notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Oct 6, 2023
1 parent c928d68 commit 1350d82
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.net.web.http.api.v2.AbstractAssetJwtConsumingHandler;
import io.cryostat.net.web.http.api.v2.ApiException;
import io.cryostat.recordings.JvmIdHelper;
import io.cryostat.recordings.RecordingArchiveHelper;
import io.cryostat.recordings.RecordingNotFoundException;
import io.cryostat.rules.ArchivePathException;
Expand All @@ -44,8 +45,9 @@

public class RecordingGetFromPathWithJwtHandler extends AbstractAssetJwtConsumingHandler {

static final String PATH = "fs/recordings/:subdirectoryName/:recordingName/jwt";
static final String PATH = "fs/recordings/:jvmId/:recordingName/jwt";

private final JvmIdHelper jvmIdHelper;
private final RecordingArchiveHelper recordingArchiveHelper;

@Inject
Expand All @@ -54,9 +56,11 @@ public class RecordingGetFromPathWithJwtHandler extends AbstractAssetJwtConsumin
CredentialsManager credentialsManager,
AssetJwtHelper jwtFactory,
Lazy<WebServer> webServer,
JvmIdHelper jvmIdHelper,
RecordingArchiveHelper recordingArchiveHelper,
Logger logger) {
super(auth, credentialsManager, jwtFactory, webServer, logger);
this.jvmIdHelper = jvmIdHelper;
this.recordingArchiveHelper = recordingArchiveHelper;
}

Expand Down Expand Up @@ -87,9 +91,10 @@ public boolean isAsync() {

@Override
public void handleWithValidJwt(RoutingContext ctx, JWT jwt) throws Exception {
String subdirectoryName = ctx.pathParam("subdirectoryName");
String jvmId = ctx.pathParam("jvmId");
String recordingName = ctx.pathParam("recordingName");
try {
String subdirectoryName = jvmIdHelper.jvmIdToSubdirectoryName(jvmId);
Path archivedRecording =
recordingArchiveHelper
.getRecordingPathFromPath(subdirectoryName, recordingName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.cryostat.net.web.http.api.v2.ApiException;
import io.cryostat.net.web.http.api.v2.IntermediateResponse;
import io.cryostat.net.web.http.api.v2.RequestParameters;
import io.cryostat.recordings.JvmIdHelper;
import io.cryostat.recordings.RecordingNotFoundException;
import io.cryostat.rules.ArchivePathException;

Expand All @@ -47,8 +48,9 @@

public class ReportGetFromPathHandler extends AbstractV2RequestHandler<Path> {

static final String PATH = "fs/reports/:subdirectoryName/:recordingName";
static final String PATH = "fs/reports/:jvmId/:recordingName";

private final JvmIdHelper jvmIdHelper;
private final ReportService reportService;
private final long reportGenerationTimeoutSeconds;

Expand All @@ -57,10 +59,12 @@ public class ReportGetFromPathHandler extends AbstractV2RequestHandler<Path> {
AuthManager auth,
CredentialsManager credentialsManager,
Gson gson,
JvmIdHelper jvmIdHelper,
ReportService reportService,
@Named(ReportsModule.REPORT_GENERATION_TIMEOUT_SECONDS)
long reportGenerationTimeoutSeconds) {
super(auth, credentialsManager, gson);
this.jvmIdHelper = jvmIdHelper;
this.reportService = reportService;
this.reportGenerationTimeoutSeconds = reportGenerationTimeoutSeconds;
}
Expand Down Expand Up @@ -105,9 +109,10 @@ public boolean isAsync() {

@Override
public IntermediateResponse<Path> handle(RequestParameters params) throws Exception {
String subdirectoryName = params.getPathParams().get("subdirectoryName");
String jvmId = params.getPathParams().get("jvmId");
String recordingName = params.getPathParams().get("recordingName");
try {
String subdirectoryName = jvmIdHelper.jvmIdToSubdirectoryName(jvmId);
List<String> queriedFilter = params.getQueryParams().getAll("filter");
String rawFilter = queriedFilter.isEmpty() ? "" : queriedFilter.get(0);
Path report =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.net.web.http.api.v2.AbstractAssetJwtConsumingHandler;
import io.cryostat.net.web.http.api.v2.ApiException;
import io.cryostat.recordings.JvmIdHelper;
import io.cryostat.recordings.RecordingArchiveHelper;
import io.cryostat.recordings.RecordingNotFoundException;
import io.cryostat.rules.ArchivePathException;
Expand All @@ -51,22 +52,25 @@

class ReportGetFromPathWithJwtHandler extends AbstractAssetJwtConsumingHandler {

static final String PATH = "fs/reports/:subdirectoryName/:recordingName/jwt";
static final String PATH = "fs/reports/:jvmId/:recordingName/jwt";

private final ReportService reportService;
private final long generationTimeoutSeconds;
private final JvmIdHelper jvmIdHelper;

@Inject
ReportGetFromPathWithJwtHandler(
AuthManager auth,
CredentialsManager credentialsManager,
AssetJwtHelper jwtFactory,
Lazy<WebServer> webServer,
JvmIdHelper jvmIdHelper,
ReportService reportService,
RecordingArchiveHelper recordingArchiveHelper,
@Named(ReportsModule.REPORT_GENERATION_TIMEOUT_SECONDS) long generationTimeoutSeconds,
Logger logger) {
super(auth, credentialsManager, jwtFactory, webServer, logger);
this.jvmIdHelper = jvmIdHelper;
this.reportService = reportService;
this.generationTimeoutSeconds = generationTimeoutSeconds;
}
Expand Down Expand Up @@ -111,9 +115,10 @@ public boolean isOrdered() {

@Override
public void handleWithValidJwt(RoutingContext ctx, JWT jwt) throws Exception {
String subdirectoryName = ctx.pathParam("subdirectoryName");
String jvmId = ctx.pathParam("jvmId");
String recordingName = ctx.pathParam("recordingName");
try {
String subdirectoryName = jvmIdHelper.jvmIdToSubdirectoryName(jvmId);
List<String> queriedFilter = ctx.queryParam("filter");
String rawFilter = queriedFilter.isEmpty() ? "" : queriedFilter.get(0);
Path report =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,20 @@ class Behaviour {
void shouldThrow404IfNoMatchingRecordingFound() throws Exception {
String recordingName = "someRecording";
String jvmId = "id";
String subdirectoryName = "someSubdirectory";

when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn(subdirectoryName);
when(params.getPathParams())
.thenReturn(
Map.of(
"id",
"jvmId",
jvmId,
"recordingName",
recordingName));

Future<ArchivedRecordingInfo> future =
CompletableFuture.failedFuture(
new RecordingNotFoundException(jvmId, recordingName));
new RecordingNotFoundException(subdirectoryName, recordingName));
when(recordingArchiveHelper.deleteRecordingFromPath(
Mockito.anyString(), Mockito.anyString()))
.thenReturn(future);
Expand All @@ -145,12 +147,15 @@ void shouldThrow404IfNoMatchingRecordingFound() throws Exception {
void shouldHandleSuccessfulDELETERequest() throws Exception {
String recordingName = "someRecording";
String jvmId = "id";
String subdirectoryName = "someSubdirectory";

when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn(subdirectoryName);
when(params.getPathParams())
.thenReturn(
Map.of(
"recordingName",
recordingName,
"id",
"jvmId",
jvmId));

CompletableFuture<ArchivedRecordingInfo> future = Mockito.mock(CompletableFuture.class);
Expand All @@ -164,7 +169,7 @@ void shouldHandleSuccessfulDELETERequest() throws Exception {

verify(recordingArchiveHelper)
.deleteRecordingFromPath(
Mockito.eq(jvmId), Mockito.eq(recordingName));
Mockito.eq(subdirectoryName), Mockito.eq(recordingName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.cryostat.net.web.WebServer;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.net.web.http.api.v2.ApiException;
import io.cryostat.recordings.JvmIdHelper;
import io.cryostat.recordings.RecordingArchiveHelper;
import io.cryostat.recordings.RecordingNotFoundException;

Expand Down Expand Up @@ -58,14 +59,15 @@ class RecordingGetFromPathWithJwtHandlerTest {
@Mock CredentialsManager credentialsManager;
@Mock AssetJwtHelper jwt;
@Mock WebServer webServer;
@Mock JvmIdHelper jvmIdHelper;
@Mock RecordingArchiveHelper archive;
@Mock Logger logger;

@BeforeEach
void setup() {
this.handler =
new RecordingGetFromPathWithJwtHandler(
auth, credentialsManager, jwt, () -> webServer, archive, logger);
auth, credentialsManager, jwt, () -> webServer, jvmIdHelper, archive, logger);
}

@Nested
Expand All @@ -86,7 +88,7 @@ void shouldUseExpectedPath() {
MatcherAssert.assertThat(
handler.path(),
Matchers.equalTo(
"/api/beta/fs/recordings/:subdirectoryName/:recordingName/jwt"));
"/api/beta/fs/recordings/:jvmId/:recordingName/jwt"));
}

@Test
Expand Down Expand Up @@ -115,8 +117,9 @@ class Behaviour {

@Test
void shouldRespond404IfNotFound() throws Exception {
when(ctx.pathParam("subdirectoryName")).thenReturn("mysubdirectory");
when(ctx.pathParam("jvmId")).thenReturn("id");
when(ctx.pathParam("recordingName")).thenReturn("myrecording");
when(jvmIdHelper.jvmIdToSubdirectoryName(Mockito.anyString())).thenReturn("mysubdirectory");
Future<Path> future =
CompletableFuture.failedFuture(
new RecordingNotFoundException("mysubdirectory", "myrecording"));
Expand All @@ -132,8 +135,9 @@ void shouldRespond404IfNotFound() throws Exception {
void shouldSendFileIfFound() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
when(ctx.response()).thenReturn(resp);
when(ctx.pathParam("subdirectoryName")).thenReturn("mysubdirectory");
when(ctx.pathParam("jvmId")).thenReturn("id");
when(ctx.pathParam("recordingName")).thenReturn("myrecording");
when(jvmIdHelper.jvmIdToSubdirectoryName(Mockito.anyString())).thenReturn("mysubdirectory");
Path path = Mockito.mock(Path.class);
when(path.toAbsolutePath()).thenReturn(path);
when(path.toString()).thenReturn("foo.jfr");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,22 @@ void shouldUpdateLabels() throws Exception {
Map<String, String> labels = Map.of("key", "value");
Metadata metadata = new Metadata(labels);
String requestLabels = labels.toString();
String subdirectoryName = "someSubdirectory";
Map<String, String> params = Mockito.mock(Map.class);


when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn(subdirectoryName);
when(requestParameters.getPathParams()).thenReturn(params);
when(params.get("recordingName")).thenReturn(recordingName);
when(params.get("jvmId")).thenReturn(jvmId);
when(requestParameters.getBody()).thenReturn(requestLabels);

when(recordingArchiveHelper.getRecordingPathFromPath(jvmId, recordingName))
when(recordingArchiveHelper.getRecordingPathFromPath(subdirectoryName, recordingName))
.thenReturn(CompletableFuture.completedFuture(Path.of(recordingName)));

when(recordingMetadataManager.parseRecordingLabels(requestLabels)).thenReturn(labels);

when(recordingMetadataManager.setRecordingMetadataFromPath(
jvmId, recordingName, metadata))
subdirectoryName, recordingName, metadata))
.thenReturn(CompletableFuture.completedFuture(metadata));

IntermediateResponse<Metadata> response = handler.handle(requestParameters);
Expand Down Expand Up @@ -187,15 +189,17 @@ void shouldThrow400OnEmptyLabels() throws Exception {
void shouldThrowWhenRecordingNotFound() throws Exception {
String jvmId = "id";
String recordingName = "someNonExistentRecording";
String subdirectoryName = "someSubdirectory";
String labels = Map.of("key", "value").toString();
Map<String, String> params = Mockito.mock(Map.class);

when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn(subdirectoryName);
when(requestParameters.getPathParams()).thenReturn(params);
when(params.get("recordingName")).thenReturn(recordingName);
when(params.get("jvmId")).thenReturn(jvmId);
when(requestParameters.getBody()).thenReturn(labels);

when(recordingArchiveHelper.getRecordingPathFromPath(jvmId, recordingName))
when(recordingArchiveHelper.getRecordingPathFromPath(subdirectoryName, recordingName))
.thenReturn(
CompletableFuture.failedFuture(
new RecordingNotFoundException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class RecordingUploadPostFromPathHandlerTest {

static final String DATASOURCE_URL = "http://localhost:8080";

static final String jvmId = "id";
static final String subdirectoryName = "foo";
static final String recordingName = "bar";

Expand Down Expand Up @@ -157,11 +158,12 @@ void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {

@Test
void shouldThrowExceptionIfRecordingNotFound() throws Exception {
when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn(subdirectoryName);
when(params.getPathParams())
.thenReturn(
Map.of(
"subdirectoryName",
subdirectoryName,
"jvmId",
jvmId,
"recordingName",
recordingName));
when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand All @@ -173,7 +175,7 @@ void shouldThrowExceptionIfRecordingNotFound() throws Exception {
ExecutionException e = Mockito.mock(ExecutionException.class);
when(future.get()).thenThrow(e);
when(e.getCause())
.thenReturn(new RecordingNotFoundException(subdirectoryName, recordingName));
.thenReturn(new RecordingNotFoundException("foo", recordingName));

ApiException ex =
Assertions.assertThrows(ApiException.class, () -> handler.handle(params));
Expand All @@ -182,11 +184,12 @@ void shouldThrowExceptionIfRecordingNotFound() throws Exception {

@Test
void shouldDoUpload() throws Exception {
when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn("foo");
when(params.getPathParams())
.thenReturn(
Map.of(
"subdirectoryName",
subdirectoryName,
"jvmId",
jvmId,
"recordingName",
recordingName));
when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -236,11 +239,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleInvalidResponseStatusCode() throws Exception {
when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn("someSubdirectory");
when(params.getPathParams())
.thenReturn(
Map.of(
"subdirectoryName",
subdirectoryName,
"jvmId",
jvmId,
"recordingName",
recordingName));
when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -297,11 +301,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleNullStatusMessage() throws Exception {
when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn("someSubdirectory");
when(params.getPathParams())
.thenReturn(
Map.of(
"subdirectoryName",
subdirectoryName,
"jvmId",
jvmId,
"recordingName",
recordingName));
when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -358,11 +363,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleNullResponseBody() throws Exception {
when(jvmIdHelper.jvmIdToSubdirectoryName(jvmId)).thenReturn("someSubdirectory");
when(params.getPathParams())
.thenReturn(
Map.of(
"subdirectoryName",
subdirectoryName,
"jvmId",
jvmId,
"recordingName",
recordingName));
when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down
Loading

0 comments on commit 1350d82

Please sign in to comment.