@@ -28,12 +28,14 @@ import io.mockk.every
28
28
import io.mockk.junit5.MockKExtension
29
29
import io.mockk.mockk
30
30
import io.mockk.mockkStatic
31
+ import java.io.FileInputStream
31
32
import java.time.Instant
32
33
import java.util.*
33
34
import kotlin.test.assertEquals
34
35
import kotlin.test.assertFalse
35
36
import kotlin.test.assertNotNull
36
37
import kotlin.test.assertTrue
38
+ import org.apache.commons.io.IOUtils
37
39
import org.junit.jupiter.api.BeforeEach
38
40
import org.junit.jupiter.api.DynamicTest
39
41
import org.junit.jupiter.api.Test
@@ -44,10 +46,13 @@ import org.junit.runner.RunWith
44
46
import org.slf4j.LoggerFactory
45
47
import org.springframework.beans.factory.annotation.Autowired
46
48
import org.springframework.boot.test.context.SpringBootTest
49
+ import org.springframework.core.io.ResourceLoader
50
+ import org.springframework.mock.web.MockMultipartFile
47
51
import org.springframework.test.context.ActiveProfiles
48
52
import org.springframework.test.context.junit.jupiter.SpringExtension
49
53
import org.springframework.test.context.junit4.SpringRunner
50
54
import org.springframework.test.util.ReflectionTestUtils
55
+ import software.amazon.awssdk.services.s3.model.NoSuchKeyException
51
56
52
57
const val CONNECTED_ADMIN_USER = " [email protected] "
53
58
const val CONNECTED_READER_USER = " [email protected] "
@@ -62,10 +67,13 @@ class SolutionServiceIntegrationTest : CsmTestBase() {
62
67
63
68
private val logger = LoggerFactory .getLogger(SolutionServiceIntegrationTest ::class .java)
64
69
70
+ val fileName = " test_solution_file.txt"
71
+
65
72
@Autowired lateinit var rediSearchIndexer: RediSearchIndexer
66
73
@Autowired lateinit var organizationApiService: OrganizationApiServiceInterface
67
74
@Autowired lateinit var solutionApiService: SolutionApiServiceInterface
68
75
@Autowired lateinit var csmPlatformProperties: CsmPlatformProperties
76
+ @Autowired lateinit var resourceLoader: ResourceLoader
69
77
70
78
private var containerRegistryService: ContainerRegistryService = mockk(relaxed = true )
71
79
private var startTime: Long = 0
@@ -2122,6 +2130,107 @@ class SolutionServiceIntegrationTest : CsmTestBase() {
2122
2130
assertEquals(" One or several solution items have same id : runTemplates" , exception.message)
2123
2131
}
2124
2132
2133
+ @Test
2134
+ fun `test createSolutionFile` () {
2135
+
2136
+ val resourceTestFile = resourceLoader.getResource(" classpath:/$fileName " ).file
2137
+ val input = FileInputStream (resourceTestFile)
2138
+ val multipartFile =
2139
+ MockMultipartFile (
2140
+ " file" , resourceTestFile.getName(), " text/plain" , IOUtils .toByteArray(input))
2141
+ every { getCurrentAuthenticatedRoles(any()) } returns listOf (" Platform.Admin" )
2142
+
2143
+ logger.info(" should create a solution file" )
2144
+ val savedFile =
2145
+ solutionApiService.createSolutionFile(
2146
+ organizationSaved.id, solutionSaved.id, multipartFile, true , null )
2147
+
2148
+ assertEquals(fileName, savedFile.fileName)
2149
+ }
2150
+
2151
+ @Test
2152
+ fun `test getSolutionFile` () {
2153
+ logger.info(" should get a solution file" )
2154
+ val resourceTestFile = resourceLoader.getResource(" classpath:/$fileName " ).file
2155
+ val input = FileInputStream (resourceTestFile)
2156
+ val expectedFile = FileInputStream (resourceTestFile)
2157
+ val multipartFile =
2158
+ MockMultipartFile (
2159
+ " file" , resourceTestFile.getName(), " text/plain" , IOUtils .toByteArray(input))
2160
+
2161
+ solutionApiService.createSolutionFile(
2162
+ organizationSaved.id, solutionSaved.id, multipartFile, true , null )
2163
+
2164
+ val fetchedFile =
2165
+ solutionApiService.getSolutionFile(organizationSaved.id, solutionSaved.id, fileName)
2166
+ val expectedText = expectedFile.bufferedReader().use { it.readText() }
2167
+ val retrievedText = fetchedFile.inputStream.bufferedReader().use { it.readText() }
2168
+ assertEquals(expectedText, retrievedText)
2169
+ }
2170
+
2171
+ @Test
2172
+ fun `test listSolutionFiles` () {
2173
+ every { getCurrentAuthenticatedRoles(any()) } returns listOf (" Platform.Admin" )
2174
+
2175
+ logger.info(" should list all solution file" )
2176
+ val resourceTestFile = resourceLoader.getResource(" classpath:/$fileName " ).file
2177
+ val input = FileInputStream (resourceTestFile)
2178
+ val multipartFile =
2179
+ MockMultipartFile (
2180
+ " file" , resourceTestFile.getName(), " text/plain" , IOUtils .toByteArray(input))
2181
+
2182
+ var solutionFiles = solutionApiService.listSolutionFiles(organizationSaved.id, solutionSaved.id)
2183
+ assertTrue(solutionFiles.isEmpty())
2184
+
2185
+ solutionApiService.createSolutionFile(
2186
+ organizationSaved.id, solutionSaved.id, multipartFile, true , null )
2187
+
2188
+ solutionFiles = solutionApiService.listSolutionFiles(organizationSaved.id, solutionSaved.id)
2189
+ assertEquals(1 , solutionFiles.size)
2190
+ }
2191
+
2192
+ @Test
2193
+ fun `test deleteSolutionFile` () {
2194
+ logger.info(" should delete a solution file" )
2195
+ val resourceTestFile = resourceLoader.getResource(" classpath:/$fileName " ).file
2196
+ val input = FileInputStream (resourceTestFile)
2197
+ val multipartFile =
2198
+ MockMultipartFile (
2199
+ " file" , resourceTestFile.getName(), " text/plain" , IOUtils .toByteArray(input))
2200
+
2201
+ solutionApiService.createSolutionFile(
2202
+ organizationSaved.id, solutionSaved.id, multipartFile, true , null )
2203
+
2204
+ solutionApiService.deleteSolutionFile(organizationSaved.id, solutionSaved.id, fileName)
2205
+
2206
+ val exception =
2207
+ assertThrows<NoSuchKeyException > {
2208
+ solutionApiService.getSolutionFile(organizationSaved.id, solutionSaved.id, fileName)
2209
+ }
2210
+
2211
+ assertEquals(" The specified key does not exist." , exception.awsErrorDetails().errorMessage())
2212
+ }
2213
+
2214
+ @Test
2215
+ fun `test deleteSolutionFiles` () {
2216
+ logger.info(" should delete all solution files" )
2217
+ val resourceTestFile = resourceLoader.getResource(" classpath:/$fileName " ).file
2218
+ val input = FileInputStream (resourceTestFile)
2219
+ val multipartFile =
2220
+ MockMultipartFile (
2221
+ " file" , resourceTestFile.getName(), " text/plain" , IOUtils .toByteArray(input))
2222
+
2223
+ solutionApiService.createSolutionFile(
2224
+ organizationSaved.id, solutionSaved.id, multipartFile, true , null )
2225
+ solutionApiService.createSolutionFile(
2226
+ organizationSaved.id, solutionSaved.id, multipartFile, true , null )
2227
+
2228
+ solutionApiService.deleteAllS3SolutionObjects(organizationSaved.id, solutionSaved)
2229
+
2230
+ assertEquals(
2231
+ 0 , solutionApiService.listSolutionFiles(organizationSaved.id, solutionSaved.id).size)
2232
+ }
2233
+
2125
2234
@Test
2126
2235
fun `assert timestamps are functional for base CRUD` () {
2127
2236
solutionSaved = solutionApiService.createSolution(organizationSaved.id, makeSolution())
0 commit comments