Commit 5fed48e8 authored by amir.yosef's avatar amir.yosef

Final

parent 49e510a1
package command; package command;
import command.validation.Validatable;
import java.io.IOException; import java.io.IOException;
import java.util.List;
public interface CommandExecutable<T> { public interface CommandExecutable<T> {
T execute() throws IOException; T execute() throws IOException;
class GetCommandValidator implements Validatable {
private static final GetCommandValidator INSTANCE = new GetCommandValidator();
private GetCommandValidator() {
}
public static GetCommandValidator getInstance() {
return INSTANCE;
}
@Override
public boolean isValid(List<String> args) {
return args != null && args.size() == 1;
}
}
} }
package command; package command;
import command.validation.GetCommandValidator;
import command.validation.Validatable; import command.validation.Validatable;
import storage.Cacheable; import storage.Cacheable;
import storage.Storage; import storage.Storage;
......
package command.validation;
import java.util.List;
public final class GetCommandValidator implements Validatable {
private static final GetCommandValidator INSTANCE = new GetCommandValidator();
private GetCommandValidator() {
}
public static GetCommandValidator getInstance() {
return INSTANCE;
}
@Override
public boolean isValid(List<String> args) {
return args != null && args.size() == 1;
}
}
\ No newline at end of file
package storage;
import server.ServiceInfo;
public class CachePolicyFactory {
private static final ServiceInfo serviceInfo = ServiceInfo.getInstance();
public static CachePolicy<String, String> getPolicy(int capacity) {
if (serviceInfo.getInfo().get("cachePolicyName").equals("caffeine")) {
return new CaffeineCachePolicy<>(capacity);
} else if (serviceInfo.getInfo().get("cachePolicyName").equals("lru")) {
return new LRUCachePolicy<>(capacity, 800000);
}
throw new IllegalArgumentException("Invalid CachePolicy name");
}
}
package storage; package storage;
import util.RdbFileReader;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
......
package storage; package storage;
import util.RdbFileReader;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
......
package storage; package util;
import util.RdbFileInfo;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
......
package client;
import client.master.MasterClient;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
import static org.mockito.Mockito.*;
public class MasterClientTest {
@Test
public void test_successful_connection_and_command_processing() throws IOException {
Socket socket = mock(Socket.class);
BufferedReader reader = new BufferedReader(new StringReader("COMMAND\n"));
OutputStream outputStream = mock(OutputStream.class);
when(socket.getInputStream()).thenReturn(new ByteArrayInputStream("COMMAND\n".getBytes()));
when(socket.getOutputStream()).thenReturn(outputStream);
MasterClient masterClient = new MasterClient(socket);
masterClient.run();
verify(outputStream, times(1)).write(any(byte[].class));
verify(outputStream, times(1)).flush();
}
@Test
public void test_handle_client_with_null_buffered_reader() throws IOException {
Socket socket = mock(Socket.class);
OutputStream outputStream = mock(OutputStream.class);
when(socket.getInputStream()).thenReturn(new ByteArrayInputStream("COMMAND\n".getBytes()));
when(socket.getOutputStream()).thenReturn(outputStream);
MasterClient masterClient = new MasterClient(socket);
masterClient.run();
verify(outputStream, times(1)).write(any(byte[].class));
verify(outputStream, times(1)).flush();
}
}
\ No newline at end of file
package client;
import client.replica.ReplicaClient;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import static org.mockito.Mockito.*;
public class ReplicaClientTest {
@Test
public void test_readLine_returns_null() throws IOException {
Socket mockSocket = mock(Socket.class);
BufferedReader mockReader = mock(BufferedReader.class);
OutputStream mockOutputStream = mock(OutputStream.class);
when(mockSocket.getOutputStream()).thenReturn(mockOutputStream);
when(mockReader.readLine()).thenReturn(null);
ReplicaClient replicaClient = new ReplicaClient(mockReader, mockSocket);
replicaClient.run();
verify(mockReader, times(1)).readLine();
verify(mockOutputStream, never()).write(any(byte[].class));
}
}
\ No newline at end of file
package command;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;
public class CommandInvokerTest {
@Test
public void test_command_execution_success() throws IOException {
CommandExecutable<byte[]> command = () -> "test".getBytes();
byte[] result = CommandInvoker.invoke(command);
assertArrayEquals("test".getBytes(), result);
}
@Test
public void test_command_execution_throws_ioexception() {
CommandExecutable<byte[]> command = () -> {
throw new IOException("Test exception");
};
assertThrows(IOException.class, () -> CommandInvoker.invoke(command));
}
}
\ No newline at end of file
package command;
import org.junit.Assert;
import org.junit.Test;
import server.SendToReplica;
import server.ServiceInfo;
import util.RdbFileInfo;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FullRsyncCommandTest {
@Test
public void test_rdb_file_content_empty() throws Exception {
SendToReplica mockReplicaSender = mock(SendToReplica.class);
RdbFileInfo mockRdbFileInfo = mock(RdbFileInfo.class);
ServiceInfo mockServiceInfo = mock(ServiceInfo.class);
OutputStream mockOutputStream = new ByteArrayOutputStream();
byte[] expectedContent = new byte[0];
when(mockRdbFileInfo.getContent()).thenReturn(expectedContent);
when(mockServiceInfo.getInfo()).thenReturn(Map.of("master_replid", "8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb"));
FullRsyncCommand command = new FullRsyncCommand(mockReplicaSender, mockOutputStream);
byte[] result = command.execute();
ByteArrayOutputStream expectedOutput = new ByteArrayOutputStream();
expectedOutput.write(("+FULLRESYNC 8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb 0\r\n").getBytes());
expectedOutput.write(("$" + expectedContent.length + "\r\n").getBytes());
expectedOutput.write(expectedContent);
assertArrayEquals(expectedOutput.toByteArray(), result);
}
@Test
public void test_execute_fullresync_command() throws Exception {
SendToReplica mockReplicaSender = mock(SendToReplica.class);
RdbFileInfo mockRdbFileInfo = mock(RdbFileInfo.class);
ServiceInfo mockServiceInfo = mock(ServiceInfo.class);
OutputStream mockOutputStream = new ByteArrayOutputStream();
byte[] expectedContent = "testContent".getBytes();
when(mockRdbFileInfo.getContent()).thenReturn(expectedContent);
when(mockServiceInfo.getInfo()).thenReturn(Map.of("master_replid", "8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb"));
FullRsyncCommand command = new FullRsyncCommand(mockReplicaSender, mockOutputStream);
byte[] result = command.execute();
ByteArrayOutputStream expectedOutput = new ByteArrayOutputStream();
expectedOutput.write(("+FULLRESYNC 8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb 0\r\n").getBytes());
expectedOutput.write(("$" + expectedContent.length + "\r\n").getBytes());
expectedOutput.write(expectedContent);
Assert.assertArrayEquals(expectedOutput.toByteArray(), result);
}
}
package command;
import org.junit.Test;
import util.Response;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
public class GetCommandTest {
@Test
public void test_execute_with_empty_arguments() {
List<String> args = List.of();
GetCommand getCommand = new GetCommand(args);
byte[] expectedResponse = Response.getResponse("wrong args");
byte[] actualResponse = getCommand.execute();
assertArrayEquals(expectedResponse, actualResponse);
}
}
\ No newline at end of file
package command;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;
public class InfoCommandTest {
@Test
public void test_valid_command_with_correct_argument() {
List<String> args = List.of("replication");
InfoCommand infoCommand = new InfoCommand(args);
byte[] response = infoCommand.execute();
String expectedResponse = "$20\r\nrole:mastercachePolicyName:caffeine\r\n";
assertArrayEquals(expectedResponse.getBytes(), response);
}
@Test
public void test_null_argument_list() {
List<String> args = null;
InfoCommand infoCommand = new InfoCommand(args);
byte[] response = infoCommand.execute();
String expectedResponse = "$16\r\nunsupported args\r\n";
assertArrayEquals(expectedResponse.getBytes(), response);
}
}
\ No newline at end of file
package command;
import org.junit.Test;
import org.mockito.Mockito;
import storage.Cacheable;
import util.Response;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
public class SetCommandTest {
@Test
public void execute_with_valid_args_size_2_returns_ok_response() {
List<String> args = Arrays.asList("key", "value");
Cacheable storageMock = Mockito.mock(Cacheable.class);
SetCommand setCommand = new SetCommand(args);
byte[] expectedResponse = "+OK\r\n".getBytes();
byte[] actualResponse = setCommand.execute();
assertArrayEquals(expectedResponse, actualResponse);
}
@Test
public void execute_with_null_args_returns_error_response() {
List<String> args = null;
Cacheable storageMock = Mockito.mock(Cacheable.class);
SetCommand setCommand = new SetCommand(args);
byte[] expectedResponse = Response.getResponse("wrong args");
byte[] actualResponse = setCommand.execute();
assertArrayEquals(expectedResponse, actualResponse);
}
}
\ No newline at end of file
package factory;
import command.CommandExecutable;
import command.PingCommand;
import model.Command;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class CommandFactoryTest {
@Test
public void test_get_command_returns_ping_command() {
CommandFactory factory = CommandFactory.getInstance();
CommandExecutable<?> command = factory.getCommand(Command.PING, List.of());
assertTrue(command instanceof PingCommand);
}
}
package factory;
import command.CommandExecutable;
import factory.replica.ReplicaCommandFactory;
import model.Command;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
public class ReplicaCommandFactoryTest {
@Test
public void test_get_instance_returns_same_instance() {
ReplicaCommandFactory instance1 = ReplicaCommandFactory.getInstance();
ReplicaCommandFactory instance2 = ReplicaCommandFactory.getInstance();
assertSame(instance1, instance2);
}
@Test
public void test_get_command_returns_null_for_unknown_command() {
ReplicaCommandFactory factory = ReplicaCommandFactory.getInstance();
CommandExecutable<?> commandExecutable = factory.getCommand(Command.UNKNOWN, List.of());
assertNull(commandExecutable);
}
}
\ No newline at end of file
package parser;
import org.junit.Test;
import java.io.BufferedReader;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
public class ParseCommandTest {
@Test
public void test_single_command_without_asterisk() {
CommandParser parser = new CommandParser();
BufferedReader bufferedReader = mock(BufferedReader.class);
String line = "COMMAND";
List<String> result = parser.parseCommand(bufferedReader, line);
assertEquals(1, result.size());
assertEquals("COMMAND", result.get(0));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment