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 */
014
015package org.basepom.mojo.propertyhelper.fields;
016
017import static org.assertj.core.api.Assertions.assertThat;
018import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setOnMissingFileProperty;
019import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.setValue;
020import static org.basepom.mojo.propertyhelper.definitions.DefinitionHelper.uuidDefinition;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022
023import org.basepom.mojo.propertyhelper.FieldContext;
024import org.basepom.mojo.propertyhelper.RandomUtil;
025import org.basepom.mojo.propertyhelper.ValueCache;
026import org.basepom.mojo.propertyhelper.ValueProvider;
027import org.basepom.mojo.propertyhelper.ValueProvider.PropertyBackedValueAdapter;
028import org.basepom.mojo.propertyhelper.ValueProvider.SingleValueProvider;
029import org.basepom.mojo.propertyhelper.definitions.UuidDefinition;
030
031import java.security.SecureRandom;
032import java.util.Properties;
033import java.util.UUID;
034
035import com.google.common.collect.ImmutableMap;
036import org.junit.jupiter.api.Assertions;
037import org.junit.jupiter.api.Test;
038
039public class TestUuidField {
040
041    @Test
042    public void testSimple() {
043        final UUID uuid = UUID.randomUUID();
044        final UuidDefinition uuidFieldDefinition = uuidDefinition("hello");
045        setValue(uuidFieldDefinition, uuid.toString());
046
047        uuidFieldDefinition.check();
048
049        final UuidField uf1 = UuidField.forTesting(uuidFieldDefinition, ValueProvider.NULL_PROVIDER);
050        Assertions.assertEquals(uuid.toString(), uf1.getValue());
051    }
052
053    @Test
054    public void testSimpleProperty() {
055        final UUID uuid = UUID.randomUUID();
056        final UuidDefinition uuidDefinition = uuidDefinition("hello");
057
058        uuidDefinition.check();
059
060        final Properties props = new Properties();
061        props.setProperty("hello", uuid.toString());
062        final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
063        Assertions.assertEquals(uuid.toString(), uf1.getValue());
064    }
065
066    @Test
067    public void testSimplePropertyWithDefault() {
068        final UUID uuid1 = UUID.randomUUID();
069        final UUID uuid2 = UUID.randomUUID();
070
071        final UuidDefinition uuidDefinition = uuidDefinition("hello");
072        setValue(uuidDefinition, uuid1.toString());
073
074        uuidDefinition.check();
075
076        final Properties props = new Properties();
077        props.setProperty("hello", uuid2.toString());
078        final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
079        Assertions.assertEquals(uuid2.toString(), uf1.getValue());
080    }
081
082    @Test
083    public void testNoProperty() {
084        final UUID uuid1 = UUID.randomUUID();
085        final UUID uuid2 = UUID.randomUUID();
086
087        final UuidDefinition uuidDefinition = uuidDefinition("hello");
088        setValue(uuidDefinition, uuid1.toString());
089
090        uuidDefinition.check();
091
092        final Properties props = new Properties();
093        props.setProperty("hello2", uuid2.toString());
094        final UuidField uf1 = UuidField.forTesting(uuidDefinition, new PropertyBackedValueAdapter(props, uuidDefinition.getId()));
095        Assertions.assertEquals(uuid1.toString(), uf1.getValue());
096    }
097
098    @Test
099    public void testNothing() {
100        final UUID uuid = UUID.randomUUID();
101
102        final UuidDefinition uuidDefinition = uuidDefinition("hello");
103
104        uuidDefinition.check();
105
106        final ValueProvider provider = new SingleValueProvider();
107        provider.setValue(uuid.toString());
108        final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
109        Assertions.assertEquals(uuid.toString(), uf1.getValue());
110    }
111
112    @Test
113    public void testMissingProperty() {
114        ValueCache valueCache = new ValueCache();
115
116        assertThrows(IllegalStateException.class, () -> {
117            final UuidDefinition uuidDefinition = uuidDefinition("hello");
118            setOnMissingFileProperty(uuidDefinition, "fail");
119
120            uuidDefinition.check();
121
122            final ValueProvider provider = valueCache.findCurrentValueProvider(ImmutableMap.of(), uuidDefinition);
123
124            final UuidField uf1 = UuidField.forTesting(uuidDefinition, provider);
125            Assertions.assertEquals("", uf1.getValue());
126        });
127    }
128
129    @Test
130    public void testReproducibleUuid() {
131
132        String seedValue = UUID.randomUUID().toString();
133
134        var random1 = RandomUtil.createRandomFromSeed(seedValue);
135        FieldContext context1 = FieldContext.forTesting(random1);
136
137        UuidDefinition uuidDefinition = uuidDefinition("hello");
138        UuidField field1 = new UuidField(uuidDefinition, ValueProvider.NULL_PROVIDER, context1);
139        String value1 = field1.getValue();
140
141        var random2 = RandomUtil.createRandomFromSeed(seedValue);
142        FieldContext context2 = FieldContext.forTesting(random2);
143
144        UuidField field2 = new UuidField(uuidDefinition, ValueProvider.NULL_PROVIDER, context2);
145        String value2 = field2.getValue();
146
147        assertThat(value1).isEqualTo(value2);
148    }
149}