From 60fc565d21c514a82a5dfd380d9c92ac40084e63 Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 21 Jun 2020 12:04:15 +0100 Subject: [PATCH 1/3] Add Tests for AcnhEventsController --- .../AcnhEventsControllerTest.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java new file mode 100644 index 0000000..0c7b010 --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java @@ -0,0 +1,85 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.AcnhEventsController; +import com.williamspires.acnhapi.Model.acnhevents; +import com.williamspires.acnhapi.Repositories.AcnhEventsRepository; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import lombok.extern.slf4j.Slf4j; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@Slf4j +@RunWith(SpringRunner.class) +@WebMvcTest(AcnhEventsController.class) +public class AcnhEventsControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + AcnhEventsRepository acnhEventsRepository; + @MockBean + ApiEventRepository apiEventRepository; + + private com.williamspires.acnhapi.Model.acnhevents acnhevents; + + @Before + public void setup() { + acnhevents = new acnhevents(); + acnhevents.setEvent("Test"); + acnhevents.setId(1); + acnhevents.setNothernHemisphereDates("Test Date"); + acnhevents.setSouthernHemisphereDates("Test Date"); + acnhevents.setTimes("Test Time"); + } + + @Test + public void EventIsReturnedWhenOneExists() throws Exception { + List events = new ArrayList<>(); + events.add(acnhevents); + Mockito.when(acnhEventsRepository.findEventsByEvent(Mockito.anyString())).thenReturn(events); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/events/Test") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "[{\"id\":1,\"event\":\"Test\",\"nothernHemisphereDates\":\"Test Date\",\"southernHemisphereDates\":\"Test Date\",\"times\":\"Test Time\"}]"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } + + @Test + public void shouldReturn404WhenEventDoesNotExist() throws Exception { + Mockito.when(acnhEventsRepository.findEventsByEvent(Mockito.anyString())).thenReturn(null); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/events/404") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + assertThat(response.getStatus()).isEqualTo(404); + assertThat(response.getContentAsString()).isEqualTo("Could not find event with name 404"); + } + +} \ No newline at end of file From d6c192494fa2e38d0e540ccef69c86e536019775 Mon Sep 17 00:00:00 2001 From: Will <50263351+villchurch@users.noreply.github.com> Date: Sun, 21 Jun 2020 13:12:56 +0100 Subject: [PATCH 2/3] Add more unit tests and remove RedController --- .../acnhapi/Advice/RedItemNotFoundAdvice.java | 21 ----- .../Controllers/FishPunController.java | 7 +- .../Controllers/InsectPunController.java | 8 +- .../acnhapi/Controllers/RedController.java | 26 ------ .../Exceptions/RedItemNotFoundException.java | 7 -- .../williamspires/acnhapi/Model/RedItem.java | 37 -------- .../Repositories/RedItemRepository.java | 17 ---- .../AcnhEventsControllerTest.java | 4 - .../ConstructionControllerTest.java | 77 +++++++++++++++++ .../FencingControllerTest.java | 82 ++++++++++++++++++ .../FishPunControllerTest.java | 70 +++++++++++++++ .../restControllers/FossilControllerTest.java | 85 +++++++++++++++++++ .../InsectPunControllerTest.java | 70 +++++++++++++++ 13 files changed, 392 insertions(+), 119 deletions(-) delete mode 100644 src/main/java/com/williamspires/acnhapi/Advice/RedItemNotFoundAdvice.java delete mode 100644 src/main/java/com/williamspires/acnhapi/Controllers/RedController.java delete mode 100644 src/main/java/com/williamspires/acnhapi/Exceptions/RedItemNotFoundException.java delete mode 100644 src/main/java/com/williamspires/acnhapi/Model/RedItem.java delete mode 100644 src/main/java/com/williamspires/acnhapi/Repositories/RedItemRepository.java create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/ConstructionControllerTest.java create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/FencingControllerTest.java create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/FishPunControllerTest.java create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/FossilControllerTest.java create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/InsectPunControllerTest.java diff --git a/src/main/java/com/williamspires/acnhapi/Advice/RedItemNotFoundAdvice.java b/src/main/java/com/williamspires/acnhapi/Advice/RedItemNotFoundAdvice.java deleted file mode 100644 index 6b3f966..0000000 --- a/src/main/java/com/williamspires/acnhapi/Advice/RedItemNotFoundAdvice.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.williamspires.acnhapi.Advice; - -import com.williamspires.acnhapi.Exceptions.RedItemNotFoundException; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; - -@Slf4j -@ControllerAdvice -public class RedItemNotFoundAdvice { - @ResponseBody - @ExceptionHandler(RedItemNotFoundException.class) - @ResponseStatus(HttpStatus.NOT_FOUND) - String redItemNotFoundException(RedItemNotFoundException ex){ - log.error(ex.getMessage()); - return ex.getMessage(); - } -} diff --git a/src/main/java/com/williamspires/acnhapi/Controllers/FishPunController.java b/src/main/java/com/williamspires/acnhapi/Controllers/FishPunController.java index d43c239..daa53fc 100644 --- a/src/main/java/com/williamspires/acnhapi/Controllers/FishPunController.java +++ b/src/main/java/com/williamspires/acnhapi/Controllers/FishPunController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.concurrent.ThreadLocalRandom; @Tag(name = "Fish puns", description = "Returns Fish puns") @RestController @@ -35,11 +36,11 @@ public class FishPunController { @GetMapping(value = "/puns/fish", produces = { "application/json" }) public FishPuns getRandomFishPun() { List allPuns = fishPunRepository.getAllFishPuns(); - int randomNumber = (int) (Math.random() * (allPuns.size() + 1)); - allPuns.get(randomNumber).setText(allPuns.get(randomNumber).getText().replaceAll("\\u000e", " ")); + int random = ThreadLocalRandom.current().nextInt(0, (allPuns.size())); + allPuns.get(random).setText(allPuns.get(random).getText().replaceAll("\\u000e", " ")); ApiEvent event = new ApiEvent(); event.setPath("/puns/fish"); apiEventRepository.insertApiEvent(event); - return allPuns.get(randomNumber); + return allPuns.get(random); } } diff --git a/src/main/java/com/williamspires/acnhapi/Controllers/InsectPunController.java b/src/main/java/com/williamspires/acnhapi/Controllers/InsectPunController.java index 4058135..541ee3e 100644 --- a/src/main/java/com/williamspires/acnhapi/Controllers/InsectPunController.java +++ b/src/main/java/com/williamspires/acnhapi/Controllers/InsectPunController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.concurrent.ThreadLocalRandom; @Tag(name = "Insect Puns", description = "Returns insect puns") @RestController @@ -38,9 +39,8 @@ public InsectPuns getRandomInsectPun() { event.setPath("/puns/insect"); apiEventRepository.insertApiEvent(event); List allPuns = insectPunRepository.getAllInsectPuns(); - int randomNumber = (int) (Math.random() * (allPuns.size() + 1)); - System.out.println(randomNumber); - allPuns.get(randomNumber).setText(allPuns.get(randomNumber).getText().replaceAll("\\u000e", " ")); - return allPuns.get(randomNumber); + int random = ThreadLocalRandom.current().nextInt(0, (allPuns.size())); + allPuns.get(random).setText(allPuns.get(random).getText().replaceAll("\\u000e", " ")); + return allPuns.get(random); } } diff --git a/src/main/java/com/williamspires/acnhapi/Controllers/RedController.java b/src/main/java/com/williamspires/acnhapi/Controllers/RedController.java deleted file mode 100644 index d8fd0c2..0000000 --- a/src/main/java/com/williamspires/acnhapi/Controllers/RedController.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.williamspires.acnhapi.Controllers; - -import com.williamspires.acnhapi.Exceptions.RedItemNotFoundException; -import com.williamspires.acnhapi.Model.RedItem; -import com.williamspires.acnhapi.Repositories.RedItemRepository; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class RedController { - - private final RedItemRepository redItemRepository; - RedController(RedItemRepository redItemRepository){ - this.redItemRepository = redItemRepository; - } - - @GetMapping("/red/{name}") - public RedItem getRedItemByName(@PathVariable String name) { - RedItem redItem = redItemRepository.findItemByName(name); - if(null == redItem){ - throw new RedItemNotFoundException(name); - } - return redItem; - } -} diff --git a/src/main/java/com/williamspires/acnhapi/Exceptions/RedItemNotFoundException.java b/src/main/java/com/williamspires/acnhapi/Exceptions/RedItemNotFoundException.java deleted file mode 100644 index c606fd3..0000000 --- a/src/main/java/com/williamspires/acnhapi/Exceptions/RedItemNotFoundException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.williamspires.acnhapi.Exceptions; - -public class RedItemNotFoundException extends RuntimeException{ - public RedItemNotFoundException(String redItem){ - super("Could not find any Red Items called " + redItem); - } -} diff --git a/src/main/java/com/williamspires/acnhapi/Model/RedItem.java b/src/main/java/com/williamspires/acnhapi/Model/RedItem.java deleted file mode 100644 index 615609d..0000000 --- a/src/main/java/com/williamspires/acnhapi/Model/RedItem.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.williamspires.acnhapi.Model; - -import javax.persistence.*; - -@Entity -@Table(name="red") -public class RedItem { - @Id - private int ID; - @Column(name="Name") - private String name; - private String Fake; - - public int getID() { - return ID; - } - - public void setID(int ID) { - this.ID = ID; - } - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public String getFake() { - return Fake; - } - - public void setFake(String fake) { - Fake = fake; - } -} diff --git a/src/main/java/com/williamspires/acnhapi/Repositories/RedItemRepository.java b/src/main/java/com/williamspires/acnhapi/Repositories/RedItemRepository.java deleted file mode 100644 index 4afef3d..0000000 --- a/src/main/java/com/williamspires/acnhapi/Repositories/RedItemRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.williamspires.acnhapi.Repositories; - -import com.williamspires.acnhapi.Model.RedItem; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface RedItemRepository extends JpaRepository { - - RedItem findItemByName(String name); - - @Query(value = "select * from red", nativeQuery = true) - List getAllRedItems(); -} diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java index 0c7b010..0367af3 100644 --- a/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/AcnhEventsControllerTest.java @@ -4,8 +4,6 @@ import com.williamspires.acnhapi.Model.acnhevents; import com.williamspires.acnhapi.Repositories.AcnhEventsRepository; import com.williamspires.acnhapi.Repositories.ApiEventRepository; -import lombok.extern.slf4j.Slf4j; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -16,7 +14,6 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; @@ -28,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThat; -@Slf4j @RunWith(SpringRunner.class) @WebMvcTest(AcnhEventsController.class) public class AcnhEventsControllerTest { diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/ConstructionControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/ConstructionControllerTest.java new file mode 100644 index 0000000..861dbc0 --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/ConstructionControllerTest.java @@ -0,0 +1,77 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.ConstructionController; +import com.williamspires.acnhapi.Model.Construction; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import com.williamspires.acnhapi.Repositories.ConstructionRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +@WebMvcTest(ConstructionController.class) +public class ConstructionControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + ConstructionRepository constructionRepository; + @MockBean + ApiEventRepository apiEventRepository; + + private Construction construction; + + @Before + public void setup() { + construction = new Construction(); + construction.setBuy(100); + construction.setCategory("Category"); + construction.setFilename("FileName"); + construction.setName("Name"); + construction.setSource("Source"); + construction.setUniqueEntryID("Unique"); + construction.setVersion("1.2.0"); + } + + @Test + public void ConstructionItemShouldBeReturnedWhenItExists() throws Exception { + Mockito.when(constructionRepository.findConstructionByName(Mockito.anyString())).thenReturn(construction); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/construction/Name") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "{\"uniqueEntryID\":\"Unique\",\"name\":\"Name\",\"buy\":100,\"category\":\"Category\",\"source\":\"Source\",\"filename\":\"FileName\",\"version\":\"1.2.0\"}"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } + + @Test + public void shouldReturn404WhenEventDoesNotExist() throws Exception { + Mockito.when(constructionRepository.findConstructionByName(Mockito.anyString())).thenReturn(null); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/construction/404") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + assertThat(response.getStatus()).isEqualTo(404); + assertThat(response.getContentAsString()).isEqualTo("Could not find construction item called 404"); + } +} \ No newline at end of file diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/FencingControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/FencingControllerTest.java new file mode 100644 index 0000000..7f33e40 --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/FencingControllerTest.java @@ -0,0 +1,82 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.FencingController; +import com.williamspires.acnhapi.Model.Fencing; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import com.williamspires.acnhapi.Repositories.FencingRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +@WebMvcTest(FencingController.class) +public class FencingControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + FencingRepository fencingRepository; + + @MockBean + ApiEventRepository apiEventRepository; + + private Fencing fencing; + + @Before + public void setup() { + fencing = new Fencing(); + fencing.setBuy("NFS"); + fencing.setDIY("DIY"); + fencing.setFilename("Filename"); + fencing.setInternalId("InternalID"); + fencing.setName("Name"); + fencing.setSell(100); + fencing.setSource("Source"); + fencing.setSourceNotes("SourceNotes"); + fencing.setStackSize(1); + fencing.setUniqueEntryId("Unique"); + fencing.setVersion("1.1.0"); + } + + @Test + public void shouldReturnFencingWhenFencingExists() throws Exception { + Mockito.when(fencingRepository.findFencingByName(Mockito.anyString())).thenReturn(fencing); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fencing/Name") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "{\"uniqueEntryId\":\"Unique\",\"name\":\"Name\",\"stackSize\":1,\"buy\":\"NFS\",\"sell\":100,\"source\":\"Source\",\"sourceNotes\":\"SourceNotes\",\"version\":\"1.1.0\",\"filename\":\"Filename\",\"internalId\":\"InternalID\",\"diy\":\"DIY\"}"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } + + @Test + public void shouldReturn404WhenFencingDoesNotExist() throws Exception { + Mockito.when(fencingRepository.findFencingByName(Mockito.anyString())).thenReturn(null); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fencing/404") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + assertThat(response.getStatus()).isEqualTo(404); + assertThat(response.getContentAsString()).isEqualTo("Could not find fencing with name 404"); + } +} diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/FishPunControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/FishPunControllerTest.java new file mode 100644 index 0000000..d09903a --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/FishPunControllerTest.java @@ -0,0 +1,70 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.FishPunController; +import com.williamspires.acnhapi.Model.FishPuns; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import com.williamspires.acnhapi.Repositories.FishPunRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +@WebMvcTest(FishPunController.class) +public class FishPunControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + FishPunRepository fishPunRepository; + + @MockBean + ApiEventRepository apiEventRepository; + + private FishPuns fishPuns; + + @Before + public void setup() { + fishPuns = new FishPuns(); + fishPuns.setLabel("Label"); + fishPuns.setText("Text"); + } + + @Test + public void shouldReturnAFishPun() throws Exception { + List puns = new ArrayList<>(); + puns.add(fishPuns); + puns.add(fishPuns); + puns.add(fishPuns); + puns.add(fishPuns); + puns.add(fishPuns); + puns.add(fishPuns); + Mockito.when(fishPunRepository.getAllFishPuns()).thenReturn(puns); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/puns/fish") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "{\"label\":\"Label\",\"text\":\"Text\"}"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } +} diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/FossilControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/FossilControllerTest.java new file mode 100644 index 0000000..63b6afc --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/FossilControllerTest.java @@ -0,0 +1,85 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.FossilController; +import com.williamspires.acnhapi.Model.Fossil; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import com.williamspires.acnhapi.Repositories.FossilRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +@WebMvcTest(FossilController.class) +public class FossilControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + FossilRepository fossilRepository; + + @MockBean + ApiEventRepository apiEventRepository; + + private Fossil fossil; + + @Before + public void setup() { + fossil = new Fossil(); + fossil.setBuy("buy"); + fossil.setCatalog("Catalog"); + fossil.setColor1("Color1"); + fossil.setColor2("Color2"); + fossil.setFilename("Filename"); + fossil.setInteract("no"); + fossil.setInternalID(1); + fossil.setMuseum("Museum"); + fossil.setName("Name"); + fossil.setSize("Size"); + fossil.setSell(0); + fossil.setSource("Source"); + fossil.setUniqueEntryID("UniqueEntryID"); + fossil.setVersion("Version"); + } + + @Test + public void shouldReturnFossilWhenFossilExits() throws Exception { + Mockito.when(fossilRepository.findFossilByName(Mockito.anyString())).thenReturn(fossil); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fossils/Name") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "{\"name\":\"Name\",\"buy\":\"buy\",\"sell\":0,\"color1\":\"Color1\",\"color2\":\"Color2\",\"size\":\"Size\",\"source\":\"Source\",\"museum\":\"Museum\",\"version\":\"Version\",\"interact\":\"no\",\"catalog\":\"Catalog\",\"filename\":\"Filename\",\"internalID\":1,\"uniqueEntryID\":\"UniqueEntryID\"}"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } + + @Test + public void shouldReturn404WhenFossilNotFound() throws Exception { + Mockito.when(fossilRepository.findFossilByName(Mockito.anyString())).thenReturn(null); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/fossils/404") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + assertThat(response.getStatus()).isEqualTo(404); + assertThat(response.getContentAsString()).isEqualTo("Could not found fossil called 404"); + } +} diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/InsectPunControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/InsectPunControllerTest.java new file mode 100644 index 0000000..ef5f5e0 --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/InsectPunControllerTest.java @@ -0,0 +1,70 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.InsectPunController; +import com.williamspires.acnhapi.Model.InsectPuns; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import com.williamspires.acnhapi.Repositories.InsectPunRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +@WebMvcTest(InsectPunController.class) +public class InsectPunControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + InsectPunRepository insectPunRepository; + + @MockBean + ApiEventRepository apiEventRepository; + + private InsectPuns insectPuns; + + @Before + public void setup() { + insectPuns = new InsectPuns(); + insectPuns.setLabel("Label"); + insectPuns.setText("Text"); + } + + @Test + public void shouldReturnAInsectPun() throws Exception { + List puns = new ArrayList<>(); + puns.add(insectPuns); + puns.add(insectPuns); + puns.add(insectPuns); + puns.add(insectPuns); + puns.add(insectPuns); + puns.add(insectPuns); + Mockito.when(insectPunRepository.getAllInsectPuns()).thenReturn(puns); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/puns/insect") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "{\"label\":\"Label\",\"text\":\"Text\"}"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } +} From 01a8e46054fa120968d0cff363408160987c9a22 Mon Sep 17 00:00:00 2001 From: Will Date: Sat, 27 Jun 2020 14:35:08 +0100 Subject: [PATCH 3/3] Add OutfitController Tests --- .../restControllers/OutfitControllerTest.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/java/com/williamspires/acnhapi/unit/restControllers/OutfitControllerTest.java diff --git a/src/test/java/com/williamspires/acnhapi/unit/restControllers/OutfitControllerTest.java b/src/test/java/com/williamspires/acnhapi/unit/restControllers/OutfitControllerTest.java new file mode 100644 index 0000000..78b37be --- /dev/null +++ b/src/test/java/com/williamspires/acnhapi/unit/restControllers/OutfitControllerTest.java @@ -0,0 +1,78 @@ +package com.williamspires.acnhapi.unit.restControllers; + +import com.williamspires.acnhapi.Controllers.OutfitController; +import com.williamspires.acnhapi.Model.Outfits; +import com.williamspires.acnhapi.Repositories.ApiEventRepository; +import com.williamspires.acnhapi.Repositories.OutfitRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@WebMvcTest(OutfitController.class) +public class OutfitControllerTest { + + @Autowired + MockMvc mockMvc; + + @MockBean + OutfitRepository outfitRepository; + + @MockBean + ApiEventRepository eventRepository; + + private Outfits outfits; + + @Before + public void setup() { + outfits = new Outfits(); + outfits.setName("Name"); + outfits.setUniqueEntryID("UniqueEntryID"); + } + + @Test + public void OutfitsAreReturnedWhenTheyExists() throws Exception { + List outfitsList = new ArrayList<>(); + outfitsList.add(outfits); + Mockito.when(outfitRepository.findOutfitsByName(Mockito.anyString())).thenReturn(outfitsList); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/outfits/Name") + .accept(MediaType.APPLICATION_JSON); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + String expected = "[{\"uniqueEntryID\":\"UniqueEntryID\",\"name\":\"Name\",\"variation\":null,\"diy\":null,\"buy\":null,\"sell\":0,\"color1\":null,\"color2\":null,\"size\":null,\"source\":null,\"sourceNotes\":null,\"seasonalAvailability\":null,\"mannequinPiece\":null,\"version\":null,\"style\":null,\"labelThemes\":null,\"villagerEquippable\":null,\"catalog\":null,\"primaryShape\":null,\"secondaryShape\":null,\"filename\":null,\"internalID\":null}]"; + JSONAssert.assertEquals(expected, response.getContentAsString(), true); + assertThat(response.getStatus()).isEqualTo(200); + } + + @Test + public void shouldReturn404WhenOutfitDoesNotExist() throws Exception { + Mockito.when(outfitRepository.findOutfitsByName(Mockito.anyString())).thenReturn(null); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/outfits/404") + .accept(MediaType.ALL); + + MvcResult result = mockMvc.perform(requestBuilder).andReturn(); + MockHttpServletResponse response = result.getResponse(); + assertThat(response.getStatus()).isEqualTo(404); + assertThat(response.getContentAsString()).isEqualTo("Could not find outfit called 404"); + } +}