<commit_before>/*
 * Copyright 2015, TeamDev Ltd. All rights reserved.
 *
 * 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 org.spine3.util;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.TimeUtil;
import org.junit.Test;
import org.spine3.base.CommandId;
import org.spine3.base.CommandRequest;
import org.spine3.base.UserId;
import org.spine3.protobuf.MessageFields;
import org.spine3.testutil.CommandRequestFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.google.protobuf.util.TimeUtil.getCurrentTime;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.spine3.server.Entity.USER_ID_AND_TIME_DELIMITER;
import static org.spine3.util.Commands.generateId;
import static org.spine3.util.Lists.filter;

/**
 * @author <NAME>
 */
@SuppressWarnings("InstanceMethodNamingConvention")
public class CommandsShould {

    @Test
    public void generate() {
        UserId userId = Users.createId("commands-should-test");

        CommandId result = generateId(userId);

        //noinspection DuplicateStringLiteralInspection
        assertThat(result, allOf(
                hasProperty("actor", equalTo(userId)),
                hasProperty("timestamp")));
    }

    @Test(expected = NullPointerException.class)
    public void fail_on_null_parameter() {
        //noinspection ConstantConditions
        generateId(null);
    }

    @Test
    public void convert_field_name_to_method_name() {
        assertEquals("getUserId", MessageFields.toAccessorMethodName("user_id"));
        assertEquals("getId", MessageFields.toAccessorMethodName("id"));
        assertEquals("getAggregateRootId", MessageFields.toAccessorMethodName("aggregate_root_id"));
    }

    @Test
    public void return_correct_were_after_predicate() throws InterruptedException {
        final Timestamp timestamp = TimeUtil.getCurrentTime();
        final CommandRequest commandRequest = CommandRequestFactory.create();
        Thread.sleep(100);
        final CommandRequest commandRequestAfter = CommandRequestFactory.create();

        final List<CommandRequest> commandList = ImmutableList.<CommandRequest>builder()
                .add(commandRequest)
                .add(commandRequestAfter)
                .build();

        final List<CommandRequest> filteredList = filter(commandList, Commands.wereAfter(timestamp));

        assertEquals(1, filteredList.size());
        assertEquals(commandRequestAfter, filteredList.get(0));
    }

    @Test
    public void sort() {
        final CommandRequest commandRequest1 = CommandRequestFactory.create();
        final CommandRequest commandRequest2 = CommandRequestFactory.create();
        final CommandRequest commandRequest3 = CommandRequestFactory.create();

        final Collection<CommandRequest> sortedList = new ArrayList<>();
        sortedList.add(commandRequest1);
        sortedList.add(commandRequest2);
        sortedList.add(commandRequest3);

        final List<CommandRequest> unSortedList = new ArrayList<>();
        unSortedList.add(commandRequest3);
        unSortedList.add(commandRequest1);
        unSortedList.add(commandRequest2);

        assertFalse(sortedList.equals(unSortedList));

        Commands.sort(unSortedList);

        assertEquals(sortedList, unSortedList);
    }

    @SuppressWarnings("MagicNumber")
    @Test
    public void return_correct_were_within_period_predicate() throws InterruptedException {
        final CommandRequest req1 = CommandRequestFactory.create();
        final Timestamp from = TimeUtil.getCurrentTime();
        Thread.sleep(100);
        final CommandRequest req2 = CommandRequestFactory.create();
        Thread.sleep(100);
        final Timestamp to = TimeUtil.getCurrentTime();

        final List<CommandRequest> commandList = ImmutableList.<CommandRequest>builder()
                .add(req1)
                .add(req2)
                .build();

        final List<CommandRequest> filteredList = filter(commandList, Commands.wereWithinPeriod(from, to));

        assertEquals(1, filteredList.size());
        assertEquals(req2, filteredList.get(0));
    }

    @Test
    public void convert_to_string_command_id_message() {

        final String userIdString = "user123123";
        final Timestamp currentTime = getCurrentTime();
        CommandId id = generateId(userIdString, currentTime);

        /* TODO[alexander.litus]: create parse() method that would restore an object from its String representation.
            Use the restored object for equality check with the original object.*/
        final String expected = userIdString + USER_ID_AND_TIME_DELIMITER +  TimeUtil.toString(currentTime);

        final String actual = Commands.idToString(id);

        assertEquals(expected, actual);
    }
}<commit_msg>Prevent warning.<commit_after>/*
 * Copyright 2015, TeamDev Ltd. All rights reserved.
 *
 * 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 org.spine3.util;

import com.google.common.collect.ImmutableList;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.TimeUtil;
import org.junit.Test;
import org.spine3.base.CommandId;
import org.spine3.base.CommandRequest;
import org.spine3.base.UserId;
import org.spine3.protobuf.MessageFields;
import org.spine3.testutil.CommandRequestFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.google.protobuf.util.TimeUtil.getCurrentTime;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.spine3.server.Entity.USER_ID_AND_TIME_DELIMITER;
import static org.spine3.util.Commands.generateId;
import static org.spine3.util.Lists.filter;

/**
 * @author <NAME>
 */
