55import static net .javacrumbs .jsonunit .core .Option .IGNORING_ARRAY_ORDER ;
66import static org .junit .jupiter .api .Assertions .*;
77import static org .junit .jupiter .api .TestInstance .Lifecycle .PER_CLASS ;
8- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
9- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
8+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
109import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
1110
1211import com .fasterxml .jackson .databind .ObjectMapper ;
12+ import com .jayway .jsonpath .JsonPath ;
1313import io .openaev .IntegrationTest ;
14- import io .openaev .database .model .Challenge ;
15- import io .openaev .database .model .Document ;
16- import io .openaev .database .model .Domain ;
14+ import io .openaev .database .model .*;
1715import io .openaev .database .repository .ChallengeRepository ;
1816import io .openaev .database .repository .DocumentRepository ;
1917import io .openaev .rest .document .DocumentService ;
18+ import io .openaev .rest .document .form .DocumentCreateInput ;
2019import io .openaev .rest .document .form .DocumentRelationsOutput ;
2120import io .openaev .rest .document .form .RelatedEntityOutput ;
2221import io .openaev .utils .fixtures .*;
23- import io .openaev .utils .fixtures .composers .ChallengeComposer ;
24- import io .openaev .utils .fixtures .composers .DocumentComposer ;
25- import io .openaev .utils .fixtures .composers .DomainComposer ;
26- import io .openaev .utils .fixtures .composers .PayloadComposer ;
22+ import io .openaev .utils .fixtures .composers .*;
2723import io .openaev .utils .fixtures .files .BinaryFile ;
2824import io .openaev .utils .mockUser .WithMockUser ;
2925import jakarta .annotation .Resource ;
3026import java .util .HashMap ;
27+ import java .util .List ;
3128import java .util .Map ;
3229import java .util .Set ;
3330import org .junit .jupiter .api .*;
3431import org .springframework .beans .factory .annotation .Autowired ;
32+ import org .springframework .http .MediaType ;
33+ import org .springframework .mock .web .MockMultipartFile ;
34+ import org .springframework .mock .web .MockPart ;
3535import org .springframework .test .web .servlet .MockMvc ;
3636import org .springframework .transaction .annotation .Transactional ;
3737
@@ -45,11 +45,14 @@ class DocumentApiTest extends IntegrationTest {
4545 @ Autowired ChallengeComposer challengeComposer ;
4646 @ Autowired PayloadComposer payloadComposer ;
4747 @ Autowired DomainComposer domainComposer ;
48+ @ Autowired ScenarioComposer scenarioComposer ;
49+ @ Autowired ExerciseComposer exerciseComposer ;
4850 @ Autowired private MockMvc mvc ;
4951 @ Autowired private DocumentRepository documentRepository ;
5052 @ Autowired private ChallengeRepository challengeRepository ;
5153
52- @ BeforeAll
54+
55+ @ BeforeAll
5356 void beforeAll () {
5457 challengeComposer .reset ();
5558 documentComposer .reset ();
@@ -142,6 +145,98 @@ void givenDocumentShouldFetchRelatedEntities() throws Exception {
142145
143146 assertThatJson (response ).when (IGNORING_ARRAY_ORDER ).isEqualTo (relationJson );
144147 }
148+
149+ @ Test
150+ @ DisplayName ("Should create a document when uploading a valid file and input" )
151+ void uploadDocumentShouldCreateDocument () throws Exception {
152+ // -- PREPARE
153+ Scenario scenario = scenarioComposer .forScenario (ScenarioFixture .getScenario ()).persist ().get ();
154+ Exercise exercise = exerciseComposer .forExercise (ExerciseFixture .getExercise ()).persist ().get ();
155+
156+ DocumentCreateInput input = new DocumentCreateInput ();
157+ input .setDescription ("My test document" );
158+ input .setScenarioIds (List .of (scenario .getId ()));
159+ input .setExerciseIds (List .of (exercise .getId ()));
160+
161+ MockPart inputPart = new MockPart (
162+ "input" ,
163+ mapper .writeValueAsBytes (input )
164+ );
165+ inputPart .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
166+
167+ MockMultipartFile filePart = new MockMultipartFile (
168+ "file" ,
169+ FileFixture .getPngSmileFileContent ().getFileName (),
170+ MediaType .APPLICATION_XML .toString (),
171+ FileFixture .getPngSmileFileContent ().getContentBytes ()
172+ );
173+
174+ // -- EXECUTE
175+ String response = mvc .perform (
176+ multipart (DOCUMENT_API + "/upsert" )
177+ .part (inputPart )
178+ .file (filePart )
179+ .accept (MediaType .APPLICATION_JSON )
180+ )
181+ .andExpect (status ().isOk ())
182+ .andReturn ()
183+ .getResponse ()
184+ .getContentAsString ();
185+
186+ // -- VERIFY
187+ assertNotNull (response );
188+ assertEquals (FileFixture .getPngSmileFileContent ().getFileName (), JsonPath .read (response , "$.document_name" ));
189+ assertEquals ("My test document" , JsonPath .read (response , "$.document_description" ));
190+ assertEquals (scenario .getId (), JsonPath .read (response , "$.document_scenarios[0]" ));
191+ assertEquals (exercise .getId (), JsonPath .read (response , "$.document_exercises[0]" ));
192+ }
193+
194+ @ Test
195+ @ DisplayName ("Should update a document when uploading a valid file and input" )
196+ void uploadDocumentShouldUpdateDocument () throws Exception {
197+ // -- PREPARE
198+ Scenario scenario = scenarioComposer .forScenario (ScenarioFixture .getScenario ()).persist ().get ();
199+ Exercise exercise = exerciseComposer .forExercise (ExerciseFixture .getExercise ()).persist ().get ();
200+
201+ Document document = documentComposer .forDocument (DocumentFixture .getDocument (FileFixture .getPlainTextFileContent ())).persist ().get ();
202+
203+ DocumentCreateInput input = new DocumentCreateInput ();
204+ input .setDescription (document .getDescription ());
205+ input .setScenarioIds (List .of (scenario .getId ()));
206+ input .setExerciseIds (List .of (exercise .getId ()));
207+
208+ MockPart inputPart = new MockPart (
209+ "input" ,
210+ mapper .writeValueAsBytes (input )
211+ );
212+ inputPart .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
213+
214+ MockMultipartFile filePart = new MockMultipartFile (
215+ "file" ,
216+ FileFixture .getPlainTextFileContent ().getFileName (),
217+ MediaType .APPLICATION_XML .toString (),
218+ FileFixture .getPlainTextFileContent ().getContentBytes ()
219+ );
220+
221+ // -- EXECUTE
222+ String response = mvc .perform (
223+ multipart (DOCUMENT_API + "/upsert" )
224+ .part (inputPart )
225+ .file (filePart )
226+ .accept (MediaType .APPLICATION_JSON )
227+ )
228+ .andExpect (status ().isOk ())
229+ .andReturn ()
230+ .getResponse ()
231+ .getContentAsString ();
232+
233+ // -- VERIFY
234+ assertNotNull (response );
235+ assertEquals (FileFixture .getPngSmileFileContent ().getFileName (), JsonPath .read (response , "$.document_name" ));
236+ assertEquals ("My test document" , JsonPath .read (response , "$.document_description" ));
237+ assertEquals (scenario .getId (), JsonPath .read (response , "$.document_scenarios[0]" ));
238+ assertEquals (exercise .getId (), JsonPath .read (response , "$.document_exercises[0]" ));
239+ }
145240 }
146241
147242 @ Test
0 commit comments