From 3f85b883584bfe4f486ff3da2463407a7635bce4 Mon Sep 17 00:00:00 2001 From: Lukas Karsch <lk224@hdm-stuttgart.de> Date: Wed, 24 Jan 2024 21:13:48 +0100 Subject: [PATCH] #59 created abstract BaseIntegrationTest to reduce boilerplate --- .../integration/BaseIntegrationTest.java | 53 +++++++++++++++++++ .../integration/GardenIntegrationTest.java | 41 +------------- .../integration/PlantsIntegrationTest.java | 16 ++++++ .../integration/WishListIntegrationTest.java | 41 +------------- 4 files changed, 71 insertions(+), 80 deletions(-) create mode 100644 src/test/java/hdm/mi/growbros/integration/BaseIntegrationTest.java create mode 100644 src/test/java/hdm/mi/growbros/integration/PlantsIntegrationTest.java diff --git a/src/test/java/hdm/mi/growbros/integration/BaseIntegrationTest.java b/src/test/java/hdm/mi/growbros/integration/BaseIntegrationTest.java new file mode 100644 index 0000000..957bff1 --- /dev/null +++ b/src/test/java/hdm/mi/growbros/integration/BaseIntegrationTest.java @@ -0,0 +1,53 @@ +package hdm.mi.growbros.integration; + +import hdm.mi.growbros.auth.AuthenticationService; +import hdm.mi.growbros.auth.RegisterRequest; +import hdm.mi.growbros.models.plant.Plant; +import hdm.mi.growbros.models.user.User; +import hdm.mi.growbros.repositories.PlantRepository; +import hdm.mi.growbros.repositories.UserRepository; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.BeforeAll; +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.http.HttpHeaders; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.List; + +@SpringBootTest +@AutoConfigureMockMvc +@Transactional //transactions will be rolled back after each test method +abstract class BaseIntegrationTest { + @Autowired + MockMvc mvc; + + static HttpHeaders authHeader; + + //set up test data + static final Plant erdbeere = Plant.builder().name("Erdbeere!!").build(); + static final Plant pflanzeNumeroUno = Plant.builder().name("Pflanze Numero Uno").build(); + static User user; + + @BeforeAll + public static void setupAll(@Autowired PlantRepository plantRepository, @Autowired AuthenticationService auth, @Autowired UserRepository userRepository) { + plantRepository.saveAll(List.of( + pflanzeNumeroUno, + erdbeere) + ); + + //have to manually set up auth and register a "real" user because + //the actual user details are needed in our tests, it's not just about + //bypassing spring security + + //generate JWT! + if (userRepository.findByEmail("lukas@growbros.com").isEmpty()) { + String jwt = auth.register(RegisterRequest.builder().email("lukas@growbros.com").password("password").firstname("Lukas").lastname("Karsch").build()).getToken(); + user = userRepository.findByEmail("lukas@growbros.com").get(); + authHeader = new HttpHeaders() {{ + set("Authorization", "Bearer " + jwt); + }}; + } + } +} diff --git a/src/test/java/hdm/mi/growbros/integration/GardenIntegrationTest.java b/src/test/java/hdm/mi/growbros/integration/GardenIntegrationTest.java index b0851c5..2e4a59a 100644 --- a/src/test/java/hdm/mi/growbros/integration/GardenIntegrationTest.java +++ b/src/test/java/hdm/mi/growbros/integration/GardenIntegrationTest.java @@ -1,23 +1,12 @@ package hdm.mi.growbros.integration; -import hdm.mi.growbros.auth.AuthenticationService; -import hdm.mi.growbros.auth.RegisterRequest; import hdm.mi.growbros.models.GardenEntry; -import hdm.mi.growbros.models.plant.Plant; -import hdm.mi.growbros.models.user.User; import hdm.mi.growbros.repositories.GardenRepository; -import hdm.mi.growbros.repositories.PlantRepository; -import hdm.mi.growbros.repositories.UserRepository; import jakarta.transaction.Transactional; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; 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.http.HttpHeaders; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.List; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -25,39 +14,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc @Transactional //transactions will be rolled back after each test method -public class GardenIntegrationTest { - @Autowired - private MockMvc mvc; - +public class GardenIntegrationTest extends BaseIntegrationTest { @Autowired private GardenRepository gardenRepository; private final static String gardenUrl = "/api/v1/garden"; - private static HttpHeaders authHeader; - - //set up test data - private static final Plant erdbeere = Plant.builder().name("Erdbeere!!").build(); - private static final Plant pflanzeNumeroUno = Plant.builder().name("Pflanze Numero Uno").build(); - private static User user; - - @BeforeAll - public static void setupAll(@Autowired PlantRepository plantRepository, @Autowired AuthenticationService auth, @Autowired UserRepository userRepository) { - plantRepository.saveAll(List.of( - pflanzeNumeroUno, - erdbeere) - ); - - //have to manually set up auth and register a "real" user because - //the actual user details are needed in our tests, it's not just about - //bypassing spring security - - //generate JWT! - String jwt = auth.register(RegisterRequest.builder().email("lukas@growbros.com").password("password").firstname("Lukas").lastname("Karsch").build()).getToken(); - user = userRepository.findByEmail("lukas@growbros.com").get(); - authHeader = new HttpHeaders() {{ - set("Authorization", "Bearer " + jwt); - }}; - } @Test void shouldReturnAllPlantsInGarden_code200() throws Exception { diff --git a/src/test/java/hdm/mi/growbros/integration/PlantsIntegrationTest.java b/src/test/java/hdm/mi/growbros/integration/PlantsIntegrationTest.java new file mode 100644 index 0000000..83f26ad --- /dev/null +++ b/src/test/java/hdm/mi/growbros/integration/PlantsIntegrationTest.java @@ -0,0 +1,16 @@ +package hdm.mi.growbros.integration; + +import org.junit.jupiter.api.Test; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +class PlantsIntegrationTest extends BaseIntegrationTest { + private final static String plantsUrl = "/api/v1/plants"; + + @Test + void shouldGetAllPlants() throws Exception { + mvc.perform(get(plantsUrl).headers(authHeader)) + .andExpect(status().isOk()); + } +} diff --git a/src/test/java/hdm/mi/growbros/integration/WishListIntegrationTest.java b/src/test/java/hdm/mi/growbros/integration/WishListIntegrationTest.java index 73ee1a4..d798a62 100644 --- a/src/test/java/hdm/mi/growbros/integration/WishListIntegrationTest.java +++ b/src/test/java/hdm/mi/growbros/integration/WishListIntegrationTest.java @@ -1,23 +1,12 @@ package hdm.mi.growbros.integration; -import hdm.mi.growbros.auth.AuthenticationService; -import hdm.mi.growbros.auth.RegisterRequest; import hdm.mi.growbros.models.WishListEntry; -import hdm.mi.growbros.models.plant.Plant; -import hdm.mi.growbros.models.user.User; -import hdm.mi.growbros.repositories.PlantRepository; -import hdm.mi.growbros.repositories.UserRepository; import hdm.mi.growbros.repositories.WishListRepository; import jakarta.transaction.Transactional; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; 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.http.HttpHeaders; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.List; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -25,39 +14,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc @Transactional //transactions will be rolled back after each test method -public class WishListIntegrationTest { - @Autowired - private MockMvc mvc; - +public class WishListIntegrationTest extends BaseIntegrationTest { @Autowired private WishListRepository wishListRepository; private final static String wishlistUrl = "/api/v1/wishlist"; - private static HttpHeaders authHeader; - - //set up test data - private static final Plant erdbeere = Plant.builder().name("Erdbeere!!").build(); - private static final Plant pflanzeNumeroUno = Plant.builder().name("Pflanze Numero Uno").build(); - private static User user; - - @BeforeAll - public static void setupAll(@Autowired PlantRepository plantRepository, @Autowired AuthenticationService auth, @Autowired UserRepository userRepository) { - plantRepository.saveAll(List.of( - pflanzeNumeroUno, - erdbeere) - ); - - //have to manually set up auth and register a "real" user because - //the actual user details are needed in our tests, it's not just about - //bypassing spring security - - //generate JWT! - String jwt = auth.register(RegisterRequest.builder().email("lukas@growbros.com").password("password").firstname("Lukas").lastname("Karsch").build()).getToken(); - user = userRepository.findByEmail("lukas@growbros.com").get(); - authHeader = new HttpHeaders() {{ - set("Authorization", "Bearer " + jwt); - }}; - } @Test void shouldGetAllEntries_code200() throws Exception { -- GitLab