From 2e6f373997d5821d501a4d99025b20c9e4570059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 12:45:51 +0100 Subject: [PATCH] Checkpoint: 5/11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeControllerTest.java | 164 ++++++++++++++++-- 1 file changed, 148 insertions(+), 16 deletions(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index 613dd8a..89f35bb 100644 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -2,64 +2,196 @@ package com.plannaplan.controllers; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.MethodMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.nio.charset.Charset; + import javax.validation.constraints.AssertTrue; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.services.GroupService; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; + @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration -public class ExchangeControllerTest extends AbstractControllerTest{ - +public class ExchangeControllerTest extends AbstractControllerTest { + + private final static String EXCHANGE_ENDPOINT = "/api/v1/exchanges/exchange"; + private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), + MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + + @Autowired + private UserService userService; + + @Autowired + private GroupService groupService; + @Test - public void shouldGetAllUsersExchanges(){ - assertTrue(false); + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldGetAllUsersExchanges() throws Exception { + final User user = this.userService.save(new User(null, null, + "shouldGetAllUsersExchanges@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test - public void shouldFailGettingExchange(){ - assertTrue(false); + public void shouldFailGettingNotExistingExchange() throws Exception { + final User user = this.userService.save(new User(null, null, + "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); + + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + token)).andExpect(status().isBadRequest()); } @Test - public void shouldFailPostDueToExchangeNoFound(){ - assertTrue(false); + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldGetSingleExchange(){ + final User user = this.userService.save(new User(null, null, + "shouldGetSingleExchange@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test + public void shouldFailGettingExchangeDueToPermission(){ + final User user = this.userService.save(new User(null, null, + "shouldFailGettingExchangeDueToPermission@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); + } + + @Test + public void shouldFailPostDueToAssignmentNotFound() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailPostDueToAssignmentNotFound@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldInsertExchange(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test + public void shouldFailInsertExchangeDueToMissingGroup() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailInsertExchangeDueToMissingGroup@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +" }")).andExpect(status().isBadRequest()); + } + + @Test + public void shouldFailInsertExchangeDueToMissingAssignment() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailInsertExchangeDueToMissingAssignment@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); + } + + @Test + public void shouldFailInsertExchangeDueToMissingParam() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailInsertExchangeDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8)).andExpect(status().isBadRequest()); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldDenyExchangeDueToAssigmentOverlapping(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); + } + @Test + public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldDenyPostDueToAssignmentNotAccepted@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); } @Test - public void shouldDenyPostDueToAssignmentNotAccepted(){ - assertTrue(false); + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldDeleteExchange(){ + final User user = this.userService.save(new User(null, null, + "shouldDeleteExchange@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test public void shouldFailDeleteDueToWrongPermissions(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test - public void shouldFailDeleteDueToExchangeNoFound(){ - assertTrue(false); + public void shouldFailDeleteDueToMissingParam(){ + final User user = this.userService.save(new User(null, null, + "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } + @Test + public void shouldFailDeleteDueToExchangeNotFound(){ + final User user = this.userService.save(new User(null, null, + "shouldFailDeleteDueToExchangeNotFound@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldFailPostDueToGroupAlreadyAccepted(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } }