Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ramueSVA committed Dec 9, 2024
1 parent d786161 commit 47d6e56
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@AllArgsConstructor
public class MessageDto {

@Schema(accessMode = Schema.AccessMode.READ_ONLY)
Long id;

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message {
@EqualsAndHashCode
public class Message implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

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;

@Repository
public interface MessageRepository extends JpaRepository<Message, Long>, PagingAndSortingRepository<Message, Long> {

Page<Message> findMessages(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MessageDto createUserMessage(MessageDto messageDto, String userId) {

public Page<MessageDto> getMessages(String userId, Pageable pageable) {
userDetailsService.checkIsUserApproved(userId);
Page<Message> messagePage = messageRepository.findMessages(pageable);
Page<Message> messagePage = messageRepository.findAll(pageable);
return messagePage.map(message -> messageMapper.convertToDTO(message));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -27,19 +25,15 @@
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")
@SecurityRequirement(name = "security_auth")
public class MessageController {

private final MessageService messageService;
private final MessageMapper messageMapper;

@ContextLog(type = "MessageManagement", description = "Create user message")
@PostMapping()
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +40,6 @@ public class MessageServiceTest {
private MessageDto updateMessageDto;
private Message message;
private Message messageToEditPlanned;
private Message messageToEditActive;

@Before
public void setup() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 47d6e56

Please sign in to comment.