001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.basepom.mojo.propertyhelper;
015
016import static org.assertj.core.api.Assertions.assertThat;
017import static org.junit.jupiter.api.Assertions.assertNotNull;
018import static org.junit.jupiter.api.Assertions.assertNotSame;
019
020import java.util.UUID;
021
022import org.junit.jupiter.api.Test;
023
024public class RandomUtilTest {
025
026    @Test
027    public void testCreateRandomFromSeedString() {
028        String seedString = UUID.randomUUID().toString();
029
030        var secureRandom1 = RandomUtil.createRandomFromSeed(seedString);
031        var secureRandom2 = RandomUtil.createRandomFromSeed(seedString);
032
033        assertNotNull(secureRandom1);
034        assertNotNull(secureRandom2);
035
036        //Verify that two different SecureRandom instances are created for each call with null parameter
037        assertNotSame(secureRandom1, secureRandom2);
038
039        long value1 = secureRandom1.nextLong();
040        long value2 = secureRandom2.nextLong();
041        assertThat(value1).isEqualTo(value2);
042    }
043
044    /**
045     * Tests `createRandomFromSeedString` method of `RandomUtil` class.
046     * When given a null, this method also generates a SecureRandom instance.
047     * This method specifically validates the nonnullness and the uniqueness of the returned SecureRandom instance.
048     */
049    @Test
050    public void testCreateRandomFromNull() {
051        var secureRandom1 = RandomUtil.createRandomFromSeed(null);
052        var secureRandom2 = RandomUtil.createRandomFromSeed(null);
053
054        assertNotNull(secureRandom1);
055        assertNotNull(secureRandom2);
056
057        //Verify that two different SecureRandom instances are created for each call with null parameter
058        assertNotSame(secureRandom1, secureRandom2);
059
060        assertThat(secureRandom1.nextLong()).isNotEqualTo(secureRandom2.nextLong());
061    }
062}