mirror of
https://github.com/robindhole/sfg-restdocs-example.git
synced 2025-03-15 19:09:56 +00:00
adding repo to controller and tests
This commit is contained in:
parent
0f2095870f
commit
d6a96f8498
@ -1,6 +1,9 @@
|
|||||||
package guru.springframework.sfgrestdocsexample.web.controller;
|
package guru.springframework.sfgrestdocsexample.web.controller;
|
||||||
|
|
||||||
|
import guru.springframework.sfgrestdocsexample.repositories.BeerRepository;
|
||||||
|
import guru.springframework.sfgrestdocsexample.web.mappers.BeerMapper;
|
||||||
import guru.springframework.sfgrestdocsexample.web.model.BeerDto;
|
import guru.springframework.sfgrestdocsexample.web.model.BeerDto;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@ -11,29 +14,39 @@ import java.util.UUID;
|
|||||||
/**
|
/**
|
||||||
* Created by jt on 2019-05-12.
|
* Created by jt on 2019-05-12.
|
||||||
*/
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
@RequestMapping("/api/v1/beer")
|
@RequestMapping("/api/v1/beer")
|
||||||
@RestController
|
@RestController
|
||||||
public class BeerController {
|
public class BeerController {
|
||||||
|
|
||||||
|
private final BeerMapper beerMapper;
|
||||||
|
private final BeerRepository beerRepository;
|
||||||
|
|
||||||
@GetMapping("/{beerId}")
|
@GetMapping("/{beerId}")
|
||||||
public ResponseEntity<BeerDto> getBeerById(@PathVariable("beerId") UUID beerId){
|
public ResponseEntity<BeerDto> getBeerById(@PathVariable("beerId") UUID beerId){
|
||||||
|
|
||||||
//todo impl
|
return new ResponseEntity<>(beerMapper.BeerToBeerDto(beerRepository.findById(beerId).get()), HttpStatus.OK);
|
||||||
return new ResponseEntity<>(BeerDto.builder().build(), HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity saveNewBeer(@RequestBody @Validated BeerDto beerDto){
|
public ResponseEntity saveNewBeer(@RequestBody @Validated BeerDto beerDto){
|
||||||
|
|
||||||
//todo impl
|
beerRepository.save(beerMapper.BeerDtoToBeer(beerDto));
|
||||||
|
|
||||||
return new ResponseEntity(HttpStatus.CREATED);
|
return new ResponseEntity(HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{beerId}")
|
@PutMapping("/{beerId}")
|
||||||
public ResponseEntity updateBeerById(@PathVariable("beerId") UUID beerId, @RequestBody @Validated BeerDto beerDto){
|
public ResponseEntity updateBeerById(@PathVariable("beerId") UUID beerId, @RequestBody @Validated BeerDto beerDto){
|
||||||
|
beerRepository.findById(beerId).ifPresent(beer -> {
|
||||||
|
beer.setBeerName(beerDto.getBeerName());
|
||||||
|
beer.setBeerStyle(beerDto.getBeerStyle().name());
|
||||||
|
beer.setPrice(beerDto.getPrice());
|
||||||
|
beer.setUpc(beerDto.getUpc());
|
||||||
|
|
||||||
|
beerRepository.save(beer);
|
||||||
|
});
|
||||||
|
|
||||||
//todo impl
|
|
||||||
return new ResponseEntity(HttpStatus.NO_CONTENT);
|
return new ResponseEntity(HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,30 @@
|
|||||||
package guru.springframework.sfgrestdocsexample.web.controller;
|
package guru.springframework.sfgrestdocsexample.web.controller;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import guru.springframework.sfgrestdocsexample.domain.Beer;
|
||||||
|
import guru.springframework.sfgrestdocsexample.repositories.BeerRepository;
|
||||||
import guru.springframework.sfgrestdocsexample.web.model.BeerDto;
|
import guru.springframework.sfgrestdocsexample.web.model.BeerDto;
|
||||||
import guru.springframework.sfgrestdocsexample.web.model.BeerStyleEnum;
|
import guru.springframework.sfgrestdocsexample.web.model.BeerStyleEnum;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
@WebMvcTest(BeerController.class)
|
@WebMvcTest(BeerController.class)
|
||||||
|
@ComponentScan(basePackages = "guru.springframework.sfgrestdocsexample.web.mappers")
|
||||||
class BeerControllerTest {
|
class BeerControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -24,8 +33,13 @@ class BeerControllerTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
ObjectMapper objectMapper;
|
ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
BeerRepository beerRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getBeerById() throws Exception {
|
void getBeerById() throws Exception {
|
||||||
|
given(beerRepository.findById(any())).willReturn(Optional.of(Beer.builder().build()));
|
||||||
|
|
||||||
mockMvc.perform(get("/api/v1/beer/" + UUID.randomUUID().toString()).accept(MediaType.APPLICATION_JSON))
|
mockMvc.perform(get("/api/v1/beer/" + UUID.randomUUID().toString()).accept(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user