Skip to content

Commit

Permalink
send jvmId through notifications to -web
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Oct 6, 2023
1 parent 5694f98 commit d3e6b03
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public class RecordingsFromIdPostHandler extends AbstractAuthenticatedRequestHan
private final FileSystem fs;
private final JvmIdHelper idHelper;
private final NotificationFactory notificationFactory;
private final JvmIdHelper jvmIdHelper;
private final RecordingArchiveHelper recordingArchiveHelper;
private final RecordingMetadataManager recordingMetadataManager;
private final Path savedRecordingsPath;
Expand All @@ -87,7 +86,6 @@ public class RecordingsFromIdPostHandler extends AbstractAuthenticatedRequestHan
FileSystem fs,
JvmIdHelper idHelper,
NotificationFactory notificationFactory,
JvmIdHelper jvmIdHelper,
RecordingArchiveHelper recordingArchiveHelper,
RecordingMetadataManager recordingMetadataManager,
@Named(MainModule.RECORDINGS_PATH) Path savedRecordingsPath,
Expand All @@ -98,7 +96,6 @@ public class RecordingsFromIdPostHandler extends AbstractAuthenticatedRequestHan
this.fs = fs;
this.idHelper = idHelper;
this.notificationFactory = notificationFactory;
this.jvmIdHelper = jvmIdHelper;
this.recordingArchiveHelper = recordingArchiveHelper;
this.recordingMetadataManager = recordingMetadataManager;
this.savedRecordingsPath = savedRecordingsPath;
Expand Down Expand Up @@ -303,13 +300,12 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
"target",
connectUrl,
"jvmId",
jvmIdHelper.getJvmId(connectUrl)))
jvmId))
.build()
.send();
} catch (URISyntaxException
| UnknownHostException
| SocketException
| JvmIdGetException e) {
| SocketException e) {
logger.error(e);
throw new ApiException(500, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public Future<ArchivedRecordingInfo> saveRecording(
"target",
connectionDescriptor.getTargetId(),
"jvmId",
jvmIdHelper.getJvmId(connectionDescriptor)))
jvmIdHelper.getJvmId(connectionDescriptor.getTargetId())))
.build()
.send();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public String answer(InvocationOnMock invocation) throws Throwable {

MatcherAssert.assertThat(
messageCaptor.getValue(),
Matchers.equalTo(Map.of("recording", recordingInfo, "target", mockConnectUrl)));
Matchers.equalTo(Map.of("recording", recordingInfo, "target", mockConnectUrl, "jvmId", mockJvmId)));
}

