diff --git a/src/main/java/org/highmed/numportal/domain/dto/MessageDto.java b/src/main/java/org/highmed/numportal/domain/dto/MessageDto.java index 2dfe28f2..fd821936 100644 --- a/src/main/java/org/highmed/numportal/domain/dto/MessageDto.java +++ b/src/main/java/org/highmed/numportal/domain/dto/MessageDto.java @@ -19,6 +19,7 @@ @AllArgsConstructor public class MessageDto { + @Schema(accessMode = Schema.AccessMode.READ_ONLY) Long id; @NotNull diff --git a/src/main/java/org/highmed/numportal/domain/model/Message.java b/src/main/java/org/highmed/numportal/domain/model/Message.java index 44bcc71c..424c0195 100644 --- a/src/main/java/org/highmed/numportal/domain/model/Message.java +++ b/src/main/java/org/highmed/numportal/domain/model/Message.java @@ -7,8 +7,10 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import java.io.Serializable; import java.time.LocalDateTime; @Entity @@ -16,7 +18,8 @@ @Data @NoArgsConstructor @AllArgsConstructor -public class Message { +@EqualsAndHashCode +public class Message implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/org/highmed/numportal/domain/repository/MessageRepository.java b/src/main/java/org/highmed/numportal/domain/repository/MessageRepository.java index 47cc4ddf..e132e476 100644 --- a/src/main/java/org/highmed/numportal/domain/repository/MessageRepository.java +++ b/src/main/java/org/highmed/numportal/domain/repository/MessageRepository.java @@ -2,8 +2,6 @@ import org.highmed.numportal.domain.model.Message; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; @@ -11,5 +9,4 @@ @Repository public interface MessageRepository extends JpaRepository, PagingAndSortingRepository { - Page findMessages(Pageable pageable); } diff --git a/src/main/java/org/highmed/numportal/service/MessageService.java b/src/main/java/org/highmed/numportal/service/MessageService.java index d668f876..2a72c5d1 100644 --- a/src/main/java/org/highmed/numportal/service/MessageService.java +++ b/src/main/java/org/highmed/numportal/service/MessageService.java @@ -42,7 +42,7 @@ public MessageDto createUserMessage(MessageDto messageDto, String userId) { public Page getMessages(String userId, Pageable pageable) { userDetailsService.checkIsUserApproved(userId); - Page messagePage = messageRepository.findMessages(pageable); + Page messagePage = messageRepository.findAll(pageable); return messagePage.map(message -> messageMapper.convertToDTO(message)); } diff --git a/src/main/java/org/highmed/numportal/web/controller/MessageController.java b/src/main/java/org/highmed/numportal/web/controller/MessageController.java index f28d03e0..79b2b2bd 100644 --- a/src/main/java/org/highmed/numportal/web/controller/MessageController.java +++ b/src/main/java/org/highmed/numportal/web/controller/MessageController.java @@ -1,7 +1,6 @@ package org.highmed.numportal.web.controller; import org.highmed.numportal.domain.dto.MessageDto; -import org.highmed.numportal.mapper.MessageMapper; import org.highmed.numportal.service.MessageService; import org.highmed.numportal.service.logger.ContextLog; import org.highmed.numportal.web.config.Role; @@ -14,7 +13,6 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -27,11 +25,8 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.time.LocalDateTime; - @RestController @AllArgsConstructor @RequestMapping(value = "/message", produces = "application/json") @@ -39,7 +34,6 @@ public class MessageController { private final MessageService messageService; - private final MessageMapper messageMapper; @ContextLog(type = "MessageManagement", description = "Create user message") @PostMapping() diff --git a/src/test/java/org/highmed/numportal/integrationtesting/tests/MessageControllerIT.java b/src/test/java/org/highmed/numportal/integrationtesting/tests/MessageControllerIT.java new file mode 100644 index 00000000..1f8a5eba --- /dev/null +++ b/src/test/java/org/highmed/numportal/integrationtesting/tests/MessageControllerIT.java @@ -0,0 +1,84 @@ +package org.highmed.numportal.integrationtesting.tests; + +import org.highmed.numportal.TestNumPortalApplication; +import org.highmed.numportal.domain.model.Message; +import org.highmed.numportal.domain.model.MessageType; +import org.highmed.numportal.domain.repository.MessageRepository; +import org.highmed.numportal.integrationtesting.security.WithMockNumUser; + +import lombok.SneakyThrows; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +import java.time.LocalDateTime; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.MOCK, + classes = TestNumPortalApplication.class) +@AutoConfigureMockMvc +@ActiveProfiles("itest") +public class MessageControllerIT extends IntegrationTest{ + + private static final String MESSAGE_PATH = "/message"; + + @Autowired + public MockMvc mockMvc; + + @Autowired + private MessageRepository messageRepository; + + @Before + public void setUpMessage(){ +// messageRepository.deleteAll(); + LocalDateTime now = java.time.LocalDateTime.now().minusMinutes(5); + Message inactiveMessage = + Message.builder() + .title("Inactive message") + .type(MessageType.ERROR) + .startDate(now.minusHours(10)) + .endDate(now.minusHours(5)) + .build(); + messageRepository.save(inactiveMessage); + Message activeMessage = + Message.builder() + .title("Active message") + .type(MessageType.INFO) + .startDate(now.minusHours(10)) + .endDate(now.plusMinutes(5)) + .build(); + messageRepository.save(activeMessage); + Message plannedMessage = + Message.builder() + .title("Planned message") + .type(MessageType.INFO) + .startDate(now.plusHours(1)) + .endDate(now.plusHours(10)) + .build(); + messageRepository.save(plannedMessage); + } + + @Test + @SneakyThrows + @WithMockNumUser(roles = {"CONTENT_ADMIN"}) + public void getUserMessages (){ + + MvcResult result = + mockMvc.perform(get(MESSAGE_PATH).with(csrf())).andExpect(status().isOk()).andReturn(); + Assert.assertEquals(3, result.getResponse().getContentAsByteArray().length); + } + +} diff --git a/src/test/java/org/highmed/numportal/service/MessageServiceTest.java b/src/test/java/org/highmed/numportal/service/MessageServiceTest.java index 26100394..88868658 100644 --- a/src/test/java/org/highmed/numportal/service/MessageServiceTest.java +++ b/src/test/java/org/highmed/numportal/service/MessageServiceTest.java @@ -6,7 +6,6 @@ import org.highmed.numportal.domain.repository.MessageRepository; import org.highmed.numportal.mapper.MessageMapper; import org.highmed.numportal.service.exception.BadRequestException; -import org.highmed.numportal.service.exception.ResourceNotFound; import org.junit.Assert; import org.junit.Before; @@ -41,7 +40,6 @@ public class MessageServiceTest { private MessageDto updateMessageDto; private Message message; private Message messageToEditPlanned; - private Message messageToEditActive; @Before public void setup() { @@ -72,14 +70,6 @@ public void setup() { .startDate(LocalDateTime.now().plusHours(3)) .endDate(LocalDateTime.MAX) .type(MessageType.INFO).build(); - - messageToEditActive = Message.builder() - .id(3L) - .title("Neue Serverzeiten") - .text("Serverzeit: 06:00 Uhr - 23:00 Uhr") - .startDate(LocalDateTime.now().minusHours(1)) - .endDate(LocalDateTime.MAX.minusHours(1)) - .type(MessageType.INFO).build(); } @Test