1
+ package com .happn .agareau .techtest .densitypop .controller ;
2
+
3
+ import com .happn .agareau .techtest .densitypop .domain .PointOfInterest ;
4
+ import com .happn .agareau .techtest .densitypop .error .Error ;
5
+ import com .happn .agareau .techtest .densitypop .properties .CoordinatesProperties ;
6
+ import com .happn .agareau .techtest .densitypop .service .TSVService ;
7
+ import io .vavr .collection .Seq ;
8
+ import io .vavr .control .Validation ;
9
+ import org .junit .jupiter .api .Test ;
10
+ import org .springframework .beans .factory .annotation .Autowired ;
11
+ import org .springframework .beans .factory .annotation .Value ;
12
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
13
+ import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
14
+ import org .springframework .boot .test .mock .mockito .MockBean ;
15
+ import org .springframework .core .io .Resource ;
16
+ import org .springframework .http .MediaType ;
17
+ import org .springframework .mock .web .MockMultipartFile ;
18
+ import org .springframework .test .web .servlet .MockMvc ;
19
+ import org .springframework .test .web .servlet .ResultActions ;
20
+
21
+ import java .io .BufferedReader ;
22
+ import java .io .IOException ;
23
+ import java .io .InputStreamReader ;
24
+ import java .util .List ;
25
+ import java .util .stream .Collectors ;
26
+
27
+ import static com .happn .agareau .techtest .densitypop .error .Error .ReadFileError ;
28
+ import static com .happn .agareau .techtest .densitypop .error .Error .UploadFileError ;
29
+ import static io .vavr .API .Seq ;
30
+ import static io .vavr .control .Validation .invalid ;
31
+ import static org .mockito .Mockito .when ;
32
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .multipart ;
33
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
34
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
35
+
36
+ @ WebMvcTest (FileUploadController .class )
37
+ @ EnableConfigurationProperties (CoordinatesProperties .class )
38
+ class FileUploadControllerTest {
39
+
40
+ @ Autowired
41
+ private MockMvc mockMvc ;
42
+
43
+ @ MockBean
44
+ private TSVService tsvService ;
45
+
46
+ @ Value ("classpath:/response/uploadTsvFileOKResponse.json" )
47
+ private Resource uploadTsvFileOKResponse ;
48
+
49
+ @ Value ("classpath:/response/uploadTsvFileKOResponse.json" )
50
+ private Resource uploadTsvFileKOResponse ;
51
+
52
+ @ Test
53
+ void upload_file_and_return_list_should_success () throws Exception {
54
+
55
+ MockMultipartFile multipartFile = new MockMultipartFile ("file" , "test.txt" ,
56
+ "text/plain" , "Test upload" .getBytes ());
57
+
58
+ when (tsvService .uploadAndReadTSVFileAndReturnListPOI (multipartFile )).thenReturn (Validation .valid (createListPOI ()));
59
+
60
+
61
+ ResultActions resultActions = this .mockMvc
62
+ .perform (multipart ("/upload" )
63
+ .file (multipartFile ));
64
+
65
+ // ResultActions resultActions = this.mockMvc.perform(post("/upload")
66
+ // .contentType(MediaType.APPLICATION_JSON)
67
+ // .content(Objects.requireNonNull(readResource(betResourceRequest))));
68
+
69
+
70
+ resultActions .andExpect (status ().is2xxSuccessful ())
71
+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
72
+ .andExpect (content ().json (readResource (uploadTsvFileOKResponse ), true ));
73
+
74
+ }
75
+
76
+ @ Test
77
+ void upload_file_should_fail () throws Exception {
78
+
79
+ Seq <Error > fileError = Seq (new UploadFileError ("cannot upload file" , new Throwable ()),
80
+ new ReadFileError ("cannot read file" , new IOException ()));
81
+
82
+ MockMultipartFile multipartFile = new MockMultipartFile ("file" , "test.txt" ,
83
+ "text/plain" , "Test upload" .getBytes ());
84
+
85
+ when (tsvService .uploadAndReadTSVFileAndReturnListPOI (multipartFile )).thenReturn (invalid (fileError ));
86
+
87
+ ResultActions resultActions = this .mockMvc
88
+ .perform (multipart ("/upload" )
89
+ .file (multipartFile ));
90
+
91
+ resultActions .andExpect (status ().is5xxServerError ())
92
+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
93
+ .andExpect (content ().json (readResource (uploadTsvFileKOResponse ), true ));
94
+
95
+ }
96
+
97
+
98
+ private String readResource (Resource resource ) throws IOException {
99
+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (resource .getInputStream ()))) {
100
+ return reader .lines ().collect (Collectors .joining ("\n " ));
101
+ }
102
+ }
103
+
104
+ private List <PointOfInterest > createListPOI () {
105
+
106
+ PointOfInterest pointOfInterest = new PointOfInterest ("id0" , -48.6 , -37.7 );
107
+ PointOfInterest pointOfInterest1 = new PointOfInterest ("id1" , -27.1 , 8.4 );
108
+ PointOfInterest pointOfInterest2 = new PointOfInterest ("id2" , 6.6 , -6.9 );
109
+ PointOfInterest pointOfInterest3 = new PointOfInterest ("id3" , -2.3 , 38.3 );
110
+ PointOfInterest pointOfInterest4 = new PointOfInterest ("id4" , 6.8 , -6.9 );
111
+ PointOfInterest pointOfInterest5 = new PointOfInterest ("id5" , -2.5 , 38.3 );
112
+ PointOfInterest pointOfInterest6 = new PointOfInterest ("id6" , 0.1 , -0.1 );
113
+ PointOfInterest pointOfInterest7 = new PointOfInterest ("id7" , -2.1 , 38.1 );
114
+ PointOfInterest pointOfInterest8 = new PointOfInterest ("id8" , -2.1 , 33.1 );
115
+ return List .of (pointOfInterest , pointOfInterest1 , pointOfInterest2 , pointOfInterest3 , pointOfInterest4 , pointOfInterest5 , pointOfInterest6 , pointOfInterest7 , pointOfInterest8 );
116
+
117
+ }
118
+
119
+ }
0 commit comments