Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenofono committed Mar 9, 2020
1 parent 2de6556 commit b246a96
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fighter.demo.controllers;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fighter.demo.models.dto.Fighter;
import com.fighter.demo.repositories.FighterRepository;
import com.fighter.demo.service.FighterService;
import com.fighter.demo.service.FighterServiceImpl;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcResultMatchersDsl;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.LinkedList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@Tag("controllers")
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class FighterControllerTest {

@Autowired
private MockMvc mvc;

private ObjectMapper mapper = new ObjectMapper();


@Test
void getAllFighters() throws Exception {
MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/fighters/"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
List<Fighter> fightersReceived = mapper.readValue(result.getResponse().getContentAsByteArray(), new TypeReference<>() {
});
assertEquals(8, fightersReceived.size());
}

@Test
void getFighterById() throws Exception {
MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/fighters/"+2))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Fighter fighterReceived = mapper.readValue(result.getResponse().getContentAsByteArray(), Fighter.class);
assertNotNull(fighterReceived.getName());

this.mvc.perform(MockMvcRequestBuilders.get("/fighters/"+1234))
.andExpect(MockMvcResultMatchers.status().isNotFound())
.andReturn();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.fighter.demo.service.TournamentServiceImpl;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -43,6 +45,7 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@Tag("controllers")
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
Expand Down Expand Up @@ -97,6 +100,7 @@ void seeTournamentTest() throws Exception {
.andReturn();
}

@Disabled
@Test
void getOldTournamentTest() throws Exception {
MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/record/"+"futa"))
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/com/fighter/demo/models/dto/FighterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fighter.demo.models.dto;

import com.fighter.demo.models.dto.Fighter;
import com.fighter.demo.repositories.FighterRepository;
import com.fighter.demo.service.FighterService;
import com.fighter.demo.service.FighterServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.LinkedList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@Tag("entity")
class FighterTest {

private FighterService fighterService;
private FighterRepository fighterRepository;

@BeforeEach
void setUp(){
List<Fighter> fighters = new LinkedList<>();
for (int i = 0; i < 8; i++) {

Fighter fighter = new Fighter();
fighter.setId(i);
fighter.setName("fighter"+i);
if(i < 2){
fighter.setWins((short) (10*i));
fighter.setLosses((short) (2*i));
}
else{
fighter.setWins((short) (Math.random()*100));
fighter.setLosses((short) (Math.random()*100));
}

fighter.calculateHealth();
fighters.add(fighter);
}
fighterRepository = Mockito.mock(FighterRepository.class);
fighterService = Mockito.mock(FighterService.class);

Mockito.when(fighterService.findAll()).thenReturn(fighters);
Mockito.when(fighterService.findById("1")).thenReturn(fighters.get(1));
Mockito.when(fighterService.findById("12345")).thenReturn(null);

}

@Test
void getAllFighters() {
assertEquals(8, fighterService.findAll().size());
}

@Test
void getFighterById() {
assertNotNull(fighterService.findById("1"));
assertNull(fighterService.findById("12345"));
}
}
10 changes: 9 additions & 1 deletion src/test/java/com/fighter/demo/models/dto/TournamentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fighter.demo.service.FighterService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand All @@ -14,6 +15,8 @@
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@Tag("entity")
class TournamentTest {

private Tournament tournament;
Expand All @@ -25,7 +28,7 @@ class TournamentTest {
@BeforeEach
void setUp(){
List<Fighter> fighters = new LinkedList<>();
for (int i = 0; i < 7; i++) {
for (int i = 0; i < 8; i++) {

Fighter fighter = new Fighter();
fighter.setId(i);
Expand Down Expand Up @@ -64,6 +67,11 @@ void fight() {

@Test
void getFightersRemaining() {
assertEquals(8, tournament.getFightersRemaining().size());
tournament.fight();
tournament.fight();
tournament.fight();
assertEquals(5, tournament.getFightersRemaining().size());
}

@Test
Expand Down

0 comments on commit b246a96

Please sign in to comment.