Hendrik Jungnitsch
2022-09-02 ca31cff26810ce280df03da252d3bbf73b932409
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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"));
    }
}