From 434ce5410bf24be96a72838e9e49758215c98ccd Mon Sep 17 00:00:00 2001 From: Bastian Doetsch Date: Fri, 16 Jun 2023 08:44:47 +0200 Subject: [PATCH] chore: integrate all (dependent) test steps into one big smoke test --- .github/detekt/detekt-baseline.xml | 3 + .../kotlin/snyk/code/DeepCodeRestApiTest.kt | 305 +++++++++--------- 2 files changed, 156 insertions(+), 152 deletions(-) diff --git a/.github/detekt/detekt-baseline.xml b/.github/detekt/detekt-baseline.xml index ede6b35c1..e99b0cd1e 100644 --- a/.github/detekt/detekt-baseline.xml +++ b/.github/detekt/detekt-baseline.xml @@ -18,6 +18,9 @@ MatchingDeclarationName:DeepCodeRestApiTest.kt$DeepCodeRestApiImplTest TooGenericExceptionCaught:SnykSettingsDialog.kt$SnykSettingsDialog$e: Exception TooGenericExceptionCaught:SnykToolWindowPanel.kt$SnykToolWindowPanel$e: Exception + LongMethod:DeepCodeRestApiTest.kt$DeepCodeRestApiImplTest$@Test fun snykCodeAnalysis() + LongMethod:DeepCodeRestApiTest.kt$DeepCodeRestApiImplTest$private fun checkBundle() + LongMethod:DeepCodeRestApiTest.kt$DeepCodeRestApiImplTest$private fun checkBundleAndAssert() ChainWrapping:io.snyk.plugin.services.SnykCliDownloaderService.kt:115 diff --git a/src/test/kotlin/snyk/code/DeepCodeRestApiTest.kt b/src/test/kotlin/snyk/code/DeepCodeRestApiTest.kt index e1c68f4b4..ab7e55fb9 100644 --- a/src/test/kotlin/snyk/code/DeepCodeRestApiTest.kt +++ b/src/test/kotlin/snyk/code/DeepCodeRestApiTest.kt @@ -24,9 +24,7 @@ import io.snyk.plugin.snykcode.newCodeRestApi import org.junit.After import org.junit.Assert import org.junit.Before -import org.junit.FixMethodOrder import org.junit.Test -import org.junit.runners.MethodSorters import snyk.common.toSnykCodeApiUrl import snyk.pluginInfo import java.io.File @@ -43,7 +41,6 @@ import java.util.Objects /* * This Java source file was generated by the Gradle 'init' task. */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) class DeepCodeRestApiImplTest { @Before @@ -64,38 +61,6 @@ class DeepCodeRestApiImplTest { unmockkAll() } - @Test - fun _025_getFilters() { - println("\n--------------Get Filters----------------\n") - val response: GetFiltersResponse = restApiClient!!.getFilters() - Assert.assertNotNull(response) - val errorMsg = ("Get Filters return status code: [" - + response.statusCode - + "] " - + response.statusDescription - + "\n") - Assert.assertEquals(errorMsg, 200, response.statusCode.toLong()) - System.out.printf( - "Get Filters call returns next filters: \nextensions: %1\$s \nconfigFiles: %2\$s\n", - response.extensions, response.configFiles - ) - } - - @Test - fun _030_createBundle_from_source() { - println("\n--------------Create Bundle from Source----------------\n") - - val response = createBundleFromSource(loggedOrgName) - - Assert.assertNotNull(response) - System.out.printf( - "Create Bundle call return:\nStatus code [%1\$d] %3\$s \nBundleId: [%2\$s]\n", - response.statusCode, response.bundleHash, response.statusDescription - ) - Assert.assertEquals(200, response.statusCode.toLong()) - bundleId = response.bundleHash - } - @Throws(NoSuchAlgorithmException::class, IOException::class) private fun createBundleFromSource(orgName: String?): CreateBundleResponse { val testFile = @@ -114,31 +79,41 @@ class DeepCodeRestApiImplTest { ) } - @Test - @Throws(NoSuchAlgorithmException::class, IOException::class) - fun _031_createBundle_wrong_request() { - println("\n--------------Create Bundle with wrong requests----------------\n") - val brokenToken = "fff" - val brokenOrgName = "org-name" - val response = createBundleFromSource(brokenOrgName) - Assert.assertNotNull(response) - Assert.assertEquals( - "Create Bundle call with malformed token should not be accepted by server", - 401, - response.statusCode.toLong() - ) - System.out.printf( - "Create Bundle call with malformed token [%1\$s] " + - "and org name [%2\$s] is not accepted by server with Status code [%3\$d].\n", - brokenToken, brokenOrgName, response.statusCode - ) - } + private fun createFileHashRequest(fakeFileName: String?): FileHashRequest { + val testFile = + File(javaClass.classLoader.getResource(TEST_FILE)!!.file) + val absolutePath = testFile.absolutePath + val deepCodedPath = ((if (absolutePath.startsWith("/")) "" else "/") + + if (fakeFileName == null) absolutePath else absolutePath.replace( + TEST_FILE, + fakeFileName + )) + System.out.printf("\nFile: %1\$s\n", deepCodedPath) + println("-----------------") - @Test - fun _035_createBundle_with_hash() { - println("\n--------------Create Bundle with Hash----------------\n") - val response = createBundleWithHash() - Assert.assertEquals(200, response.statusCode.toLong()) + // Append with System.currentTimeMillis() to make new Hash. + try { + FileOutputStream(absolutePath, true).use { fos -> + fos.write( + System.currentTimeMillis().toString().toByteArray() + ) + } + } catch (e: IOException) { + println(e.message) + } + val fileText: String + return try { + fileText = String(Files.readAllBytes(Paths.get(absolutePath))) + val hash = getHash(fileText) + System.out.printf("File hash: %1\$s\n", hash) + val fileHashRequest = FileHashRequest() + fileHashRequest[deepCodedPath] = hash + fileHashRequest + } catch (e: IOException) { + throw RuntimeException(e) + } catch (e: NoSuchAlgorithmException) { + throw RuntimeException(e) + } } private fun createBundleWithHash(): CreateBundleResponse { @@ -159,29 +134,46 @@ class DeepCodeRestApiImplTest { return response } - @Test - fun _036_Check_Bundle() { + private fun filtersAndAssert() { + println("\n--------------Get Filters----------------\n") + val filtersResponse: GetFiltersResponse = restApiClient!!.filters + Assert.assertNotNull(filtersResponse) + val errorMsg = ("Get Filters return status code: [" + + filtersResponse.statusCode + + "] " + + filtersResponse.statusDescription + + "\n") + Assert.assertEquals(errorMsg, 200, filtersResponse.statusCode.toLong()) + System.out.printf( + "Get Filters call returns next filters: \nextensions: %1\$s \nconfigFiles: %2\$s\n", + filtersResponse.extensions, filtersResponse.configFiles + ) + } + + private fun checkBundleAndAssert() { println("\n--------------Check Bundle----------------\n") val fileHashRequest = createFileHashRequest(null) - val createBundleResponse: CreateBundleResponse = + + val checkBundleCreateBundleResponse: CreateBundleResponse = restApiClient!!.createBundle( loggedOrgName, fileHashRequest ) - Assert.assertNotNull(createBundleResponse) + Assert.assertNotNull(checkBundleCreateBundleResponse) System.out.printf( "\nCreate Bundle call return:\nStatus code [%1\$d] %3\$s \n bundleId: %2\$s\n missingFiles: %4\$s\n", - createBundleResponse.statusCode, - createBundleResponse.bundleHash, - createBundleResponse.statusDescription, - createBundleResponse.missingFiles + checkBundleCreateBundleResponse.statusCode, + checkBundleCreateBundleResponse.bundleHash, + checkBundleCreateBundleResponse.statusDescription, + checkBundleCreateBundleResponse.missingFiles ) - Assert.assertEquals(200, createBundleResponse.statusCode.toLong()) - Assert.assertFalse("List of missingFiles is empty.", createBundleResponse.missingFiles.isEmpty()) + Assert.assertEquals(200, checkBundleCreateBundleResponse.statusCode.toLong()) + Assert.assertFalse("List of missingFiles is empty.", checkBundleCreateBundleResponse.missingFiles.isEmpty()) + val checkBundleResponse: CreateBundleResponse = restApiClient!!.checkBundle( loggedOrgName, - createBundleResponse.bundleHash + checkBundleCreateBundleResponse.bundleHash ) Assert.assertNotNull(checkBundleResponse) System.out.printf( @@ -195,77 +187,78 @@ class DeepCodeRestApiImplTest { Assert.assertFalse("List of missingFiles is empty.", checkBundleResponse.missingFiles.isEmpty()) Assert.assertEquals( "Checked and returned bundleId's are different.", - createBundleResponse.bundleHash, + checkBundleCreateBundleResponse.bundleHash, checkBundleResponse.bundleHash ) - val uploadFileResponse = doUploadFile(createBundleResponse, fileHashRequest) + + val uploadFileResponse = doUploadFile(checkBundleCreateBundleResponse, fileHashRequest) Assert.assertNotNull(uploadFileResponse) System.out.printf( "\nUpload Files call for file %3\$s \nStatus code [%1\$d] %2\$s\n", uploadFileResponse.statusCode, uploadFileResponse.statusDescription, - createBundleResponse.missingFiles[0] + checkBundleCreateBundleResponse.missingFiles[0] ) Assert.assertEquals(200, uploadFileResponse.statusCode.toLong()) - val createBundleResponse1: CreateBundleResponse = + val checkBundleResponseNew: CreateBundleResponse = restApiClient!!.checkBundle( loggedOrgName, - createBundleResponse.bundleHash + checkBundleCreateBundleResponse.bundleHash ) - Assert.assertNotNull(createBundleResponse1) + Assert.assertNotNull(checkBundleResponseNew) System.out.printf( "\nCheck Bundle call return:\nStatus code [%1\$d] %3\$s \n bundleId: %2\$s\n missingFiles: %4\$s\n", - createBundleResponse1.statusCode, - createBundleResponse1.bundleHash, - createBundleResponse1.statusDescription, - createBundleResponse1.missingFiles + checkBundleResponseNew.statusCode, + checkBundleResponseNew.bundleHash, + checkBundleResponseNew.statusDescription, + checkBundleResponseNew.missingFiles ) - Assert.assertEquals(200, createBundleResponse1.statusCode.toLong()) + Assert.assertEquals(200, checkBundleResponseNew.statusCode.toLong()) Assert.assertTrue( - "List of missingFiles is NOT empty.", createBundleResponse1.missingFiles.isEmpty() + "List of missingFiles is NOT empty.", checkBundleResponseNew.missingFiles.isEmpty() ) Assert.assertEquals( "Checked and returned bundleId's are different.", - createBundleResponse.bundleHash, - checkBundleResponse.bundleHash + checkBundleCreateBundleResponse.bundleHash, + checkBundleResponseNew.bundleHash ) } - private fun createFileHashRequest(fakeFileName: String?): FileHashRequest { - val testFile = - File(javaClass.classLoader.getResource(TEST_FILE)!!.file) - val absolutePath = testFile.absolutePath - val deepCodedPath = ((if (absolutePath.startsWith("/")) "" else "/") - + if (fakeFileName == null) absolutePath else absolutePath.replace( - TEST_FILE, - fakeFileName - )) - System.out.printf("\nFile: %1\$s\n", deepCodedPath) - println("-----------------") + private fun createBundleWithHashAndAssert() { + println("\n--------------Create Bundle with Hash----------------\n") + val createBundleWithHash = createBundleWithHash() + Assert.assertEquals(200, createBundleWithHash.statusCode.toLong()) + } - // Append with System.currentTimeMillis() to make new Hash. - try { - FileOutputStream(absolutePath, true).use { fos -> - fos.write( - System.currentTimeMillis().toString().toByteArray() - ) - } - } catch (e: IOException) { - println(e.message) - } - val fileText: String - return try { - fileText = String(Files.readAllBytes(Paths.get(absolutePath))) - val hash = getHash(fileText) - System.out.printf("File hash: %1\$s\n", hash) - val fileHashRequest = FileHashRequest() - fileHashRequest[deepCodedPath] = hash - fileHashRequest - } catch (e: IOException) { - throw RuntimeException(e) - } catch (e: NoSuchAlgorithmException) { - throw RuntimeException(e) - } + private fun createBundleWithWrongRequestsAndAssert() { + println("\n--------------Create Bundle with wrong requests----------------\n") + val brokenToken = "fff" + val brokenOrgName = "org-name" + val createBundleFromSource = createBundleFromSource(brokenOrgName) + Assert.assertNotNull(createBundleFromSource) + Assert.assertEquals( + "Create Bundle call with malformed token should not be accepted by server", + 401, + createBundleFromSource.statusCode.toLong() + ) + System.out.printf( + "Create Bundle call with malformed token [%1\$s] " + + "and org name [%2\$s] is not accepted by server with Status code [%3\$d].\n", + brokenToken, brokenOrgName, createBundleFromSource.statusCode + ) + } + + private fun createBundleAndAssert() { + println("\n--------------Create Bundle from Source----------------\n") + val createBundleResponse = createBundleFromSource(loggedOrgName) + + Assert.assertNotNull(createBundleResponse) + System.out.printf( + "Create Bundle call return:\nStatus code [%1\$d] %3\$s \nBundleId: [%2\$s]\n", + createBundleResponse.statusCode, createBundleResponse.bundleHash, createBundleResponse.statusDescription + ) + Assert.assertEquals(200, createBundleResponse.statusCode.toLong()) + bundleId = createBundleResponse.bundleHash } @Throws(NoSuchAlgorithmException::class) @@ -277,8 +270,7 @@ class DeepCodeRestApiImplTest { return bytesToHex(encodedhash) } - @Test - fun _037_ExtendBundle() { + private fun extendBundleAndAssert() { println("\n--------------Extend Bundle----------------\n") val createBundleResponse = createBundleWithHash() Assert.assertEquals(200, createBundleResponse.statusCode.toLong()) @@ -303,8 +295,34 @@ class DeepCodeRestApiImplTest { Assert.assertFalse("List of missingFiles is empty.", extendBundleResponse.missingFiles.isEmpty()) } - @Test - fun _040_UploadFiles() { + private fun doUploadFile( + createBundleResponse: CreateBundleResponse, fileHashRequest: FileHashRequest + ): EmptyResponse { + val testFile = File( + Objects.requireNonNull(javaClass.classLoader.getResource(TEST_FILE)) + .file + ) + val absolutePath = testFile.absolutePath + val fileText: String + fileText = try { + // ?? com.intellij.openapi.util.io.FileUtil#loadFile(java.io.File, java.nio.charset.Charset) + String(Files.readAllBytes(Paths.get(absolutePath))) + } catch (e: IOException) { + throw RuntimeException(e) + } + val filePath = createBundleResponse.missingFiles[0] + val fileHash = fileHashRequest[filePath] + val map = FileContentRequest() + map[filePath] = FileHash2ContentRequest(fileHash, fileText) + val ebr = ExtendBundleWithContentRequest(map, emptyList()) + return restApiClient!!.extendBundle( + loggedOrgName, + createBundleResponse.bundleHash, + ebr + ) + } + + private fun uploadFilesAndAssert() { println("\n--------------Upload Files by Hash----------------\n") val fileHashRequest = createFileHashRequest(null) val createBundleResponse: CreateBundleResponse = @@ -333,36 +351,7 @@ class DeepCodeRestApiImplTest { Assert.assertEquals(200, response.statusCode.toLong()) } - private fun doUploadFile( - createBundleResponse: CreateBundleResponse, fileHashRequest: FileHashRequest - ): EmptyResponse { - val testFile = File( - Objects.requireNonNull(javaClass.classLoader.getResource(TEST_FILE)) - .file - ) - val absolutePath = testFile.absolutePath - val fileText: String - fileText = try { - // ?? com.intellij.openapi.util.io.FileUtil#loadFile(java.io.File, java.nio.charset.Charset) - String(Files.readAllBytes(Paths.get(absolutePath))) - } catch (e: IOException) { - throw RuntimeException(e) - } - val filePath = createBundleResponse.missingFiles[0] - val fileHash = fileHashRequest[filePath] - val map = FileContentRequest() - map[filePath] = FileHash2ContentRequest(fileHash, fileText) - val ebr = ExtendBundleWithContentRequest(map, emptyList()) - return restApiClient!!.extendBundle( - loggedOrgName, - createBundleResponse.bundleHash, - ebr - ) - } - - @Test - @Throws(InterruptedException::class) - fun _090_getAnalysis() { + private fun getAnalysisAndAssert() { println("\n--------------Get Analysis----------------\n") Assert.assertNotNull( "`bundleId` should be initialized at `_030_createBundle_from_source()`", @@ -411,6 +400,18 @@ class DeepCodeRestApiImplTest { Assert.assertEquals("Get Analysis request not succeed", 200, response.statusCode.toLong()) } + @Test + fun snykCodeAnalysis_smoke_test() { + filtersAndAssert() + createBundleAndAssert() + createBundleWithWrongRequestsAndAssert() + createBundleWithHashAndAssert() + checkBundleAndAssert() + extendBundleAndAssert() + uploadFilesAndAssert() + getAnalysisAndAssert() + } + companion object { const val TEST_FILE = "test-fixtures/code-test.js"