@SuppressWarnings("InstanceMethodNamingConvention")
public class CommandsShould {

    @Test
    public void generate() {
        UserId userId = Users.createId("commands-should-test");

        CommandId result = generateId(userId);

        //noinspection DuplicateStringLiteralInspection
        assertThat(result, allOf(
                hasProperty("actor", equalTo(userId)),
                hasProperty("timestamp")));
    }

    @Test(expected = NullPointerException.class)
    public void fail_on_null_parameter() {
        //noinspection ConstantConditions
        generateId(null);
    }

    @Test
    public void convert_field_name_to_method_name() {
        assertEquals("getUserId", MessageFields.toAccessorMethodName("user_id"));
        assertEquals("getId", MessageFields.toAccessorMethodName("id"));
        assertEquals("getAggregateRootId", MessageFields.toAccessorMethodName("aggregate_root_id"));
    }

    @Test
    public void return_correct_were_after_predicate() throws InterruptedException {
        final Timestamp timestamp = TimeUtil.getCurrentTime();
        final CommandRequest commandRequest = CommandRequestFactory.create();
        Thread.sleep(100);
        final CommandRequest commandRequestAfter = CommandRequestFactory.create();

        final List<CommandRequest> commandList = ImmutableList.<CommandRequest>builder()
                .add(commandRequest)
                .add(commandRequestAfter)
                .build();

        final List<CommandRequest> filteredList = filter(commandList, Commands.wereAfter(timestamp));

        assertEquals(1, filteredList.size());
        assertEquals(commandRequestAfter, filteredList.get(0));
    }

    @Test
    public void sort() {
        final CommandRequest commandRequest1 = CommandRequestFactory.create();
        final CommandRequest commandRequest2 = CommandRequestFactory.create();
        final CommandRequest commandRequest3 = CommandRequestFactory.create();

        final Collection<CommandRequest> sortedList = new ArrayList<>();
        sortedList.add(commandRequest1);
        sortedList.add(commandRequest2);
        sortedList.add(commandRequest3);

        final List<CommandRequest> unSortedList = new ArrayList<>();
        unSortedList.add(commandRequest3);
        unSortedList.add(commandRequest1);
        unSortedList.add(commandRequest2);

        assertFalse(sortedList.equals(unSortedList));

        Commands.sort(unSortedList);

        assertEquals(sortedList, unSortedList);
    }

    @SuppressWarnings("MagicNumber")
    @Test
    public void return_correct_were_within_period_predicate() throws InterruptedException {
        final CommandRequest req1 = CommandRequestFactory.create();
        final Timestamp from = TimeUtil.getCurrentTime();
        Thread.sleep(100);
        final CommandRequest req2 = CommandRequestFactory.create();
        Thread.sleep(100);
        final Timestamp to = TimeUtil.getCurrentTime();

        final List<CommandRequest> commandList = ImmutableList.<CommandRequest>builder()
                .add(req1)
                .add(req2)
                .build();

        final List<CommandRequest> filteredList = filter(commandList, Commands.wereWithinPeriod(from, to));

        assertEquals(1, filteredList.size());
        assertEquals(req2, filteredList.get(0));
    }

    @Test
    public void convert_to_string_command_id_message() {

        final String userIdString = "user123";
        final Timestamp currentTime = getCurrentTime();
        CommandId id = generateId(userIdString, currentTime);

        /* TODO[alexander.litus]: create parse() method that would restore an object from its String representation.
            Use the restored object for equality check with the original object.*/
        final String expected = userIdString + USER_ID_AND_TIME_DELIMITER +  TimeUtil.toString(currentTime);

        final String actual = Commands.idToString(id);

        assertEquals(expected, actual);
    }
}
{'fraction_non_alphanumeric': 0.08756345177664974, 'fraction_numerical': 0.007529610829103215, 'mean_word_length': 3.7076861808044606, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3832393', 'n_tokens_mistral': 3164, 'n_tokens_neox': 3014, 'n_words': 805}