Skip to content

Commit

Permalink
Merge branch 'feature3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenofono committed Feb 22, 2020
2 parents 1fe860e + 9b29028 commit c4ae0f3
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/fighter/demo/CorsConfiguration.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Ingen sån turnering hittades")
public class TournamentNotFoundException extends RuntimeException {
public TournamentNotFoundException( ) {
;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/fighter/demo/models/dto/Fighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Fighter {


public void calculateHealth(){
this.health = (byte) (100+wins+losses);
this.health = (byte) (100+wins-losses);
}

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

0 comments on commit c4ae0f3

Please sign in to comment.