Skip to content

Commit

Permalink
Implement downloadedFileAssertion step
Browse files Browse the repository at this point in the history
  • Loading branch information
friduchin committed Jul 19, 2022
1 parent 708b190 commit 9a90d5b
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.personio.synthetics.model.assertion

import com.datadog.api.v1.client.model.SyntheticsCheckType
import com.personio.synthetics.model.Params

internal data class DownloadedFileAssertionParams(
val file: File,
) : Params()

internal data class File(
val nameCheck: NameCheck? = null,
val sizeCheck: SizeCheck? = null,
val md5: String? = null,
) : Params()

internal data class NameCheck(
val type: String,
val value: String?,
) : Params()

internal data class SizeCheck(
val type: String,
val value: Int,
) : Params()

enum class FileNameCheckType(val value: SyntheticsCheckType) {
NOT_CONTAINS(SyntheticsCheckType.NOT_CONTAINS),
IS_EMPTY(SyntheticsCheckType.IS_EMPTY),
STARTS_WITH(SyntheticsCheckType.STARTS_WITH),
NOT_EQUALS(SyntheticsCheckType.NOT_EQUALS),
NOT_IS_EMPTY(SyntheticsCheckType.NOT_IS_EMPTY),
CONTAINS(SyntheticsCheckType.CONTAINS),
MATCH_REGEX(SyntheticsCheckType.MATCH_REGEX),
EQUALS(SyntheticsCheckType.EQUALS),
NOT_STARTS_WITH(SyntheticsCheckType.NOT_STARTS_WITH),
}

