레스토랑 예약 사이트 만들기 8 - 가게목록 필터링

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Region {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
@Repository
public interface RegionRepository extends JpaRepository<Region,Long> {
}
@Service
@RequiredArgsConstructor
public class RegionService {

private final RegionRepository regionRepository;
public List<Region> getRegions() {
return regionRepository.findAll();
}
}
@Service
@RequiredArgsConstructor
public class RegionService {

private final RegionRepository regionRepository;
public List<Region> getRegions() {
return regionRepository.findAll();
}
}
class RegionServiceTest {

private RegionService regionService;

@Mock
private RegionRepository regionRepository;

@BeforeEach
public void setUp(){
MockitoAnnotations.openMocks(this );

regionService = new RegionService(regionRepository);
}

@Test
public void 지역정보들을_가져온다() {
List<Region> mockRegions = new ArrayList<>();
mockRegions.add(Region.builder()
.name("Seoul")
.build());
given(regionRepository.findAll()).willReturn(mockRegions);


List<Region> regions = regionService.getRegions();
Region region = regions.get(0);
assertThat(region.getName()).isEqualTo("Seoul");
}
}
@RestController
@RequiredArgsConstructor
public class RegionController {

private final RegionService regionService;

@GetMapping("/regions")
public List<Region> list(){
List<Region> regions = regionService.getRegions();
return regions;
}
}
@WebMvcTest(RegionController.class)
class RegionControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
private RegionService regionService;


@Test
public void 지역목록들을_가져온다() throws Exception {
List<Region> regions = new ArrayList<>();
regions.add(Region.builder().name("Seoul").build());

given(regionService.getRegions()).willReturn(regions);

mockMvc.perform(get("/regions"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Seoul")));
}
}

카테고리 만들기

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Category {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String name;
}
@Service
@RequiredArgsConstructor
public class CategoryService {
private final CategoryRepository categoryRepository;

public List<Category> getCategories() {
List<Category> categories = categoryRepository.findAll();
return categories;
}

public Category addCategory(String name) {
Category category = Category.builder().name(name).build();
categoryRepository.save(category);
return category;
}
}
@RestController
@RequiredArgsConstructor
public class CategoryController {

@Autowired
private final CategoryService categoryService;

@GetMapping("/categories")
public List<Category> list(){
List<Category> categories = categoryService.getCategories();

return categories;
}

@PostMapping("/categories")
public ResponseEntity<?> create(@RequestBody Category resource){
String name = resource.getName();

Category category = categoryService.addCategory(name);

String url = "/categories" + category.getId();
return ResponseEntity.created(URI.create(url)).body("{}");
}
}
@WebMvcTest(CategoryController.class)
class CategoryControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
private CategoryService categoryService;

@Test
public void 카테고리들_가져오기() throws Exception {
List<Category> categories = new ArrayList<>();
categories.add(Category.builder()
.name("Korean Food")
.build());

given(categoryService.getCategories()).willReturn(categories);

mockMvc.perform(get("/categories"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Korean Food")));
}


@Test
public void 카테고리_생성하기() throws Exception {
Category category = Category.builder()
.name("Korean Food")
.build();

given(categoryService.addCategory("Korean Food")).willReturn(category);

mockMvc.perform(post("/categories")
.contentType(MediaType.APPLICATION_JSON)
.content("{ \"name\" :\"Korean Food\" }"))
.andExpect(status().isCreated())
.andExpect(content().string("{}"));
}
}
class CategoryServiceTest {

private CategoryService categoryService;

@Mock
private CategoryRepository categoryRepository;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);

categoryService = new CategoryService(categoryRepository);
}

@Test
public void 카테고리_목록을_가져온다() {
List<Category> mockCategories = new ArrayList<>();
mockCategories.add(Category.builder()
.name("Korean Food")
.build());

given(categoryRepository.findAll()).willReturn(mockCategories);
List<Category> categories = categoryService.getCategories();

Category category = categories.get(0);
assertThat(category.getName()).isEqualTo("Korean Food");
}

@Test
public void 카테고리를_추가한다(){
Category category = categoryService.addCategory("Korean Food");
verify(categoryRepository).save(any());
assertThat(category.getName()).isEqualTo("Korean Food");
}
}

Request Parameter를 이용한 요청이 들어오는 경우

public List<Restaurant> getRestaurants(String region) {
List<Restaurant> restaurants = restaurantRepository.findAllByAddressContaining(region);
return restaurants;
}
@Test
public void 모든_레스토랑을_가져온다() {
List<Restaurant> restaurants = restaurantService.getRestaurants("Seoul");
Restaurant restaurant = restaurants.get(0);
assertThat(restaurant.getId()).isEqualTo(1004L);
}
@GetMapping("/restaurants")
public List<Restaurant> list(@RequestParam("region") String region) {
List<Restaurant> restaurants = restaurantService.getRestaurants(region);
return restaurants;
}
@Test
public void list를_확인한다() throws Exception {
List<Restaurant> restaurants = new ArrayList<>();
restaurants.add(Restaurant.builder()
.id(1004L)
.name("JOKER House")
.address("Seoul")
.build());

given(restaurantService.getRestaurants("Seoul")).willReturn(restaurants);

ResultActions resultActions = mockMvc.perform(get("/restaurants?region=Seoul"));

resultActions
.andExpect(status().isOk())
.andExpect(content().string(containsString("\"id\":1004")))
.andExpect(content().string(containsString("\"name\":\"JOKER House\"")))
.andDo(print());
}
@Test
public void RequestHeader를_이용한_list를_확인한다() throws Exception {
List<Restaurant> restaurants = new ArrayList<>();
restaurants.add(Restaurant.builder()
.id(1004L)
.name("JOKER House")
.address("Seoul")
.build());

given(restaurantService.getRestaurants("Seoul")).willReturn(restaurants);

ResultActions resultActions = mockMvc.perform(get("/restaurants?region=Seoul"));

resultActions
.andExpect(status().isOk())
.andExpect(content().string(containsString("\"id\":1004")))
.andExpect(content().string(containsString("\"name\":\"JOKER House\"")))
.andDo(print());
}

```java
@Test
public void 새로운_레스토랑_저장_및_조회하기() throws Exception {
String BobZip = "BobZip";
String Seoul = "Seoul";

Restaurant restaurant = Restaurant.builder()
.name(BobZip)
.address(Seoul)
.build();

String content = objectMapper.writeValueAsString(restaurant);

ResultActions postResultActions = mockMvc.perform(post("/restaurants")
.content(content)
.contentType(MediaType.APPLICATION_JSON));

postResultActions
.andExpect(status().isCreated())
.andDo(print());


List<Restaurant> restaurants = new ArrayList<>();
restaurants.add(restaurant);
given(restaurantService.getRestaurants("Seoul")).willReturn(restaurants);

ResultActions getResultActions = mockMvc.perform(get("/restaurants?region=Seoul"));

getResultActions
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value(BobZip))
.andExpect(jsonPath("$[0].address").value(Seoul))
.andDo(print());
}
private void mockRestaurantRepository() {
List<Restaurant> restaurants = new ArrayList<>();
Restaurant restaurant = Restaurant.builder()
.id(1004L)
.address("Seoul")
.name("Bob zip")
.build();
restaurants.add(restaurant);

given(restaurantRepository.findAllByAddressContaining("Seoul")).willReturn(restaurants);

given(restaurantRepository.findById(1004L))
.willReturn(Optional.of(restaurant));
}
Share