Checkpoint: 5/11

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-10 12:45:51 +01:00
parent c7feb10997
commit 2e6f373997
Signed by: y0rune
GPG Key ID: F204C385F57EB348

View File

@ -2,64 +2,196 @@ package com.plannaplan.controllers;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 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.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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 static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.charset.Charset;
import javax.validation.constraints.AssertTrue; 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) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ContextConfiguration @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 @Test
public void shouldGetAllUsersExchanges(){ @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); assertTrue(false);
} }
@Test @Test
public void shouldFailGettingExchange(){ 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
@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); assertTrue(false);
} }
@Test @Test
public void shouldFailPostDueToExchangeNoFound(){ public void shouldFailGettingExchangeDueToPermission(){
final User user = this.userService.save(new User(null, null,
"shouldFailGettingExchangeDueToPermission@ExchangeController.test", UserRoles.STUDENT));
assertTrue(false); assertTrue(false);
} }
@Test @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(){ public void shouldInsertExchange(){
final User user = this.userService.save(new User(null, null,
"shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT));
assertTrue(false); assertTrue(false);
} }
@Test @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(){ public void shouldDenyExchangeDueToAssigmentOverlapping(){
final User user = this.userService.save(new User(null, null,
"shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", UserRoles.STUDENT));
assertTrue(false); 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 @Test
public void shouldDenyPostDueToAssignmentNotAccepted(){ @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); assertTrue(false);
} }
@Test @Test
public void shouldFailDeleteDueToWrongPermissions(){ public void shouldFailDeleteDueToWrongPermissions(){
final User user = this.userService.save(new User(null, null,
"shouldFailDeleteDueToWrongPermissions@ExchangeController.test", UserRoles.STUDENT));
assertTrue(false); assertTrue(false);
} }
@Test @Test
public void shouldFailDeleteDueToExchangeNoFound(){ public void shouldFailDeleteDueToMissingParam(){
final User user = this.userService.save(new User(null, null,
"shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT));
assertTrue(false); assertTrue(false);
} }
@Test @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(){ public void shouldFailPostDueToGroupAlreadyAccepted(){
final User user = this.userService.save(new User(null, null,
"shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", UserRoles.STUDENT));
assertTrue(false); assertTrue(false);
} }
} }