3bcfa5bca617750040aa16a34137d83ab774e59b
[integration.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.pnfsimulator.simulator;
21
22 import io.vavr.Function0;
23 import io.vavr.Function1;
24 import io.vavr.Function2;
25
26 import java.time.Instant;
27 import java.util.Random;
28
29 import org.apache.commons.lang3.RandomStringUtils;
30
31 class KeywordsValueProvider {
32
33     private KeywordsValueProvider() {
34     }
35
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;
39
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);
44
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();
52
53     public static Function1<Integer, String> getRandomString() {
54         return randomString;
55     }
56
57     public static Function2<Integer, Integer, String> getRandomInteger() {
58         return randomInteger;
59     }
60
61     public static Function0<String> getRandomLimitedInteger() {
62         return randomLimitedInteger;
63     }
64
65     public static Function0<String> getRandomLimitedString() {
66         return randomLimitedString;
67     }
68
69     public static Function0<String> getEpochSecond() {
70         return epochSecond;
71     }
72
73     public static Function2<Integer, Integer, Long> getRandomPrimitiveInteger() {
74         return randomPrimitiveInteger;
75     }
76
77     public static Function0<Long> getTimestampPrimitive() {
78         return timestampPrimitive;
79     }
80 }