Commit cddb4a54 authored by amir.yosef's avatar amir.yosef

Adding some tests ( first stage )

parent b7615176
This diff is collapsed.
<component name="libraryTable">
<library name="junit" type="repository">
<properties maven-id="junit:junit:4.13.2" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.2/junit-4.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
...@@ -4,9 +4,28 @@ ...@@ -4,9 +4,28 @@
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" /> <orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="junit" level="project" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="io.quarkus.junit5.mockito" level="project" />
</component> </component>
</module> </module>
\ No newline at end of file
...@@ -18,14 +18,14 @@ public class Main { ...@@ -18,14 +18,14 @@ public class Main {
// server.start(); // server.start();
// } // }
// builder.setPort(16379); // builder.setPort(16379);
executor.submit(() -> { // executor.submit(() -> {
ReplicaConnectionService service = null; // ReplicaConnectionService service = null;
try { // try {
director.buildReplica(builder, service); // director.buildReplica(builder, service);
} catch (IOException e) { // } catch (IOException e) {
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
}); // });
executor.submit(() -> { executor.submit(() -> {
try (Server server = builder.build()) { try (Server server = builder.build()) {
server.start(); server.start();
......
...@@ -13,7 +13,7 @@ public class EchoCommand implements CommandExecutable<byte[]> { ...@@ -13,7 +13,7 @@ public class EchoCommand implements CommandExecutable<byte[]> {
@Override @Override
public byte[] execute() { public byte[] execute() {
if (!args.isEmpty()) { if (!args.isEmpty() && args.getFirst() != null) {
return (Response.getResponse(args.getFirst())); return (Response.getResponse(args.getFirst()));
} else { } else {
return "".getBytes(); return "".getBytes();
......
...@@ -27,7 +27,6 @@ public class FullRsyncCommand implements CommandExecutable<byte[]> { ...@@ -27,7 +27,6 @@ public class FullRsyncCommand implements CommandExecutable<byte[]> {
public byte[] execute() { public byte[] execute() {
synchronized (this) { synchronized (this) {
replicaSender.addConnection(outputStream); replicaSender.addConnection(outputStream);
System.out.println("hi");
byte[] decode = rdbFileInfo.getContent(); byte[] decode = rdbFileInfo.getContent();
try { try {
return createCommandBytes(Command.FULLRESYNC, decode, serverInfo); return createCommandBytes(Command.FULLRESYNC, decode, serverInfo);
......
...@@ -19,7 +19,6 @@ public class ReplicaReplConfCommand implements CommandExecutable<byte[]> { ...@@ -19,7 +19,6 @@ public class ReplicaReplConfCommand implements CommandExecutable<byte[]> {
@Override @Override
public byte[] execute() { public byte[] execute() {
System.out.println("ReplicaReplConfCommand processed command: ");
return commandParser.getResponseFromCommandArray(List.of(Command.REPLCONF.getValue(), "ACK", commandByteCounter.getBytes().toString())).getBytes(); return commandParser.getResponseFromCommandArray(List.of(Command.REPLCONF.getValue(), "ACK", commandByteCounter.getBytes().toString())).getBytes();
} }
......
...@@ -12,7 +12,7 @@ public class Director { ...@@ -12,7 +12,7 @@ public class Director {
} }
public void buildReplica(ServerBuilder builder, ReplicaConnectionService replicaConnectionService) throws IOException { public void buildReplica(ServerBuilder builder, ReplicaConnectionService replicaConnectionService) throws IOException {
replicaConnectionService = new ReplicaConnectionService(builder.getMasterPortAndHost(), 16378); replicaConnectionService = new ReplicaConnectionService(builder.getMasterPortAndHost(), 16380);
replicaConnectionService.checkConnection(); replicaConnectionService.checkConnection();
} }
} }
...@@ -33,10 +33,10 @@ public class ConnectionHandler { ...@@ -33,10 +33,10 @@ public class ConnectionHandler {
} }
public BufferedReader handleConnection() { public BufferedReader handleConnection() {
try (OutputStream outputStream = socket.getOutputStream(); try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { OutputStream outputStream = socket.getOutputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
commandSender.sendCommand(bufferedReader, outputStream); commandSender.sendCommand(bufferedReader, outputStream);
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
if (line.isEmpty()) { if (line.isEmpty()) {
......
package storage; package storage;
import model.RdbFile;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
......
package command;
import org.junit.Test;
import util.Response;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
public class EchoCommandTest {
@Test
public void test_execute_with_non_empty_args() {
List<String> args = List.of("hello");
EchoCommand command = new EchoCommand(args);
byte[] result = command.execute();
byte[] expected = Response.getResponse("hello");
assertArrayEquals(expected, result);
}
@Test
public void test_execute_with_null_args() {
List<String> args = Collections.singletonList((String) null);
EchoCommand command = new EchoCommand(args);
byte[] result = command.execute();
byte[] expected = "".getBytes();
assertArrayEquals(expected, result);
}
@Test
public void test_execute_with_long_strings() {
List<String> longArgs = new ArrayList<>();
StringBuilder longString = new StringBuilder();
for (int i = 0; i < 10000; i++) {
longString.append("a");
}
longArgs.add(longString.toString());
EchoCommand command = new EchoCommand(longArgs);
byte[] result = command.execute();
byte[] expected = Response.getResponse(longString.toString());
assertArrayEquals(expected, result);
}
}
\ No newline at end of file
package command;
// Generated by CodiumAI
import command.handshake.PingCommandSender;
import org.junit.Test;
import parser.CommandParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.*;
public class PingCommandSenderTest {
@Test
public void test_send_command_success() throws IOException {
CommandParser commandParser = mock(CommandParser.class);
PingCommandSender pingCommandSender = new PingCommandSender(commandParser);
BufferedReader bufferedReader = mock(BufferedReader.class);
OutputStream outputStream = mock(OutputStream.class);
when(commandParser.getResponseFromCommandArray(List.of("ping"))).thenReturn("*1\r\n$4\r\nping\r\n");
pingCommandSender.sendCommand(bufferedReader, outputStream);
verify(outputStream).write("*1\r\n$4\r\nping\r\n".getBytes());
verify(outputStream).flush();
}
}
\ No newline at end of file
package command;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
public class PingCommandTest {
@Test
public void test_execute_returns_correct_pong_response() {
PingCommand pingCommand = new PingCommand();
byte[] expectedResponse = "+PONG\r\n".getBytes();
byte[] actualResponse = pingCommand.execute();
assertArrayEquals(expectedResponse, actualResponse);
}
@Test
public void test_execute_handles_null_values_gracefully() {
PingCommand pingCommand = new PingCommand();
byte[] response = pingCommand.execute();
assertNotNull(response);
}
}
\ No newline at end of file
package command;
import org.junit.Test;
import java.util.List;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
public class ReplConfCommandTest {
@Test
public void test_execute_returns_null_for_ack() {
List<String> args = List.of("ACK");
ReplConfCommand command = new ReplConfCommand(args);
byte[] result = command.execute();
assertNull(result);
}
@Test
public void test_execute_with_empty_args_list() {
List<String> args = List.of();
ReplConfCommand command = new ReplConfCommand(args);
assertThrows(NoSuchElementException.class, command::execute);
}
}
\ No newline at end of file
package command;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class UnknownCommandTest {
@Test
public void test_execute_with_message() {
String testMessage = "Test Message";
UnknownCommand command = new UnknownCommand(testMessage);
byte[] result = command.execute();
assertArrayEquals(testMessage.getBytes(), result);
}
@Test
public void test_execute_with_null_message() {
UnknownCommand command = new UnknownCommand();
byte[] result = command.execute();
assertArrayEquals("-Unknown Command\r\n".getBytes(), result);
}
}
\ 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