@@ -25,12 +25,14 @@ import com.cosmotech.solution.api.SolutionApiService
25
25
import com.cosmotech.solution.domain.*
26
26
import io.mockk.every
27
27
import io.mockk.mockk
28
+ import org.apache.commons.io.IOUtils
28
29
import org.json.JSONObject
29
30
import org.junit.jupiter.api.BeforeEach
30
31
import org.junit.jupiter.api.Test
31
32
import org.springframework.beans.factory.annotation.Autowired
32
33
import org.springframework.boot.test.context.SpringBootTest
33
34
import org.springframework.http.MediaType
35
+ import org.springframework.mock.web.MockMultipartFile
34
36
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document
35
37
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
36
38
import org.springframework.test.context.ActiveProfiles
@@ -1411,4 +1413,135 @@ class SolutionControllerTests : ControllerTestBase() {
1411
1413
.andDo(
1412
1414
document(" organizations/{organization_id}/solutions/{solution_id}/security/users/GET" ))
1413
1415
}
1416
+
1417
+ @Test
1418
+ @WithMockOauth2User
1419
+ fun get_solution_files () {
1420
+
1421
+ val solutionId =
1422
+ createSolutionAndReturnId(mvc, organizationId, constructSolutionCreateRequest())
1423
+
1424
+ mvc.perform(
1425
+ get(" /organizations/$organizationId /solutions/$solutionId /files" )
1426
+ .contentType(MediaType .APPLICATION_JSON ))
1427
+ .andExpect(status().is2xxSuccessful)
1428
+ .andDo(MockMvcResultHandlers .print ())
1429
+ .andDo(document(" organizations/{organization_id}/solutions/{solution_id}/files/GET" ))
1430
+ }
1431
+
1432
+ @Test
1433
+ @WithMockOauth2User
1434
+ fun create_solution_files () {
1435
+ val solutionId =
1436
+ createSolutionAndReturnId(mvc, organizationId, constructSolutionCreateRequest())
1437
+ val fileName = " test.txt"
1438
+ val fileToUpload =
1439
+ this ::class .java.getResourceAsStream(" /solution/$fileName " )
1440
+ ? : throw IllegalStateException (
1441
+ " $fileName file used for organizations/{organization_id}/solutions/{solution_id}/files/POST endpoint documentation cannot be null" )
1442
+
1443
+ val mockFile =
1444
+ MockMultipartFile (
1445
+ " file" , fileName, MediaType .TEXT_PLAIN_VALUE , IOUtils .toByteArray(fileToUpload))
1446
+
1447
+ mvc.perform(
1448
+ multipart(" /organizations/$organizationId /solutions/$solutionId /files" )
1449
+ .file(mockFile)
1450
+ .param(" overwrite" , " true" )
1451
+ .param(" destination" , " path/to/a/directory/" )
1452
+ .accept(MediaType .APPLICATION_JSON )
1453
+ .with (csrf()))
1454
+ .andExpect(status().is2xxSuccessful)
1455
+ .andExpect(jsonPath(" $.fileName" ).value(" path/to/a/directory/$fileName " ))
1456
+ .andDo(MockMvcResultHandlers .print ())
1457
+ .andDo(document(" organizations/{organization_id}/solutions/{solution_id}/files/POST" ))
1458
+ }
1459
+
1460
+ @Test
1461
+ @WithMockOauth2User
1462
+ fun delete_solution_files () {
1463
+
1464
+ val solutionId =
1465
+ createSolutionAndReturnId(mvc, organizationId, constructSolutionCreateRequest())
1466
+
1467
+ mvc.perform(
1468
+ delete(" /organizations/$organizationId /solutions/$solutionId /files" )
1469
+ .accept(MediaType .APPLICATION_JSON )
1470
+ .with (csrf()))
1471
+ .andExpect(status().is2xxSuccessful)
1472
+ .andDo(MockMvcResultHandlers .print ())
1473
+ .andDo(document(" organizations/{organization_id}/solutions/{solution_id}/files/DELETE" ))
1474
+ }
1475
+
1476
+ @Test
1477
+ @WithMockOauth2User
1478
+ fun delete_solution_file () {
1479
+ val solutionId =
1480
+ createSolutionAndReturnId(mvc, organizationId, constructSolutionCreateRequest())
1481
+
1482
+ val fileName = " test.txt"
1483
+ val fileToUpload =
1484
+ this ::class .java.getResourceAsStream(" /solution/$fileName " )
1485
+ ? : throw IllegalStateException (
1486
+ " $fileName file used for organizations/{organization_id}/solutions/{solution_id}/files/POST endpoint documentation cannot be null" )
1487
+
1488
+ val mockFile =
1489
+ MockMultipartFile (
1490
+ " file" , fileName, MediaType .TEXT_PLAIN_VALUE , IOUtils .toByteArray(fileToUpload))
1491
+
1492
+ val destination = " path/to/a/directory/"
1493
+ mvc.perform(
1494
+ multipart(" /organizations/$organizationId /solutions/$solutionId /files" )
1495
+ .file(mockFile)
1496
+ .param(" overwrite" , " true" )
1497
+ .param(" destination" , destination)
1498
+ .accept(MediaType .APPLICATION_JSON )
1499
+ .with (csrf()))
1500
+
1501
+ mvc.perform(
1502
+ delete(" /organizations/$organizationId /solutions/$solutionId /files/delete" )
1503
+ .param(" file_name" , destination + fileName)
1504
+ .accept(MediaType .APPLICATION_JSON )
1505
+ .with (csrf()))
1506
+ .andExpect(status().is2xxSuccessful)
1507
+ .andDo(MockMvcResultHandlers .print ())
1508
+ .andDo(
1509
+ document(" organizations/{organization_id}/solutions/{solution_id}/files/delete/DELETE" ))
1510
+ }
1511
+
1512
+ @Test
1513
+ @WithMockOauth2User
1514
+ fun download_solution_file () {
1515
+
1516
+ val solutionId =
1517
+ createSolutionAndReturnId(mvc, organizationId, constructSolutionCreateRequest())
1518
+
1519
+ val fileName = " test.txt"
1520
+ val fileToUpload =
1521
+ this ::class .java.getResourceAsStream(" /solution/$fileName " )
1522
+ ? : throw IllegalStateException (
1523
+ " $fileName file used for organizations/{organization_id}/solutions/{solution_id}/files/POST endpoint documentation cannot be null" )
1524
+
1525
+ val mockFile =
1526
+ MockMultipartFile (
1527
+ " file" , fileName, MediaType .TEXT_PLAIN_VALUE , IOUtils .toByteArray(fileToUpload))
1528
+
1529
+ val destination = " path/to/a/directory/"
1530
+ mvc.perform(
1531
+ multipart(" /organizations/$organizationId /solutions/$solutionId /files" )
1532
+ .file(mockFile)
1533
+ .param(" overwrite" , " true" )
1534
+ .param(" destination" , destination)
1535
+ .accept(MediaType .APPLICATION_JSON )
1536
+ .with (csrf()))
1537
+
1538
+ mvc.perform(
1539
+ get(" /organizations/$organizationId /solutions/$solutionId /files/download" )
1540
+ .param(" file_name" , destination + fileName)
1541
+ .accept(MediaType .APPLICATION_OCTET_STREAM ))
1542
+ .andExpect(status().is2xxSuccessful)
1543
+ .andDo(MockMvcResultHandlers .print ())
1544
+ .andDo(
1545
+ document(" organizations/{organization_id}/solutions/{solution_id}/files/download/GET" ))
1546
+ }
1414
1547
}
0 commit comments