2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.pnfsimulator.simulator;
22 import io.vavr.Function0;
23 import io.vavr.Function1;
24 import io.vavr.Function2;
26 import java.time.Instant;
27 import java.util.Random;
29 import org.apache.commons.lang3.RandomStringUtils;
31 class KeywordsValueProvider {
33 private KeywordsValueProvider() {
36 static final int DEFAULT_STRING_LENGTH = 20;
37 public static final int RANDOM_INTEGER_MAX_LIMITATION = 9;
38 public static final int RANDOM_INTEGER_MIN_LIMITATION = 0;
40 private static Function2<Integer, Integer, Integer> bigger = (left, right) -> left >= right ? left : right;
41 private static Function2<Integer, Integer, Integer> smaller = (left, right) -> left < right ? left : right;
42 private static Function2<Integer, Integer, Integer> randomPrimitiveIntegerFromSortedRange = (min, max) -> new Random().nextInt(max - min + 1) + min;
43 private static Function2<Integer, Integer, String> randomIntegerFromSortedRange = (min, max) -> Integer.toString(new Random().nextInt(max - min + 1) + min);
45 private static Function1<Integer, String> randomString = RandomStringUtils::randomAscii;
46 private static Function2<Integer, Integer, String> randomInteger = (left, right) -> randomIntegerFromSortedRange.apply(smaller.apply(left, right), bigger.apply(left, right));
47 private static Function0<String> randomLimitedInteger = () -> randomInteger.apply(RANDOM_INTEGER_MIN_LIMITATION, RANDOM_INTEGER_MAX_LIMITATION);
48 private static Function0<String> randomLimitedString = () -> RandomStringUtils.randomAscii(DEFAULT_STRING_LENGTH);
49 private static Function0<String> epochSecond = () -> Long.toString(Instant.now().getEpochSecond());
50 private static Function2<Integer, Integer, Long> randomPrimitiveInteger = (left, right) -> randomPrimitiveIntegerFromSortedRange.apply(smaller.apply(left, right), bigger.apply(left, right)).longValue();
51 private static Function0<Long> timestampPrimitive = () -> Instant.now().getEpochSecond();
53 public static Function1<Integer, String> getRandomString() {
57 public static Function2<Integer, Integer, String> getRandomInteger() {
61 public static Function0<String> getRandomLimitedInteger() {
62 return randomLimitedInteger;
65 public static Function0<String> getRandomLimitedString() {
66 return randomLimitedString;
69 public static Function0<String> getEpochSecond() {
73 public static Function2<Integer, Integer, Long> getRandomPrimitiveInteger() {
74 return randomPrimitiveInteger;
77 public static Function0<Long> getTimestampPrimitive() {
78 return timestampPrimitive;