backend/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java

185 lines
8.6 KiB
Java
Raw Normal View History

2020-10-02 16:51:49 +02:00
package com.plannaplan.controllers;
import org.junit.Test;
import org.junit.runner.RunWith;
2020-10-05 17:56:35 +02:00
import org.springframework.beans.factory.annotation.Autowired;
2020-10-02 16:51:49 +02:00
import org.springframework.boot.test.context.SpringBootTest;
2020-10-08 15:24:23 +02:00
import org.springframework.http.MediaType;
2020-10-02 16:51:49 +02:00
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
2020-10-05 17:56:35 +02:00
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2020-10-08 15:24:23 +02:00
import java.nio.charset.Charset;
2020-10-05 17:56:35 +02:00
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2020-10-02 16:51:49 +02:00
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class CommisionControllerTest {
2020-10-05 17:56:35 +02:00
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private UserService service;
private static User user;
2020-10-30 14:27:13 +01:00
private static User otherUser;
private static User asker;
private static User otherAsker;
private static final String TEST_COMMISIONS_STUDENT_EMAIL = "commisions.student@notexisting.domain";
private static final String TEST_COMMISIONS_OTHER_STUDENT_EMAIL = "commisions.student2@notexisting.domain";
private static final String TEST_COMMISIONS_DEANERY_EMAIL = "commisions.deanery@notexisting.domain";
private static final String TEST_COMMISIONS_OTHER_DEANERY_EMAIL = "commisions.deanery2@notexisting.domain";
2020-10-05 17:56:35 +02:00
private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/add";
private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/getAllCommisions";
2020-10-30 14:27:13 +01:00
2020-10-08 15:24:23 +02:00
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
2020-10-05 17:56:35 +02:00
2020-10-02 16:51:49 +02:00
@Test
2020-10-05 17:56:35 +02:00
public void shouldReturn4xxAddingCommision() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT)).andExpect(status().is4xxClientError());
2020-10-02 16:51:49 +02:00
}
@Test
2020-10-08 15:24:23 +02:00
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
2020-10-05 17:56:35 +02:00
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
2020-10-08 15:24:23 +02:00
.andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOkAddingCommision() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
2020-10-08 15:24:23 +02:00
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
.contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().isOk());
2020-10-02 16:51:49 +02:00
}
@Test
2020-10-05 17:56:35 +02:00
public void shouldReturn4xxGettingAllCommisions() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT)).andExpect(status().is4xxClientError());
2020-10-02 16:51:49 +02:00
}
@Test
2020-10-05 17:56:35 +02:00
public void shouldReturnOkGettingAllCommisions() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
2020-10-05 17:56:35 +02:00
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
2020-10-29 17:45:15 +01:00
@Test
public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().isOk());
2020-10-29 17:45:15 +01:00
}
@Test
public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().is4xxClientError());
2020-10-29 17:45:15 +01:00
}
@Test
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
.contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().is4xxClientError());
2020-10-29 17:45:15 +01:00
}
@Test
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().is4xxClientError());
2020-10-29 17:45:15 +01:00
}
@Test
public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().isOk());
2020-10-29 17:45:15 +01:00
}
@Test
public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception {
2020-10-30 14:27:13 +01:00
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().is4xxClientError());
2020-10-29 17:45:15 +01:00
}
2020-10-30 14:27:13 +01:00
private void checkUsers() {
2020-10-05 17:56:35 +02:00
if (CommisionControllerTest.user == null) {
2020-10-30 14:27:13 +01:00
CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_STUDENT_EMAIL, UserRoles.STUDENT);
2020-10-05 17:56:35 +02:00
this.service.save(user);
}
2020-10-30 14:27:13 +01:00
if (CommisionControllerTest.otherUser == null) {
CommisionControllerTest.otherUser = new User(null, null, TEST_COMMISIONS_OTHER_STUDENT_EMAIL,
UserRoles.STUDENT);
this.service.save(otherUser);
}
if (CommisionControllerTest.asker == null) {
CommisionControllerTest.asker = new User(null, null, TEST_COMMISIONS_DEANERY_EMAIL, UserRoles.DEANERY);
this.service.save(asker);
}
if (CommisionControllerTest.otherAsker == null) {
CommisionControllerTest.otherAsker = new User(null, null, TEST_COMMISIONS_OTHER_DEANERY_EMAIL,
UserRoles.DEANERY);
this.service.save(otherAsker);
}
2020-10-02 16:51:49 +02:00
}
}