Skip to content

Commit

Permalink
first Int Test
Browse files Browse the repository at this point in the history
  • Loading branch information
mafasva committed Dec 9, 2024
1 parent 510674e commit 4934e28
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/main/resources/db/migration/num-portal/V08__user_messages.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS message;

CREATE TABLE message(
id BIGSERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
text text,
start_date timestamp NOT NULL,
end_date timestamp NOT NULL,
type varchar(125) NOT NULL ,
to_delete boolean
);
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package org.highmed.numportal.integrationtesting.tests;

import org.highmed.numportal.domain.dto.MessageDto;
import org.highmed.numportal.domain.model.Message;
import org.highmed.numportal.domain.model.MessageType;
import org.highmed.numportal.domain.model.admin.UserDetails;
import org.highmed.numportal.domain.repository.MessageRepository;
import org.highmed.numportal.domain.repository.UserDetailsRepository;
import org.highmed.numportal.integrationtesting.security.WithMockNumUser;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.support.PageJacksonModule;
import org.springframework.cloud.openfeign.support.SortJacksonModule;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.time.LocalDateTime;
import java.util.List;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand All @@ -25,6 +32,11 @@ public class MessageControllerIT extends IntegrationTest{

private static final String MESSAGE_PATH = "/message";

private ObjectMapper mapper = new ObjectMapper()
.registerModule(new PageJacksonModule())
.registerModule(new SortJacksonModule())
.registerModule(new JavaTimeModule());

@Autowired
public MockMvc mockMvc;

Expand All @@ -39,7 +51,7 @@ public void setUpMessage(){
Message.builder()
.title("Inactive message")
.type(MessageType.ERROR)
.startDate(now.minusHours(10))
.startDate(now.minusHours(15))
.endDate(now.minusHours(5))
.build();
messageRepository.save(inactiveMessage);
Expand All @@ -61,14 +73,22 @@ public void setUpMessage(){
messageRepository.save(plannedMessage);
}

@SuppressWarnings("rawtypes")
@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);
String contentAsString = result.getResponse().getContentAsString();
Page page = mapper.readValue(contentAsString, Page.class);
List content = page.getContent();

Assert.assertEquals(3, page.getTotalElements());
MessageDto firstMessage = mapper.convertValue(content.get(0), MessageDto.class);
Assert.assertEquals("Inactive message", firstMessage.getTitle());
Assert.assertEquals(MessageType.ERROR, firstMessage.getType());
}

}

0 comments on commit 4934e28

Please sign in to comment.