<commit_before>/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.server.entity

import com.google.common.truth.Truth.assertThat
import io.spine.server.test.shared.StringEntity
import io.spine.testing.TestValues.randomString
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test

@DisplayName("Kotlin extensions of `TransactionalEntity` should")
internal class ExtensionsTest {

    @Test
    fun `add 'update' block handler for passing properties to 'builder'`() {
        val entity = createEntity()
        val str = randomString()
        entity.apply(str)
        assertThat(entity.value()).isEqualTo(str)
    }
}

/**
 * Creates the test fixture entity and injects a stub transaction so
 * that [TransactionalEntity.builder] is available.
 */
private fun createEntity() : Fixture {
    val entity = Fixture()
    val tx: Transaction<String, Fixture, StringEntity, StringEntity.Builder> =
        StubTransaction(entity, true, true)
    entity.injectTransaction(tx)
    return entity
}

/**
 * An entity which uses the [TransactionalEntity.update] extension function in its [apply] method.
 */
private class Fixture : TransactionalEntity<String, StringEntity, StringEntity.Builder>() {

    init {
        setId(randomString())
    }

    fun apply(s: String) {
        update {
            value = s
        }
        setState(builder().vBuild())
    }

    fun value(): String = state().value
}
<commit_msg>Add tests for the new method<commit_after>/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.server.entity

import com.google.common.truth.Truth.assertThat
import io.spine.server.test.shared.StringEntity
import io.spine.testing.TestValues.randomString
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test

@DisplayName("Kotlin extensions of `TransactionalEntity` should")
internal class ExtensionsTest {

    @Test
    fun `add 'update' block handler for passing properties to 'builder'`() {
        val entity = createEntity()
        val str = randomString()
        entity.applyUpdate(str)
        assertThat(entity.value())
            .isEqualTo(str)
    }

    @Test
    fun `add 'alter' block handler for passing properties to 'builder'`() {
        val entity = createEntity()
        val str = randomString()
        entity.txApplyAlteration(str)
        assertThat(entity.value())
            .isEqualTo(str)
    }
}

/**
 * Creates the test fixture entity and injects a stub transaction so
 * that [TransactionalEntity.builder] is available.
 */
private fun createEntity() : Fixture {
    val entity = Fixture()
    val tx: Transaction<String, Fixture, StringEntity, StringEntity.Builder> =
        StubTransaction(entity, true, true)
    entity.injectTransaction(tx)
    return entity
}

/**
 * An entity which uses the [TransactionalEntity.update] extension function in its [applyUpdate] method.
 */
private class Fixture : TransactionalEntity<String, StringEntity, StringEntity.Builder>() {

    init {
        setId(randomString())
    }

    fun applyUpdate(s: String) {
        val builder = update {
            value = s
        }
        setState(builder.vBuild())
    }

    fun applyAlteration(s: String): Unit = alter {
        value = s
    }

    fun txApplyAlteration(s: String) {
        applyAlteration(s)
        setState(builder().vBuild())
    }

    fun value(): String = state().value
}

{'fraction_non_alphanumeric': 0.08648368720870016, 'fraction_numerical': 0.002761954082513378, 'mean_word_length': 3.4949573312645463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3876446', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1517, 'n_words': 600}