@Test
Expand Down Expand Up @@ -491,7 +491,7 @@ public String answer(InvocationOnMock invocation) throws Throwable {

MatcherAssert.assertThat(
messageCaptor.getValue(),
Matchers.equalTo(Map.of("recording", recordingInfo, "target", mockConnectUrl)));
Matchers.equalTo(Map.of("recording", recordingInfo, "target", mockConnectUrl, "jvmId", mockJvmId)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.recordings.JvmIdHelper;

import com.google.gson.Gson;
import io.vertx.core.MultiMap;
Expand All @@ -54,7 +55,9 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

@ExtendWith(MockitoExtension.class)
public class TargetProbePostHandlerTest {
Expand All @@ -68,6 +71,7 @@ public class TargetProbePostHandlerTest {
@Mock NotificationFactory notificationFactory;
@Mock Notification notification;
@Mock Notification.Builder notificationBuilder;
@Mock JvmIdHelper jvmIdHelper;
@Mock TargetConnectionManager targetConnectionManager;
@Mock Environment env;
Gson gson = MainModule.provideGson(logger);
Expand All @@ -86,6 +90,7 @@ void setup() {
.thenReturn(notificationBuilder);
lenient().when(notificationBuilder.message(Mockito.any())).thenReturn(notificationBuilder);
lenient().when(notificationBuilder.build()).thenReturn(notification);

this.handler =
new TargetProbePostHandler(
logger,
Expand All @@ -94,6 +99,7 @@ void setup() {
fs,
auth,
credentialsManager,
jvmIdHelper,
targetConnectionManager,
env,
gson);
Expand Down Expand Up @@ -144,7 +150,7 @@ class Requests {
@Test
public void shouldRespondOK() throws Exception {
Mockito.when(requestParams.getPathParams())
.thenReturn(Map.of("targetId", "foo", "probeTemplate", "bar"));
.thenReturn(Map.of("targetId", "foo", "probeTemplate", "bar", "jvmId", "id"));
Mockito.when(requestParams.getHeaders()).thenReturn(MultiMap.caseInsensitiveMultiMap());
JFRConnection connection = Mockito.mock(JFRConnection.class);
IConnectionHandle handle = Mockito.mock(IConnectionHandle.class);
Expand All @@ -170,6 +176,7 @@ public void shouldRespondOK() throws Exception {
+ " </event> </events> </jfragent>";
Mockito.when(templateService.getTemplateContent(Mockito.anyString()))
.thenReturn(templateContent);
Mockito.when(jvmIdHelper.getJvmId(Mockito.anyString())).thenReturn("id");
Mockito.when(connection.getHandle()).thenReturn(handle);
Mockito.when(handle.getServiceOrDummy(MBeanServerConnection.class)).thenReturn(mbsc);
Object result = Mockito.mock(Object.class);
Expand All @@ -188,6 +195,7 @@ public void shouldRespondOK() throws Exception {
MatcherAssert.assertThat(s.get("probeTemplate"), Matchers.equalTo("bar"));
MatcherAssert.assertThat(s.get("targetId"), Matchers.equalTo("foo"));
MatcherAssert.assertThat(s.get("events"), Matchers.instanceOf(List.class));
MatcherAssert.assertThat(s.get("jvmId"), Matchers.equalTo("id"));
MatcherAssert.assertThat(response.getStatusCode(), Matchers.equalTo(200));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
Mockito.verify(notificationFactory).createBuilder();
Mockito.verify(notificationBuilder).metaCategory("ActiveRecordingSaved");
Mockito.verify(notificationBuilder).metaType(HttpMimeType.JSON);
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId));
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId, "jvmId", "mockId"));
Mockito.verify(notificationBuilder).build();
Mockito.verify(notification).send();
}
Expand Down Expand Up @@ -355,7 +355,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
"recording",
info,
"target",
serviceRef1.getServiceUri().toString()));
serviceRef1.getServiceUri().toString(),
"jvmId",
"mockId"));
Mockito.verify(notificationBuilder).build();
Mockito.verify(notification).send();
}
Expand Down Expand Up @@ -443,7 +445,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
Mockito.verify(notificationFactory).createBuilder();
Mockito.verify(notificationBuilder).metaCategory("ActiveRecordingSaved");
Mockito.verify(notificationBuilder).metaType(HttpMimeType.JSON);
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId));
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId, "jvmId", "mockId"));
Mockito.verify(notificationBuilder).build();
Mockito.verify(notification).send();
}
Expand Down Expand Up @@ -524,7 +526,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
Mockito.verify(notificationFactory).createBuilder();
Mockito.verify(notificationBuilder).metaCategory("ActiveRecordingSaved");
Mockito.verify(notificationBuilder).metaType(HttpMimeType.JSON);
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId));
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId, "jvmId", "mockId"));
Mockito.verify(notificationBuilder).build();
Mockito.verify(notification).send();
}
Expand Down Expand Up @@ -612,7 +614,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
Mockito.verify(notificationFactory).createBuilder();
Mockito.verify(notificationBuilder).metaCategory("ActiveRecordingSaved");
Mockito.verify(notificationBuilder).metaType(HttpMimeType.JSON);
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId));
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId, "jvmId", "mockId"));
Mockito.verify(notificationBuilder).build();
Mockito.verify(notification).send();
}
Expand Down Expand Up @@ -753,7 +755,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
Mockito.verify(notificationFactory).createBuilder();
Mockito.verify(notificationBuilder).metaCategory("ActiveRecordingSaved");
Mockito.verify(notificationBuilder).metaType(HttpMimeType.JSON);
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId));
Mockito.verify(notificationBuilder).message(Map.of("recording", info, "target", targetId, "jvmId", "mockId"));
Mockito.verify(notificationBuilder).build();
Mockito.verify(notification).send();
}
Expand Down Expand Up @@ -856,7 +858,7 @@ public String answer(InvocationOnMock invocation) throws Throwable {

MatcherAssert.assertThat(
messageCaptor.getValue(),
Matchers.equalTo(Map.of("recording", matcher, "target", "uploads")));
Matchers.equalTo(Map.of("recording", matcher, "target", "uploads", "jvmId", "mockId")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgument(0);
}
});
lenient()
.when(jvmIdHelper.subdirectoryNameToJvmId(Mockito.anyString()))
.thenAnswer(
new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgument(0);
}
});

this.recordingMetadataManager =
new RecordingMetadataManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,6 @@ void setup() {
lenient().when(notificationBuilder.message(Mockito.any())).thenReturn(notificationBuilder);
lenient().when(notificationBuilder.build()).thenReturn(notification);
lenient().when(vertx.setTimer(Mockito.anyLong(), Mockito.any())).thenReturn(1234L);
lenient()
.when(jvmIdHelper.jvmIdToSubdirectoryName(Mockito.anyString()))
.thenAnswer(
new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgument(0);
}
});

this.recordingTargetHelper =
new RecordingTargetHelper(
Expand Down

0 comments on commit d3e6b03

Please sign in to comment.