Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Issue #3 addition of more unit testing #6

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,11 +36,11 @@ public class FishPunController {
@GetMapping(value = "/puns/fish", produces = { "application/json" })
public FishPuns getRandomFishPun() {
List<FishPuns> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,9 +39,8 @@ public InsectPuns getRandomInsectPun() {
event.setPath("/puns/insect");
apiEventRepository.insertApiEvent(event);
List<InsectPuns> 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);
}
}

This file was deleted.

This file was deleted.

37 changes: 0 additions & 37 deletions src/main/java/com/williamspires/acnhapi/Model/RedItem.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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 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(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<acnhevents> 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");
}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading