Add tests for CashShopSurpriseHandler
This commit is contained in:
12
pom.xml
12
pom.xml
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cosmic-maplestory</groupId>
|
||||
@@ -52,7 +53,8 @@
|
||||
<!-- Maven plugins -->
|
||||
<maven-surefire-plugin.version>3.2.5</maven-surefire-plugin.version> <!-- For running unit tests -->
|
||||
<maven-jar-plugin.version>3.4.1</maven-jar-plugin.version> <!-- Disabled. (for building thin jar) -->
|
||||
<maven-assembly-plugin.version>3.7.1</maven-assembly-plugin.version> <!-- For packaging the executable fat jar -->
|
||||
<maven-assembly-plugin.version>3.7.1
|
||||
</maven-assembly-plugin.version> <!-- For packaging the executable fat jar -->
|
||||
|
||||
<!-- Dependencies -->
|
||||
<slf4j-api.version>2.0.13</slf4j-api.version> <!-- Logging facade -->
|
||||
@@ -180,6 +182,12 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -82,6 +82,11 @@ public class ByteBufInPacket implements InPacket {
|
||||
return byteBuf.readerIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof ByteBufInPacket other && byteBuf.equals(other.byteBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final int readerIndex = byteBuf.readerIndex();
|
||||
|
||||
@@ -91,4 +91,9 @@ public class ByteBufOutPacket implements OutPacket {
|
||||
public void skip(int numberOfBytes) {
|
||||
writeBytes(new byte[numberOfBytes]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof ByteBufOutPacket other && byteBuf.equals(other.byteBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6480,14 +6480,15 @@ public class PacketCreator {
|
||||
return p;
|
||||
}
|
||||
|
||||
public static Packet onCashGachaponOpenSuccess(int accountid, long sn, int remainingBoxes, Item item, int itemid, int nSelectedItemCount, boolean bJackpot) {
|
||||
public static Packet onCashGachaponOpenSuccess(int accountid, long boxCashId, int remainingBoxes, Item reward,
|
||||
int rewardItemId, int rewardQuantity, boolean bJackpot) {
|
||||
OutPacket p = OutPacket.create(SendOpcode.CASHSHOP_CASH_ITEM_GACHAPON_RESULT);
|
||||
p.writeByte(0xE5); // subopcode thanks to Ubaware
|
||||
p.writeLong(sn);// sn of the box used
|
||||
p.writeLong(boxCashId);
|
||||
p.writeInt(remainingBoxes);
|
||||
addCashItemInformation(p, item, accountid);
|
||||
p.writeInt(itemid);// the itemid of the liSN?
|
||||
p.writeByte(nSelectedItemCount);// the total count now? o.O
|
||||
addCashItemInformation(p, reward, accountid);
|
||||
p.writeInt(rewardItemId);
|
||||
p.writeByte(rewardQuantity); // nSelectedItemCount
|
||||
p.writeBool(bJackpot);// "CashGachaponJackpot"
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -235,4 +235,12 @@ class ByteBufInPacketTest {
|
||||
|
||||
assertEquals(initial.length(), afterReadingOpcode.length());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsShouldCompareBytes() {
|
||||
ByteBufInPacket packet1 = new ByteBufInPacket(Unpooled.wrappedBuffer(new byte[]{ 11, 22, 33, 44 }));
|
||||
ByteBufInPacket packet2 = new ByteBufInPacket(Unpooled.wrappedBuffer(new byte[]{ 11, 22, 33, 44 }));
|
||||
|
||||
assertEquals(packet1, packet2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,4 +203,14 @@ class ByteBufOutPacketTest {
|
||||
assertEquals(0, wrapped.readByte());
|
||||
assertEquals(secondWrittenByte, wrapped.readByte());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsShouldCompareBytes() {
|
||||
ByteBufOutPacket packet1 = new ByteBufOutPacket();
|
||||
packet1.writeBytes(new byte[] { 55, 66, 77, 88 });
|
||||
ByteBufOutPacket packet2 = new ByteBufOutPacket();
|
||||
packet2.writeBytes(new byte[] { 55, 66, 77, 88 });
|
||||
|
||||
assertEquals(packet1, packet2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.server.channel.handlers;
|
||||
|
||||
import client.inventory.Item;
|
||||
import constants.id.ItemId;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import server.CashShop;
|
||||
import testutil.HandlerTest;
|
||||
import testutil.Items;
|
||||
import testutil.Packets;
|
||||
import tools.PacketCreator;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CashShopSurpriseHandlerTest extends HandlerTest {
|
||||
private final CashShopSurpriseHandler handler = new CashShopSurpriseHandler();
|
||||
|
||||
@Mock
|
||||
private CashShop cashShop;
|
||||
|
||||
@BeforeEach
|
||||
void prepareCashShop() {
|
||||
when(chr.getCashShop()).thenReturn(cashShop);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDoNothingWhenCsIsNotOpened() {
|
||||
when(cashShop.isOpened()).thenReturn(false);
|
||||
|
||||
handler.handlePacket(Packets.emptyInPacket(), client);
|
||||
|
||||
verify(cashShop, never()).openCashShopSurprise();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendFailurePacketWhenFailToOpen() {
|
||||
when(cashShop.isOpened()).thenReturn(true);
|
||||
when(cashShop.openCashShopSurprise()).thenReturn(Optional.empty());
|
||||
|
||||
handler.handlePacket(Packets.emptyInPacket(), client);
|
||||
|
||||
verify(client).sendPacket(PacketCreator.onCashItemGachaponOpenFailed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendSuccessPacketWhenSuccessfullyOpen() {
|
||||
when(cashShop.isOpened()).thenReturn(true);
|
||||
Item cashShopSurprise = Items.itemWithQuantity(ItemId.CASH_SHOP_SURPRISE, 3);
|
||||
Item reward = Items.itemWithQuantity(5000012, 1);
|
||||
when(cashShop.openCashShopSurprise()).thenReturn(Optional.of(new CashShop.CashShopSurpriseResult(
|
||||
cashShopSurprise, reward)));
|
||||
|
||||
handler.handlePacket(Packets.emptyInPacket(), client);
|
||||
|
||||
verify(client).sendPacket(PacketCreator.onCashGachaponOpenSuccess(ACCOUNT_ID, cashShopSurprise.getCashId(), 3,
|
||||
reward, 5000012, 1, true));
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,10 @@ public class AnyValues {
|
||||
return "string";
|
||||
}
|
||||
|
||||
public static short anyShort() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
public static DaoException daoException() {
|
||||
return new DaoException(string(), new RuntimeException());
|
||||
}
|
||||
|
||||
24
src/test/java/testutil/HandlerTest.java
Normal file
24
src/test/java/testutil/HandlerTest.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package testutil;
|
||||
|
||||
import client.Character;
|
||||
import client.Client;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import static org.mockito.Mockito.lenient;
|
||||
|
||||
public abstract class HandlerTest {
|
||||
protected static final int ACCOUNT_ID = 1702;
|
||||
|
||||
@Mock
|
||||
protected Client client;
|
||||
|
||||
@Mock
|
||||
protected Character chr;
|
||||
|
||||
@BeforeEach
|
||||
void prepareClient() {
|
||||
lenient().when(client.getAccID()).thenReturn(ACCOUNT_ID);
|
||||
lenient().when(client.getPlayer()).thenReturn(chr);
|
||||
}
|
||||
}
|
||||
10
src/test/java/testutil/Items.java
Normal file
10
src/test/java/testutil/Items.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package testutil;
|
||||
|
||||
import client.inventory.Item;
|
||||
|
||||
public class Items {
|
||||
|
||||
public static Item itemWithQuantity(int itemId, int quantity) {
|
||||
return new Item(itemId, AnyValues.anyShort(), (short) quantity);
|
||||
}
|
||||
}
|
||||
12
src/test/java/testutil/Packets.java
Normal file
12
src/test/java/testutil/Packets.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package testutil;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.packet.ByteBufInPacket;
|
||||
import net.packet.InPacket;
|
||||
|
||||
public class Packets {
|
||||
|
||||
public static InPacket emptyInPacket() {
|
||||
return new ByteBufInPacket(Unpooled.buffer());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user