diff --git a/src/main/java/com/fighter/demo/CorsConfiguration.java b/src/main/java/com/fighter/demo/CorsConfiguration.java new file mode 100644 index 0000000..459be90 --- /dev/null +++ b/src/main/java/com/fighter/demo/CorsConfiguration.java @@ -0,0 +1,18 @@ +package com.fighter.demo; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +public class CorsConfiguration implements WebMvcConfigurer +{ + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedMethods("GET", "POST"); + } +} diff --git a/src/main/java/com/fighter/demo/controllers/TournamentController.java b/src/main/java/com/fighter/demo/controllers/TournamentController.java index dca6615..52c1d14 100644 --- a/src/main/java/com/fighter/demo/controllers/TournamentController.java +++ b/src/main/java/com/fighter/demo/controllers/TournamentController.java @@ -9,10 +9,10 @@ import com.fighter.demo.service.TournamentService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; - @RestController public class TournamentController { diff --git a/src/main/java/com/fighter/demo/exception/TournamentNotFoundException.java b/src/main/java/com/fighter/demo/exception/TournamentNotFoundException.java index 235ad01..2669632 100644 --- a/src/main/java/com/fighter/demo/exception/TournamentNotFoundException.java +++ b/src/main/java/com/fighter/demo/exception/TournamentNotFoundException.java @@ -7,6 +7,5 @@ @ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Ingen sån turnering hittades") public class TournamentNotFoundException extends RuntimeException { public TournamentNotFoundException( ) { - ; } } diff --git a/src/main/java/com/fighter/demo/models/dto/Fighter.java b/src/main/java/com/fighter/demo/models/dto/Fighter.java index 9eb6a85..b738161 100644 --- a/src/main/java/com/fighter/demo/models/dto/Fighter.java +++ b/src/main/java/com/fighter/demo/models/dto/Fighter.java @@ -17,7 +17,7 @@ public class Fighter { public void calculateHealth(){ - this.health = (byte) (100+wins+losses); + this.health = (byte) (100+wins-losses); } } diff --git a/src/test/java/com/fighter/demo/controllers/TournamentControllerTest.java b/src/test/java/com/fighter/demo/controllers/TournamentControllerTest.java new file mode 100644 index 0000000..8f0739f --- /dev/null +++ b/src/test/java/com/fighter/demo/controllers/TournamentControllerTest.java @@ -0,0 +1,139 @@ +package com.fighter.demo.controllers; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fighter.demo.entities.TournamentEntity; +import com.fighter.demo.models.dto.Fighter; +import com.fighter.demo.models.dto.FighterMatch; +import com.fighter.demo.models.dto.Tournament; +import com.fighter.demo.models.response.TournamentResponse; +import com.fighter.demo.service.FighterService; +import com.fighter.demo.service.FighterServiceImpl; +import com.fighter.demo.service.TournamentService; +import com.fighter.demo.service.TournamentServiceImpl; +import com.jayway.jsonpath.JsonPath; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.net.http.HttpResponse; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc +class TournamentControllerTest { + + + @Autowired + private MockMvc mvc; + + ObjectMapper mapper = new ObjectMapper(); + + String currentMatchId; + + @BeforeEach + public void setup() throws Exception { + MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/new")) + .andDo(print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").isString()) + .andReturn(); + + currentMatchId = JsonPath.read(result.getResponse().getContentAsString(), "$.id"); + } + + @Test + void startNewTournament() throws Exception { + MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/new")) + .andDo(print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").isString()) + .andReturn(); + + final TournamentResponse tournament = mapper.readValue(result.getResponse().getContentAsByteArray(), TournamentResponse.class); + assertEquals(tournament.getId().length(), 4); + assertEquals(tournament.getFightersRemaining().size(), 8); + + } + + @Test + void seeTournament() throws Exception { + + } + + @Test + void getOldTournament() throws Exception { + MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/record/"+"futa")) + .andDo(print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").isString()) + .andReturn(); + + final TournamentEntity tournament = mapper.readValue(result.getResponse().getContentAsByteArray(), TournamentEntity.class); + assertEquals(tournament.getId(), "futa"); + assertEquals(tournament.getWinnerId(), 8); + } + + @Test + void getNextFight() throws Exception { + MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/"+currentMatchId+"/upcoming")) + .andDo(print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.fighter1").exists()) + .andExpect(MockMvcResultMatchers.jsonPath("$.fighter2").exists()) + .andReturn(); + + final FighterMatch match = mapper.readValue(result.getResponse().getContentAsByteArray(), FighterMatch.class); + assertNotNull(match.getFighter1()); + assertNotNull(match.getFighter2()); + } + + @Test + void startFight() throws Exception { + this.mvc.perform(MockMvcRequestBuilders.get("/"+currentMatchId+"/fight")) + .andDo(print()) + .andReturn(); + + MvcResult result = this.mvc.perform(MockMvcRequestBuilders.get("/"+currentMatchId+"/fight")) + .andDo(print()) + .andReturn(); + + final FighterMatch match = mapper.readValue(result.getResponse().getContentAsByteArray(), FighterMatch.class); + assertNotNull(match.getFighter1()); + assertNotNull(match.getFighter2()); + assertNotNull(match.getFightLog()); + + MvcResult tournamentAfter2Fights = this.mvc.perform(MockMvcRequestBuilders.get("/"+currentMatchId)) + .andDo(print()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists()) + .andExpect(MockMvcResultMatchers.jsonPath("$.id").isString()) + .andReturn(); + + final TournamentResponse tournament = mapper.readValue(tournamentAfter2Fights.getResponse().getContentAsByteArray(), TournamentResponse.class); + assertEquals(tournament.getId(), currentMatchId); + assertEquals(tournament.getFightersRemaining().size(), 6); + } +} \ No newline at end of file