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

Added unit tests for most of the pubsub classes that did not have any #2808

Merged
merged 1 commit into from
Mar 29, 2024
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
@@ -0,0 +1,127 @@
package io.lettuce.core.pubsub;

import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.output.IntegerOutput;
import io.lettuce.core.output.KeyListOutput;
import io.lettuce.core.output.MapOutput;
import io.lettuce.core.protocol.Command;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static io.lettuce.core.protocol.CommandType.*;
import static org.junit.jupiter.api.Assertions.*;

class PubSubCommandBuilderUnitTests {

private PubSubCommandBuilder<String, String> commandBuilder;
private final RedisCodec<String, String> codec = StringCodec.UTF8;

@BeforeEach
void setup() {
this.commandBuilder = new PubSubCommandBuilder<>(codec);
}

@Test
void publish() {
String channel = "channel";
String message = "message payload";
Command<String, String, Long> command = this.commandBuilder.publish(channel, message);

assertEquals( PUBLISH, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "key<channel> value<message payload>", command.getArgs().toCommandString());
assertInstanceOf(IntegerOutput.class, command.getOutput());
}

@Test
void pubsubChannels() {
String pattern = "channelPattern";
Command<String, String, List<String>> command = this.commandBuilder.pubsubChannels(pattern);

assertEquals( PUBSUB, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "CHANNELS key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(KeyListOutput.class, command.getOutput());
}

@Test
void pubsubNumsub() {
String pattern = "channelPattern";
Command<String, String, Map<String, Long>> command = this.commandBuilder.pubsubNumsub(pattern);

assertEquals( PUBSUB, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "NUMSUB key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(MapOutput.class, command.getOutput());
}

@Test
void pubsubShardChannels() {
String pattern = "channelPattern";
Command<String, String, List<String>> command = this.commandBuilder.pubsubShardChannels(pattern);

assertEquals( PUBSUB, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "SHARDCHANNELS key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(KeyListOutput.class, command.getOutput());
}

@Test
void pubsubShardNumsub() {
String pattern = "channelPattern";
Command<String, String, Map<String, Long>> command = this.commandBuilder.pubsubShardNumsub(pattern);

assertEquals( PUBSUB, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "SHARDNUMSUB key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(MapOutput.class, command.getOutput());
}

@Test
void psubscribe() {
String pattern = "channelPattern";
Command<String, String, String> command = this.commandBuilder.psubscribe(pattern);

assertEquals( PSUBSCRIBE, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(PubSubOutput.class, command.getOutput());
}

@Test
void punsubscribe() {
String pattern = "channelPattern";
Command<String, String, String> command = this.commandBuilder.punsubscribe(pattern);

assertEquals( PUNSUBSCRIBE, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(PubSubOutput.class, command.getOutput());
}

@Test
void subscribe() {
String pattern = "channelPattern";
Command<String, String, String> command = this.commandBuilder.subscribe(pattern);

assertEquals( SUBSCRIBE, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(PubSubOutput.class, command.getOutput());
}

@Test
void unsubscribe() {
String pattern = "channelPattern";
Command<String, String, String> command = this.commandBuilder.unsubscribe(pattern);

assertEquals( UNSUBSCRIBE, command.getType());
assertInstanceOf(PubSubCommandArgs.class, command.getArgs());
assertEquals( "key<channelPattern>", command.getArgs().toCommandString());
assertInstanceOf(PubSubOutput.class, command.getOutput());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package io.lettuce.core.pubsub;

import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.output.IntegerOutput;
import io.lettuce.core.output.KeyListOutput;
import io.lettuce.core.output.MapOutput;
import io.lettuce.core.protocol.AsyncCommand;
import io.lettuce.core.protocol.RedisCommand;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.concurrent.ExecutionException;

import static io.lettuce.core.protocol.CommandType.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.Mockito.*;

class RedisPubSubAsyncCommandsImplUnitTests {

private RedisPubSubAsyncCommandsImpl<String,String> commands;
private StatefulRedisPubSubConnection<String, String> mockedConnection;
private final RedisCodec<String, String> codec = StringCodec.UTF8;

@BeforeEach
void setup() {
mockedConnection = mock(StatefulRedisPubSubConnection.class);
commands = new RedisPubSubAsyncCommandsImpl<>(mockedConnection, codec);
}

@Test
void psubscribe() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";
AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.psubscribe(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PSUBSCRIBE, capturedCommand.getValue().getType());
assertInstanceOf(PubSubOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void punsubscribe() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";
AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.punsubscribe(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PUNSUBSCRIBE, capturedCommand.getValue().getType());
assertInstanceOf(PubSubOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void subscribe() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";
AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.subscribe(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( SUBSCRIBE, capturedCommand.getValue().getType());
assertInstanceOf(PubSubOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void unsubscribe() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";
AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.unsubscribe(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( UNSUBSCRIBE, capturedCommand.getValue().getType());
assertInstanceOf(PubSubOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void publish() throws ExecutionException, InterruptedException {
String channel = "acmeChannel";
String message = "acmeMessage";

AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.publish(channel, message).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PUBLISH, capturedCommand.getValue().getType());
assertInstanceOf(IntegerOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "key<acmeChannel> value<acmeMessage>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void pubsubChannels() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";

AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.pubsubChannels(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PUBSUB, capturedCommand.getValue().getType());
assertInstanceOf(KeyListOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "CHANNELS key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void pubsubNumsub() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";

AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.pubsubNumsub(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PUBSUB, capturedCommand.getValue().getType());
assertInstanceOf(MapOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "NUMSUB key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void pubsubShardChannels() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";

AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.pubsubShardChannels(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PUBSUB, capturedCommand.getValue().getType());
assertInstanceOf(KeyListOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "SHARDCHANNELS key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}

@Test
void pubsubShardNumsub() throws ExecutionException, InterruptedException {
String pattern = "channelPattern";

AsyncCommand dispachedMock = mock (AsyncCommand.class);
when(mockedConnection.dispatch((RedisCommand<String, String, Object>) any())).thenReturn(dispachedMock);

commands.pubsubShardNumsub(pattern).get();

ArgumentCaptor<AsyncCommand> capturedCommand = ArgumentCaptor.forClass(AsyncCommand.class);;
verify(mockedConnection).dispatch(capturedCommand.capture());

Assertions.assertEquals( PUBSUB, capturedCommand.getValue().getType());
assertInstanceOf(MapOutput.class, capturedCommand.getValue().getOutput());
Assertions.assertEquals( "SHARDNUMSUB key<channelPattern>", capturedCommand.getValue().getArgs().toCommandString());

assertNotEquals(capturedCommand.getValue(), dispachedMock);
}
}
Loading
Loading