티스토리 뷰

반응형

07. 07. 가게 목록 -08. 08. 가게 상세 - 1

package kr.co.fastcampus.eatgo.interfaces;

import io.jsonwebtoken.Claims;
import kr.co.fastcampus.eatgo.application.ReservationService;
import kr.co.fastcampus.eatgo.domain.Reservation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController // rest api 사용
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @GetMapping("/reservations")
    public List<Reservation> list(
            Authentication authentication
    ) {
        Claims claims = (Claims) authentication.getPrincipal();

        Long restaurantId = claims.get("restaurantId", Long.class);

        List<Reservation> reservations =
                reservationService.getReservations(restaurantId);

        return reservations;
    }
    
    
    @GetMapping("restaurant/{id}")
    public Restaurant detail(@PathVariable("id") Long id){ //path 처리를 보아라
    
    
    List<Restaurant> restaurants = new ArrayList();
    restaurants.add(new Restaurant(1004L, "Bob xip", "Seoul"));
    restaurants.add(new Restaurant(1005L, "Bob xip2", "Seoul"));
    
    
    //restaurant 에서 id 와 같은 것을 filter 로 가져오기
    // 이거  필기 
    Restaurant restaurant = restaurants.stream()
    .filter(r-> r.getId().equals(id)).findFirst().get().orElse(null);
    
    
    
    
    
    return restaurant;
    
    }
    
    

}

 

package kr.co.fastcampus.eatgo.interfaces;

import kr.co.fastcampus.eatgo.application.ReservationService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringRunner.class) // 스프링을 이용해서 테스트를 진행하겠습니다.
@WebMvcTest(ReservationController.class)  //restaurant controller 을 test 를 합니다.
public class ReservationControllerTests {

    @Autowired//우리가 안만들어도 스프링에서 알아서 만들어라.
    private MockMvc mvc; //mocmvc

    @MockBean
    private ReservationService reservationService;

    @Test
    public void list() throws Exception {//perform 은 예외에 나올 수 있다.
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjIwMjAsIm5hbWUiOiJPd25lciIsInJlc3RhdXJhbnRJZCI6MTAwNH0.a5n4PWJ2-3yVyMaLGG0HSPXtH_mgpOvofpQ1OFkgDOQ";
//contains 가 juit 5 부터 지원을 안함
        mvc.perform(get("/reservations") // get 요청 수행하라 
                .header("Authorization", "Bearer " + token))
                .andExpect(status().isOk()); //예상결과
//asset eq : 같은지 확인
        verify(reservationService).getReservations(1004L);
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

바 인강이 듣고 싶다면 =>https://bit.ly/3ilMbIO

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함