Merge branch 'P0nk:master' into master

This commit is contained in:
NoirReverie
2024-06-17 18:53:21 -04:00
committed by GitHub
20 changed files with 392 additions and 179 deletions

View File

@@ -0,0 +1,19 @@
package constants.id;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ItemIdTest {
@Test
void isCashPackage() {
assertTrue(ItemId.isCashPackage(9102237));
}
@Test
void isNotCashPackage() {
assertFalse(ItemId.isCashPackage(4000000));
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,72 @@
package net.server.channel.handlers;
import client.inventory.Item;
import constants.id.ItemId;
import net.packet.InPacket;
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.ArgumentMatchers.anyLong;
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);
}
private InPacket useCashShopSurprisePacket(long cashId) {
return Packets.buildInPacket(out -> out.writeLong(cashId));
}
@Test
void shouldDoNothingWhenCsIsNotOpened() {
when(cashShop.isOpened()).thenReturn(false);
handler.handlePacket(useCashShopSurprisePacket(123), client);
verify(cashShop, never()).openCashShopSurprise(anyLong());
}
@Test
void shouldSendFailurePacketWhenFailToOpen() {
when(cashShop.isOpened()).thenReturn(true);
when(cashShop.openCashShopSurprise(anyLong())).thenReturn(Optional.empty());
handler.handlePacket(useCashShopSurprisePacket(456), 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(789)).thenReturn(Optional.of(new CashShop.CashShopSurpriseResult(
cashShopSurprise, reward)));
handler.handlePacket(useCashShopSurprisePacket(789), client);
verify(client).sendPacket(PacketCreator.onCashGachaponOpenSuccess(ACCOUNT_ID, cashShopSurprise.getCashId(), 3,
reward, 5000012, 1, true));
}
}

View File

@@ -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());
}

View 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);
}
}

View 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);
}
}

View File

@@ -0,0 +1,18 @@
package testutil;
import io.netty.buffer.Unpooled;
import net.packet.ByteBufInPacket;
import net.packet.ByteBufOutPacket;
import net.packet.InPacket;
import net.packet.OutPacket;
import java.util.function.Consumer;
public class Packets {
public static InPacket buildInPacket(Consumer<OutPacket> contentProvider) {
OutPacket builderInput = new ByteBufOutPacket();
contentProvider.accept(builderInput);
return new ByteBufInPacket(Unpooled.wrappedBuffer(builderInput.getBytes()));
}
}