Skip to content
Open
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 @@ -228,6 +228,7 @@ protected MetaDataRegisterDTO buildMetaDataDTO(final Object bean, @NonNull final
.rpcType(RpcTypeEnum.WEB_SOCKET.getName())
.enabled(true)
.ruleName(StringUtils.defaultIfBlank(webSocketClient.ruleName(), getContextPath()))
.registerMetaData(webSocketClient.registerMetaData())
.namespaceId(namespaceId)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
import org.apache.shenyu.client.spring.websocket.annotation.ShenyuServerEndpoint;
import org.apache.shenyu.client.spring.websocket.annotation.ShenyuSpringWebSocketClient;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
Expand All @@ -31,6 +32,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -44,11 +49,13 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -119,6 +126,35 @@ public void testHandle() {
eventListener.handle("mock", mockClass);
}

@ParameterizedTest
@MethodSource("metadataRegistrationCases")
void testHandlePreservesRegisterMetaData(final Object bean, final boolean registerMetaData) {
try (MockedStatic<ShenyuClientRegisterEventPublisher> publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) {
publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(publisher);
SpringWebSocketClientEventListener listener = buildEventListener(false);

listener.handle("endpoint", bean);

ArgumentCaptor<MetaDataRegisterDTO> captor = ArgumentCaptor.forClass(MetaDataRegisterDTO.class);
verify(publisher, atLeastOnce()).publishEvent(captor.capture());
for (MetaDataRegisterDTO metadata : captor.getAllValues()) {
assertEquals(registerMetaData, metadata.isRegisterMetaData());
assertEquals(RpcTypeEnum.WEB_SOCKET.getName(), metadata.getRpcType());
assertEquals("/contextPath", metadata.getContextPath());
}
}
}

private static Stream<Arguments> metadataRegistrationCases() {
return Stream.of(
Arguments.of(new MockClass(), false),
Arguments.of(new MetadataEnabledClient(), true),
Arguments.of(new MetadataDisabledClient(), false),
Arguments.of(new DefaultEndpoint(), false),
Arguments.of(new MetadataEnabledEndpoint(), true),
Arguments.of(new MetadataDisabledEndpoint(), false));
}

@Test
public void testBuildApiSuperPath() {
String annotationPath = "/path";
Expand Down Expand Up @@ -209,4 +245,34 @@ public void mockMethod() {
}
}

@ShenyuSpringWebSocketClient(registerMetaData = true)
private static class MetadataEnabledClient {
public void onMessage() {
}
}

@ShenyuSpringWebSocketClient(registerMetaData = false)
private static class MetadataDisabledClient {
public void onMessage() {
}
}

@ShenyuServerEndpoint("/default")
private static class DefaultEndpoint {
public void onMessage() {
}
}

@ShenyuServerEndpoint(value = "/enabled", registerMetaData = true)
private static class MetadataEnabledEndpoint {
public void onMessage() {
}
}

@ShenyuServerEndpoint(value = "/disabled", registerMetaData = false)
private static class MetadataDisabledEndpoint {
public void onMessage() {
}
}

}
Loading