Skip to content

Commit

Permalink
Merge pull request #8 from Cognifide/feature/new-approach-of-tests
Browse files Browse the repository at this point in the history
New approach of tests.
  • Loading branch information
mjedraszczyk authored Oct 9, 2019
2 parents 54a3214 + 8179ba1 commit da84be7
Show file tree
Hide file tree
Showing 64 changed files with 1,701 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ branches:
- /^release.*$/

jdk:
- oraclejdk8
- openjdk8

before_install:
- chmod +x gradlew
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ subprojects {

dependencies {
"implementation"(kotlin("stdlib-jdk8"))
"implementation"("org.junit.jupiter:junit-jupiter-api:5.4.2")
"testRuntime"("org.junit.jupiter:junit-jupiter-engine:5.4.2")
"implementation"("org.junit.jupiter:junit-jupiter-api:5.5.1")
"implementation"("org.junit.jupiter:junit-jupiter-params:5.5.1")
"testRuntime"("org.junit.jupiter:junit-jupiter-engine:5.5.1")

"implementation"("org.apache.commons:commons-lang3:3.8.1")
}
Expand Down
6 changes: 6 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ plugins {

dependencies {
implementation("io.rest-assured:rest-assured:4.0.0")
implementation("org.yaml:snakeyaml:1.19")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.8")
implementation("com.fasterxml.jackson.core:jackson-databind:2.9.8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8")
implementation("com.github.tomakehurst:wiremock-jre8:2.23.2")
implementation("org.junit.jupiter:junit-jupiter-api:5.5.1")
implementation("org.junit.jupiter:junit-jupiter-params:5.5.1")
}
90 changes: 90 additions & 0 deletions core/src/main/kotlin/com/cognifide/apmt/ArgumentsFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.cognifide.apmt

import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.params.provider.Arguments

fun createAllowed(testCases: List<TestCaseConfiguration>): List<Arguments> {
return toArguments(testCases.flatMap { createAllowed(it) }.distinct().sorted())
}

fun createDenied(testCases: List<TestCaseConfiguration>): List<Arguments> {
return toArguments(testCases.flatMap { createDenied(it) }.distinct().sorted())
}

private fun createAllowed(testCase: TestCaseConfiguration): List<UserAndPath> {
return createArguments(
testCase.paths,
testCase.allowedUsers,
testCase.deniedUsers,
testCase.allUsers,
testCase.allowedPairsPredicate,
testCase.deniedPairsPredicate
)
}

private fun createDenied(testCase: TestCaseConfiguration): List<UserAndPath> {
return createArguments(
testCase.paths,
testCase.deniedUsers,
testCase.allowedUsers,
testCase.allUsers,
testCase.deniedPairsPredicate,
testCase.allowedPairsPredicate
)
}

private fun createArguments(
paths: List<String>,
users: List<User>,
opposedUsers: List<User>,
allUsers: List<User>,
predicate: ((user: User, path: String) -> Boolean)?,
opposedPredicate: ((user: User, path: String) -> Boolean)?
): List<UserAndPath> {
return when {
users.isNotEmpty() -> pairs(users, paths, predicate)
opposedUsers.isNotEmpty() && allUsers.isNotEmpty() -> {
val allPairs = pairs(allUsers, paths)
allPairs - pairs(opposedUsers, paths, opposedPredicate)
}
else -> listOf()
}
}

private fun toArguments(usersAndPaths: Collection<UserAndPath>): List<Arguments> {
val arguments = mutableListOf<Arguments>()
usersAndPaths.forEach { arguments.add(Arguments.of(it.user, it.path)) }

Assumptions.assumeFalse(arguments.isEmpty(), "No arguments")

return arguments.toList()
}

private fun pairs(
users: List<User>,
paths: List<String>,
predicate: ((user: User, path: String) -> Boolean)?
): List<UserAndPath> {
val pairs = pairs(users, paths)
if (predicate != null) {
return pairs.filter { predicate(it.user, it.path) }
}
return pairs
}

private fun pairs(users: List<User>, paths: List<String>): List<UserAndPath> {
val results = mutableListOf<UserAndPath>()
for (user in users) {
for (path in paths) {
results.add(UserAndPath(user, path))
}
}
return results.toList()
}

data class UserAndPath(val user: User, val path: String) : Comparable<UserAndPath> {

override fun compareTo(other: UserAndPath): Int {
return this.user.username.compareTo(other.user.username)
}
}
14 changes: 0 additions & 14 deletions core/src/main/kotlin/com/cognifide/apmt/Checks.kt

This file was deleted.

6 changes: 0 additions & 6 deletions core/src/main/kotlin/com/cognifide/apmt/Group.kt

This file was deleted.

10 changes: 0 additions & 10 deletions core/src/main/kotlin/com/cognifide/apmt/PermissionTest.kt

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions core/src/main/kotlin/com/cognifide/apmt/PermissionTestsFactory.kt

This file was deleted.

6 changes: 6 additions & 0 deletions core/src/main/kotlin/com/cognifide/apmt/TestCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cognifide.apmt

interface TestCase {

fun toTestCaseConfiguration(): TestCaseConfiguration
}
41 changes: 31 additions & 10 deletions core/src/main/kotlin/com/cognifide/apmt/TestCaseConfiguration.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package com.cognifide.apmt

import kotlin.reflect.KFunction2

class TestCaseConfiguration(
var name: String = "",
var test: KFunction2<String, String, Unit>? = null,
internal val paths: MutableList<String> = mutableListOf(),
internal val users: MutableList<User> = mutableListOf()
class TestCaseConfiguration @JvmOverloads constructor(
internal var allowedUsers: List<User> = listOf(),
internal var deniedUsers: List<User> = listOf(),
internal var paths: List<String> = listOf(),
internal var allUsers: List<User> = listOf(),
internal var allowedPairsPredicate: ((user: User, path: String) -> Boolean)? = null,
internal var deniedPairsPredicate: ((user: User, path: String) -> Boolean)? = null
) {

fun addPath(path: String) {
paths.add(path)
fun allUsers(allUsers: Array<out User>) {
this.allUsers = allUsers.toList()
}

fun addUser(user: User) {
users.add(user)
fun allUsers(allUsers: List<User>) {
this.allUsers = allUsers
}

fun paths(vararg paths: String) {
this.paths = paths.toList()
}

fun allowedUsers(vararg users: User) {
this.allowedUsers = users.toList()
}

fun deniedUsers(vararg users: User) {
this.deniedUsers = users.toList()
}

fun allowedPairsPredicate(predicate: (user: User, path: String) -> Boolean) {
this.allowedPairsPredicate = predicate
}

fun deniedPairsPredicate(predicate: (user: User, path: String) -> Boolean) {
this.deniedPairsPredicate = predicate
}
}
2 changes: 1 addition & 1 deletion core/src/main/kotlin/com/cognifide/apmt/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ interface User {

val username: String
val password: String
val groups: List<Group>

}
6 changes: 3 additions & 3 deletions core/src/main/kotlin/com/cognifide/apmt/actions/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.cognifide.apmt.actions
import io.restassured.response.Response

interface Action {
fun prepare(): Response?
fun execute(): Response?
fun undo(): Response?
fun prepare(): Response? = null
fun execute(): Response
fun undo(): Response? = null

fun successCode(): Int
fun failureCode(): Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ object ActionContext {
fun basicRequestSpec(user: User, instance: Instance): RequestSpecification {
return RestAssured
.given()
.headers(instance.headers)
.header(CSRF_TOKEN, obtainCsrfToken(user, instance))
.auth().preemptive().basic(user.username, user.password)
}

private fun obtainCsrfToken(user: User, instance: Instance): String {
return RestAssured
.given()
.headers(instance.headers)
.auth().preemptive().basic(user.username, user.password)
.`when`()
.get(instance.url + CSRF_ENDPOINT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.cognifide.apmt.actions.asset

import com.cognifide.apmt.User
import com.cognifide.apmt.actions.Action
import com.cognifide.apmt.actions.ActionContext
import com.cognifide.apmt.config.Instance
import io.restassured.response.Response

class CreateAsset(
private val instance: Instance,
private val user: User,
private val path: String
) : Action {

override fun execute(): Response {
return ActionContext.basicRequestSpec(user, instance)
.formParam("jcr:primaryType", "sling:OrderedFolder")
.formParam("jcr:content/jcr:primaryType", "nt:unstructured")
.formParam("jcr:content/jcr:title", "Test Title")
.`when`()
.post(instance.url + path)
}

override fun undo(): Response? {
return ActionContext.basicRequestSpec(user, instance)
.delete(instance.url + path)
}

override fun successCode(): Int = 200
override fun failureCode(): Int = 404
}
24 changes: 24 additions & 0 deletions core/src/main/kotlin/com/cognifide/apmt/actions/asset/EditAsset.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cognifide.apmt.actions.asset

import com.cognifide.apmt.User
import com.cognifide.apmt.actions.Action
import com.cognifide.apmt.actions.ActionContext
import com.cognifide.apmt.config.Instance
import io.restassured.response.Response

class EditAsset(
private val instance: Instance,
private val user: User,
private val path: String
) : Action {

override fun execute(): Response {
return ActionContext.basicRequestSpec(user, instance)
.formParam("jcr:title", "Some Title")
.`when`()
.post(instance.url + path)
}

override fun successCode(): Int = 200
override fun failureCode(): Int = 500
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cognifide.apmt.actions.asset

import com.cognifide.apmt.User
import com.cognifide.apmt.actions.Action
import com.cognifide.apmt.actions.ActionContext
import com.cognifide.apmt.config.Instance
import io.restassured.response.Response

class RemoveAsset(
private val instance: Instance,
private val user: User,
private val path: String
) : Action {

override fun execute(): Response {
return ActionContext.basicRequestSpec(user, instance)
.delete(instance.url + path)
}

override fun successCode(): Int = 204
override fun failureCode(): Int = 403
}
Loading

0 comments on commit da84be7

Please sign in to comment.