Skip to content

Commit

Permalink
chore: update mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy committed Sep 22, 2024
1 parent 70c119d commit f349c59
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions src/test/java/dinkplugin/notifiers/LootNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import dinkplugin.notifiers.data.SerializedItemStack;
import dinkplugin.util.ItemUtils;
import dinkplugin.util.KillCountService;
import net.runelite.api.IndexedObjectSet;
import net.runelite.api.ItemID;
import net.runelite.api.NPC;
import net.runelite.api.NpcID;
import net.runelite.api.Player;
import net.runelite.api.WorldView;
import net.runelite.api.coords.WorldPoint;
import net.runelite.client.events.NpcLootReceived;
import net.runelite.client.events.PlayerLootReceived;
Expand All @@ -26,10 +28,12 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand Down Expand Up @@ -80,12 +84,22 @@ protected void setUp() {
mockItem(ItemID.TUNA, TUNA_PRICE, "Tuna");
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private void mockWorldNpcs(NPC... npcs) {
WorldView worldView = Mockito.mock(WorldView.class);
when(client.getTopLevelWorldView()).thenReturn(worldView);
IndexedObjectSet ios = new IndexedObjectSet<>(npcs, IntStream.range(0, npcs.length).toArray(), npcs.length);
when(worldView.npcs()).thenReturn(ios);
}

@Test
void testNotifyNpc() {
// prepare mocks
NPC npc = mock(NPC.class);
String name = "Rasmus";
when(npc.getName()).thenReturn(name);
when(npc.getId()).thenReturn(9999);
mockWorldNpcs(npc);
int kc = 69;
when(configManager.getConfiguration(eq(LootTrackerConfig.GROUP), any(), eq("drops_NPC_" + name)))
.thenReturn("{\"type\":\"NPC\",\"name\":\"Rasmus\",\"kills\":" + kc +
Expand All @@ -107,7 +121,7 @@ void testNotifyNpc() {
.replacement("{{source}}", Replacements.ofWiki(name))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), name, LootRecordType.NPC, kc + 1, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), name, LootRecordType.NPC, kc + 1, null, null, 9999))
.type(NotificationType.LOOT)
.build()
);
Expand All @@ -123,6 +137,8 @@ void testNotifyNpcRarity() {
NPC npc = mock(NPC.class);
String name = "Ice spider";
when(npc.getName()).thenReturn(name);
when(npc.getId()).thenReturn(NpcID.ICE_SPIDER);
mockWorldNpcs(npc);

// fire event
double rarity = 1.0 / 208;
Expand All @@ -142,7 +158,7 @@ void testNotifyNpcRarity() {
.replacement("{{source}}", Replacements.ofWiki(name))
.build()
)
.extra(new LootNotificationData(List.of(new RareItemStack(ItemID.LARRANS_KEY, 1, LARRAN_PRICE, "Larran's key", rarity)), name, LootRecordType.NPC, 1, rarity, null))
.extra(new LootNotificationData(List.of(new RareItemStack(ItemID.LARRANS_KEY, 1, LARRAN_PRICE, "Larran's key", rarity)), name, LootRecordType.NPC, 1, rarity, null, NpcID.ICE_SPIDER))
.type(NotificationType.LOOT)
.thumbnailUrl(ItemUtils.getItemImageUrl(ItemID.LARRANS_KEY))
.build()
Expand Down Expand Up @@ -215,7 +231,7 @@ void testNotifyAllowlist() {
.replacement("{{source}}", Replacements.ofLink(LOOTED_NAME, config.playerLookupService().getPlayerUrl(LOOTED_NAME)))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -244,7 +260,7 @@ void testNotifyAllowlistWildcard() {
.replacement("{{source}}", Replacements.ofLink(LOOTED_NAME, config.playerLookupService().getPlayerUrl(LOOTED_NAME)))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -276,6 +292,10 @@ void testIgnoreDenylistWildcard() {
@DisplayName("Ensure LootReceived event for The Whisperer fires a notification - https://github.com/pajlads/DinkPlugin/pull/286")
void testNotifyWhisperer() {
String name = "The Whisperer";
NPC npc = Mockito.mock(NPC.class);
when(npc.getName()).thenReturn(name);
when(npc.getId()).thenReturn(NpcID.THE_WHISPERER);
mockWorldNpcs(npc);

// fire event
LootReceived event = new LootReceived(name, 99, LootRecordType.NPC, Collections.singletonList(new ItemStack(ItemID.RUBY, 1)), 1);
Expand All @@ -293,7 +313,7 @@ void testNotifyWhisperer() {
.replacement("{{source}}", Replacements.ofWiki(name))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), name, LootRecordType.NPC, 1, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), name, LootRecordType.NPC, 1, null, null, NpcID.THE_WHISPERER))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -332,6 +352,11 @@ void testIgnoreNpc() {

@Test
void testNotifyPickpocket() {
NPC npc = Mockito.mock(NPC.class);
when(npc.getName()).thenReturn(LOOTED_NAME);
when(npc.getId()).thenReturn(9999);
mockWorldNpcs(npc);

// fire event
LootReceived event = new LootReceived(LOOTED_NAME, 99, LootRecordType.PICKPOCKET, Collections.singletonList(new ItemStack(ItemID.RUBY, 1)), 1);
plugin.onLootReceived(event);
Expand All @@ -348,7 +373,7 @@ void testNotifyPickpocket() {
.replacement("{{source}}", Replacements.ofWiki(LOOTED_NAME))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), LOOTED_NAME, LootRecordType.PICKPOCKET, 1, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), LOOTED_NAME, LootRecordType.PICKPOCKET, 1, null, null, 9999))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -386,7 +411,7 @@ void testNotifyClue() {
.replacement("{{source}}", Replacements.ofWiki(source))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, 42, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, 42, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -429,7 +454,7 @@ void testNotifyPlayer() {
.replacement("{{source}}", Replacements.ofLink(LOOTED_NAME, config.playerLookupService().getPlayerUrl(LOOTED_NAME)))
.build()
)
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null))
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -460,7 +485,7 @@ void testNotifyPlayerForwarded() {
.replacement("{{source}}", Replacements.ofLink(LOOTED_NAME, config.playerLookupService().getPlayerUrl(LOOTED_NAME)))
.build()
)
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null))
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -490,7 +515,7 @@ void testNotifyPlayerForwardBlank() {
.replacement("{{source}}", Replacements.ofLink(LOOTED_NAME, config.playerLookupService().getPlayerUrl(LOOTED_NAME)))
.build()
)
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null))
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.PLAYER, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -522,7 +547,7 @@ void testNotifyPkChest() {
.replacement("{{source}}", Replacements.ofWiki(source))
.build()
)
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.OPAL, 1, OPAL_PRICE, "Opal"), new SerializedItemStack(ItemID.TUNA, 2, TUNA_PRICE, "Tuna")), source, LootRecordType.EVENT, 1, null, null))
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.OPAL, 1, OPAL_PRICE, "Opal"), new SerializedItemStack(ItemID.TUNA, 2, TUNA_PRICE, "Tuna")), source, LootRecordType.EVENT, 1, null, null, null))
.type(NotificationType.LOOT)
.thumbnailUrl(ItemUtils.getItemImageUrl(ItemID.TUNA))
.build()
Expand Down Expand Up @@ -591,7 +616,7 @@ void testNotifyMultiple() {
.replacement("{{source}}", Replacements.ofWiki(LOOTED_NAME))
.build()
)
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.OPAL, 1, OPAL_PRICE, "Opal"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.EVENT, 1, null, null))
.extra(new LootNotificationData(Arrays.asList(new SerializedItemStack(ItemID.RUBY, 1, RUBY_PRICE, "Ruby"), new SerializedItemStack(ItemID.OPAL, 1, OPAL_PRICE, "Opal"), new SerializedItemStack(ItemID.TUNA, 1, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.EVENT, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -628,7 +653,7 @@ void testNotifyRepeated() {
.replacement("{{source}}", Replacements.ofWiki(LOOTED_NAME))
.build()
)
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.TUNA, 5, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.EVENT, 1, null, null))
.extra(new LootNotificationData(Collections.singletonList(new SerializedItemStack(ItemID.TUNA, 5, TUNA_PRICE, "Tuna")), LOOTED_NAME, LootRecordType.EVENT, 1, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -680,7 +705,7 @@ void testNotifyGauntlet() {
.replacement("{{source}}", Replacements.ofWiki(source))
.build()
)
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, kc, null, null))
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, kc, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -712,7 +737,7 @@ void testNotifyCorruptedGauntlet() {
.replacement("{{source}}", Replacements.ofWiki(realSource))
.build()
)
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), realSource, LootRecordType.EVENT, kc, null, null))
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), realSource, LootRecordType.EVENT, kc, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -743,7 +768,7 @@ void testNotifyAmascut() {
.replacement("{{source}}", Replacements.ofWiki(source))
.build()
)
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, kc, null, null))
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, kc, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand Down Expand Up @@ -774,7 +799,7 @@ void testNotifyAmascutExpert() {
.replacement("{{source}}", Replacements.ofWiki(source))
.build()
)
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, kc, null, null))
.extra(new LootNotificationData(List.of(new SerializedItemStack(ItemID.RUBY, quantity, RUBY_PRICE, "Ruby")), source, LootRecordType.EVENT, kc, null, null, null))
.type(NotificationType.LOOT)
.build()
);
Expand All @@ -791,6 +816,8 @@ void testNotifyRarityValueIntersectionValue() {
NPC npc = mock(NPC.class);
String name = "Ice spider";
when(npc.getName()).thenReturn(name);
when(npc.getId()).thenReturn(NpcID.ICE_SPIDER);
mockWorldNpcs(npc);

// fire event
double rarity = 1.0 / 208;
Expand All @@ -810,7 +837,7 @@ void testNotifyRarityValueIntersectionValue() {
.replacement("{{source}}", Replacements.ofWiki(name))
.build()
)
.extra(new LootNotificationData(List.of(new RareItemStack(ItemID.LARRANS_KEY, 1, LARRAN_PRICE, "Larran's key", rarity)), name, LootRecordType.NPC, 1, rarity, null))
.extra(new LootNotificationData(List.of(new RareItemStack(ItemID.LARRANS_KEY, 1, LARRAN_PRICE, "Larran's key", rarity)), name, LootRecordType.NPC, 1, rarity, null, NpcID.ICE_SPIDER))
.type(NotificationType.LOOT)
.thumbnailUrl(ItemUtils.getItemImageUrl(ItemID.LARRANS_KEY))
.build()
Expand Down

0 comments on commit f349c59

Please sign in to comment.