Skip to content

Commit

Permalink
Added unit tests for most of the pubsub classes that did not have any
Browse files Browse the repository at this point in the history
  • Loading branch information
tishun committed Mar 28, 2024
1 parent 8457a41 commit 347078e
Show file tree
Hide file tree
Showing 3 changed files with 431 additions and 0 deletions.
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

0 comments on commit 347078e

Please sign in to comment.