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 @@ -94,6 +94,7 @@ public void handlerSelector(final SelectorData selectorData) {
Object hostObj = Objects.isNull(rabbitmqJsonMap) ? null : rabbitmqJsonMap.get("host");
if (Objects.isNull(hostObj) || !(hostObj instanceof String) || ((String) hostObj).trim().isEmpty()) {
RabbitmqClientCache.getInstance().invalidate(selectorData.getId());
getSelectApiConfigMap().remove(selectorData.getId());
Optional.ofNullable(RabbitmqClientCache.getInstance().getClientCache())
.filter(Map::isEmpty)
.ifPresent(map -> MULTI_CLIENT.set(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.MockedConstruction;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -49,6 +52,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mockConstruction;

/**
* The Test Case For LoggingRabbitmqPluginDataHandler.
Expand Down Expand Up @@ -120,6 +124,7 @@ public void testHandlerPluginUpdateSameConfig() {

PluginData pluginData = createPluginData();
pluginData.setConfig(GsonUtils.getGson().toJson(existingConfig));
LoggingRabbitmqPluginDataHandler.getPluginGlobalConfigMap().put(pluginData.getId(), existingConfig);

loggingRabbitmqPluginDataHandler.handlerPlugin(pluginData);

Expand Down Expand Up @@ -179,6 +184,40 @@ public void testHandlerSelectorWithEmptyHandle() {
Assertions.assertNull(RabbitmqClientCache.getInstance().getClientCache().get(selectorData.getId()));
}

@ParameterizedTest
@ValueSource(strings = {"{}", "", "null", "{\"host\":\" \"}", "{\"host\":1}"})
public void testRestoreSameSelectorConfigAfterUsingGlobal(final String globalHandle) {
RabbitmqClientCache cache = RabbitmqClientCache.getInstance();
cache.invalidateAll();
LoggingRabbitmqPluginDataHandler.getSelectApiConfigMap().clear();
String customHandle = GsonUtils.getGson().toJson(createValidConfig());
selectorData.setHandle(customHandle);
try (MockedConstruction<RabbitmqLogCollectClient> clients = mockConstruction(RabbitmqLogCollectClient.class)) {
loggingRabbitmqPluginDataHandler.handlerSelector(selectorData);
final RabbitmqLogCollectClient first = cache.getRabbitmqClient(selectorData.getId());
loggingRabbitmqPluginDataHandler.handlerSelector(selectorData);
Assertions.assertEquals(1, clients.constructed().size());
selectorData.setHandle(globalHandle);
loggingRabbitmqPluginDataHandler.handlerSelector(selectorData);
verify(first).close();
Assertions.assertNull(cache.getRabbitmqClient(selectorData.getId()));
Assertions.assertFalse(LoggingRabbitmqPluginDataHandler.getMultiClient());
selectorData.setHandle(customHandle);
loggingRabbitmqPluginDataHandler.handlerSelector(selectorData);
Assertions.assertEquals(2, clients.constructed().size());
Assertions.assertSame(clients.constructed().get(1), cache.getRabbitmqClient(selectorData.getId()));
Assertions.assertTrue(LoggingRabbitmqPluginDataHandler.getMultiClient());
selectorData.setHandle(customHandle.replace("topic.logging", "topic.changed"));
loggingRabbitmqPluginDataHandler.handlerSelector(selectorData);
Assertions.assertEquals(3, clients.constructed().size());
verify(clients.constructed().get(1)).close();
} finally {
selectorData.setHandle("{}");
loggingRabbitmqPluginDataHandler.handlerSelector(selectorData);
LoggingRabbitmqPluginDataHandler.getSelectApiConfigMap().clear();
}
}

private PluginData createPluginData() {
PluginData pluginData = new PluginData();
pluginData.setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static RuleData cacheRule(final String pluginName, final String selectorI
.selectorId(selectorId)
.name(pluginName + "-rule")
.enabled(true)
.loged(true)
.logged(true)
.sort(1)
.matchMode(MatchModeEnum.AND.getCode())
.matchRestful(false)
Expand Down