<commit_before>/*
 * Copyright 2022, 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.client;

import io.spine.base.CommandMessage;
import io.spine.base.Identifier;
import io.spine.core.TenantId;
import io.spine.test.commands.CmdCreateProject;
import io.spine.validate.ValidationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static io.spine.client.given.ActorRequestFactoryTestEnv.ACTOR;
import static io.spine.client.given.ActorRequestFactoryTestEnv.requestFactory;
import static io.spine.client.given.ActorRequestFactoryTestEnv.requestFactoryBuilder;
import static io.spine.client.given.CommandFactoryTestEnv.INVALID_COMMAND;
import static io.spine.time.testing.Future.secondsFromNow;
import static io.spine.time.testing.Past.secondsAgo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayName("Command factory should")
class CommandFactoryTest {

    private CommandFactory factory;

    @BeforeEach
    void createFactory() {
        factory = requestFactory().command();
    }

    @Nested
    @DisplayName("create command")
    class CreateCommand {

        /**
         * Tests that a command is created with the current time.
         *
         * @implNote We are creating a range of +/- second between the call to make sure the
         *         timestamp would fit into this range. This way the test ensures the sub-second
         *         precision of timestamps, which is enough for the purpose of this test.
         */
        @Test
        @DisplayName("with current time")
        void withTimestamp() {
            var beforeCall = secondsAgo(1);
            var commandMessage = CmdCreateProject.newBuilder()
                    .setId(Identifier.newUuid())
                    .build();
            var command = factory.create(commandMessage);
            var afterCall = secondsFromNow(1);

            assertTrue(command.isBetween(beforeCall, afterCall));
        }

        @Test
        @DisplayName("with given entity version")
        void withEntityVersion() {
            var command = factory.create(command(), 2);

            var context = command.context();
            assertEquals(2, context.getTargetVersion());
        }

        @Test
        @DisplayName("with own tenant ID")
        void withOwnTenantId() {
            var tenantId = TenantId.newBuilder()
                    .setValue(getClass().getSimpleName())
                    .build();
            var mtFactory = requestFactoryBuilder()
                    .setTenantId(tenantId)
                    .setActor(ACTOR)
                    .build();
            var command = mtFactory.command().create(command());
            assertEquals(tenantId, command.context()
                                          .getActorContext()
                                          .getTenantId());
        }

        private CommandMessage command() {
            var commandMessage = CmdCreateProject.newBuilder()
                    .setId(Identifier.newUuid())
                    .build();
            return commandMessage;
        }
    }

    @Nested
    @DisplayName("throw `ValidationException` when creating command")
    class NotAccept {

        @Test
        @DisplayName("from invalid `Message`")
        void invalidMessage() {
            assertThrows(ValidationException.class, () -> factory.create(INVALID_COMMAND));
        }

        @Test
        @DisplayName("from invalid `Message` with version")
        void invalidMessageWithVersion() {
            assertThrows(ValidationException.class, () -> factory.create(INVALID_COMMAND, 42));
        }
    }
}<commit_msg>Migrate test to Kotlin<commit_after>/*
 * Copyright 2022, 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.client

import com.google.common.truth.Truth.assertThat
import io.spine.base.CommandMessage
import io.spine.base.Identifier.newUuid
import io.spine.client.given.ActorRequestFactoryTestEnv
import io.spine.client.given.ActorRequestFactoryTestEnv.requestFactoryBuilder
import io.spine.client.given.CommandFactoryTestEnv.INVALID_COMMAND
import io.spine.core.tenantId
import io.spine.test.commands.cmdCreateProject
import io.spine.time.testing.Future
import io.spine.time.testing.Past
import io.spine.validate.ValidationException
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

@DisplayName("Command factory should")
internal class CommandFactorySpec {

    private lateinit var factory: CommandFactory

    @BeforeEach
    fun createFactory() {
        factory = ActorRequestFactoryTestEnv.requestFactory().command()
    }

    @Nested
    internal inner class `create command` {

        /**
         * Tests that a command is created with the current time.
         *
         * @implNote We are creating a range of +/- second between the call to make sure the
         * timestamp would fit into this range. This way the test ensures the sub-second
         * precision of timestamps, which is enough for the purpose of this test.
         */
        @Test
        fun `with current time`() {
            val beforeCall = Past.secondsAgo(1)
            val command = factory.create(command())
            val afterCall = Future.secondsFromNow(1)
            assertTrue(command.isBetween(beforeCall, afterCall))
        }

        @Test
        fun `with given entity version`() {
            val command = factory.create(command(), 2)
            val context = command.context()
            assertThat(context.targetVersion).isEqualTo(2)
        }

        @Test
        fun `with own tenant ID`() {
            val tenantId = tenantId { value = javaClass.simpleName }
            val mtFactory = requestFactoryBuilder().also { f ->
                f.tenantId = tenantId
                f.actor = ActorRequestFactoryTestEnv.ACTOR
            }.build()

            val command = mtFactory.command().create(command())

            assertThat(command.context().actorContext.tenantId)
                .isEqualTo(tenantId)
        }

        private fun command(): CommandMessage = cmdCreateProject { id = newUuid() }
    }

    @Nested
    @DisplayName("throw `ValidationException` when creating command")
    internal inner class NotAccept {

        @Test
        fun `from invalid 'Message'`() {
            assertThrows<ValidationException> {
                factory.create(INVALID_COMMAND)
            }
        }

        @Test
        fun `from invalid 'Message' with version`() {
            assertThrows<ValidationException> {
                factory.create(INVALID_COMMAND, 42)
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.0839041095890411, 'fraction_numerical': 0.0029965753424657533, 'mean_word_length': 2.7024564183835182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4331118', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2347, 'n_words': 767}