commit 90a58295b65f16ce21d6848c9736f6b6c7d7ce29
parent 4ac54038b2b79c3a8a922e44134b0ceb877aea68
Author: WimDupont <WimDupont@users.noreply.gitlab.com>
Date: Sat, 6 Nov 2021 22:15:53 +0100
Added client tests
Diffstat:
3 files changed, 142 insertions(+), 0 deletions(-)
diff --git a/src/test/java/com/sxcy/sxcybot/SxcyBotApplicationTests.java b/src/test/java/com/sxcy/sxcybot/SxcyBotApplicationTests.java
@@ -1,9 +1,11 @@
package com.sxcy.sxcybot;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
+@Disabled
@SpringBootTest
@ActiveProfiles("dev")
public class SxcyBotApplicationTests {
diff --git a/src/test/java/com/sxcy/sxcybot/client/GrandExchangeClientTest.java b/src/test/java/com/sxcy/sxcybot/client/GrandExchangeClientTest.java
@@ -0,0 +1,47 @@
+package com.sxcy.sxcybot.client;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sxcy.sxcybot.exceptions.EntityNotFoundException;
+import net.dv8tion.jda.api.entities.MessageChannel;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+@ExtendWith(SpringExtension.class)
+class GrandExchangeClientTest {
+
+ private GrandExchangeClient grandExchangeClient;
+
+ @MockBean
+ private MessageChannel messageChannel;
+
+ @BeforeEach
+ void setup() {
+ grandExchangeClient = new GrandExchangeClient(new ObjectMapper());
+ }
+
+ @Test
+ void getPrice() throws EntityNotFoundException {
+ String itemName = "tinderbox";
+ String result = grandExchangeClient.getPrice(itemName, messageChannel);
+ System.out.println(result);
+
+ assertNotNull(result);
+ }
+
+ @Test
+ void getPriceInvalidItem() {
+ String itemName = "invalidItem";
+ EntityNotFoundException exception = assertThrows(EntityNotFoundException.class, () ->
+ grandExchangeClient.getPrice(itemName, messageChannel));
+
+ assertEquals(String.format("Item with name %s not found.", itemName), exception.getMessage());
+ }
+
+}
diff --git a/src/test/java/com/sxcy/sxcybot/client/HiScoreClientTest.java b/src/test/java/com/sxcy/sxcybot/client/HiScoreClientTest.java
@@ -0,0 +1,93 @@
+package com.sxcy.sxcybot.client;
+
+import com.sxcy.sxcybot.model.OsrsBossKc;
+import com.sxcy.sxcybot.model.OsrsStat;
+import com.sxcy.sxcybot.repository.osrs.dao.OsrsHiscoreBoss;
+import com.sxcy.sxcybot.repository.osrs.dao.OsrsHiscoreStat;
+import com.sxcy.sxcybot.services.osrs.OsrsHiscoreBossService;
+import com.sxcy.sxcybot.services.osrs.OsrsHiscoreStatService;
+import net.dv8tion.jda.api.entities.MessageChannel;
+import net.dv8tion.jda.api.requests.restaction.MessageAction;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import java.util.List;
+import java.util.Optional;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.BDDMockito.given;
+
+@ExtendWith(SpringExtension.class)
+class HiScoreClientTest {
+
+ @MockBean
+ private OsrsHiscoreBossService osrsHiscoreBossService;
+ @MockBean
+ private OsrsHiscoreStatService osrsHiscoreStatService;
+ @MockBean
+ private MessageChannel messageChannel;
+ @MockBean
+ private MessageAction messageAction;
+
+ private HiScoreClient hiScoreClient;
+
+ @BeforeEach
+ void setup() {
+ hiScoreClient = new HiScoreClient(osrsHiscoreBossService, osrsHiscoreStatService);
+ }
+
+ @Test
+ void getHiScoreStatsValid() {
+ String playerName = "Zxxy";
+ List<OsrsHiscoreStat> osrsHiscoreStats = newArrayList(OsrsHiscoreStat.builder()
+ .name("Overall")
+ .orderValue(0)
+ .build());
+ given(osrsHiscoreStatService.findAll()).willReturn(osrsHiscoreStats);
+ Optional<List<OsrsStat>> result = hiScoreClient.getHiScoreStats(playerName, messageChannel);
+ assertFalse(result.isEmpty());
+ assertEquals(1, result.get().size());
+ assertEquals("Overall", result.get().get(0).getName());
+ assertEquals("1939", result.get().get(0).getLevel());
+ }
+
+ @Test
+ void getHiscoreStatsNonExistingPlayer() {
+ String playerName = "007thisplayershouldneverexist007";
+ given(messageChannel.sendMessage("No results found. ()")).willReturn(messageAction);
+ Optional<List<OsrsStat>> result = hiScoreClient.getHiScoreStats(playerName, messageChannel);
+
+ assertTrue(result.isEmpty());
+ }
+
+ @Test
+ void getHiscoreBossKcValidKbdKc() {
+ String playerName = "Zxxy";
+ List<OsrsHiscoreBoss> hiscoreBosses = newArrayList(OsrsHiscoreBoss.builder()
+ .name("KBD")
+ .orderValue(58)
+ .build());
+ given(osrsHiscoreBossService.findAll()).willReturn(hiscoreBosses);
+ Optional<List<OsrsBossKc>> result = hiScoreClient.getHiScoreBossKc(playerName, messageChannel);
+
+ assertFalse(result.isEmpty());
+ assertEquals(1, result.get().size());
+ assertEquals("KBD", result.get().get(0).getName());
+ assertEquals("723", result.get().get(0).getKc());
+ }
+
+ @Test
+ void getHiscoreBossKcNonExistingPlayer() {
+ String playerName = "007thisplayershouldneverexist007";
+ given(messageChannel.sendMessage("No results found. ()")).willReturn(messageAction);
+ Optional<List<OsrsBossKc>> result = hiScoreClient.getHiScoreBossKc(playerName, messageChannel);
+
+ assertTrue(result.isEmpty());
+ }
+}