| Neue Datei |
| | |
| | | package de.gedoplan.seminar.jpa.exercise; |
| | | |
| | | import static org.junit.jupiter.params.provider.Arguments.arguments; |
| | | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| | | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| | | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| | | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| | | |
| | | import java.util.stream.Stream; |
| | | |
| | | import org.hamcrest.MatcherAssert; |
| | | import org.junit.jupiter.api.MethodOrderer; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.junit.jupiter.api.TestMethodOrder; |
| | | import org.junit.jupiter.params.ParameterizedTest; |
| | | import org.junit.jupiter.params.provider.Arguments; |
| | | import org.junit.jupiter.params.provider.MethodSource; |
| | | 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.MediaType; |
| | | import org.springframework.test.web.servlet.MockMvc; |
| | | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | |
| | | import de.gedoplan.seminar.jpa.exercise.domain.Highway; |
| | | import de.gedoplan.seminar.jpa.exercise.domain.Junction; |
| | | |
| | | @TestMethodOrder(MethodOrderer.MethodName.class) |
| | | @AutoConfigureMockMvc |
| | | @SpringBootTest |
| | | public class Exercise04Test { |
| | | |
| | | @Autowired |
| | | MockMvc mockMvc; |
| | | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | @Test |
| | | void test01_joinJunctionsToHighways() throws Exception { |
| | | |
| | | /* |
| | | * Read highways and junctions from the database and associate them. |
| | | * Do not use the objects from TestData directly, because they are transient! |
| | | */ |
| | | |
| | | /* |
| | | * Connect highway 4610 with junctions "Schwerte", "Westhofener Kreuz" and "Hagen-Nord" |
| | | */ |
| | | Highway highwayA1_DO_K = loadHighway(4610); |
| | | |
| | | Junction junctionSchwerte = loadJunction("Schwerte"); |
| | | // highwayA1_DO_K.getJunctions().add(junctionSchwerte); |
| | | // junctionSchwerte.setHighway(highwayA1_DO_K); |
| | | |
| | | Junction junctionWesthofen = loadJunction("Westhofener Kreuz"); |
| | | // highwayA1_DO_K.getJunctions().add(junctionWesthofen); |
| | | // junctionWesthofen.setHighway(highwayA1_DO_K); |
| | | |
| | | Junction junctionHagenNord = loadJunction("Hagen-Nord"); |
| | | // highwayA1_DO_K.getJunctions().add(junctionHagenNord); |
| | | // junctionHagenNord.setHighway(highwayA1_DO_K); |
| | | |
| | | updateJunctions(junctionSchwerte,junctionWesthofen,junctionHagenNord); |
| | | |
| | | /* |
| | | * Connect highway 4711 with junctions "Bielefeld-Ost", "Ostwestfalen/Lippe" and "Herford/Bad Salzuflen" |
| | | */ |
| | | Highway highwayA2_DO_H = loadHighway(4711); |
| | | |
| | | Junction junctionBielefeldOst = loadJunction("Bielefeld-Ost"); |
| | | // highwayA2_DO_H.getJunctions().add(junctionBielefeldOst); |
| | | // junctionBielefeldOst.setHighway(highwayA2_DO_H); |
| | | |
| | | Junction junctionOWL = loadJunction("Ostwestfalen/Lippe"); |
| | | // highwayA2_DO_H.getJunctions().add(junctionOWL); |
| | | // junctionOWL.setHighway(highwayA2_DO_H); |
| | | |
| | | Junction junctionSennestadt = loadJunction("Herford/Bad Salzuflen"); |
| | | // highwayA2_DO_H.getJunctions().add(junctionSennestadt); |
| | | // junctionSennestadt.setHighway(highwayA2_DO_H); |
| | | |
| | | updateJunctions(junctionBielefeldOst,junctionOWL,junctionSennestadt); |
| | | |
| | | /* |
| | | * Connect highway 4812 with junction "Paderborn-Zentrum" |
| | | */ |
| | | Highway highwayA33_BI_PB = loadHighway(4812); |
| | | |
| | | Junction junctionStukenbrock = loadJunction("Paderborn-Zentrum"); |
| | | // highwayA33_BI_PB.getJunctions().add(junctionStukenbrock); |
| | | // junctionStukenbrock.setHighway(highwayA33_BI_PB); |
| | | |
| | | updateJunctions(junctionStukenbrock); |
| | | |
| | | } |
| | | |
| | | @ParameterizedTest |
| | | @MethodSource("getTestData") |
| | | void test2_findById(String junctionName, String highwayName) throws Exception { |
| | | mockMvc.perform(get("/junctions/loadByName").param("name", junctionName)) |
| | | .andExpect(jsonPath("$.highway.name").value(highwayName)); |
| | | } |
| | | |
| | | |
| | | private void updateJunctions(Junction... junctions) { |
| | | Stream.of(junctions).forEach(junction -> |
| | | { |
| | | try { |
| | | mockMvc.perform( |
| | | post("/junctions").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(junction))) |
| | | .andExpect(status().isOk()); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private Highway loadHighway(Integer id) throws Exception { |
| | | return objectMapper.readValue(mockMvc.perform(get("/highways/{id}", id)).andExpect(status().isOk()) |
| | | .andReturn().getResponse().getContentAsString(), Highway.class); |
| | | } |
| | | |
| | | private Junction loadJunction(String name) throws Exception { |
| | | return objectMapper.readValue(mockMvc.perform(get("/junctions/loadByName").param("name", name)).andExpect(status().isOk()) |
| | | .andReturn().getResponse().getContentAsString(), Junction.class); |
| | | } |
| | | |
| | | private static Stream<Arguments> getTestData() { |
| | | return Stream.of( |
| | | arguments("Schwerte","A2"), |
| | | arguments("Westhofener Kreuz", "A1"), |
| | | arguments("Hagen-Nord", "A1"), |
| | | arguments("Bielefeld-Ost", "A2"), |
| | | arguments("Ostwestfalen/Lippe", "A2"), |
| | | arguments("Herford/Bad Salzuflen", "A2"), |
| | | arguments("Paderborn-Zentrum", "A33")); |
| | | } |
| | | } |