2323import io .openaev .utils .fixtures .files .BinaryFile ;
2424import io .openaev .utils .mockUser .WithMockUser ;
2525import jakarta .annotation .Resource ;
26- import java .util . HashMap ;
27- import java .util .List ;
28- import java . util . Map ;
29- import java . util . Set ;
26+ import java .io . ByteArrayInputStream ;
27+ import java .util .* ;
28+ import org . apache . commons . codec . digest . DigestUtils ;
29+ import org . apache . commons . io . FilenameUtils ;
3030import org .junit .jupiter .api .*;
3131import org .springframework .beans .factory .annotation .Autowired ;
3232import org .springframework .http .MediaType ;
@@ -45,14 +45,13 @@ class DocumentApiTest extends IntegrationTest {
4545 @ Autowired ChallengeComposer challengeComposer ;
4646 @ Autowired PayloadComposer payloadComposer ;
4747 @ Autowired DomainComposer domainComposer ;
48- @ Autowired ScenarioComposer scenarioComposer ;
49- @ Autowired ExerciseComposer exerciseComposer ;
48+ @ Autowired ScenarioComposer scenarioComposer ;
49+ @ Autowired ExerciseComposer exerciseComposer ;
5050 @ Autowired private MockMvc mvc ;
5151 @ Autowired private DocumentRepository documentRepository ;
5252 @ Autowired private ChallengeRepository challengeRepository ;
5353
54-
55- @ BeforeAll
54+ @ BeforeAll
5655 void beforeAll () {
5756 challengeComposer .reset ();
5857 documentComposer .reset ();
@@ -146,97 +145,166 @@ void givenDocumentShouldFetchRelatedEntities() throws Exception {
146145 assertThatJson (response ).when (IGNORING_ARRAY_ORDER ).isEqualTo (relationJson );
147146 }
148147
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- }
148+ @ Test
149+ @ DisplayName ("Should create a document when uploading a valid file and input" )
150+ void uploadDocumentShouldCreateDocument () throws Exception {
151+ // -- PREPARE
152+ Scenario scenario =
153+ scenarioComposer .forScenario (ScenarioFixture .getScenario ()).persist ().get ();
154+ Exercise exercise =
155+ exerciseComposer .forExercise (ExerciseFixture .createDefaultExercise ()).persist ().get ();
156+
157+ DocumentCreateInput input = new DocumentCreateInput ();
158+ input .setDescription ("My test document" );
159+ input .setScenarioIds (List .of (scenario .getId ()));
160+ input .setExerciseIds (List .of (exercise .getId ()));
161+
162+ MockPart inputPart = new MockPart ("input" , mapper .writeValueAsBytes (input ));
163+ inputPart .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
164+
165+ MockMultipartFile filePart =
166+ new MockMultipartFile (
167+ "file" ,
168+ FileFixture .getPngSmileFileContent ().getFileName (),
169+ MediaType .APPLICATION_XML .toString (),
170+ FileFixture .getPngSmileFileContent ().getContentBytes ());
171+
172+ // -- EXECUTE
173+ String response =
174+ mvc .perform (
175+ multipart (DOCUMENT_API + "/upsert" )
176+ .part (inputPart )
177+ .file (filePart )
178+ .accept (MediaType .APPLICATION_JSON ))
179+ .andExpect (status ().isOk ())
180+ .andReturn ()
181+ .getResponse ()
182+ .getContentAsString ();
183+
184+ // -- VERIFY
185+ assertNotNull (response );
186+ assertEquals (
187+ FileFixture .getPngSmileFileContent ().getFileName (),
188+ 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 =
199+ scenarioComposer .forScenario (ScenarioFixture .getScenario ()).persist ().get ();
200+ Exercise exercise =
201+ exerciseComposer .forExercise (ExerciseFixture .createDefaultExercise ()).persist ().get ();
202+
203+ Document document =
204+ documentComposer
205+ .forDocument (DocumentFixture .getDocument (FileFixture .getPlainTextFileContent ()))
206+ .persist ()
207+ .get ();
208+ document .setExercises (new HashSet <>(Set .of (exercise )));
209+ document .setScenarios (new HashSet <>(Set .of (scenario )));
210+ documentRepository .save (document );
211+
212+ DocumentCreateInput input = new DocumentCreateInput ();
213+ input .setDescription ("My test document" );
214+ input .setScenarioIds (List .of (scenario .getId ()));
215+ input .setExerciseIds (List .of (exercise .getId ()));
216+
217+ MockPart inputPart = new MockPart ("input" , mapper .writeValueAsBytes (input ));
218+ inputPart .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
219+
220+ MockMultipartFile filePart =
221+ new MockMultipartFile (
222+ "file" ,
223+ document .getName (),
224+ MediaType .APPLICATION_XML .toString (),
225+ FileFixture .getPlainTextFileContent ().getContentBytes ());
226+
227+ // -- EXECUTE
228+ String response =
229+ mvc .perform (
230+ multipart (DOCUMENT_API + "/upsert" )
231+ .part (inputPart )
232+ .file (filePart )
233+ .accept (MediaType .APPLICATION_JSON ))
234+ .andExpect (status ().isOk ())
235+ .andReturn ()
236+ .getResponse ()
237+ .getContentAsString ();
238+
239+ // -- VERIFY
240+ assertNotNull (response );
241+ assertEquals (document .getName (), JsonPath .read (response , "$.document_name" ));
242+ assertEquals ("My test document" , JsonPath .read (response , "$.document_description" ));
243+ assertEquals (scenario .getId (), JsonPath .read (response , "$.document_scenarios[0]" ));
244+ assertEquals (exercise .getId (), JsonPath .read (response , "$.document_exercises[0]" ));
245+ }
246+
247+ @ Test
248+ @ DisplayName ("Should update a document by target id when uploading a valid file and input" )
249+ void uploadDocumentShouldUpdateDocumentByTargetId () throws Exception {
250+ // -- PREPARE
251+ Scenario scenario =
252+ scenarioComposer .forScenario (ScenarioFixture .getScenario ()).persist ().get ();
253+ Exercise exercise =
254+ exerciseComposer .forExercise (ExerciseFixture .createDefaultExercise ()).persist ().get ();
255+
256+ Document document =
257+ documentComposer
258+ .forDocument (DocumentFixture .getDocument (FileFixture .getPlainTextFileContent ()))
259+ .persist ()
260+ .get ();
261+ document .setExercises (new HashSet <>(Set .of (exercise )));
262+ document .setScenarios (new HashSet <>(Set .of (scenario )));
263+
264+ String extension = FilenameUtils .getExtension (document .getName ());
265+ String fileTarget =
266+ DigestUtils .md5Hex (
267+ new ByteArrayInputStream (FileFixture .getPlainTextFileContent ().getContentBytes ()))
268+ + "."
269+ + extension ;
270+ document .setDescription ("My test document" );
271+ document .setTarget (fileTarget );
272+ documentRepository .save (document );
273+
274+ DocumentCreateInput input = new DocumentCreateInput ();
275+ input .setDescription ("Should not have this description" );
276+ input .setScenarioIds (List .of (scenario .getId ()));
277+ input .setExerciseIds (List .of (exercise .getId ()));
278+
279+ MockPart inputPart = new MockPart ("input" , mapper .writeValueAsBytes (input ));
280+ inputPart .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
281+
282+ MockMultipartFile filePart =
283+ new MockMultipartFile (
284+ "file" ,
285+ document .getName (),
286+ MediaType .APPLICATION_XML .toString (),
287+ FileFixture .getPlainTextFileContent ().getContentBytes ());
288+
289+ // -- EXECUTE
290+ String response =
291+ mvc .perform (
292+ multipart (DOCUMENT_API + "/upsert" )
293+ .part (inputPart )
294+ .file (filePart )
295+ .accept (MediaType .APPLICATION_JSON ))
296+ .andExpect (status ().isOk ())
297+ .andReturn ()
298+ .getResponse ()
299+ .getContentAsString ();
300+
301+ // -- VERIFY
302+ assertNotNull (response );
303+ assertEquals (document .getName (), JsonPath .read (response , "$.document_name" ));
304+ assertEquals ("My test document" , JsonPath .read (response , "$.document_description" ));
305+ assertEquals (scenario .getId (), JsonPath .read (response , "$.document_scenarios[0]" ));
306+ assertEquals (exercise .getId (), JsonPath .read (response , "$.document_exercises[0]" ));
307+ }
240308 }
241309
242310 @ Test
0 commit comments