Merged with master

This commit is contained in:
Filip Izydorczyk
2020-12-12 15:38:47 +01:00
10 changed files with 278 additions and 16 deletions

View File

@ -43,6 +43,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_SOMEONE_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
private static final String GET_USER_SCHEDULE_ENDPOINT = "/api/v1/commisions/user/schedule";
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
@ -190,6 +191,58 @@ public class CommisionControllerTest extends AbstractControllerTest {
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturn4xxInScheduleEndpoint() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_USER_SCHEDULE_ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOkInScheduleEdnpoint() throws Exception {
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_USER_SCHEDULE_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldReturnOkInScheduleEdnpointByDeanery() throws Exception {
this.checkUsers();
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + student.getId() + "/schedule").header("Authorization",
"Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailInScheduleEdnpointByOtherStudent() throws Exception {
this.checkUsers();
final User student = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final User otherStudent = this.service.checkForUser(TEST_COMMISIONS_OTHER_STUDENT_EMAIL, null);
final String token = this.service.login(student).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + otherStudent.getId() + "/schedule").header("Authorization",
"Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldFailInScheduleEdnpointByDeaneryDeaneryRelation() throws Exception {
this.checkUsers();
final User deanery = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final User otherDeanery = this.service.checkForUser(TEST_COMMISIONS_OTHER_DEANERY_EMAIL, null);
final String token = this.service.login(deanery).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT + "/" + otherDeanery.getId() + "/schedule").header("Authorization",
"Bearer " + token)).andExpect(status().is4xxClientError());
}
private void checkUsers() {
if (CommisionControllerTest.user == null) {
CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_STUDENT_EMAIL, UserRoles.STUDENT);

View File

@ -2,11 +2,16 @@ package com.plannaplan.responses.mappers;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.CommisionResponse;
import com.plannaplan.responses.models.CommisionWithGroupsResponse;
import com.plannaplan.types.WeekDay;
import org.junit.Test;
@ -17,7 +22,36 @@ public class CommisionResponseMappersTest {
final List<CommisionResponse> resposne = CommisionResponseMappers.mapToResponse(coms);
assertTrue(resposne.get(0) instanceof CommisionResponse);
assertTrue(resposne.get(0).getCommisionDate().equals(coms.get(0).getCommisionDate()));
assertTrue(resposne.get(1).getCommisionDate().equals(coms.get(1).getCommisionDate()));
assertTrue(resposne.get(0).getCommisionDate().equals(coms.get(0).getCommisionDate().toString()));
assertTrue(resposne.get(1).getCommisionDate().equals(coms.get(1).getCommisionDate().toString()));
}
@Test
public void shouldMapListWithEntityToListOfResponsesWithGroups()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final Field reader = Commision.class.getDeclaredField("assignments");
final Field groupReader = Groups.class.getDeclaredField("id");
reader.setAccessible(true);
groupReader.setAccessible(true);
final Commision com1 = new Commision(null);
final Groups group1 = new Groups(43, "BRAK", null, 840, WeekDay.MONDAY, null);
groupReader.set(group1, Long.valueOf(8));
reader.set(com1, Arrays.asList(new Assignment(group1, com1)));
final Commision com2 = new Commision(null);
final Groups group2 = new Groups(124, "BRAK", null, 900, WeekDay.WEDNESDAY, null);
groupReader.set(group2, Long.valueOf(9));
reader.set(com2, Arrays.asList(new Assignment(group2, com2)));
final List<CommisionWithGroupsResponse> resposne = CommisionResponseMappers
.mapToResponseWithGroups(Arrays.asList(com1, com2));
assertTrue(resposne.size() == 2);
assertTrue(resposne.get(0).getGroups().get(0) == 8);
assertTrue(resposne.get(1).getGroups().get(0) == 9);
}
}

View File

@ -12,6 +12,6 @@ public class CommisionResponseTest {
final Commision com = new Commision(null);
final CommisionResponse comResponse = new CommisionResponse(com);
assertTrue(comResponse.getCommisionDate().equals(com.getCommisionDate()));
assertTrue(comResponse.getCommisionDate().equals(com.getCommisionDate().toString()));
}
}

View File

@ -0,0 +1,34 @@
package com.plannaplan.responses.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Field;
import java.util.Arrays;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.types.WeekDay;
import org.junit.Test;
public class CommisionWithGroupsResponseTest {
@Test
public void shouldMapCommisionToResponse()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final Field reader = Commision.class.getDeclaredField("assignments");
final Field groupReader = Groups.class.getDeclaredField("id");
reader.setAccessible(true);
groupReader.setAccessible(true);
final Commision com = new Commision(null);
final Groups group = new Groups(43, "BRAK", null, 840, WeekDay.MONDAY, null);
groupReader.set(group, Long.valueOf(8));
reader.set(com, Arrays.asList(new Assignment(group, com)));
final CommisionWithGroupsResponse comResponse = new CommisionWithGroupsResponse(com);
assertTrue(comResponse.getGroups().get(0) == 8);
}
}