|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-2022, University of Oslo |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * Neither the name of the HISP project nor the names of its contributors may |
| 14 | + * be used to endorse or promote products derived from this software without |
| 15 | + * specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 21 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 24 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +package org.hisp.dhis.utils; |
| 30 | + |
| 31 | +import com.github.javafaker.Faker; |
| 32 | + |
| 33 | +import java.time.LocalDate; |
| 34 | +import java.time.ZoneId; |
| 35 | +import java.time.format.DateTimeFormatter; |
| 36 | +import java.util.*; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +/** |
| 40 | + * This Randomizer will return random values based on a seed. |
| 41 | + * Using the same seed will make the Randomizer to generate |
| 42 | + * the same sequence of random values. |
| 43 | + */ |
| 44 | +public class PredictableRandomizer implements Randomizer { |
| 45 | + |
| 46 | + private final Random rnd; |
| 47 | + |
| 48 | + private final Faker faker; |
| 49 | + |
| 50 | + public PredictableRandomizer( long seed ) |
| 51 | + { |
| 52 | + this.rnd = new Random( seed ); |
| 53 | + this.faker = new Faker( rnd ); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Random getRandom() { |
| 58 | + return this.rnd; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int randomInt(int bound) |
| 63 | + { |
| 64 | + return rnd.nextInt(bound); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public <T> List<T> randomElementsFromList( List<T> list, int size ) |
| 69 | + { |
| 70 | + List<T> copiedList = new ArrayList<>(list); |
| 71 | + List<T> resultList = new ArrayList<>(); |
| 72 | + |
| 73 | + for ( int i = 0; i < size && !copiedList.isEmpty(); i++) { |
| 74 | + T randomElementFromList = randomElementFromList( copiedList ); |
| 75 | + resultList.add( copiedList.get( copiedList.indexOf( randomElementFromList ) ) ); |
| 76 | + copiedList.remove( randomElementFromList ); |
| 77 | + } |
| 78 | + |
| 79 | + return resultList; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String randomString() |
| 84 | + { |
| 85 | + return this.faker.programmingLanguage().name(); |
| 86 | + } |
| 87 | + |
| 88 | + public String randomString( int size ) |
| 89 | + { |
| 90 | + return this.faker.lorem().characters( size, true, false ); |
| 91 | + } |
| 92 | + |
| 93 | + public int randomIntInRange( int min, int max ) |
| 94 | + { |
| 95 | + return this.faker.number().numberBetween( min, max ); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public double randomDoubleInRange( int min, int max, int decimals ) |
| 100 | + { |
| 101 | + return this.faker.number().randomDouble( decimals, min, max ); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean randomBoolean() |
| 106 | + { |
| 107 | + return this.faker.bool().bool(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String randomUsername() { |
| 112 | + return this.faker.name().username(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String randomFirstName() { |
| 117 | + return this.faker.name().firstName(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String randomLastName() { |
| 122 | + return this.faker.name().lastName(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String randomAddress() { |
| 127 | + return this.faker.address().fullAddress(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String randomNationalId() { |
| 132 | + return this.faker.idNumber().valid(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Date randomAdultBirthday() { |
| 137 | + return this.faker.date().birthday( 18, 80 ); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Date randomBirthday(int minAge, int maxAge){ |
| 142 | + return this.faker.date().birthday( minAge, maxAge ); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String randomLongText( int wordCount ) { |
| 147 | + return this.faker.lorem().sentence( wordCount ); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String randomPhoneNumber() { |
| 152 | + return this.faker.phoneNumber().phoneNumber(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Date randomFutureDate() |
| 157 | + { |
| 158 | + return this.faker.date().future( 1825, TimeUnit.DAYS ); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String randomFutureDate( DateTimeFormatter formatter ) |
| 163 | + { |
| 164 | + return formatDate( toLocalDate( randomFutureDate() ), formatter ); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public Date randomPastDate() |
| 169 | + { |
| 170 | + return this.faker.date().past( 1825, TimeUnit.DAYS ); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public String randomPastDate( DateTimeFormatter formatter ) |
| 175 | + { |
| 176 | + return formatDate( toLocalDate( randomPastDate() ), formatter ); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String randomDate(DateTimeFormatter formatter ) |
| 181 | + { |
| 182 | + return formatDate( toLocalDate( getDate() ), formatter ); |
| 183 | + } |
| 184 | + |
| 185 | + private String formatDate(LocalDate localDate, DateTimeFormatter format ) |
| 186 | + { |
| 187 | + return localDate.format( format ); |
| 188 | + } |
| 189 | + |
| 190 | + private LocalDate toLocalDate( Date dateToConvert ) |
| 191 | + { |
| 192 | + return dateToConvert.toInstant().atZone( ZoneId.systemDefault() ).toLocalDate(); |
| 193 | + } |
| 194 | +} |
0 commit comments