enum class FileSizeCheckType(val value: SyntheticsCheckType) {
LOWER(SyntheticsCheckType.LOWER),
GREATER(SyntheticsCheckType.GREATER),
NOT_EQUALS(SyntheticsCheckType.NOT_EQUALS),
GREATER_EQUALS(SyntheticsCheckType.GREATER_EQUALS),
LOWER_EQUALS(SyntheticsCheckType.LOWER_EQUALS),
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.personio.synthetics.step.assertion

import com.datadog.api.v1.client.model.SyntheticsCheckType
import com.datadog.api.v1.client.model.SyntheticsStep
import com.datadog.api.v1.client.model.SyntheticsStepType
import com.personio.synthetics.client.BrowserTest
import com.personio.synthetics.model.assertion.AssertionParams
import com.personio.synthetics.model.assertion.DownloadedFileAssertionParams
import com.personio.synthetics.model.assertion.File
import com.personio.synthetics.model.assertion.FileNameCheckType
import com.personio.synthetics.model.assertion.FileSizeCheckType
import com.personio.synthetics.model.assertion.NameCheck
import com.personio.synthetics.model.assertion.SizeCheck
import com.personio.synthetics.step.addStep
import com.personio.synthetics.step.withParamType
import org.intellij.lang.annotations.Language

/**
Expand Down Expand Up @@ -33,15 +39,52 @@ fun BrowserTest.customJavascriptAssertion(
* @param f Add all the parameters required for this test step
* @return SyntheticsCommonAssertionSteps object with the downloaded file assertion step added
*/
fun BrowserTest.downloadedFileAssertion(
stepName: String,
fileNameCheckType: SyntheticsCheckType,
expectedFileName: String,
fileSizeCheckType: SyntheticsCheckType,
expectedFileSize: Long,
f: (SyntheticsStep.() -> Unit)? = null
) = addStep(stepName) {
type = SyntheticsStepType.ASSERT_FILE_DOWNLOAD
params = AssertionParams()
if (f != null) f()
fun BrowserTest.downloadedFileAssertion(stepName: String, f: DownloadedFileAssertionStep.() -> Unit): DownloadedFileAssertionStep =
addStep(stepName, DownloadedFileAssertionStep()) {
type = SyntheticsStepType.ASSERT_FILE_DOWNLOAD
params = DownloadedFileAssertionParams(File())
f()
}

/**
* Configure the Downloaded file assertion step for the synthetic browser test
*/
class DownloadedFileAssertionStep : SyntheticsStep() {
/**
* Sets the file name to be checked for the Downloaded file assertion step
* @param checkType The type of check to be done on the name of the downloaded file
* @param value Expected name for the Downloaded file
* @return DownloadedFileAssertionStep object with file.nameCheck param set
*/
fun nameCheck(checkType: FileNameCheckType, value: String = "") = apply {
if (checkType !in listOf(FileNameCheckType.IS_EMPTY, FileNameCheckType.NOT_IS_EMPTY)) {
check(!value.isEmpty()) { "Expected value is a required parameter for the file name check in the step '${this.name}' when the passed check type is $checkType" }
}
params = withParamType<DownloadedFileAssertionParams> {
copy(file = file.copy(nameCheck = NameCheck(checkType.value.toString(), value)))
}
}

/**
* Sets the file size to be checked for the Downloaded file assertion step
* @param checkType Check type for the file size check of the Downloaded file assertion step
* @param value Expected file size in KB for the Downloaded file assertion step
* @return DownloadedFileAssertionStep object with file.sizeCheck param set
*/
fun sizeCheck(checkType: FileSizeCheckType, value: Int) = apply {
params = withParamType<DownloadedFileAssertionParams> {
copy(file = file.copy(sizeCheck = SizeCheck(checkType.value.toString(), value)))
}
}

/**
* Sets the file MD5 value to be checked for the Downloaded file assertion step
* @param value Expected MD5 value for the Downloaded file assertion step
* @return DownloadedFileAssertionStep object with file.md5 param set
*/
fun expectedMd5(value: String) = apply {
params = withParamType<DownloadedFileAssertionParams> {
copy(file = file.copy(md5 = value))
}
}
}
8 changes: 8 additions & 0 deletions src/test/kotlin/com/personio/synthetics/e2e/E2ETest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ import com.personio.synthetics.config.timestampPatternVariable
import com.personio.synthetics.config.useGlobalVariable
import com.personio.synthetics.model.actions.Key
import com.personio.synthetics.model.actions.Modifier
import com.personio.synthetics.model.assertion.FileNameCheckType
import com.personio.synthetics.model.assertion.FileSizeCheckType
import com.personio.synthetics.model.config.Location
import com.personio.synthetics.model.config.MonitorPriority
import com.personio.synthetics.model.config.RenotifyInterval
import com.personio.synthetics.step.api.apiStep
import com.personio.synthetics.step.assertion.currentUrlAssertion
import com.personio.synthetics.step.assertion.customJavascriptAssertion
import com.personio.synthetics.step.assertion.downloadedFileAssertion
import com.personio.synthetics.step.assertion.elementAttributeAssertion
import com.personio.synthetics.step.assertion.elementContentAssertion
import com.personio.synthetics.step.assertion.elementPresentAssertion
Expand Down Expand Up @@ -148,6 +151,11 @@ class E2ETest {
stepName = "Check custom JS",
code = "return true;"
)
downloadedFileAssertion("Check downloaded file assertion step") {
nameCheck(FileNameCheckType.NOT_IS_EMPTY)
sizeCheck(FileSizeCheckType.GREATER, 1)
expectedMd5("123")
}
refreshStep("Refresh test page")
waitStep(
stepName = "Wait for a few seconds on the page",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import com.datadog.api.v1.client.model.SyntheticsStepType
import com.personio.synthetics.client.BrowserTest
import com.personio.synthetics.client.SyntheticsApiClient
import com.personio.synthetics.model.assertion.AssertionParams
import com.personio.synthetics.model.assertion.DownloadedFileAssertionParams
import com.personio.synthetics.model.assertion.FileNameCheckType
import com.personio.synthetics.model.assertion.FileSizeCheckType
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.mockito.kotlin.mock

internal class SpecialAssertionStepTest {
Expand Down Expand Up @@ -63,4 +68,90 @@ internal class SpecialAssertionStepTest {

assertEquals(10, browserTest.steps?.get(0)?.timeout)
}

@Test
fun `downloadedFileAssertion adds the new step item to the browser test object`() {
browserTest.downloadedFileAssertion("Step") {}

assertEquals(1, browserTest.steps?.size)
}

@Test
fun `downloadedFileAssertion adds the step item of type Test downloaded file assertion`() {
browserTest.downloadedFileAssertion("Step") {}

assertEquals(SyntheticsStepType.ASSERT_FILE_DOWNLOAD, browserTest.steps!![0].type)
}

@Test
fun `downloadedFileAssertion adds DownloadedFileAssertionParams to the browser test object`() {
browserTest.downloadedFileAssertion("Step") {}

assertInstanceOf(DownloadedFileAssertionParams::class.java, browserTest.steps!![0].params)
}

@Test
fun `nameCheck adds the passed file name parameters to the DownloadedFileAssertionParams object`() {
val checkType = FileNameCheckType.EQUALS
val value = "expectedName"
browserTest.downloadedFileAssertion("Step") {
nameCheck(checkType, value)
}
val params = browserTest.steps?.get(0)?.params as DownloadedFileAssertionParams

assertEquals(checkType.value.toString(), params.file.nameCheck?.type)
assertEquals(value, params.file.nameCheck?.value)
}

@Test
fun `nameCheck doesn't throw exception if expected value is not passed and check type is IS_EMPTY`() {
assertDoesNotThrow {
browserTest.downloadedFileAssertion("Step") {
nameCheck(FileNameCheckType.IS_EMPTY)
}
}
}

@Test
fun `nameCheck doesn't throw exception if expected value is not passed and check type is NOT_IS_EMPTY`() {
assertDoesNotThrow {
browserTest.downloadedFileAssertion("Step") {
nameCheck(FileNameCheckType.NOT_IS_EMPTY)
}
}
}

@Test
fun `nameCheck throws exception if expected value is not passed and check type is neither IS_EMPTY, nor NOT_IS_EMPTY`() {
assertThrows<IllegalStateException> {
browserTest.downloadedFileAssertion("Step") {
nameCheck(FileNameCheckType.EQUALS)
}
}
}

@Test
fun `sizeCheck adds the passed file size parameters to the DownloadedFileAssertionParams object`() {
val checkType = FileSizeCheckType.GREATER
val value = 1
browserTest
.downloadedFileAssertion("Step") {
sizeCheck(checkType, value)
}
val params = browserTest.steps?.get(0)?.params as DownloadedFileAssertionParams

assertEquals(checkType.value.toString(), params.file.sizeCheck?.type)
assertEquals(value, params.file.sizeCheck?.value)
}

@Test
fun `expectedMd5 adds the passed MD5 to the DownloadedFileAssertionParams object`() {
browserTest
.downloadedFileAssertion("Step") {
expectedMd5("expectedMd5")
}
val params = browserTest.steps?.get(0)?.params as DownloadedFileAssertionParams

assertEquals("expectedMd5", params.file.md5)
}
}

0 comments on commit 9a90d5b

Please sign in to comment.