Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion intellij-extension/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
implementation("com.google.code.gson:gson:2.10.1")
implementation(platform("software.amazon.awssdk:bom:2.20.0"))
implementation("software.amazon.awssdk:cloudformation")
testImplementation("junit:junit:4.13.2")
testImplementation("org.testng:testng:7.4.0")
testImplementation("org.mockito:mockito-core:5.3.1")
testImplementation("org.mockito.kotlin:mockito-kotlin:5.1.0")
}
Expand All @@ -38,4 +38,8 @@ tasks {
sinceBuild.set("231")
untilBuild.set("243.*")
}

test {
useTestNG()
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
package com.amazon.guardrail

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.mockito.Mockito.mock
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever
import org.testng.Assert.assertEquals
import org.testng.Assert.assertNull
import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.cloudformation.CloudFormationClient
import software.amazon.awssdk.services.cloudformation.model.DescribeTypeRequest
import software.amazon.awssdk.services.cloudformation.model.DescribeTypeResponse
import java.io.File
import java.nio.file.Files

class CloudFormationSchemaFetcherTest {

@get:Rule
val tempFolder = TemporaryFolder()

private lateinit var tempDir: File
private lateinit var mockClient: CloudFormationClient
private lateinit var fetcher: CloudFormationSchemaFetcher

@Before
@BeforeMethod
fun setup() {
tempDir = Files.createTempDirectory("test").toFile()
mockClient = mock(CloudFormationClient::class.java)
fetcher = CloudFormationSchemaFetcher { mockClient }
}

@AfterMethod
fun teardown() {
tempDir.deleteRecursively()
}

@Test
fun `extractTypeName returns null when typeName field is missing`() {
val file = tempFolder.newFile("no-typename.json")
val file = File(tempDir, "no-typename.json")
file.writeText("""{"properties": {}}""")

val result = fetcher.extractTypeName(file.absolutePath)
Expand All @@ -39,25 +44,25 @@ class CloudFormationSchemaFetcherTest {

@Test
fun `extractTypeName extracts typeName from valid schema`() {
val file = tempFolder.newFile("valid.json")
val file = File(tempDir, "valid.json")
file.writeText("""{"typeName": "AWS::S3::Bucket"}""")

val result = fetcher.extractTypeName(file.absolutePath)
assertEquals("AWS::S3::Bucket", result)
assertEquals(result, "AWS::S3::Bucket")
}

@Test
fun `extractTypeName handles complex typeName`() {
val file = tempFolder.newFile("complex.json")
val file = File(tempDir, "complex.json")
file.writeText("""{"typeName": "AWS::EC2::Instance", "properties": {"foo": "bar"}}""")

val result = fetcher.extractTypeName(file.absolutePath)
assertEquals("AWS::EC2::Instance", result)
assertEquals(result, "AWS::EC2::Instance")
}

@Test
fun `fetchSchema returns schema from CloudFormation API`() {
val file = tempFolder.newFile("schema.json")
val file = File(tempDir, "schema.json")
file.writeText("""{"typeName": "AWS::S3::Bucket"}""")

val mockResponse = mock(DescribeTypeResponse::class.java)
Expand All @@ -66,12 +71,12 @@ class CloudFormationSchemaFetcherTest {

val result = fetcher.fetchSchema(file.absolutePath)

assertEquals("""{"type": "object"}""", result)
assertEquals(result, """{"type": "object"}""")
}

@Test
fun `fetchSchema returns null when CloudFormation returns null schema`() {
val file = tempFolder.newFile("schema.json")
val file = File(tempDir, "schema.json")
file.writeText("""{"typeName": "AWS::Custom::Resource"}""")

val mockResponse = mock(DescribeTypeResponse::class.java)
Expand All @@ -85,7 +90,7 @@ class CloudFormationSchemaFetcherTest {

@Test
fun `fetchSchema uses correct region`() {
val file = tempFolder.newFile("schema.json")
val file = File(tempDir, "schema.json")
file.writeText("""{"typeName": "AWS::S3::Bucket"}""")

var capturedRegion: Region? = null
Expand All @@ -100,15 +105,15 @@ class CloudFormationSchemaFetcherTest {

customFetcher.fetchSchema(file.absolutePath, "eu-west-1")

assertEquals(Region.EU_WEST_1, capturedRegion)
assertEquals(capturedRegion, Region.EU_WEST_1)
}

@Test
fun `fetchSchema handles multiple resource types`() {
val file1 = tempFolder.newFile("s3.json")
val file1 = File(tempDir, "s3.json")
file1.writeText("""{"typeName": "AWS::S3::Bucket"}""")

val file2 = tempFolder.newFile("ec2.json")
val file2 = File(tempDir, "ec2.json")
file2.writeText("""{"typeName": "AWS::EC2::Instance"}""")

val mockResponse = mock(DescribeTypeResponse::class.java)
Expand All @@ -118,7 +123,7 @@ class CloudFormationSchemaFetcherTest {
val result1 = fetcher.fetchSchema(file1.absolutePath)
val result2 = fetcher.fetchSchema(file2.absolutePath)

assertEquals("""{"type": "object"}""", result1)
assertEquals("""{"type": "object"}""", result2)
assertEquals(result1, """{"type": "object"}""")
assertEquals(result2, """{"type": "object"}""")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.json.psi.JsonFile
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiFile
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever
import org.testng.Assert.assertEquals
import org.testng.Assert.assertTrue
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

class GuardRailDiagnosticMapperTest {

private lateinit var mapper: GuardRailDiagnosticMapper
private lateinit var mockFile: PsiFile

@Before
@BeforeMethod
fun setup() {
mapper = GuardRailDiagnosticMapper()
mockFile = mock(PsiFile::class.java)
Expand Down Expand Up @@ -50,11 +50,11 @@ class GuardRailDiagnosticMapperTest {

val result = mapper.mapResults(cliOutput, mockFile)

assertEquals(1, result.size)
assertEquals("[R001] Test error", result[0].description)
assertEquals(ProblemHighlightType.ERROR, result[0].highlightType)
assertEquals("TestRule", result[0].ruleName)
assertEquals("/properties/test", result[0].violation.path)
assertEquals(result.size, 1)
assertEquals(result[0].description, "[R001] Test error")
assertEquals(result[0].highlightType, ProblemHighlightType.ERROR)
assertEquals(result[0].ruleName, "TestRule")
assertEquals(result[0].violation.path, "/properties/test")
}

@Test
Expand All @@ -72,10 +72,10 @@ class GuardRailDiagnosticMapperTest {

val result = mapper.mapResults(cliOutput, mockFile)

assertEquals(1, result.size)
assertEquals("[W001] Test warning", result[0].description)
assertEquals(ProblemHighlightType.WARNING, result[0].highlightType)
assertEquals("WarningRule", result[0].ruleName)
assertEquals(result.size, 1)
assertEquals(result[0].description, "[W001] Test warning")
assertEquals(result[0].highlightType, ProblemHighlightType.WARNING)
assertEquals(result[0].ruleName, "WarningRule")
}

@Test
Expand All @@ -92,9 +92,9 @@ class GuardRailDiagnosticMapperTest {

val result = mapper.mapResults(cliOutput, mockFile)

assertEquals(2, result.size)
assertEquals("[E001] Error 1", result[0].description)
assertEquals("[E002] Error 2", result[1].description)
assertEquals(result.size, 2)
assertEquals(result[0].description, "[E001] Error 1")
assertEquals(result[1].description, "[E002] Error 2")
}

@Test
Expand All @@ -109,7 +109,7 @@ class GuardRailDiagnosticMapperTest {

val result = mapper.mapResults(cliOutput, mockFile)

assertEquals(2, result.size)
assertEquals(result.size, 2)
assertTrue(result.any { it.ruleName == "Rule1" })
assertTrue(result.any { it.ruleName == "Rule2" })
}
Expand All @@ -127,11 +127,11 @@ class GuardRailDiagnosticMapperTest {

val result = mapper.mapResults(cliOutput, mockFile)

assertEquals(2, result.size)
assertEquals(result.size, 2)
val errors = result.filter { it.highlightType == ProblemHighlightType.ERROR }
val warnings = result.filter { it.highlightType == ProblemHighlightType.WARNING }
assertEquals(1, errors.size)
assertEquals(1, warnings.size)
assertEquals(errors.size, 1)
assertEquals(warnings.size, 1)
}

@Test
Expand All @@ -145,20 +145,20 @@ class GuardRailDiagnosticMapperTest {

val result = mapper.mapResults(cliOutput, mockFile)

assertEquals(1, result.size)
assertEquals("[E001] ", result[0].description)
assertEquals(result.size, 1)
assertEquals(result[0].description, "[E001] ")
}

@Test
fun `extractLineNumber returns 0 for non-JsonFile`() {
val lineNumber = mapper.extractLineNumber("/properties/test", mockFile)
assertEquals(0, lineNumber)
assertEquals(lineNumber, 0)
}

@Test
fun `extractLineNumber returns 0 for empty path`() {
val lineNumber = mapper.extractLineNumber("", mockFile)
assertEquals(0, lineNumber)
assertEquals(lineNumber, 0)
}

@Test
Expand All @@ -170,6 +170,6 @@ class GuardRailDiagnosticMapperTest {
whenever(mockViewProvider.document).thenReturn(null)

val lineNumber = mapper.extractLineNumber("/properties/test", mockJsonFile)
assertEquals(0, lineNumber)
assertEquals(lineNumber, 0)
}
}