11package ru .javawebinar .topjava .web .meal ;
22
33
4- import org .junit .jupiter .api .Disabled ;
54import org .junit .jupiter .api .Test ;
65import org .springframework .beans .factory .annotation .Autowired ;
76import org .springframework .http .MediaType ;
1817import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
1918import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
2019import static ru .javawebinar .topjava .MealTestData .*;
20+ import static ru .javawebinar .topjava .TestUtil .userHttpBasic ;
2121import static ru .javawebinar .topjava .UserTestData .USER_ID ;
2222import static ru .javawebinar .topjava .UserTestData .user ;
2323import static ru .javawebinar .topjava .util .MealsUtil .createTo ;
2424import static ru .javawebinar .topjava .util .MealsUtil .getTos ;
2525
26- @ Disabled
2726class MealRestControllerTest extends AbstractControllerTest {
2827
2928 private static final String REST_URL = MealRestController .REST_URL + '/' ;
@@ -33,16 +32,24 @@ class MealRestControllerTest extends AbstractControllerTest {
3332
3433 @ Test
3534 void get () throws Exception {
36- perform (MockMvcRequestBuilders .get (REST_URL + MEAL1_ID ))
35+ perform (MockMvcRequestBuilders .get (REST_URL + MEAL1_ID )
36+ .with (userHttpBasic (user )))
3737 .andExpect (status ().isOk ())
3838 .andDo (print ())
3939 .andExpect (content ().contentTypeCompatibleWith (MediaType .APPLICATION_JSON ))
4040 .andExpect (MEAL_MATCHER .contentJson (meal1 ));
4141 }
4242
43+ @ Test
44+ void getUnauth () throws Exception {
45+ perform (MockMvcRequestBuilders .get (REST_URL + MEAL1_ID ))
46+ .andExpect (status ().isUnauthorized ());
47+ }
48+
4349 @ Test
4450 void delete () throws Exception {
45- perform (MockMvcRequestBuilders .delete (REST_URL + MEAL1_ID ))
51+ perform (MockMvcRequestBuilders .delete (REST_URL + MEAL1_ID )
52+ .with (userHttpBasic (user )))
4653 .andExpect (status ().isNoContent ());
4754 assertThrows (NotFoundException .class , () -> mealService .get (MEAL1_ID , USER_ID ));
4855 }
@@ -51,6 +58,7 @@ void delete() throws Exception {
5158 void update () throws Exception {
5259 Meal updated = getUpdated ();
5360 perform (MockMvcRequestBuilders .put (REST_URL + MEAL1_ID ).contentType (MediaType .APPLICATION_JSON )
61+ .with (userHttpBasic (user ))
5462 .content (JsonUtil .writeValue (updated )))
5563 .andExpect (status ().isNoContent ());
5664
@@ -62,6 +70,7 @@ void createWithLocation() throws Exception {
6270 Meal newMeal = getNew ();
6371 ResultActions action = perform (MockMvcRequestBuilders .post (REST_URL )
6472 .contentType (MediaType .APPLICATION_JSON )
73+ .with (userHttpBasic (user ))
6574 .content (JsonUtil .writeValue (newMeal )))
6675 .andExpect (status ().isCreated ());
6776
@@ -74,7 +83,8 @@ void createWithLocation() throws Exception {
7483
7584 @ Test
7685 void getAll () throws Exception {
77- perform (MockMvcRequestBuilders .get (REST_URL ))
86+ perform (MockMvcRequestBuilders .get (REST_URL )
87+ .with (userHttpBasic (user )))
7888 .andExpect (status ().isOk ())
7989 .andDo (print ())
8090 .andExpect (content ().contentTypeCompatibleWith (MediaType .APPLICATION_JSON ))
@@ -85,15 +95,17 @@ void getAll() throws Exception {
8595 void getBetween () throws Exception {
8696 perform (MockMvcRequestBuilders .get (REST_URL + "filter" )
8797 .param ("startDate" , "2020-01-30" ).param ("startTime" , "07:00" )
88- .param ("endDate" , "2020-01-31" ).param ("endTime" , "11:00" ))
98+ .param ("endDate" , "2020-01-31" ).param ("endTime" , "11:00" )
99+ .with (userHttpBasic (user )))
89100 .andExpect (status ().isOk ())
90101 .andDo (print ())
91102 .andExpect (TO_MATCHER .contentJson (createTo (meal5 , true ), createTo (meal1 , false )));
92103 }
93104
94105 @ Test
95106 void getBetweenAll () throws Exception {
96- perform (MockMvcRequestBuilders .get (REST_URL + "filter?startDate=&endTime=" ))
107+ perform (MockMvcRequestBuilders .get (REST_URL + "filter?startDate=&endTime=" )
108+ .with (userHttpBasic (user )))
97109 .andExpect (status ().isOk ())
98110 .andExpect (TO_MATCHER .contentJson (getTos (meals , user .getCaloriesPerDay ())));
99111 }
0 commit comments